[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:29 PDT 2026


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

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`.

>From c5923d21bc4379ddf949c7f1af86fe4b17ffb4fc Mon Sep 17 00:00:00 2001
From: Sirraide <aeternalmail at gmail.com>
Date: Fri, 1 May 2026 21:31:03 +0200
Subject: [PATCH] [Clang] ASTUnit should use the CompilerInstance's
 CodeGenOptions

---
 clang/lib/Frontend/ASTUnit.cpp | 1 +
 1 file changed, 1 insertion(+)

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;



More information about the cfe-commits mailing list