[clang] [Clang] ASTUnit should use the CompilerInstance's CodeGenOptions (PR #195338)

via cfe-commits cfe-commits at lists.llvm.org
Fri May 1 12:37:51 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Sirraide

<details>
<summary>Changes</summary>

Currently, `ASTUnit::LoadFromCompilerInvocation()` just calls the constructor of `ASTUnit`, which default-constructs a `CodeGenOptions` instance. The options from the `CompilerInstance` are never actually saved in the `ASTUnit`. As a result, serialising the `ASTUnit` ends up serialising the default-constructed `CodeGenOptions` rather than the _actual_ `CodeGenOptions`.

This is problematic if you attempt to do the following:
1. Call e.g. `buildASTFromCodeWithArgs()` to build an `ASTUnit`
2. Serialise that `ASTUnit`.
3. Call e.g. `buildASTFromCodeWithArgs()` again with the exact same arguments and add use the first `ASTUnit` as a PCH using `-include-pch`.

This causes Clang to error because the `CodeGenOptions` we deserialised for the first `ASTUnit` are incompatible with the `CodeGenOptions` implicitly created for the second TU... despite the fact that we basically just attempted to build two TUs with the same arguments.

To fix this, copy the `CodeGenOptions` from the `CompilerInstance` into the `ASTUnit`.

---
Full diff: https://github.com/llvm/llvm-project/pull/195338.diff


1 Files Affected:

- (modified) clang/lib/Frontend/ASTUnit.cpp (+1) 


``````````diff
diff --git a/clang/lib/Frontend/ASTUnit.cpp b/clang/lib/Frontend/ASTUnit.cpp
index 05ae1f348f920..6a31da13fe1d1 100644
--- a/clang/lib/Frontend/ASTUnit.cpp
+++ b/clang/lib/Frontend/ASTUnit.cpp
@@ -1679,6 +1679,7 @@ std::unique_ptr<ASTUnit> ASTUnit::LoadFromCompilerInvocation(
   // Create the AST unit.
   std::unique_ptr<ASTUnit> AST(new ASTUnit(false));
   ConfigureDiags(Diags, *AST, CaptureDiagnostics);
+  *AST->CodeGenOpts = CI->getCodeGenOpts();
   AST->DiagOpts = DiagOpts;
   AST->Diagnostics = Diags;
   AST->OnlyLocalDecls = OnlyLocalDecls;

``````````

</details>


https://github.com/llvm/llvm-project/pull/195338


More information about the cfe-commits mailing list