Toolchain development

From carbon-language/carbon-lang · View interactive version

Toolchain development is an AI agent skill from the GitHub repository carbon-language/carbon-lang. Instructions for checking, building, debugging, and understanding the Carbon toolchain.

How to use this skill

Skill file

Toolchain development

<!--

Part of the Carbon Language project, under the Apache License v2.0 with LLVM

Exceptions. See /LICENSE for license information.

SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

-->

Toolchain structure

(SemIR) definitions.

Toolchain architecture

architecture design and patterns.

comprehensive list of patterns (for example, ValueStore, formatting

.def files, struct reflection) used throughout the implementation.

(SKILL.md) for guidelines on registering, mapping,

constant evaluating, and lowering compiler builtin primitives (e.g.

"int.convert_float").

(SKILL.md) before working on

toolchain/language_server/ or utils/vscode/. Neither follows the

patterns described here.

defined in .def files and expanded by way of macros.

stack exhaustion on complex codebases.

Essential commands

--test_arg=--file_tests=<path_to_carbon_file>`

Updating test data

Carbon tests often use file_test (for example,

//toolchain/testing/file_test). For detailed guidelines on authoring tests,

including file splits, naming conventions (fail_, todo_), and generating

minimal output with SemIR dumps, please refer to the Toolchain tests skill.

If you change compiler behavior, you likely need to update expected test

outputs. Do not manually edit thousands of lines of expected output. Use the

script:

./toolchain/autoupdate_testdata.py
# Or for a specific file:
./toolchain/autoupdate_testdata.py toolchain/check/testdata/my_test.carbon

Debugging and diagnostics

(SKILL.md) for strict rules on declaring,

formatting, emitting, testing, and styling compiler diagnostic messages

(errors, warnings, notes).

but often running the binary directly from bazel-bin/ is easier for

debugging.

Error handling

LLVM.

Context-Aware Diagnostics

When declaring and emitting errors, ensure semantic wording matches the exact

context:

unsized expressions like IntLiteral or FloatLiteral. For example, use

RealLiteralTooLargeForUnsizedInt instead of a diagnostic referencing an

"integer type".

kind.def, search for existing

diagnostics in the targeted implementation files (for example, other uses of

MaxIntWidth) to align message structures and parameter expectations.

Casting (LLVM style)

Leverage LLVM APIs

Before implementing custom algorithms for mathematical, logical, or bitwise

operations, inspect target LLVM ADT class APIs:

APSInt) already offer native equivalents (for example, .pow(),

ilogb(), .changeSign(), convertFromAPInt()). Avoid duplicate, naive,

or inefficient custom loops.

Data structures

use Map instead of llvm::DenseMap.

example llvm::SmallVector, llvm::StringRef).

Common pitfalls

  1. Legacy explorer references: The explorer prototype has been moved.

Ignore references to it in proposals or old docs; focus on toolchain.

  1. Manually updating test files: Always check if autoupdate_testdata.py

can do it for you.

  1. Using std::string unnecessarily: Prefer llvm::StringRef for

arguments.

  1. Header includes: Use specific include orders (often enforced by

clang-format).

  1. Parse node order: Semantics processes parse nodes in post-order; ensure

your parser transitions support this.

  1. Builtin implementation gaps: If adding a primitive builtin function,

make sure you address all phases of the lifecycle: macro definition

registration, signature validation, compile-time constant evaluation

(interpreter), LLVM IR lowering, and prelude modular implementation bindings

(avoiding orphan rules). Refer to the Builtin functions skill

(SKILL.md) for details.

  1. Premature helper abstraction: Avoid extracting tiny helper functions

that are called from exactly one place and do not significantly modularize

complex code. Prefer inlining directly to keep the implementation compact,

readable, and localized.

  1. Redundant bounds calculations: Avoid repeating calculations of complex

boundary estimations (such as lower and upper bound estimations). Refactor

the logic to calculate unified values once, preserving compactness.

  1. Trusting stale clangd diagnostics: In-editor diagnostics are only as

good as compile_commands.json. If it predates a newly added file, clangd

falls back to a default command and reports nonsense, such as missing

standard headers or "no member named None". Regenerate it with

./scripts/create_compdb.py, which only takes a few seconds.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.