$ show andreasfertig/cppinsights
C++ Insights is a source-to-source transformation tool built on LLVM/Clang that reveals compiler-generated code by making implicit C++ mechanisms explicit. It processes C++ source through Clang's AST, applies transformations to show template expansions, lambda desugaring, coroutine state machines, lifetime tracking, and other low-level details, outputting valid C++ that illustrates what the compiler does internally. The architecture centers on AST traversal via custom visitors and code generators, with modular components for different transformation modes (standard, Cfront-style, coroutine-specific). Key entry points include Insights.cpp (main driver) and specialized generators like CoroutinesCodeGenerator.cpp. The tool serves an educational purpose, helping developers understand C++ language semantics through visualizing compiler transformations.
The LLM identified 5 files worth highlighting for reuse:
Provides a lightweight, efficient string concatenation utility (StrCat) inspired by abseil. Zero dependencies beyond standard library, type-safe, and reusable in any C++ project for building strings without heap allocations for small cases. Demonstrates skill in utility design with perfect forwarding and compile-time length calculation.
Implements strongly typed boolean and integer aliases (e.g., DoCast, AsReference) to prevent primitive obsession and mix-ups of semantically different values. Self-contained header with macros for easy definition. Reusable across projects to add compile-time safety to configuration flags and enumerations without runtime overhead.
Implements a stack-allocated vector (small vector optimization) for efficient storage of small numbers of elements without heap allocation. Fixed-size internal buffer with fallback to heap only when needed. Reusable in performance-sensitive contexts like parsers, AST traversals, or any scenario with typically small collections. Shows skill in container design with move semantics and allocator awareness.
Provides OnceTrue and OnceFalse helper classes for one-time initialization flags using atomic operations under the hood. Thread-safe, lightweight, and reusable for lazy initialization, one-time setup, or tracking state that should only change once. Demonstrates proper use of std::atomic and memory ordering for efficient flag implementation.
Manages indentation, scoping, and buffered output for generated code with RAII-based scope handling (e.g., ScopedIndent). Handles newline placement, indentation levels, and wrapping constructs in parentheses or braces. Reusable in any code generation tool needing formatted output. Shows skill in separating formatting logic from transformation logic with clean, composable interface.