From 3dfa76e78d87e04dedcd82a7122c44536c7e632e Mon Sep 17 00:00:00 2001 From: Jan Svoboda Date: Mon, 30 Jun 2025 13:44:36 -0700 Subject: [PATCH 1/5] [clang] Serialize `CodeGenOptions` Some `LangOptions` duplicate their `CodeGenOptions` counterparts. My understanding is that this was done solely because some infrastructure (like serialization, module compatibility checks, etc.) were only implemented for `LangOptions`. This PR starts adds support for `CodeGenOptions`, serializes them into AST files and implements compatibility checking, which allows deduplicating some of these fields. --- clang/include/clang/Basic/CodeGenOptions.def | 26 ++-- .../Basic/DiagnosticSerializationKinds.td | 2 + clang/include/clang/Basic/LangOptions.def | 3 - clang/include/clang/Frontend/ASTUnit.h | 1 + clang/include/clang/Frontend/Utils.h | 3 +- clang/include/clang/Lex/Preprocessor.h | 8 +- .../include/clang/Serialization/ASTBitCodes.h | 5 +- clang/include/clang/Serialization/ASTReader.h | 34 +++-- clang/lib/Basic/CodeGenOptions.cpp | 3 +- clang/lib/CodeGen/CGDebugInfo.cpp | 36 +++--- clang/lib/CodeGen/CGOpenMPRuntime.cpp | 4 +- clang/lib/Frontend/ASTUnit.cpp | 2 +- clang/lib/Frontend/CompilerInstance.cpp | 12 +- clang/lib/Frontend/CompilerInvocation.cpp | 25 ++-- clang/lib/Frontend/FrontendAction.cpp | 5 +- clang/lib/Frontend/InitPreprocessor.cpp | 119 +++++++++--------- clang/lib/Lex/Preprocessor.cpp | 6 +- clang/lib/Serialization/ASTReader.cpp | 87 ++++++++++++- clang/lib/Serialization/ASTWriter.cpp | 12 ++ clang/test/Modules/implicit-opt-level.c | 15 +++ clang/test/PCH/no-validate-pch.cl | 2 +- .../Analysis/MacroExpansionContextTest.cpp | 4 +- clang/unittests/Basic/SourceManagerTest.cpp | 16 ++- clang/unittests/Lex/LexerTest.cpp | 6 +- clang/unittests/Lex/ModuleDeclStateTest.cpp | 5 +- clang/unittests/Lex/PPCallbacksTest.cpp | 26 ++-- .../Lex/PPConditionalDirectiveRecordTest.cpp | 6 +- .../Lex/PPDependencyDirectivesTest.cpp | 6 +- .../unittests/Lex/PPMemoryAllocationsTest.cpp | 6 +- .../Parse/ParseHLSLRootSignatureTest.cpp | 5 +- 30 files changed, 322 insertions(+), 168 deletions(-) create mode 100644 clang/test/Modules/implicit-opt-level.c diff --git a/clang/include/clang/Basic/CodeGenOptions.def b/clang/include/clang/Basic/CodeGenOptions.def index e5566a540dc65..0526ac2a7f2b7 100644 --- a/clang/include/clang/Basic/CodeGenOptions.def +++ b/clang/include/clang/Basic/CodeGenOptions.def @@ -12,8 +12,10 @@ // that have enumeration type and VALUE_CODEGENOPT is a code // generation option that describes a value rather than a flag. // -// AFFECTING_VALUE_CODEGENOPT is used for code generation options that can -// affect the AST. +// COMPATIBLE_VALUE_CODEGENOPT is used for code generation options that affect +// the construction of the AST in a way that doesn't prevent +// interoperability (that is, the value can be different between an explicit +// module and the user of that module). // //===----------------------------------------------------------------------===// #ifndef CODEGENOPT @@ -30,11 +32,16 @@ CODEGENOPT(Name, Bits, Default) CODEGENOPT(Name, Bits, Default) #endif -#ifndef AFFECTING_VALUE_CODEGENOPT -# define AFFECTING_VALUE_CODEGENOPT(Name, Bits, Default) \ +#ifndef COMPATIBLE_VALUE_CODEGENOPT +# define COMPATIBLE_VALUE_CODEGENOPT(Name, Bits, Default, Description) \ VALUE_CODEGENOPT(Name, Bits, Default) #endif +#ifndef COMPATIBLE_ENUM_CODEGENOPT +# define COMPATIBLE_ENUM_CODEGENOPT(Name, Type, Bits, Default, Description) \ +ENUM_CODEGENOPT(Name, Type, Bits, Default) +#endif + CODEGENOPT(DisableIntegratedAS, 1, 0) ///< -no-integrated-as CODEGENOPT(Crel, 1, 0) ///< -Wa,--crel CODEGENOPT(ImplicitMapSyms, 1, 0) ///< -Wa,-mmapsyms=implicit @@ -216,9 +223,9 @@ CODEGENOPT(ObjCConvertMessagesToRuntimeCalls , 1, 1) CODEGENOPT(ObjCAvoidHeapifyLocalBlocks, 1, 0) -// The optimization options affect frontend options, whicn in turn do affect the AST. -AFFECTING_VALUE_CODEGENOPT(OptimizationLevel, 2, 0) ///< The -O[0-3] option specified. -AFFECTING_VALUE_CODEGENOPT(OptimizeSize, 2, 0) ///< If -Os (==1) or -Oz (==2) is specified. +// The optimization options affect frontend options, which in turn do affect the AST. +COMPATIBLE_VALUE_CODEGENOPT(OptimizationLevel, 2, 0, "optimization level") ///< The -O[0-3] option specified. +COMPATIBLE_VALUE_CODEGENOPT(OptimizeSize, 2, 0, "optimizing for size") ///< If -Os (==1) or -Oz (==2) is specified. CODEGENOPT(AtomicProfileUpdate , 1, 0) ///< Set -fprofile-update=atomic CODEGENOPT(ContinuousProfileSync, 1, 0) ///< Enable continuous instrumentation profiling @@ -383,7 +390,7 @@ VALUE_CODEGENOPT(SmallDataLimit, 32, 0) VALUE_CODEGENOPT(SSPBufferSize, 32, 0) /// The kind of inlining to perform. -ENUM_CODEGENOPT(Inlining, InliningMethod, 2, NormalInlining) +COMPATIBLE_ENUM_CODEGENOPT(Inlining, InliningMethod, 2, NormalInlining, "inlining kind") /// The maximum stack size a function can have to be considered for inlining. VALUE_CODEGENOPT(InlineMaxStackSize, 32, UINT_MAX) @@ -494,4 +501,5 @@ ENUM_CODEGENOPT(WinX64EHUnwindV2, llvm::WinX64EHUnwindV2Mode, #undef CODEGENOPT #undef ENUM_CODEGENOPT #undef VALUE_CODEGENOPT -#undef AFFECTING_VALUE_CODEGENOPT +#undef COMPATIBLE_VALUE_CODEGENOPT +#undef COMPATIBLE_ENUM_CODEGENOPT diff --git a/clang/include/clang/Basic/DiagnosticSerializationKinds.td b/clang/include/clang/Basic/DiagnosticSerializationKinds.td index 584c8d62280bf..49eb6c195715c 100644 --- a/clang/include/clang/Basic/DiagnosticSerializationKinds.td +++ b/clang/include/clang/Basic/DiagnosticSerializationKinds.td @@ -41,6 +41,8 @@ def err_ast_file_langopt_mismatch : Error<"%0 was %select{disabled|enabled}1 in "precompiled file '%3' but is currently %select{disabled|enabled}2">; def err_ast_file_langopt_value_mismatch : Error< "%0 differs in precompiled file '%1' vs. current file">; +def err_ast_file_codegenopt_value_mismatch + : Error<"%0 differs in precompiled file '%1' vs. current file">; def err_ast_file_diagopt_mismatch : Error<"%0 is currently enabled, but was not in " "the precompiled file '%1'">; def err_ast_file_modulecache_mismatch : Error<"precompiled file '%2' was compiled with module cache " diff --git a/clang/include/clang/Basic/LangOptions.def b/clang/include/clang/Basic/LangOptions.def index 789761c1f3647..2a6eed0fdc9b3 100644 --- a/clang/include/clang/Basic/LangOptions.def +++ b/clang/include/clang/Basic/LangOptions.def @@ -206,8 +206,6 @@ COMPATIBLE_LANGOPT(ModulesValidateTextualHeaderIncludes, 1, 1, "validation of te BENIGN_LANGOPT(ModulesErrorRecovery, 1, 1, "automatically importing modules as needed when performing error recovery") BENIGN_LANGOPT(ImplicitModules, 1, 1, "building modules that are not specified via -fmodule-file") COMPATIBLE_LANGOPT(ModulesLocalVisibility, 1, 0, "local submodule visibility") -COMPATIBLE_LANGOPT(Optimize , 1, 0, "__OPTIMIZE__ predefined macro") -COMPATIBLE_LANGOPT(OptimizeSize , 1, 0, "__OPTIMIZE_SIZE__ predefined macro") COMPATIBLE_LANGOPT(Static , 1, 0, "__STATIC__ predefined macro (as opposed to __DYNAMIC__)") VALUE_LANGOPT(PackStruct , 32, 0, "default struct packing maximum alignment") @@ -224,7 +222,6 @@ COMPATIBLE_VALUE_LANGOPT(PIE , 1, 0, "is pie") LANGOPT(ROPI , 1, 0, "Read-only position independence") LANGOPT(RWPI , 1, 0, "Read-write position independence") COMPATIBLE_LANGOPT(GNUInline , 1, 0, "GNU inline semantics") -COMPATIBLE_LANGOPT(NoInlineDefine , 1, 0, "__NO_INLINE__ predefined macro") COMPATIBLE_LANGOPT(Deprecated , 1, 0, "__DEPRECATED predefined macro") COMPATIBLE_LANGOPT(FastMath , 1, 0, "fast FP math optimizations, and __FAST_MATH__ predefined macro") COMPATIBLE_LANGOPT(UnsafeFPMath , 1, 0, "Unsafe Floating Point Math") diff --git a/clang/include/clang/Frontend/ASTUnit.h b/clang/include/clang/Frontend/ASTUnit.h index 1485192e8f1e3..f0076d2a7cdb0 100644 --- a/clang/include/clang/Frontend/ASTUnit.h +++ b/clang/include/clang/Frontend/ASTUnit.h @@ -107,6 +107,7 @@ class ASTUnit { private: std::unique_ptr LangOpts; + std::unique_ptr CGOpts = std::make_unique(); // FIXME: The documentation on \c LoadFrom* member functions states that the // DiagnosticsEngine (and therefore DiagnosticOptions) must outlive the // returned ASTUnit. This is not the case. Enfore it by storing non-owning diff --git a/clang/include/clang/Frontend/Utils.h b/clang/include/clang/Frontend/Utils.h index 604e42067a3f1..fe6dc923363d6 100644 --- a/clang/include/clang/Frontend/Utils.h +++ b/clang/include/clang/Frontend/Utils.h @@ -49,8 +49,7 @@ class CodeGenOptions; /// environment ready to process a single file. void InitializePreprocessor(Preprocessor &PP, const PreprocessorOptions &PPOpts, const PCHContainerReader &PCHContainerRdr, - const FrontendOptions &FEOpts, - const CodeGenOptions &CodeGenOpts); + const FrontendOptions &FEOpts); /// DoPrintPreprocessedInput - Implement -E mode. void DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS, diff --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h index 4d82e20e5d4f3..237ae290be698 100644 --- a/clang/include/clang/Lex/Preprocessor.h +++ b/clang/include/clang/Lex/Preprocessor.h @@ -14,6 +14,7 @@ #ifndef LLVM_CLANG_LEX_PREPROCESSOR_H #define LLVM_CLANG_LEX_PREPROCESSOR_H +#include "clang/Basic/CodeGenOptions.h" #include "clang/Basic/Diagnostic.h" #include "clang/Basic/DiagnosticIDs.h" #include "clang/Basic/IdentifierTable.h" @@ -155,6 +156,7 @@ class Preprocessor { const PreprocessorOptions &PPOpts; DiagnosticsEngine *Diags; const LangOptions &LangOpts; + const CodeGenOptions &CGOpts; const TargetInfo *Target = nullptr; const TargetInfo *AuxTarget = nullptr; FileManager &FileMgr; @@ -1181,8 +1183,9 @@ class Preprocessor { public: Preprocessor(const PreprocessorOptions &PPOpts, DiagnosticsEngine &diags, - const LangOptions &LangOpts, SourceManager &SM, - HeaderSearch &Headers, ModuleLoader &TheModuleLoader, + const LangOptions &LangOpts, const CodeGenOptions &CGOPts, + SourceManager &SM, HeaderSearch &Headers, + ModuleLoader &TheModuleLoader, IdentifierInfoLookup *IILookup = nullptr, bool OwnsHeaderSearch = false, TranslationUnitKind TUKind = TU_Complete); @@ -1216,6 +1219,7 @@ class Preprocessor { void setDiagnostics(DiagnosticsEngine &D) { Diags = &D; } const LangOptions &getLangOpts() const { return LangOpts; } + const CodeGenOptions &getCodeGenOpts() const { return CGOpts; } const TargetInfo &getTargetInfo() const { return *Target; } const TargetInfo *getAuxTargetInfo() const { return AuxTarget; } FileManager &getFileManager() const { return FileMgr; } diff --git a/clang/include/clang/Serialization/ASTBitCodes.h b/clang/include/clang/Serialization/ASTBitCodes.h index 9d265f27b8e31..441047d64f48c 100644 --- a/clang/include/clang/Serialization/ASTBitCodes.h +++ b/clang/include/clang/Serialization/ASTBitCodes.h @@ -44,7 +44,7 @@ namespace serialization { /// Version 4 of AST files also requires that the version control branch and /// revision match exactly, since there is no backward compatibility of /// AST files at this time. -const unsigned VERSION_MAJOR = 34; +const unsigned VERSION_MAJOR = 35; /// AST file minor version number supported by this version of /// Clang. @@ -399,6 +399,9 @@ enum OptionsRecordTypes { /// Record code for the preprocessor options table. PREPROCESSOR_OPTIONS, + + /// Record code for the codegen options table. + CODEGEN_OPTIONS, }; /// Record codes for the unhashed control block. diff --git a/clang/include/clang/Serialization/ASTReader.h b/clang/include/clang/Serialization/ASTReader.h index 7d4b4467eb97d..303b4c5941877 100644 --- a/clang/include/clang/Serialization/ASTReader.h +++ b/clang/include/clang/Serialization/ASTReader.h @@ -72,6 +72,7 @@ class ASTContext; class ASTDeserializationListener; class ASTReader; class ASTRecordReader; +class CodeGenOptions; class CXXTemporary; class Decl; class DeclarationName; @@ -137,6 +138,15 @@ class ASTReaderListener { return false; } + /// Receives the codegen options. + /// + /// \returns true to indicate the options are invalid or false otherwise. + virtual bool ReadCodeGenOptions(const CodeGenOptions &CGOpts, + StringRef ModuleFilename, bool Complain, + bool AllowCompatibleDifferences) { + return false; + } + /// Receives the target options. /// /// \returns true to indicate the target options are invalid, or false @@ -281,6 +291,9 @@ class ChainedASTReaderListener : public ASTReaderListener { bool ReadLanguageOptions(const LangOptions &LangOpts, StringRef ModuleFilename, bool Complain, bool AllowCompatibleDifferences) override; + bool ReadCodeGenOptions(const CodeGenOptions &CGOpts, + StringRef ModuleFilename, bool Complain, + bool AllowCompatibleDifferences) override; bool ReadTargetOptions(const TargetOptions &TargetOpts, StringRef ModuleFilename, bool Complain, bool AllowCompatibleDifferences) override; @@ -322,6 +335,9 @@ class PCHValidator : public ASTReaderListener { bool ReadLanguageOptions(const LangOptions &LangOpts, StringRef ModuleFilename, bool Complain, bool AllowCompatibleDifferences) override; + bool ReadCodeGenOptions(const CodeGenOptions &CGOpts, + StringRef ModuleFilename, bool Complain, + bool AllowCompatibleDifferences) override; bool ReadTargetOptions(const TargetOptions &TargetOpts, StringRef ModuleFilename, bool Complain, bool AllowCompatibleDifferences) override; @@ -1586,6 +1602,10 @@ class ASTReader StringRef ModuleFilename, bool Complain, ASTReaderListener &Listener, bool AllowCompatibleDifferences); + static bool ParseCodeGenOptions(const RecordData &Record, + StringRef ModuleFilename, bool Complain, + ASTReaderListener &Listener, + bool AllowCompatibleDifferences); static bool ParseTargetOptions(const RecordData &Record, StringRef ModuleFilename, bool Complain, ASTReaderListener &Listener, @@ -1996,14 +2016,12 @@ class ASTReader /// Determine whether the given AST file is acceptable to load into a /// translation unit with the given language and target options. - static bool isAcceptableASTFile(StringRef Filename, FileManager &FileMgr, - const ModuleCache &ModCache, - const PCHContainerReader &PCHContainerRdr, - const LangOptions &LangOpts, - const TargetOptions &TargetOpts, - const PreprocessorOptions &PPOpts, - StringRef ExistingModuleCachePath, - bool RequireStrictOptionMatches = false); + static bool isAcceptableASTFile( + StringRef Filename, FileManager &FileMgr, const ModuleCache &ModCache, + const PCHContainerReader &PCHContainerRdr, const LangOptions &LangOpts, + const CodeGenOptions &CGOpts, const TargetOptions &TargetOpts, + const PreprocessorOptions &PPOpts, StringRef ExistingModuleCachePath, + bool RequireStrictOptionMatches = false); /// Returns the suggested contents of the predefines buffer, /// which contains a (typically-empty) subset of the predefines diff --git a/clang/lib/Basic/CodeGenOptions.cpp b/clang/lib/Basic/CodeGenOptions.cpp index 1f899dee48dbd..4b077c6071da5 100644 --- a/clang/lib/Basic/CodeGenOptions.cpp +++ b/clang/lib/Basic/CodeGenOptions.cpp @@ -26,7 +26,8 @@ void CodeGenOptions::resetNonModularOptions(StringRef ModuleFormat) { #define CODEGENOPT(Name, Bits, Default) Name = Default; #define ENUM_CODEGENOPT(Name, Type, Bits, Default) set##Name(Default); // Do not reset AST affecting code generation options. -#define AFFECTING_VALUE_CODEGENOPT(Name, Bits, Default) +#define COMPATIBLE_VALUE_CODEGENOPT(Name, Bits, Default, Description) +#define COMPATIBLE_ENUM_CODEGENOPT(Name, Type, Bits, Default, Description) #include "clang/Basic/CodeGenOptions.def" // Next reset all debug options that can always be reset, because they never diff --git a/clang/lib/CodeGen/CGDebugInfo.cpp b/clang/lib/CodeGen/CGDebugInfo.cpp index 3feadf1ba0c7d..f2ff0f6d55da1 100644 --- a/clang/lib/CodeGen/CGDebugInfo.cpp +++ b/clang/lib/CodeGen/CGDebugInfo.cpp @@ -797,7 +797,8 @@ void CGDebugInfo::CreateCompileUnit() { // Create new compile unit. TheCU = DBuilder.createCompileUnit( LangTag, CUFile, CGOpts.EmitVersionIdentMetadata ? Producer : "", - LO.Optimize || CGOpts.PrepareForLTO || CGOpts.PrepareForThinLTO, + CGOpts.OptimizationLevel != 0 || CGOpts.PrepareForLTO || + CGOpts.PrepareForThinLTO, CGOpts.DwarfDebugFlags, RuntimeVers, CGOpts.SplitDwarfFile, EmissionKind, DwoId, CGOpts.SplitDwarfInlining, CGOpts.DebugInfoForProfiling, NameTableKind, CGOpts.DebugRangesBaseAddress, remapDIPath(Sysroot), SDK); @@ -2273,7 +2274,7 @@ llvm::DISubprogram *CGDebugInfo::CreateCXXMemberFunction( Flags |= llvm::DINode::FlagRValueReference; if (!Method->isExternallyVisible()) SPFlags |= llvm::DISubprogram::SPFlagLocalToUnit; - if (CGM.getLangOpts().Optimize) + if (CGM.getCodeGenOpts().OptimizationLevel != 0) SPFlags |= llvm::DISubprogram::SPFlagOptimized; // In this debug mode, emit type info for a class when its constructor type @@ -4344,7 +4345,7 @@ llvm::DISubprogram *CGDebugInfo::getFunctionFwdDeclOrStub(GlobalDecl GD, FD->getReturnType(), ArgTypes, FunctionProtoType::ExtProtoInfo(CC)); if (!FD->isExternallyVisible()) SPFlags |= llvm::DISubprogram::SPFlagLocalToUnit; - if (CGM.getLangOpts().Optimize) + if (CGM.getCodeGenOpts().OptimizationLevel != 0) SPFlags |= llvm::DISubprogram::SPFlagOptimized; if (Stub) { @@ -4664,7 +4665,7 @@ void CGDebugInfo::emitFunctionStart(GlobalDecl GD, SourceLocation Loc, if (Fn->hasLocalLinkage()) SPFlags |= llvm::DISubprogram::SPFlagLocalToUnit; - if (CGM.getLangOpts().Optimize) + if (CGM.getCodeGenOpts().OptimizationLevel != 0) SPFlags |= llvm::DISubprogram::SPFlagOptimized; llvm::DINode::DIFlags FlagsForDef = Flags | getCallSiteRelatedAttrs(); @@ -4747,7 +4748,7 @@ void CGDebugInfo::EmitFunctionDecl(GlobalDecl GD, SourceLocation Loc, unsigned LineNo = getLineNumber(Loc); unsigned ScopeLine = 0; llvm::DISubprogram::DISPFlags SPFlags = llvm::DISubprogram::SPFlagZero; - if (CGM.getLangOpts().Optimize) + if (CGM.getCodeGenOpts().OptimizationLevel != 0) SPFlags |= llvm::DISubprogram::SPFlagOptimized; llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(D); @@ -5079,7 +5080,8 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD, // Use VarDecl's Tag, Scope and Line number. auto FieldAlign = getDeclAlignIfRequired(Field, CGM.getContext()); auto *D = DBuilder.createAutoVariable( - Scope, FieldName, Unit, Line, FieldTy, CGM.getLangOpts().Optimize, + Scope, FieldName, Unit, Line, FieldTy, + CGM.getCodeGenOpts().OptimizationLevel != 0, Flags | llvm::DINode::FlagArtificial, FieldAlign); // Insert an llvm.dbg.declare into the current block. @@ -5105,9 +5107,9 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD, llvm::DILocalVariable *D = nullptr; if (ArgNo) { llvm::DINodeArray Annotations = CollectBTFDeclTagAnnotations(VD); - D = DBuilder.createParameterVariable(Scope, Name, *ArgNo, Unit, Line, Ty, - CGM.getLangOpts().Optimize, Flags, - Annotations); + D = DBuilder.createParameterVariable( + Scope, Name, *ArgNo, Unit, Line, Ty, + CGM.getCodeGenOpts().OptimizationLevel != 0, Flags, Annotations); } else { // For normal local variable, we will try to find out whether 'VD' is the // copy parameter of coroutine. @@ -5148,8 +5150,9 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD, D = RemapCoroArgToLocalVar(); // Or we will create a new DIVariable for this Decl if D dose not exists. if (!D) - D = DBuilder.createAutoVariable(Scope, Name, Unit, Line, Ty, - CGM.getLangOpts().Optimize, Flags, Align); + D = DBuilder.createAutoVariable( + Scope, Name, Unit, Line, Ty, + CGM.getCodeGenOpts().OptimizationLevel != 0, Flags, Align); } // Insert an llvm.dbg.declare into the current block. DBuilder.insertDeclare(Storage, D, DBuilder.createExpression(Expr), @@ -5203,7 +5206,7 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const BindingDecl *BD, auto *Scope = cast(LexicalBlockStack.back()); // Create the descriptor for the variable. llvm::DILocalVariable *D = DBuilder.createAutoVariable( - Scope, Name, Unit, Line, Ty, CGM.getLangOpts().Optimize, + Scope, Name, Unit, Line, Ty, CGM.getCodeGenOpts().OptimizationLevel != 0, llvm::DINode::FlagZero, Align); if (const MemberExpr *ME = dyn_cast(BD->getBinding())) { @@ -5301,8 +5304,8 @@ void CGDebugInfo::EmitLabel(const LabelDecl *D, CGBuilderTy &Builder) { StringRef Name = D->getName(); // Create the descriptor for the label. - auto *L = - DBuilder.createLabel(Scope, Name, Unit, Line, CGM.getLangOpts().Optimize); + auto *L = DBuilder.createLabel(Scope, Name, Unit, Line, + CGM.getCodeGenOpts().OptimizationLevel != 0); // Insert an llvm.dbg.label into the current block. DBuilder.insertLabel(L, @@ -5565,7 +5568,8 @@ void CGDebugInfo::EmitDeclareOfBlockLiteralArgVariable(const CGBlockInfo &block, // Create the descriptor for the parameter. auto *debugVar = DBuilder.createParameterVariable( - scope, Name, ArgNo, tunit, line, type, CGM.getLangOpts().Optimize, flags); + scope, Name, ArgNo, tunit, line, type, + CGM.getCodeGenOpts().OptimizationLevel != 0, flags); // Insert an llvm.dbg.declare into the current block. DBuilder.insertDeclare(Alloca, debugVar, DBuilder.createExpression(), @@ -6340,7 +6344,7 @@ llvm::DebugLoc CGDebugInfo::SourceLocToDebugLoc(SourceLocation Loc) { llvm::DINode::DIFlags CGDebugInfo::getCallSiteRelatedAttrs() const { // Call site-related attributes are only useful in optimized programs, and // when there's a possibility of debugging backtraces. - if (!CGM.getLangOpts().Optimize || + if (CGM.getCodeGenOpts().OptimizationLevel == 0 || DebugKind == llvm::codegenoptions::NoDebugInfo || DebugKind == llvm::codegenoptions::LocTrackingOnly) return llvm::DINode::FlagZero; diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp index 8ccc37ef98a74..d26461d8e427a 100644 --- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp +++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp @@ -1092,7 +1092,7 @@ emitCombinerOrInitializer(CodeGenModule &CGM, QualType Ty, auto *Fn = llvm::Function::Create(FnTy, llvm::GlobalValue::InternalLinkage, Name, &CGM.getModule()); CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, FnInfo); - if (CGM.getLangOpts().Optimize) { + if (CGM.getCodeGenOpts().OptimizationLevel != 0) { Fn->removeFnAttr(llvm::Attribute::NoInline); Fn->removeFnAttr(llvm::Attribute::OptimizeNone); Fn->addFnAttr(llvm::Attribute::AlwaysInline); @@ -3199,7 +3199,7 @@ emitTaskPrivateMappingFunction(CodeGenModule &CGM, SourceLocation Loc, &CGM.getModule()); CGM.SetInternalFunctionAttributes(GlobalDecl(), TaskPrivatesMap, TaskPrivatesMapFnInfo); - if (CGM.getLangOpts().Optimize) { + if (CGM.getCodeGenOpts().OptimizationLevel != 0) { TaskPrivatesMap->removeFnAttr(llvm::Attribute::NoInline); TaskPrivatesMap->removeFnAttr(llvm::Attribute::OptimizeNone); TaskPrivatesMap->addFnAttr(llvm::Attribute::AlwaysInline); diff --git a/clang/lib/Frontend/ASTUnit.cpp b/clang/lib/Frontend/ASTUnit.cpp index 3beff3d6d849f..5db3ae8385acf 100644 --- a/clang/lib/Frontend/ASTUnit.cpp +++ b/clang/lib/Frontend/ASTUnit.cpp @@ -842,7 +842,7 @@ std::unique_ptr ASTUnit::LoadFromASTFile( HeaderSearch &HeaderInfo = *AST->HeaderInfo; AST->PP = std::make_shared( - *AST->PPOpts, AST->getDiagnostics(), *AST->LangOpts, + *AST->PPOpts, AST->getDiagnostics(), *AST->LangOpts, *AST->CGOpts, AST->getSourceManager(), HeaderInfo, AST->ModuleLoader, /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp index 09a66b652518f..f254b23eec0ed 100644 --- a/clang/lib/Frontend/CompilerInstance.cpp +++ b/clang/lib/Frontend/CompilerInstance.cpp @@ -449,11 +449,11 @@ void CompilerInstance::createPreprocessor(TranslationUnitKind TUKind) { HeaderSearch *HeaderInfo = new HeaderSearch(getHeaderSearchOpts(), getSourceManager(), getDiagnostics(), getLangOpts(), &getTarget()); - PP = std::make_shared(Invocation->getPreprocessorOpts(), - getDiagnostics(), getLangOpts(), - getSourceManager(), *HeaderInfo, *this, - /*IdentifierInfoLookup=*/nullptr, - /*OwnsHeaderSearch=*/true, TUKind); + PP = std::make_shared( + Invocation->getPreprocessorOpts(), getDiagnostics(), getLangOpts(), + getCodeGenOpts(), getSourceManager(), *HeaderInfo, *this, + /*IdentifierInfoLookup=*/nullptr, + /*OwnsHeaderSearch=*/true, TUKind); getTarget().adjust(getDiagnostics(), getLangOpts()); PP->Initialize(getTarget(), getAuxTarget()); @@ -466,7 +466,7 @@ void CompilerInstance::createPreprocessor(TranslationUnitKind TUKind) { // Predefine macros and configure the preprocessor. InitializePreprocessor(*PP, PPOpts, getPCHContainerReader(), - getFrontendOpts(), getCodeGenOpts()); + getFrontendOpts()); // Initialize the header search object. In CUDA compilations, we use the aux // triple (the host triple) to initialize our header search, since we need to diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index f366e90945dac..b3a2fdcc5127e 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -4397,22 +4397,6 @@ bool CompilerInvocation::ParseLangArgs(LangOptions &Opts, ArgList &Args, Opts.OpenACCMacroOverride = A->getValue(); } - // FIXME: Eliminate this dependency. - unsigned Opt = getOptimizationLevel(Args, IK, Diags), - OptSize = getOptimizationLevelSize(Args); - Opts.Optimize = Opt != 0; - Opts.OptimizeSize = OptSize != 0; - - // This is the __NO_INLINE__ define, which just depends on things like the - // optimization level and -fno-inline, not actually whether the backend has - // inlining enabled. - Opts.NoInlineDefine = !Opts.Optimize; - if (Arg *InlineArg = Args.getLastArg( - options::OPT_finline_functions, options::OPT_finline_hint_functions, - options::OPT_fno_inline_functions, options::OPT_fno_inline)) - if (InlineArg->getOption().matches(options::OPT_fno_inline)) - Opts.NoInlineDefine = true; - if (Arg *A = Args.getLastArg(OPT_ffp_contract)) { StringRef Val = A->getValue(); if (Val == "fast") @@ -5254,6 +5238,15 @@ std::string CompilerInvocation::getModuleHash() const { HBuilder.add(*Build); } + // The default CODEGENOPT is benign, so only include the ones marked as + // compatible into the hash. +#define CODEGENOPT(Name, Bits, Default) +#define COMPATIBLE_VALUE_CODEGENOPT(Name, Bits, Default, Description) \ + HBuilder.add(CodeGenOpts->Name); +#define COMPATIBLE_ENUM_CODEGENOPT(Name, Type, Bits, Default, Description) \ + HBuilder.add(static_cast(CodeGenOpts->get##Name())); +#include "clang/Basic/CodeGenOptions.def" + // When compiling with -gmodules, also hash -fdebug-prefix-map as it // affects the debug info in the PCM. if (getCodeGenOpts().DebugTypeExtRefs) diff --git a/clang/lib/Frontend/FrontendAction.cpp b/clang/lib/Frontend/FrontendAction.cpp index ef7ae27a2694a..7e5668d56f7cd 100644 --- a/clang/lib/Frontend/FrontendAction.cpp +++ b/clang/lib/Frontend/FrontendAction.cpp @@ -947,8 +947,9 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI, if (ASTReader::isAcceptableASTFile( Dir->path(), FileMgr, CI.getModuleCache(), CI.getPCHContainerReader(), CI.getLangOpts(), - CI.getTargetOpts(), CI.getPreprocessorOpts(), - SpecificModuleCachePath, /*RequireStrictOptionMatches=*/true)) { + CI.getCodeGenOpts(), CI.getTargetOpts(), + CI.getPreprocessorOpts(), SpecificModuleCachePath, + /*RequireStrictOptionMatches=*/true)) { PPOpts.ImplicitPCHInclude = std::string(Dir->path()); Found = true; break; diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp index f64613fb4a6cb..e24f37dc171ca 100644 --- a/clang/lib/Frontend/InitPreprocessor.cpp +++ b/clang/lib/Frontend/InitPreprocessor.cpp @@ -47,8 +47,7 @@ static void DefineBuiltinMacro(MacroBuilder &Builder, StringRef Macro, // Per GCC -D semantics, the macro ends at \n if it exists. StringRef::size_type End = MacroBody.find_first_of("\n\r"); if (End != StringRef::npos) - Diags.Report(diag::warn_fe_macro_contains_embedded_newline) - << MacroName; + Diags.Report(diag::warn_fe_macro_contains_embedded_newline) << MacroName; MacroBody = MacroBody.substr(0, End); // We handle macro bodies which end in a backslash by appending an extra // backslash+newline. This makes sure we don't accidentally treat the @@ -97,17 +96,17 @@ template static T PickFP(const llvm::fltSemantics *Sem, T IEEEHalfVal, T IEEESingleVal, T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal, T IEEEQuadVal) { - if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEhalf()) + if (Sem == (const llvm::fltSemantics *)&llvm::APFloat::IEEEhalf()) return IEEEHalfVal; - if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle()) + if (Sem == (const llvm::fltSemantics *)&llvm::APFloat::IEEEsingle()) return IEEESingleVal; - if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble()) + if (Sem == (const llvm::fltSemantics *)&llvm::APFloat::IEEEdouble()) return IEEEDoubleVal; - if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended()) + if (Sem == (const llvm::fltSemantics *)&llvm::APFloat::x87DoubleExtended()) return X87DoubleExtendedVal; - if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble()) + if (Sem == (const llvm::fltSemantics *)&llvm::APFloat::PPCDoubleDouble()) return PPCDoubleDoubleVal; - assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad()); + assert(Sem == (const llvm::fltSemantics *)&llvm::APFloat::IEEEquad()); return IEEEQuadVal; } @@ -133,8 +132,8 @@ static void DefineFloatMacros(MacroBuilder &Builder, StringRef Prefix, int Max10Exp = PickFP(Sem, 4, 38, 308, 4932, 308, 4932); int MinExp = PickFP(Sem, -13, -125, -1021, -16381, -968, -16381); int MaxExp = PickFP(Sem, 16, 128, 1024, 16384, 1024, 16384); - Min = PickFP(Sem, "6.103515625e-5", "1.17549435e-38", "2.2250738585072014e-308", - "3.36210314311209350626e-4932", + Min = PickFP(Sem, "6.103515625e-5", "1.17549435e-38", + "2.2250738585072014e-308", "3.36210314311209350626e-4932", "2.00416836000897277799610805135016e-292", "3.36210314311209350626267781732175260e-4932"); Max = PickFP(Sem, "6.5504e+4", "3.40282347e+38", "1.7976931348623157e+308", @@ -147,26 +146,25 @@ static void DefineFloatMacros(MacroBuilder &Builder, StringRef Prefix, DefPrefix += Prefix; DefPrefix += "_"; - Builder.defineMacro(DefPrefix + "DENORM_MIN__", Twine(DenormMin)+Ext); - Builder.defineMacro(DefPrefix + "NORM_MAX__", Twine(NormMax)+Ext); + Builder.defineMacro(DefPrefix + "DENORM_MIN__", Twine(DenormMin) + Ext); + Builder.defineMacro(DefPrefix + "NORM_MAX__", Twine(NormMax) + Ext); Builder.defineMacro(DefPrefix + "HAS_DENORM__"); Builder.defineMacro(DefPrefix + "DIG__", Twine(Digits)); Builder.defineMacro(DefPrefix + "DECIMAL_DIG__", Twine(DecimalDigits)); - Builder.defineMacro(DefPrefix + "EPSILON__", Twine(Epsilon)+Ext); + Builder.defineMacro(DefPrefix + "EPSILON__", Twine(Epsilon) + Ext); Builder.defineMacro(DefPrefix + "HAS_INFINITY__"); Builder.defineMacro(DefPrefix + "HAS_QUIET_NAN__"); Builder.defineMacro(DefPrefix + "MANT_DIG__", Twine(MantissaDigits)); Builder.defineMacro(DefPrefix + "MAX_10_EXP__", Twine(Max10Exp)); Builder.defineMacro(DefPrefix + "MAX_EXP__", Twine(MaxExp)); - Builder.defineMacro(DefPrefix + "MAX__", Twine(Max)+Ext); + Builder.defineMacro(DefPrefix + "MAX__", Twine(Max) + Ext); - Builder.defineMacro(DefPrefix + "MIN_10_EXP__","("+Twine(Min10Exp)+")"); - Builder.defineMacro(DefPrefix + "MIN_EXP__", "("+Twine(MinExp)+")"); - Builder.defineMacro(DefPrefix + "MIN__", Twine(Min)+Ext); + Builder.defineMacro(DefPrefix + "MIN_10_EXP__", "(" + Twine(Min10Exp) + ")"); + Builder.defineMacro(DefPrefix + "MIN_EXP__", "(" + Twine(MinExp) + ")"); + Builder.defineMacro(DefPrefix + "MIN__", Twine(Min) + Ext); } - /// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro /// named MacroName with the max value for a type with width 'TypeWidth' a /// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL). @@ -215,8 +213,7 @@ static void DefineTypeWidth(const Twine &MacroName, TargetInfo::IntType Ty, static void DefineTypeSizeof(StringRef MacroName, unsigned BitWidth, const TargetInfo &TI, MacroBuilder &Builder) { - Builder.defineMacro(MacroName, - Twine(BitWidth / TI.getCharWidth())); + Builder.defineMacro(MacroName, Twine(BitWidth / TI.getCharWidth())); } // This will generate a macro based on the prefix with `_MAX__` as the suffix @@ -313,7 +310,6 @@ static void DefineFastIntType(const LangOptions &LangOpts, unsigned TypeWidth, DefineFmt(LangOpts, Prefix + Twine(TypeWidth), Ty, TI, Builder); } - /// Get the value the ATOMIC_*_LOCK_FREE macro should have for a type with /// the specified properties. static const char *getLockFreeValue(unsigned TypeWidth, const TargetInfo &TI) { @@ -354,7 +350,8 @@ static void AddObjCXXARCLibstdcxxDefines(const LangOptions &LangOpts, if (LangOpts.ObjCAutoRefCount) { Out << "template\n" - << "struct __is_scalar<__attribute__((objc_ownership(strong))) _Tp> {\n" + << "struct __is_scalar<__attribute__((objc_ownership(strong))) _Tp> " + "{\n" << " enum { __value = 0 };\n" << " typedef __false_type __type;\n" << "};\n" @@ -549,12 +546,12 @@ static void InitializeStandardPredefinedMacros(const TargetInfo &TI, Builder.defineMacro("__CL_CPP_VERSION_2021__", "202100"); } else { // OpenCL v1.0 and v1.1 do not have a predefined macro to indicate the - // language standard with which the program is compiled. __OPENCL_VERSION__ - // is for the OpenCL version supported by the OpenCL device, which is not - // necessarily the language standard with which the program is compiled. - // A shared OpenCL header file requires a macro to indicate the language - // standard. As a workaround, __OPENCL_C_VERSION__ is defined for - // OpenCL v1.0 and v1.1. + // language standard with which the program is compiled. + // __OPENCL_VERSION__ is for the OpenCL version supported by the OpenCL + // device, which is not necessarily the language standard with which the + // program is compiled. A shared OpenCL header file requires a macro to + // indicate the language standard. As a workaround, __OPENCL_C_VERSION__ + // is defined for OpenCL v1.0 and v1.1. switch (LangOpts.OpenCLVersion) { case 100: Builder.defineMacro("__OPENCL_C_VERSION__", "100"); @@ -720,7 +717,7 @@ static void InitializeCPlusPlusFeatureTestMacros(const LangOptions &LangOpts, Builder.defineMacro("__cpp_capture_star_this", "201603L"); Builder.defineMacro("__cpp_if_constexpr", "201606L"); Builder.defineMacro("__cpp_deduction_guides", "201703L"); // (not latest) - Builder.defineMacro("__cpp_template_auto", "201606L"); // (old name) + Builder.defineMacro("__cpp_template_auto", "201606L"); // (old name) Builder.defineMacro("__cpp_namespace_attributes", "201411L"); Builder.defineMacro("__cpp_enumerator_attributes", "201411L"); Builder.defineMacro("__cpp_nested_namespace_definitions", "201411L"); @@ -750,7 +747,7 @@ static void InitializeCPlusPlusFeatureTestMacros(const LangOptions &LangOpts, Builder.defineMacro("__cpp_impl_coroutine", "201902L"); Builder.defineMacro("__cpp_designated_initializers", "201707L"); Builder.defineMacro("__cpp_impl_three_way_comparison", "201907L"); - //Builder.defineMacro("__cpp_modules", "201907L"); + // Builder.defineMacro("__cpp_modules", "201907L"); Builder.defineMacro("__cpp_using_enum", "201907L"); } // C++23 features. @@ -865,6 +862,7 @@ static void InitializePredefinedMacros(const TargetInfo &TI, const LangOptions &LangOpts, const FrontendOptions &FEOpts, const PreprocessorOptions &PPOpts, + const CodeGenOptions &CGOpts, MacroBuilder &Builder) { // Compiler version introspection macros. Builder.defineMacro("__llvm__"); // LLVM Backend @@ -876,9 +874,9 @@ static void InitializePredefinedMacros(const TargetInfo &TI, Builder.defineMacro("__clang_patchlevel__", TOSTR(CLANG_VERSION_PATCHLEVEL)); #undef TOSTR #undef TOSTR2 - Builder.defineMacro("__clang_version__", - "\"" CLANG_VERSION_STRING " " - + getClangFullRepositoryVersion() + "\""); + Builder.defineMacro("__clang_version__", "\"" CLANG_VERSION_STRING " " + + getClangFullRepositoryVersion() + + "\""); if (LangOpts.GNUCVersion != 0) { // Major, minor, patch, are given two decimal places each, so 4.2.1 becomes @@ -943,8 +941,8 @@ static void InitializePredefinedMacros(const TargetInfo &TI, // Previously this macro was set to a string aiming to achieve compatibility // with GCC 4.2.1. Now, just return the full Clang version - Builder.defineMacro("__VERSION__", "\"" + - Twine(getClangFullCPPVersion()) + "\""); + Builder.defineMacro("__VERSION__", + "\"" + Twine(getClangFullCPPVersion()) + "\""); // Initialize language-specific preprocessor defines. @@ -992,9 +990,9 @@ static void InitializePredefinedMacros(const TargetInfo &TI, VersionTuple tuple = LangOpts.ObjCRuntime.getVersion(); unsigned minor = tuple.getMinor().value_or(0); unsigned subminor = tuple.getSubminor().value_or(0); - Builder.defineMacro("__OBJFW_RUNTIME_ABI__", - Twine(tuple.getMajor() * 10000 + minor * 100 + - subminor)); + Builder.defineMacro( + "__OBJFW_RUNTIME_ABI__", + Twine(tuple.getMajor() * 10000 + minor * 100 + subminor)); } Builder.defineMacro("IBOutlet", "__attribute__((iboutlet))"); @@ -1016,7 +1014,7 @@ static void InitializePredefinedMacros(const TargetInfo &TI, // darwin_constant_cfstrings controls this. This is also dependent // on other things like the runtime I believe. This is set even for C code. if (!LangOpts.NoConstantCFStrings) - Builder.defineMacro("__CONSTANT_CFSTRINGS__"); + Builder.defineMacro("__CONSTANT_CFSTRINGS__"); if (LangOpts.ObjC) Builder.defineMacro("OBJC_NEW_PROPERTIES"); @@ -1073,9 +1071,9 @@ static void InitializePredefinedMacros(const TargetInfo &TI, Builder.defineMacro("__clang_wide_literal_encoding__", "\"UTF-16\""); } - if (LangOpts.Optimize) + if (CGOpts.OptimizationLevel != 0) Builder.defineMacro("__OPTIMIZE__"); - if (LangOpts.OptimizeSize) + if (CGOpts.OptimizeSize != 0) Builder.defineMacro("__OPTIMIZE_SIZE__"); if (LangOpts.FastMath) @@ -1089,8 +1087,8 @@ static void InitializePredefinedMacros(const TargetInfo &TI, // We don't support the PDP-11 as a target, but include // the define so it can still be compared against. Builder.defineMacro("__ORDER_LITTLE_ENDIAN__", "1234"); - Builder.defineMacro("__ORDER_BIG_ENDIAN__", "4321"); - Builder.defineMacro("__ORDER_PDP_ENDIAN__", "3412"); + Builder.defineMacro("__ORDER_BIG_ENDIAN__", "4321"); + Builder.defineMacro("__ORDER_PDP_ENDIAN__", "3412"); if (TI.isBigEndian()) { Builder.defineMacro("__BYTE_ORDER__", "__ORDER_BIG_ENDIAN__"); Builder.defineMacro("__BIG_ENDIAN__"); @@ -1156,7 +1154,8 @@ static void InitializePredefinedMacros(const TargetInfo &TI, DefineTypeSizeof("__SIZEOF_FLOAT__", TI.getFloatWidth(), TI, Builder); DefineTypeSizeof("__SIZEOF_INT__", TI.getIntWidth(), TI, Builder); DefineTypeSizeof("__SIZEOF_LONG__", TI.getLongWidth(), TI, Builder); - DefineTypeSizeof("__SIZEOF_LONG_DOUBLE__",TI.getLongDoubleWidth(),TI,Builder); + DefineTypeSizeof("__SIZEOF_LONG_DOUBLE__", TI.getLongDoubleWidth(), TI, + Builder); DefineTypeSizeof("__SIZEOF_LONG_LONG__", TI.getLongLongWidth(), TI, Builder); DefineTypeSizeof("__SIZEOF_POINTER__", TI.getPointerWidth(LangAS::Default), TI, Builder); @@ -1164,12 +1163,12 @@ static void InitializePredefinedMacros(const TargetInfo &TI, DefineTypeSizeof("__SIZEOF_PTRDIFF_T__", TI.getTypeWidth(TI.getPtrDiffType(LangAS::Default)), TI, Builder); - DefineTypeSizeof("__SIZEOF_SIZE_T__", - TI.getTypeWidth(TI.getSizeType()), TI, Builder); - DefineTypeSizeof("__SIZEOF_WCHAR_T__", - TI.getTypeWidth(TI.getWCharType()), TI, Builder); - DefineTypeSizeof("__SIZEOF_WINT_T__", - TI.getTypeWidth(TI.getWIntType()), TI, Builder); + DefineTypeSizeof("__SIZEOF_SIZE_T__", TI.getTypeWidth(TI.getSizeType()), TI, + Builder); + DefineTypeSizeof("__SIZEOF_WCHAR_T__", TI.getTypeWidth(TI.getWCharType()), TI, + Builder); + DefineTypeSizeof("__SIZEOF_WINT_T__", TI.getTypeWidth(TI.getWIntType()), TI, + Builder); if (TI.hasInt128Type()) DefineTypeSizeof("__SIZEOF_INT128__", 128, TI, Builder); @@ -1265,7 +1264,7 @@ static void InitializePredefinedMacros(const TargetInfo &TI, // Define __BIGGEST_ALIGNMENT__ to be compatible with gcc. Builder.defineMacro("__BIGGEST_ALIGNMENT__", - Twine(TI.getSuitableAlign() / TI.getCharWidth()) ); + Twine(TI.getSuitableAlign() / TI.getCharWidth())); if (!LangOpts.CharIsSigned) Builder.defineMacro("__CHAR_UNSIGNED__"); @@ -1370,7 +1369,7 @@ static void InitializePredefinedMacros(const TargetInfo &TI, Builder.append("#pragma push_macro(\"__GCC_CONSTRUCTIVE_SIZE\")"); auto addLockFreeMacros = [&](const llvm::Twine &Prefix) { - // Used by libc++ and libstdc++ to implement ATOMIC__LOCK_FREE. + // Used by libc++ and libstdc++ to implement ATOMIC__LOCK_FREE. #define DEFINE_LOCK_FREE_MACRO(TYPE, Type) \ Builder.defineMacro(Prefix + #TYPE "_LOCK_FREE", \ getLockFreeValue(TI.get##Type##Width(), TI)); @@ -1396,7 +1395,7 @@ static void InitializePredefinedMacros(const TargetInfo &TI, if (LangOpts.GNUCVersion) addLockFreeMacros("__GCC_ATOMIC_"); - if (LangOpts.NoInlineDefine) + if (CGOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining) Builder.defineMacro("__NO_INLINE__"); if (unsigned PICLevel = LangOpts.PICLevel) { @@ -1535,6 +1534,7 @@ static void InitializePredefinedMacros(const TargetInfo &TI, #define TARGET_OS(Name, Predicate) \ Builder.defineMacro(#Name, (Predicate) ? "1" : "0"); #include "clang/Basic/TargetOSMacros.def" + #undef TARGET_OS } @@ -1556,8 +1556,7 @@ static void InitializePGOProfileMacros(const CodeGenOptions &CodeGenOpts, void clang::InitializePreprocessor(Preprocessor &PP, const PreprocessorOptions &InitOpts, const PCHContainerReader &PCHContainerRdr, - const FrontendOptions &FEOpts, - const CodeGenOptions &CodeGenOpts) { + const FrontendOptions &FEOpts) { const LangOptions &LangOpts = PP.getLangOpts(); std::string PredefineBuffer; PredefineBuffer.reserve(4080); @@ -1575,10 +1574,12 @@ void clang::InitializePreprocessor(Preprocessor &PP, // macros. This is not the right way to handle this. if ((LangOpts.CUDA || LangOpts.isTargetDevice()) && PP.getAuxTargetInfo()) InitializePredefinedMacros(*PP.getAuxTargetInfo(), LangOpts, FEOpts, - PP.getPreprocessorOpts(), Builder); + PP.getPreprocessorOpts(), PP.getCodeGenOpts(), + Builder); InitializePredefinedMacros(PP.getTargetInfo(), LangOpts, FEOpts, - PP.getPreprocessorOpts(), Builder); + PP.getPreprocessorOpts(), PP.getCodeGenOpts(), + Builder); // Install definitions to make Objective-C++ ARC work well with various // C++ Standard Library implementations. @@ -1605,7 +1606,7 @@ void clang::InitializePreprocessor(Preprocessor &PP, // The PGO instrumentation profile macros are driven by options // -fprofile[-instr]-generate/-fcs-profile-generate/-fprofile[-instr]-use, // hence they are not guarded by InitOpts.UsePredefines. - InitializePGOProfileMacros(CodeGenOpts, Builder); + InitializePGOProfileMacros(PP.getCodeGenOpts(), Builder); // Add on the predefines from the driver. Wrap in a #line directive to report // that they come from the command line. @@ -1613,7 +1614,7 @@ void clang::InitializePreprocessor(Preprocessor &PP, // Process #define's and #undef's in the order they are given. for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; ++i) { - if (InitOpts.Macros[i].second) // isUndef + if (InitOpts.Macros[i].second) // isUndef Builder.undefineMacro(InitOpts.Macros[i].first); else DefineBuiltinMacro(Builder, InitOpts.Macros[i].first, diff --git a/clang/lib/Lex/Preprocessor.cpp b/clang/lib/Lex/Preprocessor.cpp index bcd3ea60ce3da..35bb9139cf816 100644 --- a/clang/lib/Lex/Preprocessor.cpp +++ b/clang/lib/Lex/Preprocessor.cpp @@ -79,11 +79,11 @@ ExternalPreprocessorSource::~ExternalPreprocessorSource() = default; Preprocessor::Preprocessor(const PreprocessorOptions &PPOpts, DiagnosticsEngine &diags, const LangOptions &opts, - SourceManager &SM, HeaderSearch &Headers, - ModuleLoader &TheModuleLoader, + const CodeGenOptions &CGOpts, SourceManager &SM, + HeaderSearch &Headers, ModuleLoader &TheModuleLoader, IdentifierInfoLookup *IILookup, bool OwnsHeaders, TranslationUnitKind TUKind) - : PPOpts(PPOpts), Diags(&diags), LangOpts(opts), + : PPOpts(PPOpts), Diags(&diags), LangOpts(opts), CGOpts(CGOpts), FileMgr(Headers.getFileMgr()), SourceMgr(SM), ScratchBuf(new ScratchBuffer(SourceMgr)), HeaderInfo(Headers), TheModuleLoader(TheModuleLoader), ExternalSource(nullptr), diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp index 486971818f109..4c0f1f6371b88 100644 --- a/clang/lib/Serialization/ASTReader.cpp +++ b/clang/lib/Serialization/ASTReader.cpp @@ -175,6 +175,15 @@ bool ChainedASTReaderListener::ReadLanguageOptions( AllowCompatibleDifferences); } +bool ChainedASTReaderListener::ReadCodeGenOptions( + const CodeGenOptions &CGOpts, StringRef ModuleFilename, bool Complain, + bool AllowCompatibleDifferences) { + return First->ReadCodeGenOptions(CGOpts, ModuleFilename, Complain, + AllowCompatibleDifferences) || + Second->ReadCodeGenOptions(CGOpts, ModuleFilename, Complain, + AllowCompatibleDifferences); +} + bool ChainedASTReaderListener::ReadTargetOptions( const TargetOptions &TargetOpts, StringRef ModuleFilename, bool Complain, bool AllowCompatibleDifferences) { @@ -377,6 +386,32 @@ static bool checkLanguageOptions(const LangOptions &LangOpts, return false; } +static bool checkCodegenOptions(const CodeGenOptions &CGOpts, + const CodeGenOptions &ExistingCGOpts, + StringRef ModuleFilename, + DiagnosticsEngine *Diags, + bool AllowCompatibleDifferences = true) { +#define CODEGENOPT(Name, Bits, Default) +#define COMPATIBLE_VALUE_CODEGENOPT(Name, Bits, Default, Description) \ + if (!AllowCompatibleDifferences && ExistingCGOpts.Name != CGOpts.Name) { \ + if (Diags) \ + Diags->Report(diag::err_ast_file_codegenopt_value_mismatch) \ + << Description << ModuleFilename; \ + return true; \ + } +#define COMPATIBLE_ENUM_CODEGENOPT(Name, Type, Bits, Default, Description) \ + if (!AllowCompatibleDifferences && \ + ExistingCGOpts.get##Name() != CGOpts.get##Name()) { \ + if (Diags) \ + Diags->Report(diag::err_ast_file_codegenopt_value_mismatch) \ + << Description << ModuleFilename; \ + return true; \ + } +#include "clang/Basic/CodeGenOptions.def" + + return false; +} + /// Compare the given set of target options against an existing set of /// target options. /// @@ -456,6 +491,15 @@ bool PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts, AllowCompatibleDifferences); } +bool PCHValidator::ReadCodeGenOptions(const CodeGenOptions &CGOpts, + StringRef ModuleFilename, bool Complain, + bool AllowCompatibleDifferences) { + const CodeGenOptions &ExistingCGOpts = PP.getCodeGenOpts(); + return checkCodegenOptions(ExistingCGOpts, CGOpts, ModuleFilename, + Complain ? &Reader.Diags : nullptr, + AllowCompatibleDifferences); +} + bool PCHValidator::ReadTargetOptions(const TargetOptions &TargetOpts, StringRef ModuleFilename, bool Complain, bool AllowCompatibleDifferences) { @@ -2987,6 +3031,14 @@ ASTReader::ASTReadResult ASTReader::ReadOptionsBlock( break; } + case CODEGEN_OPTIONS: { + bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; + if (ParseCodeGenOptions(Record, Filename, Complain, Listener, + AllowCompatibleConfigurationMismatch)) + Result = ConfigurationMismatch; + break; + } + case TARGET_OPTIONS: { bool Complain = (ClientLoadCapabilities & ARR_ConfigurationMismatch) == 0; if (ParseTargetOptions(Record, Filename, Complain, Listener, @@ -5633,6 +5685,7 @@ namespace { class SimplePCHValidator : public ASTReaderListener { const LangOptions &ExistingLangOpts; + const CodeGenOptions &ExistingCGOpts; const TargetOptions &ExistingTargetOpts; const PreprocessorOptions &ExistingPPOpts; std::string ExistingModuleCachePath; @@ -5641,11 +5694,12 @@ namespace { public: SimplePCHValidator(const LangOptions &ExistingLangOpts, + const CodeGenOptions &ExistingCGOpts, const TargetOptions &ExistingTargetOpts, const PreprocessorOptions &ExistingPPOpts, StringRef ExistingModuleCachePath, FileManager &FileMgr, bool StrictOptionMatches) - : ExistingLangOpts(ExistingLangOpts), + : ExistingLangOpts(ExistingLangOpts), ExistingCGOpts(ExistingCGOpts), ExistingTargetOpts(ExistingTargetOpts), ExistingPPOpts(ExistingPPOpts), ExistingModuleCachePath(ExistingModuleCachePath), FileMgr(FileMgr), @@ -5658,6 +5712,13 @@ namespace { nullptr, AllowCompatibleDifferences); } + bool ReadCodeGenOptions(const CodeGenOptions &CGOpts, + StringRef ModuleFilename, bool Complain, + bool AllowCompatibleDifferences) override { + return checkCodegenOptions(ExistingCGOpts, CGOpts, ModuleFilename, + nullptr, AllowCompatibleDifferences); + } + bool ReadTargetOptions(const TargetOptions &TargetOpts, StringRef ModuleFilename, bool Complain, bool AllowCompatibleDifferences) override { @@ -6006,9 +6067,10 @@ bool ASTReader::readASTFileControlBlock( bool ASTReader::isAcceptableASTFile( StringRef Filename, FileManager &FileMgr, const ModuleCache &ModCache, const PCHContainerReader &PCHContainerRdr, const LangOptions &LangOpts, - const TargetOptions &TargetOpts, const PreprocessorOptions &PPOpts, - StringRef ExistingModuleCachePath, bool RequireStrictOptionMatches) { - SimplePCHValidator validator(LangOpts, TargetOpts, PPOpts, + const CodeGenOptions &CGOpts, const TargetOptions &TargetOpts, + const PreprocessorOptions &PPOpts, StringRef ExistingModuleCachePath, + bool RequireStrictOptionMatches) { + SimplePCHValidator validator(LangOpts, CGOpts, TargetOpts, PPOpts, ExistingModuleCachePath, FileMgr, RequireStrictOptionMatches); return !readASTFileControlBlock(Filename, FileMgr, ModCache, PCHContainerRdr, @@ -6389,6 +6451,23 @@ bool ASTReader::ParseLanguageOptions(const RecordData &Record, AllowCompatibleDifferences); } +bool ASTReader::ParseCodeGenOptions(const RecordData &Record, + StringRef ModuleFilename, bool Complain, + ASTReaderListener &Listener, + bool AllowCompatibleDifferences) { + unsigned Idx = 0; + CodeGenOptions CGOpts; +#define CODEGENOPT(Name, Bits, Default) +#define COMPATIBLE_VALUE_CODEGENOPT(Name, Bits, Default, Description) \ + CGOpts.Name = static_cast(Record[Idx++]); +#define COMPATIBLE_ENUM_CODEGENOPT(Name, Type, Bits, Default, Description) \ + CGOpts.set##Name(static_cast(Record[Idx++])); +#include "clang/Basic/CodeGenOptions.def" + + return Listener.ReadCodeGenOptions(CGOpts, ModuleFilename, Complain, + AllowCompatibleDifferences); +} + bool ASTReader::ParseTargetOptions(const RecordData &Record, StringRef ModuleFilename, bool Complain, ASTReaderListener &Listener, diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp index 06cd6c7305114..374a35372b87f 100644 --- a/clang/lib/Serialization/ASTWriter.cpp +++ b/clang/lib/Serialization/ASTWriter.cpp @@ -898,6 +898,7 @@ void ASTWriter::WriteBlockInfoBlock() { BLOCK(OPTIONS_BLOCK); RECORD(LANGUAGE_OPTIONS); + RECORD(CODEGEN_OPTIONS); RECORD(TARGET_OPTIONS); RECORD(FILE_SYSTEM_OPTIONS); RECORD(HEADER_SEARCH_OPTIONS); @@ -1646,6 +1647,17 @@ void ASTWriter::WriteControlBlock(Preprocessor &PP, StringRef isysroot) { Stream.EmitRecord(LANGUAGE_OPTIONS, Record); + // Codegen options. + Record.clear(); + const CodeGenOptions &CGOpts = PP.getCodeGenOpts(); +#define CODEGENOPT(Name, Bits, Default) +#define COMPATIBLE_VALUE_CODEGENOPT(Name, Bits, Default, Description) \ + Record.push_back(static_cast(CGOpts.Name)); +#define COMPATIBLE_ENUM_CODEGENOPT(Name, Type, Bits, Default, Description) \ + Record.push_back(static_cast(CGOpts.get##Name())); +#include "clang/Basic/CodeGenOptions.def" + Stream.EmitRecord(CODEGEN_OPTIONS, Record); + // Target options. Record.clear(); const TargetInfo &Target = PP.getTargetInfo(); diff --git a/clang/test/Modules/implicit-opt-level.c b/clang/test/Modules/implicit-opt-level.c new file mode 100644 index 0000000000000..f6f1f58d31d79 --- /dev/null +++ b/clang/test/Modules/implicit-opt-level.c @@ -0,0 +1,15 @@ +// This test checks that under implicit modules, different optimization levels +// get different context hashes. + +// RUN: rm -rf %t +// RUN: split-file %s %t + +//--- module.modulemap +module M { header "M.h" } +//--- M.h +//--- tu.c +#include "M.h" + +// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/cache -O0 -fsyntax-only %t/tu.c +// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/cache -O1 -fsyntax-only %t/tu.c +// RUN: find %t/cache -name "M-*.pcm" | count 2 diff --git a/clang/test/PCH/no-validate-pch.cl b/clang/test/PCH/no-validate-pch.cl index f6a7c65461fe1..7d24d85175046 100644 --- a/clang/test/PCH/no-validate-pch.cl +++ b/clang/test/PCH/no-validate-pch.cl @@ -16,7 +16,7 @@ // CHECK: note: previous definition is here // CHECK: #define X 4 -// CHECK-VAL: error: __OPTIMIZE__ predefined macro was enabled in precompiled file '{{.*}}' but is currently disabled +// CHECK-VAL: error: optimization level differs in precompiled file '{{.*}}' vs. current file // CHECK-VAL: error: definition of macro 'X' differs between the precompiled file '{{.*}}' ('4') and the command line ('5') void test(void) { diff --git a/clang/unittests/Analysis/MacroExpansionContextTest.cpp b/clang/unittests/Analysis/MacroExpansionContextTest.cpp index 9874ea687f3ed..7ed381b399197 100644 --- a/clang/unittests/Analysis/MacroExpansionContextTest.cpp +++ b/clang/unittests/Analysis/MacroExpansionContextTest.cpp @@ -50,6 +50,7 @@ class MacroExpansionContextTest : public ::testing::Test { DiagnosticsEngine Diags; SourceManager SourceMgr; LangOptions LangOpts; + CodeGenOptions CGOpts; std::shared_ptr TargetOpts; IntrusiveRefCntPtr Target; @@ -62,7 +63,8 @@ class MacroExpansionContextTest : public ::testing::Test { TrivialModuleLoader ModLoader; PreprocessorOptions PPOpts; HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, Target.get()); - Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, + Preprocessor PP(PPOpts, Diags, LangOpts, CGOpts, SourceMgr, HeaderInfo, + ModLoader, /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); PP.Initialize(*Target); diff --git a/clang/unittests/Basic/SourceManagerTest.cpp b/clang/unittests/Basic/SourceManagerTest.cpp index cbe047b5e599a..2565c734ba473 100644 --- a/clang/unittests/Basic/SourceManagerTest.cpp +++ b/clang/unittests/Basic/SourceManagerTest.cpp @@ -54,6 +54,7 @@ class SourceManagerTest : public ::testing::Test { DiagnosticsEngine Diags; SourceManager SourceMgr; LangOptions LangOpts; + CodeGenOptions CGOpts; std::shared_ptr TargetOpts; IntrusiveRefCntPtr Target; }; @@ -138,7 +139,8 @@ TEST_F(SourceManagerTest, isBeforeInTranslationUnit) { PreprocessorOptions PPOpts; TrivialModuleLoader ModLoader; HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, &*Target); - Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, + Preprocessor PP(PPOpts, Diags, LangOpts, CGOpts, SourceMgr, HeaderInfo, + ModLoader, /*IILookup =*/nullptr, /*OwnsHeaderSearch =*/false); PP.Initialize(*Target); PP.EnterMainSourceFile(); @@ -187,7 +189,8 @@ TEST_F(SourceManagerTest, isBeforeInTranslationUnitWithTokenSplit) { PreprocessorOptions PPOpts; TrivialModuleLoader ModLoader; HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, &*Target); - Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, + Preprocessor PP(PPOpts, Diags, LangOpts, CGOpts, SourceMgr, HeaderInfo, + ModLoader, /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); PP.Initialize(*Target); PP.EnterMainSourceFile(); @@ -461,7 +464,8 @@ TEST_F(SourceManagerTest, ResetsIncludeLocMap) { HeaderSearchOptions HSOpts; PreprocessorOptions PPOpts; HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, &*Target); - Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, + Preprocessor PP(PPOpts, Diags, LangOpts, CGOpts, SourceMgr, HeaderInfo, + ModLoader, /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); PP.Initialize(*Target); PP.EnterMainSourceFile(); @@ -538,7 +542,8 @@ TEST_F(SourceManagerTest, getMacroArgExpandedLocation) { TrivialModuleLoader ModLoader; HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, &*Target); - Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, + Preprocessor PP(PPOpts, Diags, LangOpts, CGOpts, SourceMgr, HeaderInfo, + ModLoader, /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); // Ensure we can get expanded locations in presence of implicit includes. // These are different than normal includes since predefines buffer doesn't @@ -655,7 +660,8 @@ TEST_F(SourceManagerTest, isBeforeInTranslationUnitWithMacroInInclude) { PreprocessorOptions PPOpts; TrivialModuleLoader ModLoader; HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, &*Target); - Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, + Preprocessor PP(PPOpts, Diags, LangOpts, CGOpts, SourceMgr, HeaderInfo, + ModLoader, /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); PP.Initialize(*Target); diff --git a/clang/unittests/Lex/LexerTest.cpp b/clang/unittests/Lex/LexerTest.cpp index 86df872f6b7df..f1307d406255c 100644 --- a/clang/unittests/Lex/LexerTest.cpp +++ b/clang/unittests/Lex/LexerTest.cpp @@ -58,9 +58,8 @@ class LexerTest : public ::testing::Test { HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, Target.get()); PreprocessorOptions PPOpts; std::unique_ptr PP = std::make_unique( - PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, - /*IILookup =*/nullptr, - /*OwnsHeaderSearch =*/false); + PPOpts, Diags, LangOpts, CGOpts, SourceMgr, HeaderInfo, ModLoader, + /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); if (!PreDefines.empty()) PP->setPredefines(PreDefines); PP->Initialize(*Target); @@ -107,6 +106,7 @@ class LexerTest : public ::testing::Test { DiagnosticsEngine Diags; SourceManager SourceMgr; LangOptions LangOpts; + CodeGenOptions CGOpts; std::shared_ptr TargetOpts; IntrusiveRefCntPtr Target; std::unique_ptr PP; diff --git a/clang/unittests/Lex/ModuleDeclStateTest.cpp b/clang/unittests/Lex/ModuleDeclStateTest.cpp index 6ecba4de3187c..c87cdf6ea37c7 100644 --- a/clang/unittests/Lex/ModuleDeclStateTest.cpp +++ b/clang/unittests/Lex/ModuleDeclStateTest.cpp @@ -77,8 +77,8 @@ class ModuleDeclStateTest : public ::testing::Test { HeaderInfo.emplace(HSOpts, SourceMgr, Diags, LangOpts, Target.get()); - return std::make_unique(PPOpts, Diags, LangOpts, SourceMgr, - *HeaderInfo, ModLoader, + return std::make_unique(PPOpts, Diags, LangOpts, CGOpts, + SourceMgr, *HeaderInfo, ModLoader, /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); } @@ -100,6 +100,7 @@ class ModuleDeclStateTest : public ::testing::Test { std::shared_ptr TargetOpts; IntrusiveRefCntPtr Target; LangOptions LangOpts; + CodeGenOptions CGOpts; TrivialModuleLoader ModLoader; HeaderSearchOptions HSOpts; std::optional HeaderInfo; diff --git a/clang/unittests/Lex/PPCallbacksTest.cpp b/clang/unittests/Lex/PPCallbacksTest.cpp index af86c1888f2c7..d35e9b8c48d4e 100644 --- a/clang/unittests/Lex/PPCallbacksTest.cpp +++ b/clang/unittests/Lex/PPCallbacksTest.cpp @@ -149,6 +149,7 @@ class PPCallbacksTest : public ::testing::Test { DiagnosticsEngine Diags; SourceManager SourceMgr; LangOptions LangOpts; + CodeGenOptions CGOpts; std::shared_ptr TargetOpts; IntrusiveRefCntPtr Target; @@ -200,8 +201,9 @@ class PPCallbacksTest : public ::testing::Test { AddFakeHeader(HeaderInfo, HeaderPath, SystemHeader); PreprocessorOptions PPOpts; - Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, - /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); + Preprocessor PP(PPOpts, Diags, LangOpts, CGOpts, SourceMgr, HeaderInfo, + ModLoader, /*IILookup=*/nullptr, + /*OwnsHeaderSearch=*/false); return InclusionDirectiveCallback(PP)->FilenameRange; } @@ -218,8 +220,9 @@ class PPCallbacksTest : public ::testing::Test { AddFakeHeader(HeaderInfo, HeaderPath, SystemHeader); PreprocessorOptions PPOpts; - Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, - /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); + Preprocessor PP(PPOpts, Diags, LangOpts, CGOpts, SourceMgr, HeaderInfo, + ModLoader, /*IILookup=*/nullptr, + /*OwnsHeaderSearch=*/false); return InclusionDirectiveCallback(PP)->FileType; } @@ -244,7 +247,8 @@ class PPCallbacksTest : public ::testing::Test { llvm::MemoryBuffer::getMemBuffer(SourceText); SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(Buf))); HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, Target.get()); - Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, + Preprocessor PP(PPOpts, Diags, LangOpts, CGOpts, SourceMgr, HeaderInfo, + ModLoader, /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); PP.Initialize(*Target); auto *Callbacks = new CondDirectiveCallbacks; @@ -269,7 +273,8 @@ class PPCallbacksTest : public ::testing::Test { HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, Target.get()); - Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, + Preprocessor PP(PPOpts, Diags, LangOpts, CGOpts, SourceMgr, HeaderInfo, + ModLoader, /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); PP.Initialize(*Target); @@ -299,8 +304,8 @@ class PPCallbacksTest : public ::testing::Test { HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, OpenCLLangOpts, Target.get()); - Preprocessor PP(PPOpts, Diags, OpenCLLangOpts, SourceMgr, HeaderInfo, - ModLoader, /*IILookup=*/nullptr, + Preprocessor PP(PPOpts, Diags, OpenCLLangOpts, CGOpts, SourceMgr, + HeaderInfo, ModLoader, /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); PP.Initialize(*Target); @@ -438,8 +443,9 @@ TEST_F(PPCallbacksTest, FileNotFoundSkipped) { DiagnosticConsumer *DiagConsumer = new DiagnosticConsumer; DiagnosticsEngine FileNotFoundDiags(DiagID, DiagOpts, DiagConsumer); - Preprocessor PP(PPOpts, FileNotFoundDiags, LangOpts, SourceMgr, HeaderInfo, - ModLoader, /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); + Preprocessor PP(PPOpts, FileNotFoundDiags, LangOpts, CGOpts, SourceMgr, + HeaderInfo, ModLoader, /*IILookup=*/nullptr, + /*OwnsHeaderSearch=*/false); PP.Initialize(*Target); class FileNotFoundCallbacks : public PPCallbacks { diff --git a/clang/unittests/Lex/PPConditionalDirectiveRecordTest.cpp b/clang/unittests/Lex/PPConditionalDirectiveRecordTest.cpp index 54c1d020aa0ea..2ff19281ae262 100644 --- a/clang/unittests/Lex/PPConditionalDirectiveRecordTest.cpp +++ b/clang/unittests/Lex/PPConditionalDirectiveRecordTest.cpp @@ -43,6 +43,7 @@ class PPConditionalDirectiveRecordTest : public ::testing::Test { DiagnosticsEngine Diags; SourceManager SourceMgr; LangOptions LangOpts; + CodeGenOptions CGOpts; std::shared_ptr TargetOpts; IntrusiveRefCntPtr Target; }; @@ -75,9 +76,8 @@ TEST_F(PPConditionalDirectiveRecordTest, PPRecAPI) { TrivialModuleLoader ModLoader; HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, Target.get()); PreprocessorOptions PPOpts; - Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, - /*IILookup =*/nullptr, - /*OwnsHeaderSearch =*/false); + Preprocessor PP(PPOpts, Diags, LangOpts, CGOpts, SourceMgr, HeaderInfo, + ModLoader, /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); PP.Initialize(*Target); PPConditionalDirectiveRecord * PPRec = new PPConditionalDirectiveRecord(SourceMgr); diff --git a/clang/unittests/Lex/PPDependencyDirectivesTest.cpp b/clang/unittests/Lex/PPDependencyDirectivesTest.cpp index 061cb136a552a..2d3ba18928dce 100644 --- a/clang/unittests/Lex/PPDependencyDirectivesTest.cpp +++ b/clang/unittests/Lex/PPDependencyDirectivesTest.cpp @@ -45,6 +45,7 @@ class PPDependencyDirectivesTest : public ::testing::Test { DiagnosticsEngine Diags; SourceManager SourceMgr; LangOptions LangOpts; + CodeGenOptions CGOpts; std::shared_ptr TargetOpts; IntrusiveRefCntPtr Target; }; @@ -134,9 +135,8 @@ TEST_F(PPDependencyDirectivesTest, MacroGuard) { HeaderSearchOptions HSOpts; TrivialModuleLoader ModLoader; HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, Target.get()); - Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, - /*IILookup =*/nullptr, - /*OwnsHeaderSearch =*/false); + Preprocessor PP(PPOpts, Diags, LangOpts, CGOpts, SourceMgr, HeaderInfo, + ModLoader, /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); PP.Initialize(*Target); PP.setDependencyDirectivesGetter(GetDependencyDirectives); diff --git a/clang/unittests/Lex/PPMemoryAllocationsTest.cpp b/clang/unittests/Lex/PPMemoryAllocationsTest.cpp index 4d83003e28b36..9fa6fee5124c8 100644 --- a/clang/unittests/Lex/PPMemoryAllocationsTest.cpp +++ b/clang/unittests/Lex/PPMemoryAllocationsTest.cpp @@ -41,6 +41,7 @@ class PPMemoryAllocationsTest : public ::testing::Test { DiagnosticsEngine Diags; SourceManager SourceMgr; LangOptions LangOpts; + CodeGenOptions CGOpts; std::shared_ptr TargetOpts; IntrusiveRefCntPtr Target; }; @@ -70,9 +71,8 @@ TEST_F(PPMemoryAllocationsTest, PPMacroDefinesAllocations) { TrivialModuleLoader ModLoader; HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, Target.get()); PreprocessorOptions PPOpts; - Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, - /*IILookup =*/nullptr, - /*OwnsHeaderSearch =*/false); + Preprocessor PP(PPOpts, Diags, LangOpts, CGOpts, SourceMgr, HeaderInfo, + ModLoader, /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); PP.Initialize(*Target); PP.EnterMainSourceFile(); diff --git a/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp b/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp index 1e46ee35d5d49..b0aa73b0a01d4 100644 --- a/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp +++ b/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp @@ -84,8 +84,8 @@ class ParseHLSLRootSignatureTest : public ::testing::Test { HeaderSearch HeaderInfo(SearchOpts, SourceMgr, Diags, LangOpts, Target.get()); auto PP = std::make_unique( - PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, - /*IILookup =*/nullptr, /*OwnsHeaderSearch =*/false); + PPOpts, Diags, LangOpts, CGOpts, SourceMgr, HeaderInfo, ModLoader, + /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); PP->Initialize(*Target); PP->EnterMainSourceFile(); return PP; @@ -99,6 +99,7 @@ class ParseHLSLRootSignatureTest : public ::testing::Test { DiagnosticsEngine Diags; SourceManager SourceMgr; LangOptions LangOpts; + CodeGenOptions CGOpts; PreprocessorOptions PPOpts; std::shared_ptr TargetOpts; IntrusiveRefCntPtr Target; From 740fd6f671aa8cf19bac6b0bfd99df81d3c59700 Mon Sep 17 00:00:00 2001 From: Jan Svoboda Date: Mon, 30 Jun 2025 15:04:33 -0700 Subject: [PATCH 2/5] Fix clang-tools-extra tests --- .../clang-tidy/ExpandModularHeadersPPCallbacks.cpp | 4 ++-- .../clang-tidy/ExpandModularHeadersPPCallbacks.h | 1 + clang-tools-extra/clangd/ModulesBuilder.cpp | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp b/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp index 2c17cd3b6e979..e599f499e2bfc 100644 --- a/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp +++ b/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp @@ -90,14 +90,14 @@ ExpandModularHeadersPPCallbacks::ExpandModularHeadersPPCallbacks( &Compiler.getTarget()); PP = std::make_unique(Compiler.getPreprocessorOpts(), - Diags, LangOpts, Sources, + Diags, LangOpts, CGOpts, Sources, *HeaderInfo, ModuleLoader, /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); PP->Initialize(Compiler.getTarget(), Compiler.getAuxTarget()); InitializePreprocessor(*PP, Compiler.getPreprocessorOpts(), Compiler.getPCHContainerReader(), - Compiler.getFrontendOpts(), Compiler.getCodeGenOpts()); + Compiler.getFrontendOpts()); ApplyHeaderSearchOptions(*HeaderInfo, HSOpts, LangOpts, Compiler.getTarget().getTriple()); } diff --git a/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.h b/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.h index e599bda92c25c..1bd1ba40c65de 100644 --- a/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.h +++ b/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.h @@ -131,6 +131,7 @@ class ExpandModularHeadersPPCallbacks : public PPCallbacks { DiagnosticOptions DiagOpts; DiagnosticsEngine Diags; LangOptions LangOpts; + CodeGenOptions CGOpts; HeaderSearchOptions HSOpts; TrivialModuleLoader ModuleLoader; diff --git a/clang-tools-extra/clangd/ModulesBuilder.cpp b/clang-tools-extra/clangd/ModulesBuilder.cpp index d88aa01aad05d..665de0e93f7b0 100644 --- a/clang-tools-extra/clangd/ModulesBuilder.cpp +++ b/clang-tools-extra/clangd/ModulesBuilder.cpp @@ -194,6 +194,7 @@ bool IsModuleFileUpToDate(PathRef ModuleFilePath, LangOptions LangOpts; LangOpts.SkipODRCheckInGMF = true; + CodeGenOptions CGOpts; FileManager FileMgr(FileSystemOptions(), VFS); @@ -204,7 +205,7 @@ bool IsModuleFileUpToDate(PathRef ModuleFilePath, PreprocessorOptions PPOpts; TrivialModuleLoader ModuleLoader; - Preprocessor PP(PPOpts, *Diags, LangOpts, SourceMgr, HeaderInfo, + Preprocessor PP(PPOpts, *Diags, LangOpts, CGOpts, SourceMgr, HeaderInfo, ModuleLoader); IntrusiveRefCntPtr ModCache = createCrossProcessModuleCache(); From a3acbd07110c7e5d404237c52c55f132eb04db8c Mon Sep 17 00:00:00 2001 From: Jan Svoboda Date: Mon, 30 Jun 2025 15:36:13 -0700 Subject: [PATCH 3/5] Undo InitPreprocessor.cpp changes --- clang/lib/Frontend/InitPreprocessor.cpp | 119 ++++++++++++------------ 1 file changed, 59 insertions(+), 60 deletions(-) diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp index e24f37dc171ca..f64613fb4a6cb 100644 --- a/clang/lib/Frontend/InitPreprocessor.cpp +++ b/clang/lib/Frontend/InitPreprocessor.cpp @@ -47,7 +47,8 @@ static void DefineBuiltinMacro(MacroBuilder &Builder, StringRef Macro, // Per GCC -D semantics, the macro ends at \n if it exists. StringRef::size_type End = MacroBody.find_first_of("\n\r"); if (End != StringRef::npos) - Diags.Report(diag::warn_fe_macro_contains_embedded_newline) << MacroName; + Diags.Report(diag::warn_fe_macro_contains_embedded_newline) + << MacroName; MacroBody = MacroBody.substr(0, End); // We handle macro bodies which end in a backslash by appending an extra // backslash+newline. This makes sure we don't accidentally treat the @@ -96,17 +97,17 @@ template static T PickFP(const llvm::fltSemantics *Sem, T IEEEHalfVal, T IEEESingleVal, T IEEEDoubleVal, T X87DoubleExtendedVal, T PPCDoubleDoubleVal, T IEEEQuadVal) { - if (Sem == (const llvm::fltSemantics *)&llvm::APFloat::IEEEhalf()) + if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEhalf()) return IEEEHalfVal; - if (Sem == (const llvm::fltSemantics *)&llvm::APFloat::IEEEsingle()) + if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEsingle()) return IEEESingleVal; - if (Sem == (const llvm::fltSemantics *)&llvm::APFloat::IEEEdouble()) + if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEdouble()) return IEEEDoubleVal; - if (Sem == (const llvm::fltSemantics *)&llvm::APFloat::x87DoubleExtended()) + if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::x87DoubleExtended()) return X87DoubleExtendedVal; - if (Sem == (const llvm::fltSemantics *)&llvm::APFloat::PPCDoubleDouble()) + if (Sem == (const llvm::fltSemantics*)&llvm::APFloat::PPCDoubleDouble()) return PPCDoubleDoubleVal; - assert(Sem == (const llvm::fltSemantics *)&llvm::APFloat::IEEEquad()); + assert(Sem == (const llvm::fltSemantics*)&llvm::APFloat::IEEEquad()); return IEEEQuadVal; } @@ -132,8 +133,8 @@ static void DefineFloatMacros(MacroBuilder &Builder, StringRef Prefix, int Max10Exp = PickFP(Sem, 4, 38, 308, 4932, 308, 4932); int MinExp = PickFP(Sem, -13, -125, -1021, -16381, -968, -16381); int MaxExp = PickFP(Sem, 16, 128, 1024, 16384, 1024, 16384); - Min = PickFP(Sem, "6.103515625e-5", "1.17549435e-38", - "2.2250738585072014e-308", "3.36210314311209350626e-4932", + Min = PickFP(Sem, "6.103515625e-5", "1.17549435e-38", "2.2250738585072014e-308", + "3.36210314311209350626e-4932", "2.00416836000897277799610805135016e-292", "3.36210314311209350626267781732175260e-4932"); Max = PickFP(Sem, "6.5504e+4", "3.40282347e+38", "1.7976931348623157e+308", @@ -146,25 +147,26 @@ static void DefineFloatMacros(MacroBuilder &Builder, StringRef Prefix, DefPrefix += Prefix; DefPrefix += "_"; - Builder.defineMacro(DefPrefix + "DENORM_MIN__", Twine(DenormMin) + Ext); - Builder.defineMacro(DefPrefix + "NORM_MAX__", Twine(NormMax) + Ext); + Builder.defineMacro(DefPrefix + "DENORM_MIN__", Twine(DenormMin)+Ext); + Builder.defineMacro(DefPrefix + "NORM_MAX__", Twine(NormMax)+Ext); Builder.defineMacro(DefPrefix + "HAS_DENORM__"); Builder.defineMacro(DefPrefix + "DIG__", Twine(Digits)); Builder.defineMacro(DefPrefix + "DECIMAL_DIG__", Twine(DecimalDigits)); - Builder.defineMacro(DefPrefix + "EPSILON__", Twine(Epsilon) + Ext); + Builder.defineMacro(DefPrefix + "EPSILON__", Twine(Epsilon)+Ext); Builder.defineMacro(DefPrefix + "HAS_INFINITY__"); Builder.defineMacro(DefPrefix + "HAS_QUIET_NAN__"); Builder.defineMacro(DefPrefix + "MANT_DIG__", Twine(MantissaDigits)); Builder.defineMacro(DefPrefix + "MAX_10_EXP__", Twine(Max10Exp)); Builder.defineMacro(DefPrefix + "MAX_EXP__", Twine(MaxExp)); - Builder.defineMacro(DefPrefix + "MAX__", Twine(Max) + Ext); + Builder.defineMacro(DefPrefix + "MAX__", Twine(Max)+Ext); - Builder.defineMacro(DefPrefix + "MIN_10_EXP__", "(" + Twine(Min10Exp) + ")"); - Builder.defineMacro(DefPrefix + "MIN_EXP__", "(" + Twine(MinExp) + ")"); - Builder.defineMacro(DefPrefix + "MIN__", Twine(Min) + Ext); + Builder.defineMacro(DefPrefix + "MIN_10_EXP__","("+Twine(Min10Exp)+")"); + Builder.defineMacro(DefPrefix + "MIN_EXP__", "("+Twine(MinExp)+")"); + Builder.defineMacro(DefPrefix + "MIN__", Twine(Min)+Ext); } + /// DefineTypeSize - Emit a macro to the predefines buffer that declares a macro /// named MacroName with the max value for a type with width 'TypeWidth' a /// signedness of 'isSigned' and with a value suffix of 'ValSuffix' (e.g. LL). @@ -213,7 +215,8 @@ static void DefineTypeWidth(const Twine &MacroName, TargetInfo::IntType Ty, static void DefineTypeSizeof(StringRef MacroName, unsigned BitWidth, const TargetInfo &TI, MacroBuilder &Builder) { - Builder.defineMacro(MacroName, Twine(BitWidth / TI.getCharWidth())); + Builder.defineMacro(MacroName, + Twine(BitWidth / TI.getCharWidth())); } // This will generate a macro based on the prefix with `_MAX__` as the suffix @@ -310,6 +313,7 @@ static void DefineFastIntType(const LangOptions &LangOpts, unsigned TypeWidth, DefineFmt(LangOpts, Prefix + Twine(TypeWidth), Ty, TI, Builder); } + /// Get the value the ATOMIC_*_LOCK_FREE macro should have for a type with /// the specified properties. static const char *getLockFreeValue(unsigned TypeWidth, const TargetInfo &TI) { @@ -350,8 +354,7 @@ static void AddObjCXXARCLibstdcxxDefines(const LangOptions &LangOpts, if (LangOpts.ObjCAutoRefCount) { Out << "template\n" - << "struct __is_scalar<__attribute__((objc_ownership(strong))) _Tp> " - "{\n" + << "struct __is_scalar<__attribute__((objc_ownership(strong))) _Tp> {\n" << " enum { __value = 0 };\n" << " typedef __false_type __type;\n" << "};\n" @@ -546,12 +549,12 @@ static void InitializeStandardPredefinedMacros(const TargetInfo &TI, Builder.defineMacro("__CL_CPP_VERSION_2021__", "202100"); } else { // OpenCL v1.0 and v1.1 do not have a predefined macro to indicate the - // language standard with which the program is compiled. - // __OPENCL_VERSION__ is for the OpenCL version supported by the OpenCL - // device, which is not necessarily the language standard with which the - // program is compiled. A shared OpenCL header file requires a macro to - // indicate the language standard. As a workaround, __OPENCL_C_VERSION__ - // is defined for OpenCL v1.0 and v1.1. + // language standard with which the program is compiled. __OPENCL_VERSION__ + // is for the OpenCL version supported by the OpenCL device, which is not + // necessarily the language standard with which the program is compiled. + // A shared OpenCL header file requires a macro to indicate the language + // standard. As a workaround, __OPENCL_C_VERSION__ is defined for + // OpenCL v1.0 and v1.1. switch (LangOpts.OpenCLVersion) { case 100: Builder.defineMacro("__OPENCL_C_VERSION__", "100"); @@ -717,7 +720,7 @@ static void InitializeCPlusPlusFeatureTestMacros(const LangOptions &LangOpts, Builder.defineMacro("__cpp_capture_star_this", "201603L"); Builder.defineMacro("__cpp_if_constexpr", "201606L"); Builder.defineMacro("__cpp_deduction_guides", "201703L"); // (not latest) - Builder.defineMacro("__cpp_template_auto", "201606L"); // (old name) + Builder.defineMacro("__cpp_template_auto", "201606L"); // (old name) Builder.defineMacro("__cpp_namespace_attributes", "201411L"); Builder.defineMacro("__cpp_enumerator_attributes", "201411L"); Builder.defineMacro("__cpp_nested_namespace_definitions", "201411L"); @@ -747,7 +750,7 @@ static void InitializeCPlusPlusFeatureTestMacros(const LangOptions &LangOpts, Builder.defineMacro("__cpp_impl_coroutine", "201902L"); Builder.defineMacro("__cpp_designated_initializers", "201707L"); Builder.defineMacro("__cpp_impl_three_way_comparison", "201907L"); - // Builder.defineMacro("__cpp_modules", "201907L"); + //Builder.defineMacro("__cpp_modules", "201907L"); Builder.defineMacro("__cpp_using_enum", "201907L"); } // C++23 features. @@ -862,7 +865,6 @@ static void InitializePredefinedMacros(const TargetInfo &TI, const LangOptions &LangOpts, const FrontendOptions &FEOpts, const PreprocessorOptions &PPOpts, - const CodeGenOptions &CGOpts, MacroBuilder &Builder) { // Compiler version introspection macros. Builder.defineMacro("__llvm__"); // LLVM Backend @@ -874,9 +876,9 @@ static void InitializePredefinedMacros(const TargetInfo &TI, Builder.defineMacro("__clang_patchlevel__", TOSTR(CLANG_VERSION_PATCHLEVEL)); #undef TOSTR #undef TOSTR2 - Builder.defineMacro("__clang_version__", "\"" CLANG_VERSION_STRING " " + - getClangFullRepositoryVersion() + - "\""); + Builder.defineMacro("__clang_version__", + "\"" CLANG_VERSION_STRING " " + + getClangFullRepositoryVersion() + "\""); if (LangOpts.GNUCVersion != 0) { // Major, minor, patch, are given two decimal places each, so 4.2.1 becomes @@ -941,8 +943,8 @@ static void InitializePredefinedMacros(const TargetInfo &TI, // Previously this macro was set to a string aiming to achieve compatibility // with GCC 4.2.1. Now, just return the full Clang version - Builder.defineMacro("__VERSION__", - "\"" + Twine(getClangFullCPPVersion()) + "\""); + Builder.defineMacro("__VERSION__", "\"" + + Twine(getClangFullCPPVersion()) + "\""); // Initialize language-specific preprocessor defines. @@ -990,9 +992,9 @@ static void InitializePredefinedMacros(const TargetInfo &TI, VersionTuple tuple = LangOpts.ObjCRuntime.getVersion(); unsigned minor = tuple.getMinor().value_or(0); unsigned subminor = tuple.getSubminor().value_or(0); - Builder.defineMacro( - "__OBJFW_RUNTIME_ABI__", - Twine(tuple.getMajor() * 10000 + minor * 100 + subminor)); + Builder.defineMacro("__OBJFW_RUNTIME_ABI__", + Twine(tuple.getMajor() * 10000 + minor * 100 + + subminor)); } Builder.defineMacro("IBOutlet", "__attribute__((iboutlet))"); @@ -1014,7 +1016,7 @@ static void InitializePredefinedMacros(const TargetInfo &TI, // darwin_constant_cfstrings controls this. This is also dependent // on other things like the runtime I believe. This is set even for C code. if (!LangOpts.NoConstantCFStrings) - Builder.defineMacro("__CONSTANT_CFSTRINGS__"); + Builder.defineMacro("__CONSTANT_CFSTRINGS__"); if (LangOpts.ObjC) Builder.defineMacro("OBJC_NEW_PROPERTIES"); @@ -1071,9 +1073,9 @@ static void InitializePredefinedMacros(const TargetInfo &TI, Builder.defineMacro("__clang_wide_literal_encoding__", "\"UTF-16\""); } - if (CGOpts.OptimizationLevel != 0) + if (LangOpts.Optimize) Builder.defineMacro("__OPTIMIZE__"); - if (CGOpts.OptimizeSize != 0) + if (LangOpts.OptimizeSize) Builder.defineMacro("__OPTIMIZE_SIZE__"); if (LangOpts.FastMath) @@ -1087,8 +1089,8 @@ static void InitializePredefinedMacros(const TargetInfo &TI, // We don't support the PDP-11 as a target, but include // the define so it can still be compared against. Builder.defineMacro("__ORDER_LITTLE_ENDIAN__", "1234"); - Builder.defineMacro("__ORDER_BIG_ENDIAN__", "4321"); - Builder.defineMacro("__ORDER_PDP_ENDIAN__", "3412"); + Builder.defineMacro("__ORDER_BIG_ENDIAN__", "4321"); + Builder.defineMacro("__ORDER_PDP_ENDIAN__", "3412"); if (TI.isBigEndian()) { Builder.defineMacro("__BYTE_ORDER__", "__ORDER_BIG_ENDIAN__"); Builder.defineMacro("__BIG_ENDIAN__"); @@ -1154,8 +1156,7 @@ static void InitializePredefinedMacros(const TargetInfo &TI, DefineTypeSizeof("__SIZEOF_FLOAT__", TI.getFloatWidth(), TI, Builder); DefineTypeSizeof("__SIZEOF_INT__", TI.getIntWidth(), TI, Builder); DefineTypeSizeof("__SIZEOF_LONG__", TI.getLongWidth(), TI, Builder); - DefineTypeSizeof("__SIZEOF_LONG_DOUBLE__", TI.getLongDoubleWidth(), TI, - Builder); + DefineTypeSizeof("__SIZEOF_LONG_DOUBLE__",TI.getLongDoubleWidth(),TI,Builder); DefineTypeSizeof("__SIZEOF_LONG_LONG__", TI.getLongLongWidth(), TI, Builder); DefineTypeSizeof("__SIZEOF_POINTER__", TI.getPointerWidth(LangAS::Default), TI, Builder); @@ -1163,12 +1164,12 @@ static void InitializePredefinedMacros(const TargetInfo &TI, DefineTypeSizeof("__SIZEOF_PTRDIFF_T__", TI.getTypeWidth(TI.getPtrDiffType(LangAS::Default)), TI, Builder); - DefineTypeSizeof("__SIZEOF_SIZE_T__", TI.getTypeWidth(TI.getSizeType()), TI, - Builder); - DefineTypeSizeof("__SIZEOF_WCHAR_T__", TI.getTypeWidth(TI.getWCharType()), TI, - Builder); - DefineTypeSizeof("__SIZEOF_WINT_T__", TI.getTypeWidth(TI.getWIntType()), TI, - Builder); + DefineTypeSizeof("__SIZEOF_SIZE_T__", + TI.getTypeWidth(TI.getSizeType()), TI, Builder); + DefineTypeSizeof("__SIZEOF_WCHAR_T__", + TI.getTypeWidth(TI.getWCharType()), TI, Builder); + DefineTypeSizeof("__SIZEOF_WINT_T__", + TI.getTypeWidth(TI.getWIntType()), TI, Builder); if (TI.hasInt128Type()) DefineTypeSizeof("__SIZEOF_INT128__", 128, TI, Builder); @@ -1264,7 +1265,7 @@ static void InitializePredefinedMacros(const TargetInfo &TI, // Define __BIGGEST_ALIGNMENT__ to be compatible with gcc. Builder.defineMacro("__BIGGEST_ALIGNMENT__", - Twine(TI.getSuitableAlign() / TI.getCharWidth())); + Twine(TI.getSuitableAlign() / TI.getCharWidth()) ); if (!LangOpts.CharIsSigned) Builder.defineMacro("__CHAR_UNSIGNED__"); @@ -1369,7 +1370,7 @@ static void InitializePredefinedMacros(const TargetInfo &TI, Builder.append("#pragma push_macro(\"__GCC_CONSTRUCTIVE_SIZE\")"); auto addLockFreeMacros = [&](const llvm::Twine &Prefix) { - // Used by libc++ and libstdc++ to implement ATOMIC__LOCK_FREE. + // Used by libc++ and libstdc++ to implement ATOMIC__LOCK_FREE. #define DEFINE_LOCK_FREE_MACRO(TYPE, Type) \ Builder.defineMacro(Prefix + #TYPE "_LOCK_FREE", \ getLockFreeValue(TI.get##Type##Width(), TI)); @@ -1395,7 +1396,7 @@ static void InitializePredefinedMacros(const TargetInfo &TI, if (LangOpts.GNUCVersion) addLockFreeMacros("__GCC_ATOMIC_"); - if (CGOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining) + if (LangOpts.NoInlineDefine) Builder.defineMacro("__NO_INLINE__"); if (unsigned PICLevel = LangOpts.PICLevel) { @@ -1534,7 +1535,6 @@ static void InitializePredefinedMacros(const TargetInfo &TI, #define TARGET_OS(Name, Predicate) \ Builder.defineMacro(#Name, (Predicate) ? "1" : "0"); #include "clang/Basic/TargetOSMacros.def" - #undef TARGET_OS } @@ -1556,7 +1556,8 @@ static void InitializePGOProfileMacros(const CodeGenOptions &CodeGenOpts, void clang::InitializePreprocessor(Preprocessor &PP, const PreprocessorOptions &InitOpts, const PCHContainerReader &PCHContainerRdr, - const FrontendOptions &FEOpts) { + const FrontendOptions &FEOpts, + const CodeGenOptions &CodeGenOpts) { const LangOptions &LangOpts = PP.getLangOpts(); std::string PredefineBuffer; PredefineBuffer.reserve(4080); @@ -1574,12 +1575,10 @@ void clang::InitializePreprocessor(Preprocessor &PP, // macros. This is not the right way to handle this. if ((LangOpts.CUDA || LangOpts.isTargetDevice()) && PP.getAuxTargetInfo()) InitializePredefinedMacros(*PP.getAuxTargetInfo(), LangOpts, FEOpts, - PP.getPreprocessorOpts(), PP.getCodeGenOpts(), - Builder); + PP.getPreprocessorOpts(), Builder); InitializePredefinedMacros(PP.getTargetInfo(), LangOpts, FEOpts, - PP.getPreprocessorOpts(), PP.getCodeGenOpts(), - Builder); + PP.getPreprocessorOpts(), Builder); // Install definitions to make Objective-C++ ARC work well with various // C++ Standard Library implementations. @@ -1606,7 +1605,7 @@ void clang::InitializePreprocessor(Preprocessor &PP, // The PGO instrumentation profile macros are driven by options // -fprofile[-instr]-generate/-fcs-profile-generate/-fprofile[-instr]-use, // hence they are not guarded by InitOpts.UsePredefines. - InitializePGOProfileMacros(PP.getCodeGenOpts(), Builder); + InitializePGOProfileMacros(CodeGenOpts, Builder); // Add on the predefines from the driver. Wrap in a #line directive to report // that they come from the command line. @@ -1614,7 +1613,7 @@ void clang::InitializePreprocessor(Preprocessor &PP, // Process #define's and #undef's in the order they are given. for (unsigned i = 0, e = InitOpts.Macros.size(); i != e; ++i) { - if (InitOpts.Macros[i].second) // isUndef + if (InitOpts.Macros[i].second) // isUndef Builder.undefineMacro(InitOpts.Macros[i].first); else DefineBuiltinMacro(Builder, InitOpts.Macros[i].first, From 9039837cf9f1c4c7326210945f7d88fc723af26c Mon Sep 17 00:00:00 2001 From: Jan Svoboda Date: Mon, 30 Jun 2025 15:38:17 -0700 Subject: [PATCH 4/5] Redo InitPreprocessor.cpp changes without formatting changes --- clang/lib/Frontend/InitPreprocessor.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp index f64613fb4a6cb..d3eaa3658f277 100644 --- a/clang/lib/Frontend/InitPreprocessor.cpp +++ b/clang/lib/Frontend/InitPreprocessor.cpp @@ -865,6 +865,7 @@ static void InitializePredefinedMacros(const TargetInfo &TI, const LangOptions &LangOpts, const FrontendOptions &FEOpts, const PreprocessorOptions &PPOpts, + const CodeGenOptions &CGOpts, MacroBuilder &Builder) { // Compiler version introspection macros. Builder.defineMacro("__llvm__"); // LLVM Backend @@ -1073,9 +1074,9 @@ static void InitializePredefinedMacros(const TargetInfo &TI, Builder.defineMacro("__clang_wide_literal_encoding__", "\"UTF-16\""); } - if (LangOpts.Optimize) + if (CGOpts.OptimizationLevel != 0) Builder.defineMacro("__OPTIMIZE__"); - if (LangOpts.OptimizeSize) + if (CGOpts.OptimizeSize != 0) Builder.defineMacro("__OPTIMIZE_SIZE__"); if (LangOpts.FastMath) @@ -1396,7 +1397,7 @@ static void InitializePredefinedMacros(const TargetInfo &TI, if (LangOpts.GNUCVersion) addLockFreeMacros("__GCC_ATOMIC_"); - if (LangOpts.NoInlineDefine) + if (CGOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining) Builder.defineMacro("__NO_INLINE__"); if (unsigned PICLevel = LangOpts.PICLevel) { @@ -1556,8 +1557,7 @@ static void InitializePGOProfileMacros(const CodeGenOptions &CodeGenOpts, void clang::InitializePreprocessor(Preprocessor &PP, const PreprocessorOptions &InitOpts, const PCHContainerReader &PCHContainerRdr, - const FrontendOptions &FEOpts, - const CodeGenOptions &CodeGenOpts) { + const FrontendOptions &FEOpts) { const LangOptions &LangOpts = PP.getLangOpts(); std::string PredefineBuffer; PredefineBuffer.reserve(4080); @@ -1575,10 +1575,12 @@ void clang::InitializePreprocessor(Preprocessor &PP, // macros. This is not the right way to handle this. if ((LangOpts.CUDA || LangOpts.isTargetDevice()) && PP.getAuxTargetInfo()) InitializePredefinedMacros(*PP.getAuxTargetInfo(), LangOpts, FEOpts, - PP.getPreprocessorOpts(), Builder); + PP.getPreprocessorOpts(), PP.getCodeGenOpts(), + Builder); InitializePredefinedMacros(PP.getTargetInfo(), LangOpts, FEOpts, - PP.getPreprocessorOpts(), Builder); + PP.getPreprocessorOpts(), PP.getCodeGenOpts(), + Builder); // Install definitions to make Objective-C++ ARC work well with various // C++ Standard Library implementations. @@ -1605,7 +1607,7 @@ void clang::InitializePreprocessor(Preprocessor &PP, // The PGO instrumentation profile macros are driven by options // -fprofile[-instr]-generate/-fcs-profile-generate/-fprofile[-instr]-use, // hence they are not guarded by InitOpts.UsePredefines. - InitializePGOProfileMacros(CodeGenOpts, Builder); + InitializePGOProfileMacros(PP.getCodeGenOpts(), Builder); // Add on the predefines from the driver. Wrap in a #line directive to report // that they come from the command line. From d544174ff67a6dbc923041bf814a2f02573a2bc8 Mon Sep 17 00:00:00 2001 From: Jan Svoboda Date: Tue, 1 Jul 2025 12:58:10 -0700 Subject: [PATCH 5/5] Do not store `CodeGenOptions` on `Preprocessor` --- .../ExpandModularHeadersPPCallbacks.cpp | 4 +- .../ExpandModularHeadersPPCallbacks.h | 1 - clang-tools-extra/clangd/ModulesBuilder.cpp | 6 +-- clang/include/clang/Frontend/ASTUnit.h | 3 +- .../include/clang/Frontend/CompilerInstance.h | 1 + clang/include/clang/Frontend/Utils.h | 3 +- clang/include/clang/Lex/Preprocessor.h | 8 +--- clang/include/clang/Serialization/ASTReader.h | 5 +++ clang/include/clang/Serialization/ASTWriter.h | 21 ++++++---- clang/lib/CodeGen/CodeGenAction.cpp | 2 +- clang/lib/Frontend/ASTUnit.cpp | 38 ++++++++++++------- clang/lib/Frontend/ChainedIncludesSource.cpp | 5 ++- clang/lib/Frontend/CompilerInstance.cpp | 20 +++++----- clang/lib/Frontend/FrontendActions.cpp | 17 +++++---- clang/lib/Frontend/InitPreprocessor.cpp | 10 ++--- clang/lib/Frontend/PrecompiledPreamble.cpp | 8 ++-- clang/lib/Lex/Preprocessor.cpp | 6 +-- clang/lib/Serialization/ASTReader.cpp | 4 +- clang/lib/Serialization/ASTWriter.cpp | 5 ++- clang/lib/Serialization/GeneratePCH.cpp | 6 ++- .../Analysis/MacroExpansionContextTest.cpp | 4 +- clang/unittests/Basic/SourceManagerTest.cpp | 16 +++----- clang/unittests/Lex/LexerTest.cpp | 6 +-- clang/unittests/Lex/ModuleDeclStateTest.cpp | 5 +-- clang/unittests/Lex/PPCallbacksTest.cpp | 26 +++++-------- .../Lex/PPConditionalDirectiveRecordTest.cpp | 6 +-- .../Lex/PPDependencyDirectivesTest.cpp | 6 +-- .../unittests/Lex/PPMemoryAllocationsTest.cpp | 6 +-- .../Parse/ParseHLSLRootSignatureTest.cpp | 5 +-- 29 files changed, 135 insertions(+), 118 deletions(-) diff --git a/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp b/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp index e599f499e2bfc..2c17cd3b6e979 100644 --- a/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp +++ b/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp @@ -90,14 +90,14 @@ ExpandModularHeadersPPCallbacks::ExpandModularHeadersPPCallbacks( &Compiler.getTarget()); PP = std::make_unique(Compiler.getPreprocessorOpts(), - Diags, LangOpts, CGOpts, Sources, + Diags, LangOpts, Sources, *HeaderInfo, ModuleLoader, /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); PP->Initialize(Compiler.getTarget(), Compiler.getAuxTarget()); InitializePreprocessor(*PP, Compiler.getPreprocessorOpts(), Compiler.getPCHContainerReader(), - Compiler.getFrontendOpts()); + Compiler.getFrontendOpts(), Compiler.getCodeGenOpts()); ApplyHeaderSearchOptions(*HeaderInfo, HSOpts, LangOpts, Compiler.getTarget().getTriple()); } diff --git a/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.h b/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.h index 1bd1ba40c65de..e599bda92c25c 100644 --- a/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.h +++ b/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.h @@ -131,7 +131,6 @@ class ExpandModularHeadersPPCallbacks : public PPCallbacks { DiagnosticOptions DiagOpts; DiagnosticsEngine Diags; LangOptions LangOpts; - CodeGenOptions CGOpts; HeaderSearchOptions HSOpts; TrivialModuleLoader ModuleLoader; diff --git a/clang-tools-extra/clangd/ModulesBuilder.cpp b/clang-tools-extra/clangd/ModulesBuilder.cpp index 665de0e93f7b0..b9aeb3889f5f2 100644 --- a/clang-tools-extra/clangd/ModulesBuilder.cpp +++ b/clang-tools-extra/clangd/ModulesBuilder.cpp @@ -194,7 +194,6 @@ bool IsModuleFileUpToDate(PathRef ModuleFilePath, LangOptions LangOpts; LangOpts.SkipODRCheckInGMF = true; - CodeGenOptions CGOpts; FileManager FileMgr(FileSystemOptions(), VFS); @@ -205,13 +204,14 @@ bool IsModuleFileUpToDate(PathRef ModuleFilePath, PreprocessorOptions PPOpts; TrivialModuleLoader ModuleLoader; - Preprocessor PP(PPOpts, *Diags, LangOpts, CGOpts, SourceMgr, HeaderInfo, + Preprocessor PP(PPOpts, *Diags, LangOpts, SourceMgr, HeaderInfo, ModuleLoader); IntrusiveRefCntPtr ModCache = createCrossProcessModuleCache(); PCHContainerOperations PCHOperations; + CodeGenOptions CodeGenOpts; ASTReader Reader(PP, *ModCache, /*ASTContext=*/nullptr, - PCHOperations.getRawReader(), {}); + PCHOperations.getRawReader(), CodeGenOpts, {}); // We don't need any listener here. By default it will use a validator // listener. diff --git a/clang/include/clang/Frontend/ASTUnit.h b/clang/include/clang/Frontend/ASTUnit.h index f0076d2a7cdb0..1286fe4cca0f4 100644 --- a/clang/include/clang/Frontend/ASTUnit.h +++ b/clang/include/clang/Frontend/ASTUnit.h @@ -62,6 +62,7 @@ class ASTContext; class ASTDeserializationListener; class ASTMutationListener; class ASTReader; +class CodeGenOptions; class CompilerInstance; class CompilerInvocation; class Decl; @@ -107,7 +108,7 @@ class ASTUnit { private: std::unique_ptr LangOpts; - std::unique_ptr CGOpts = std::make_unique(); + std::unique_ptr CodeGenOpts; // FIXME: The documentation on \c LoadFrom* member functions states that the // DiagnosticsEngine (and therefore DiagnosticOptions) must outlive the // returned ASTUnit. This is not the case. Enfore it by storing non-owning diff --git a/clang/include/clang/Frontend/CompilerInstance.h b/clang/include/clang/Frontend/CompilerInstance.h index 0ae490f0e8073..2408367e19833 100644 --- a/clang/include/clang/Frontend/CompilerInstance.h +++ b/clang/include/clang/Frontend/CompilerInstance.h @@ -720,6 +720,7 @@ class CompilerInstance : public ModuleLoader { DisableValidationForModuleKind DisableValidation, bool AllowPCHWithCompilerErrors, Preprocessor &PP, ModuleCache &ModCache, ASTContext &Context, const PCHContainerReader &PCHContainerRdr, + const CodeGenOptions &CodeGenOpts, ArrayRef> Extensions, ArrayRef> DependencyCollectors, void *DeserializationListener, bool OwnDeserializationListener, diff --git a/clang/include/clang/Frontend/Utils.h b/clang/include/clang/Frontend/Utils.h index fe6dc923363d6..7e2104a412327 100644 --- a/clang/include/clang/Frontend/Utils.h +++ b/clang/include/clang/Frontend/Utils.h @@ -49,7 +49,8 @@ class CodeGenOptions; /// environment ready to process a single file. void InitializePreprocessor(Preprocessor &PP, const PreprocessorOptions &PPOpts, const PCHContainerReader &PCHContainerRdr, - const FrontendOptions &FEOpts); + const FrontendOptions &FEOpts, + const CodeGenOptions &CodeGenOptions); /// DoPrintPreprocessedInput - Implement -E mode. void DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS, diff --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h index 237ae290be698..4d82e20e5d4f3 100644 --- a/clang/include/clang/Lex/Preprocessor.h +++ b/clang/include/clang/Lex/Preprocessor.h @@ -14,7 +14,6 @@ #ifndef LLVM_CLANG_LEX_PREPROCESSOR_H #define LLVM_CLANG_LEX_PREPROCESSOR_H -#include "clang/Basic/CodeGenOptions.h" #include "clang/Basic/Diagnostic.h" #include "clang/Basic/DiagnosticIDs.h" #include "clang/Basic/IdentifierTable.h" @@ -156,7 +155,6 @@ class Preprocessor { const PreprocessorOptions &PPOpts; DiagnosticsEngine *Diags; const LangOptions &LangOpts; - const CodeGenOptions &CGOpts; const TargetInfo *Target = nullptr; const TargetInfo *AuxTarget = nullptr; FileManager &FileMgr; @@ -1183,9 +1181,8 @@ class Preprocessor { public: Preprocessor(const PreprocessorOptions &PPOpts, DiagnosticsEngine &diags, - const LangOptions &LangOpts, const CodeGenOptions &CGOPts, - SourceManager &SM, HeaderSearch &Headers, - ModuleLoader &TheModuleLoader, + const LangOptions &LangOpts, SourceManager &SM, + HeaderSearch &Headers, ModuleLoader &TheModuleLoader, IdentifierInfoLookup *IILookup = nullptr, bool OwnsHeaderSearch = false, TranslationUnitKind TUKind = TU_Complete); @@ -1219,7 +1216,6 @@ class Preprocessor { void setDiagnostics(DiagnosticsEngine &D) { Diags = &D; } const LangOptions &getLangOpts() const { return LangOpts; } - const CodeGenOptions &getCodeGenOpts() const { return CGOpts; } const TargetInfo &getTargetInfo() const { return *Target; } const TargetInfo *getAuxTargetInfo() const { return AuxTarget; } FileManager &getFileManager() const { return FileMgr; } diff --git a/clang/include/clang/Serialization/ASTReader.h b/clang/include/clang/Serialization/ASTReader.h index 303b4c5941877..5b6a296f8a157 100644 --- a/clang/include/clang/Serialization/ASTReader.h +++ b/clang/include/clang/Serialization/ASTReader.h @@ -509,6 +509,9 @@ class ASTReader /// The AST consumer. ASTConsumer *Consumer = nullptr; + /// The codegen options. + const CodeGenOptions &CodeGenOpts; + /// The module manager which manages modules and their dependencies ModuleManager ModuleMgr; @@ -1797,6 +1800,7 @@ class ASTReader /// deserializing. ASTReader(Preprocessor &PP, ModuleCache &ModCache, ASTContext *Context, const PCHContainerReader &PCHContainerRdr, + const CodeGenOptions &CodeGenOpts, ArrayRef> Extensions, StringRef isysroot = "", DisableValidationForModuleKind DisableValidationKind = @@ -1815,6 +1819,7 @@ class ASTReader SourceManager &getSourceManager() const { return SourceMgr; } FileManager &getFileManager() const { return FileMgr; } DiagnosticsEngine &getDiags() const { return Diags; } + const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; } /// Flags that indicate what kind of AST loading failures the client /// of the AST reader can directly handle. diff --git a/clang/include/clang/Serialization/ASTWriter.h b/clang/include/clang/Serialization/ASTWriter.h index c86019f01d9f3..edf5bbaddf1aa 100644 --- a/clang/include/clang/Serialization/ASTWriter.h +++ b/clang/include/clang/Serialization/ASTWriter.h @@ -49,6 +49,7 @@ namespace clang { class ASTContext; class ASTReader; class Attr; +class CodeGenOptions; class CXXRecordDecl; class FileEntry; class FPOptionsOverride; @@ -124,6 +125,8 @@ class ASTWriter : public ASTDeserializationListener, /// The PCM manager which manages memory buffers for pcm files. ModuleCache &ModCache; + const CodeGenOptions &CodeGenOpts; + /// The preprocessor we're writing. Preprocessor *PP = nullptr; @@ -686,13 +689,14 @@ class ASTWriter : public ASTDeserializationListener, /// Create a new precompiled header writer that outputs to /// the given bitstream. ASTWriter(llvm::BitstreamWriter &Stream, SmallVectorImpl &Buffer, - ModuleCache &ModCache, + ModuleCache &ModCache, const CodeGenOptions &CodeGenOpts, ArrayRef> Extensions, bool IncludeTimestamps = true, bool BuildingImplicitModule = false, bool GeneratingReducedBMI = false); ~ASTWriter() override; const LangOptions &getLangOpts() const; + const CodeGenOptions &getCodeGenOpts() const { return CodeGenOpts; } /// Get a timestamp for output into the AST file. The actual timestamp /// of the specified file may be ignored if we have been instructed to not @@ -999,6 +1003,7 @@ class PCHGenerator : public SemaConsumer { public: PCHGenerator(Preprocessor &PP, ModuleCache &ModCache, StringRef OutputFile, StringRef isysroot, std::shared_ptr Buffer, + const CodeGenOptions &CodeGenOpts, ArrayRef> Extensions, bool AllowASTWithErrors = false, bool IncludeTimestamps = true, bool BuildingImplicitModule = false, @@ -1021,13 +1026,14 @@ class CXX20ModulesGenerator : public PCHGenerator { virtual Module *getEmittingModule(ASTContext &Ctx) override; CXX20ModulesGenerator(Preprocessor &PP, ModuleCache &ModCache, - StringRef OutputFile, bool GeneratingReducedBMI, - bool AllowASTWithErrors); + StringRef OutputFile, const CodeGenOptions &CodeGenOpts, + bool GeneratingReducedBMI, bool AllowASTWithErrors); public: CXX20ModulesGenerator(Preprocessor &PP, ModuleCache &ModCache, - StringRef OutputFile, bool AllowASTWithErrors = false) - : CXX20ModulesGenerator(PP, ModCache, OutputFile, + StringRef OutputFile, const CodeGenOptions &CodeGenOpts, + bool AllowASTWithErrors = false) + : CXX20ModulesGenerator(PP, ModCache, OutputFile, CodeGenOpts, /*GeneratingReducedBMI=*/false, AllowASTWithErrors) {} @@ -1039,8 +1045,9 @@ class ReducedBMIGenerator : public CXX20ModulesGenerator { public: ReducedBMIGenerator(Preprocessor &PP, ModuleCache &ModCache, - StringRef OutputFile, bool AllowASTWithErrors = false) - : CXX20ModulesGenerator(PP, ModCache, OutputFile, + StringRef OutputFile, const CodeGenOptions &CodeGenOpts, + bool AllowASTWithErrors = false) + : CXX20ModulesGenerator(PP, ModCache, OutputFile, CodeGenOpts, /*GeneratingReducedBMI=*/true, AllowASTWithErrors) {} }; diff --git a/clang/lib/CodeGen/CodeGenAction.cpp b/clang/lib/CodeGen/CodeGenAction.cpp index 5493cc92bd8b0..eb5b6045510df 100644 --- a/clang/lib/CodeGen/CodeGenAction.cpp +++ b/clang/lib/CodeGen/CodeGenAction.cpp @@ -995,7 +995,7 @@ CodeGenAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { std::vector> Consumers(2); Consumers[0] = std::make_unique( CI.getPreprocessor(), CI.getModuleCache(), - CI.getFrontendOpts().ModuleOutputPath); + CI.getFrontendOpts().ModuleOutputPath, CI.getCodeGenOpts()); Consumers[1] = std::move(Result); return std::make_unique(std::move(Consumers)); } diff --git a/clang/lib/Frontend/ASTUnit.cpp b/clang/lib/Frontend/ASTUnit.cpp index 5db3ae8385acf..1b6f03be38c62 100644 --- a/clang/lib/Frontend/ASTUnit.cpp +++ b/clang/lib/Frontend/ASTUnit.cpp @@ -215,8 +215,8 @@ struct ASTUnit::ASTWriterData { llvm::BitstreamWriter Stream; ASTWriter Writer; - ASTWriterData(ModuleCache &ModCache) - : Stream(Buffer), Writer(Stream, Buffer, ModCache, {}) {} + ASTWriterData(ModuleCache &ModCache, const CodeGenOptions &CGOpts) + : Stream(Buffer), Writer(Stream, Buffer, ModCache, CGOpts, {}) {} }; void ASTUnit::clearFileLevelDecls() { @@ -235,7 +235,8 @@ const unsigned DefaultPreambleRebuildInterval = 5; static std::atomic ActiveASTUnitObjects; ASTUnit::ASTUnit(bool _MainFileIsAST) - : MainFileIsAST(_MainFileIsAST), WantTiming(getenv("LIBCLANG_TIMING")), + : CodeGenOpts(std::make_unique()), + MainFileIsAST(_MainFileIsAST), WantTiming(getenv("LIBCLANG_TIMING")), ShouldCacheCodeCompletionResults(false), IncludeBriefCommentsInCodeCompletion(false), UserFilesAreVolatile(false), UnsafeToFree(false) { @@ -516,6 +517,7 @@ class ASTInfoCollector : public ASTReaderListener { HeaderSearchOptions &HSOpts; PreprocessorOptions &PPOpts; LangOptions &LangOpt; + CodeGenOptions &CodeGenOpts; std::shared_ptr &TargetOpts; IntrusiveRefCntPtr &Target; unsigned &Counter; @@ -525,12 +527,12 @@ class ASTInfoCollector : public ASTReaderListener { public: ASTInfoCollector(Preprocessor &PP, ASTContext *Context, HeaderSearchOptions &HSOpts, PreprocessorOptions &PPOpts, - LangOptions &LangOpt, + LangOptions &LangOpt, CodeGenOptions &CodeGenOpts, std::shared_ptr &TargetOpts, IntrusiveRefCntPtr &Target, unsigned &Counter) : PP(PP), Context(Context), HSOpts(HSOpts), PPOpts(PPOpts), - LangOpt(LangOpt), TargetOpts(TargetOpts), Target(Target), - Counter(Counter) {} + LangOpt(LangOpt), CodeGenOpts(CodeGenOpts), TargetOpts(TargetOpts), + Target(Target), Counter(Counter) {} bool ReadLanguageOptions(const LangOptions &LangOpts, StringRef ModuleFilename, bool Complain, @@ -555,6 +557,13 @@ class ASTInfoCollector : public ASTReaderListener { return false; } + bool ReadCodeGenOptions(const CodeGenOptions &CGOpts, + StringRef ModuleFilename, bool Complain, + bool AllowCompatibleDifferences) override { + this->CodeGenOpts = CGOpts; + return false; + } + bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts, StringRef ModuleFilename, StringRef SpecificModuleCachePath, @@ -842,7 +851,7 @@ std::unique_ptr ASTUnit::LoadFromASTFile( HeaderSearch &HeaderInfo = *AST->HeaderInfo; AST->PP = std::make_shared( - *AST->PPOpts, AST->getDiagnostics(), *AST->LangOpts, *AST->CGOpts, + *AST->PPOpts, AST->getDiagnostics(), *AST->LangOpts, AST->getSourceManager(), HeaderInfo, AST->ModuleLoader, /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); @@ -858,14 +867,16 @@ std::unique_ptr ASTUnit::LoadFromASTFile( DisableValidationForModuleKind::None; if (::getenv("LIBCLANG_DISABLE_PCH_VALIDATION")) disableValid = DisableValidationForModuleKind::All; - AST->Reader = new ASTReader( - PP, *AST->ModCache, AST->Ctx.get(), PCHContainerRdr, {}, /*isysroot=*/"", - /*DisableValidationKind=*/disableValid, AllowASTWithCompilerErrors); + AST->Reader = new ASTReader(PP, *AST->ModCache, AST->Ctx.get(), + PCHContainerRdr, *AST->CodeGenOpts, {}, + /*isysroot=*/"", + /*DisableValidationKind=*/disableValid, + AllowASTWithCompilerErrors); unsigned Counter = 0; AST->Reader->setListener(std::make_unique( *AST->PP, AST->Ctx.get(), *AST->HSOpts, *AST->PPOpts, *AST->LangOpts, - AST->TargetOpts, AST->Target, Counter)); + *AST->CodeGenOpts, AST->TargetOpts, AST->Target, Counter)); // Attach the AST reader to the AST context as an external AST // source, so that declarations will be deserialized from the @@ -1836,6 +1847,7 @@ std::unique_ptr ASTUnit::LoadFromCommandLine( AST->DiagOpts = DiagOpts; AST->Diagnostics = Diags; AST->FileSystemOpts = CI->getFileSystemOpts(); + AST->CodeGenOpts = std::make_unique(CI->getCodeGenOpts()); VFS = createVFSFromCompilerInvocation(*CI, *Diags, VFS); AST->FileMgr = new FileManager(AST->FileSystemOpts, VFS); AST->StorePreamblesInMemory = StorePreamblesInMemory; @@ -1851,7 +1863,7 @@ std::unique_ptr ASTUnit::LoadFromCommandLine( AST->Invocation = CI; AST->SkipFunctionBodies = SkipFunctionBodies; if (ForSerialization) - AST->WriterData.reset(new ASTWriterData(*AST->ModCache)); + AST->WriterData.reset(new ASTWriterData(*AST->ModCache, *AST->CodeGenOpts)); // Zero out now to ease cleanup during crash recovery. CI = nullptr; Diags = nullptr; @@ -2385,7 +2397,7 @@ bool ASTUnit::serialize(raw_ostream &OS) { SmallString<128> Buffer; llvm::BitstreamWriter Stream(Buffer); IntrusiveRefCntPtr ModCache = createCrossProcessModuleCache(); - ASTWriter Writer(Stream, Buffer, *ModCache, {}); + ASTWriter Writer(Stream, Buffer, *ModCache, *CodeGenOpts, {}); return serializeUnit(Writer, Buffer, getSema(), OS); } diff --git a/clang/lib/Frontend/ChainedIncludesSource.cpp b/clang/lib/Frontend/ChainedIncludesSource.cpp index f9a398dbfb90f..ba7c767f8e987 100644 --- a/clang/lib/Frontend/ChainedIncludesSource.cpp +++ b/clang/lib/Frontend/ChainedIncludesSource.cpp @@ -62,7 +62,7 @@ createASTReader(CompilerInstance &CI, StringRef pchFile, std::unique_ptr Reader; Reader.reset(new ASTReader( PP, CI.getModuleCache(), &CI.getASTContext(), CI.getPCHContainerReader(), - /*Extensions=*/{}, + CI.getCodeGenOpts(), /*Extensions=*/{}, /*isysroot=*/"", DisableValidationForModuleKind::PCH)); for (unsigned ti = 0; ti < bufNames.size(); ++ti) { StringRef sr(bufNames[ti]); @@ -138,7 +138,8 @@ IntrusiveRefCntPtr clang::createChainedIncludesSource( ArrayRef> Extensions; auto consumer = std::make_unique( Clang->getPreprocessor(), Clang->getModuleCache(), "-", /*isysroot=*/"", - Buffer, Extensions, /*AllowASTWithErrors=*/true); + Buffer, Clang->getCodeGenOpts(), Extensions, + /*AllowASTWithErrors=*/true); Clang->getASTContext().setASTMutationListener( consumer->GetASTMutationListener()); Clang->setASTConsumer(std::move(consumer)); diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp index f254b23eec0ed..6c8b08b6acb1b 100644 --- a/clang/lib/Frontend/CompilerInstance.cpp +++ b/clang/lib/Frontend/CompilerInstance.cpp @@ -449,11 +449,11 @@ void CompilerInstance::createPreprocessor(TranslationUnitKind TUKind) { HeaderSearch *HeaderInfo = new HeaderSearch(getHeaderSearchOpts(), getSourceManager(), getDiagnostics(), getLangOpts(), &getTarget()); - PP = std::make_shared( - Invocation->getPreprocessorOpts(), getDiagnostics(), getLangOpts(), - getCodeGenOpts(), getSourceManager(), *HeaderInfo, *this, - /*IdentifierInfoLookup=*/nullptr, - /*OwnsHeaderSearch=*/true, TUKind); + PP = std::make_shared(Invocation->getPreprocessorOpts(), + getDiagnostics(), getLangOpts(), + getSourceManager(), *HeaderInfo, *this, + /*IdentifierInfoLookup=*/nullptr, + /*OwnsHeaderSearch=*/true, TUKind); getTarget().adjust(getDiagnostics(), getLangOpts()); PP->Initialize(getTarget(), getAuxTarget()); @@ -466,7 +466,7 @@ void CompilerInstance::createPreprocessor(TranslationUnitKind TUKind) { // Predefine macros and configure the preprocessor. InitializePreprocessor(*PP, PPOpts, getPCHContainerReader(), - getFrontendOpts()); + getFrontendOpts(), getCodeGenOpts()); // Initialize the header search object. In CUDA compilations, we use the aux // triple (the host triple) to initialize our header search, since we need to @@ -614,7 +614,7 @@ void CompilerInstance::createPCHExternalASTSource( TheASTReader = createPCHExternalASTSource( Path, getHeaderSearchOpts().Sysroot, DisableValidation, AllowPCHWithCompilerErrors, getPreprocessor(), getModuleCache(), - getASTContext(), getPCHContainerReader(), + getASTContext(), getPCHContainerReader(), getCodeGenOpts(), getFrontendOpts().ModuleFileExtensions, DependencyCollectors, DeserializationListener, OwnDeserializationListener, Preamble, getFrontendOpts().UseGlobalModuleIndex); @@ -625,6 +625,7 @@ IntrusiveRefCntPtr CompilerInstance::createPCHExternalASTSource( DisableValidationForModuleKind DisableValidation, bool AllowPCHWithCompilerErrors, Preprocessor &PP, ModuleCache &ModCache, ASTContext &Context, const PCHContainerReader &PCHContainerRdr, + const CodeGenOptions &CodeGenOpts, ArrayRef> Extensions, ArrayRef> DependencyCollectors, void *DeserializationListener, bool OwnDeserializationListener, @@ -633,7 +634,7 @@ IntrusiveRefCntPtr CompilerInstance::createPCHExternalASTSource( PP.getHeaderSearchInfo().getHeaderSearchOpts(); IntrusiveRefCntPtr Reader(new ASTReader( - PP, ModCache, &Context, PCHContainerRdr, Extensions, + PP, ModCache, &Context, PCHContainerRdr, CodeGenOpts, Extensions, Sysroot.empty() ? "" : Sysroot.data(), DisableValidation, AllowPCHWithCompilerErrors, /*AllowConfigurationMismatch*/ false, HSOpts.ModulesValidateSystemHeaders, @@ -1746,7 +1747,8 @@ void CompilerInstance::createASTReader() { "Reading modules", *timerGroup); TheASTReader = new ASTReader( getPreprocessor(), getModuleCache(), &getASTContext(), - getPCHContainerReader(), getFrontendOpts().ModuleFileExtensions, + getPCHContainerReader(), getCodeGenOpts(), + getFrontendOpts().ModuleFileExtensions, Sysroot.empty() ? "" : Sysroot.c_str(), PPOpts.DisablePCHOrModuleValidation, /*AllowASTWithCompilerErrors=*/FEOpts.AllowPCMWithCompilerErrors, diff --git a/clang/lib/Frontend/FrontendActions.cpp b/clang/lib/Frontend/FrontendActions.cpp index c0ab708d8b844..1d660262d4f4c 100644 --- a/clang/lib/Frontend/FrontendActions.cpp +++ b/clang/lib/Frontend/FrontendActions.cpp @@ -139,7 +139,7 @@ GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { std::vector> Consumers; Consumers.push_back(std::make_unique( CI.getPreprocessor(), CI.getModuleCache(), OutputFile, Sysroot, Buffer, - FrontendOpts.ModuleFileExtensions, + CI.getCodeGenOpts(), FrontendOpts.ModuleFileExtensions, CI.getPreprocessorOpts().AllowPCHWithCompilerErrors, FrontendOpts.IncludeTimestamps, FrontendOpts.BuildingImplicitModule, +CI.getLangOpts().CacheGeneratedPCH)); @@ -199,7 +199,7 @@ GenerateModuleAction::CreateMultiplexConsumer(CompilerInstance &CI, Consumers.push_back(std::make_unique( CI.getPreprocessor(), CI.getModuleCache(), OutputFile, Sysroot, Buffer, - CI.getFrontendOpts().ModuleFileExtensions, + CI.getCodeGenOpts(), CI.getFrontendOpts().ModuleFileExtensions, /*AllowASTWithErrors=*/ +CI.getFrontendOpts().AllowPCMWithCompilerErrors, /*IncludeTimestamps=*/ @@ -292,13 +292,13 @@ GenerateModuleInterfaceAction::CreateASTConsumer(CompilerInstance &CI, !CI.getFrontendOpts().ModuleOutputPath.empty()) { Consumers.push_back(std::make_unique( CI.getPreprocessor(), CI.getModuleCache(), - CI.getFrontendOpts().ModuleOutputPath, + CI.getFrontendOpts().ModuleOutputPath, CI.getCodeGenOpts(), +CI.getFrontendOpts().AllowPCMWithCompilerErrors)); } Consumers.push_back(std::make_unique( CI.getPreprocessor(), CI.getModuleCache(), - CI.getFrontendOpts().OutputFile, + CI.getFrontendOpts().OutputFile, CI.getCodeGenOpts(), +CI.getFrontendOpts().AllowPCMWithCompilerErrors)); return std::make_unique(std::move(Consumers)); @@ -313,9 +313,9 @@ GenerateModuleInterfaceAction::CreateOutputFile(CompilerInstance &CI, std::unique_ptr GenerateReducedModuleInterfaceAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { - return std::make_unique(CI.getPreprocessor(), - CI.getModuleCache(), - CI.getFrontendOpts().OutputFile); + return std::make_unique( + CI.getPreprocessor(), CI.getModuleCache(), + CI.getFrontendOpts().OutputFile, CI.getCodeGenOpts()); } bool GenerateHeaderUnitAction::BeginSourceFileAction(CompilerInstance &CI) { @@ -358,7 +358,8 @@ void VerifyPCHAction::ExecuteAction() { const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot; std::unique_ptr Reader(new ASTReader( CI.getPreprocessor(), CI.getModuleCache(), &CI.getASTContext(), - CI.getPCHContainerReader(), CI.getFrontendOpts().ModuleFileExtensions, + CI.getPCHContainerReader(), CI.getCodeGenOpts(), + CI.getFrontendOpts().ModuleFileExtensions, Sysroot.empty() ? "" : Sysroot.c_str(), DisableValidationForModuleKind::None, /*AllowASTWithCompilerErrors*/ false, diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp index d3eaa3658f277..3eb5e6ff1c0bd 100644 --- a/clang/lib/Frontend/InitPreprocessor.cpp +++ b/clang/lib/Frontend/InitPreprocessor.cpp @@ -1557,7 +1557,8 @@ static void InitializePGOProfileMacros(const CodeGenOptions &CodeGenOpts, void clang::InitializePreprocessor(Preprocessor &PP, const PreprocessorOptions &InitOpts, const PCHContainerReader &PCHContainerRdr, - const FrontendOptions &FEOpts) { + const FrontendOptions &FEOpts, + const CodeGenOptions &CodeGenOpts) { const LangOptions &LangOpts = PP.getLangOpts(); std::string PredefineBuffer; PredefineBuffer.reserve(4080); @@ -1575,12 +1576,11 @@ void clang::InitializePreprocessor(Preprocessor &PP, // macros. This is not the right way to handle this. if ((LangOpts.CUDA || LangOpts.isTargetDevice()) && PP.getAuxTargetInfo()) InitializePredefinedMacros(*PP.getAuxTargetInfo(), LangOpts, FEOpts, - PP.getPreprocessorOpts(), PP.getCodeGenOpts(), + PP.getPreprocessorOpts(), CodeGenOpts, Builder); InitializePredefinedMacros(PP.getTargetInfo(), LangOpts, FEOpts, - PP.getPreprocessorOpts(), PP.getCodeGenOpts(), - Builder); + PP.getPreprocessorOpts(), CodeGenOpts, Builder); // Install definitions to make Objective-C++ ARC work well with various // C++ Standard Library implementations. @@ -1607,7 +1607,7 @@ void clang::InitializePreprocessor(Preprocessor &PP, // The PGO instrumentation profile macros are driven by options // -fprofile[-instr]-generate/-fcs-profile-generate/-fprofile[-instr]-use, // hence they are not guarded by InitOpts.UsePredefines. - InitializePGOProfileMacros(PP.getCodeGenOpts(), Builder); + InitializePGOProfileMacros(CodeGenOpts, Builder); // Add on the predefines from the driver. Wrap in a #line directive to report // that they come from the command line. diff --git a/clang/lib/Frontend/PrecompiledPreamble.cpp b/clang/lib/Frontend/PrecompiledPreamble.cpp index 3f3fe3c9937e4..146cf903a5727 100644 --- a/clang/lib/Frontend/PrecompiledPreamble.cpp +++ b/clang/lib/Frontend/PrecompiledPreamble.cpp @@ -293,8 +293,9 @@ class PrecompilePreambleConsumer : public PCHGenerator { public: PrecompilePreambleConsumer(PrecompilePreambleAction &Action, Preprocessor &PP, ModuleCache &ModCache, StringRef isysroot, - std::shared_ptr Buffer) - : PCHGenerator(PP, ModCache, "", isysroot, std::move(Buffer), + std::shared_ptr Buffer, + const CodeGenOptions &CodeGenOpts) + : PCHGenerator(PP, ModCache, "", isysroot, std::move(Buffer), CodeGenOpts, ArrayRef>(), /*AllowASTWithErrors=*/true), Action(Action) {} @@ -337,7 +338,8 @@ PrecompilePreambleAction::CreateASTConsumer(CompilerInstance &CI, Sysroot.clear(); return std::make_unique( - *this, CI.getPreprocessor(), CI.getModuleCache(), Sysroot, Buffer); + *this, CI.getPreprocessor(), CI.getModuleCache(), Sysroot, Buffer, + CI.getCodeGenOpts()); } template bool moveOnNoError(llvm::ErrorOr Val, T &Output) { diff --git a/clang/lib/Lex/Preprocessor.cpp b/clang/lib/Lex/Preprocessor.cpp index 35bb9139cf816..bcd3ea60ce3da 100644 --- a/clang/lib/Lex/Preprocessor.cpp +++ b/clang/lib/Lex/Preprocessor.cpp @@ -79,11 +79,11 @@ ExternalPreprocessorSource::~ExternalPreprocessorSource() = default; Preprocessor::Preprocessor(const PreprocessorOptions &PPOpts, DiagnosticsEngine &diags, const LangOptions &opts, - const CodeGenOptions &CGOpts, SourceManager &SM, - HeaderSearch &Headers, ModuleLoader &TheModuleLoader, + SourceManager &SM, HeaderSearch &Headers, + ModuleLoader &TheModuleLoader, IdentifierInfoLookup *IILookup, bool OwnsHeaders, TranslationUnitKind TUKind) - : PPOpts(PPOpts), Diags(&diags), LangOpts(opts), CGOpts(CGOpts), + : PPOpts(PPOpts), Diags(&diags), LangOpts(opts), FileMgr(Headers.getFileMgr()), SourceMgr(SM), ScratchBuf(new ScratchBuffer(SourceMgr)), HeaderInfo(Headers), TheModuleLoader(TheModuleLoader), ExternalSource(nullptr), diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp index 4c0f1f6371b88..1e0a0f2fc6022 100644 --- a/clang/lib/Serialization/ASTReader.cpp +++ b/clang/lib/Serialization/ASTReader.cpp @@ -494,7 +494,7 @@ bool PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts, bool PCHValidator::ReadCodeGenOptions(const CodeGenOptions &CGOpts, StringRef ModuleFilename, bool Complain, bool AllowCompatibleDifferences) { - const CodeGenOptions &ExistingCGOpts = PP.getCodeGenOpts(); + const CodeGenOptions &ExistingCGOpts = Reader.getCodeGenOpts(); return checkCodegenOptions(ExistingCGOpts, CGOpts, ModuleFilename, Complain ? &Reader.Diags : nullptr, AllowCompatibleDifferences); @@ -11055,6 +11055,7 @@ void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) { ASTReader::ASTReader(Preprocessor &PP, ModuleCache &ModCache, ASTContext *Context, const PCHContainerReader &PCHContainerRdr, + const CodeGenOptions &CodeGenOpts, ArrayRef> Extensions, StringRef isysroot, DisableValidationForModuleKind DisableValidationKind, @@ -11069,6 +11070,7 @@ ASTReader::ASTReader(Preprocessor &PP, ModuleCache &ModCache, SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()), PCHContainerRdr(PCHContainerRdr), Diags(PP.getDiagnostics()), StackHandler(Diags), PP(PP), ContextObj(Context), + CodeGenOpts(CodeGenOpts), ModuleMgr(PP.getFileManager(), ModCache, PCHContainerRdr, PP.getHeaderSearchInfo()), DummyIdResolver(PP), ReadTimer(std::move(ReadTimer)), isysroot(isysroot), diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp index 374a35372b87f..50d5eb14e456a 100644 --- a/clang/lib/Serialization/ASTWriter.cpp +++ b/clang/lib/Serialization/ASTWriter.cpp @@ -1649,7 +1649,7 @@ void ASTWriter::WriteControlBlock(Preprocessor &PP, StringRef isysroot) { // Codegen options. Record.clear(); - const CodeGenOptions &CGOpts = PP.getCodeGenOpts(); + const CodeGenOptions &CGOpts = getCodeGenOpts(); #define CODEGENOPT(Name, Bits, Default) #define COMPATIBLE_VALUE_CODEGENOPT(Name, Bits, Default, Description) \ Record.push_back(static_cast(CGOpts.Name)); @@ -5396,11 +5396,12 @@ void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) { ASTWriter::ASTWriter(llvm::BitstreamWriter &Stream, SmallVectorImpl &Buffer, ModuleCache &ModCache, + const CodeGenOptions &CodeGenOpts, ArrayRef> Extensions, bool IncludeTimestamps, bool BuildingImplicitModule, bool GeneratingReducedBMI) : Stream(Stream), Buffer(Buffer), ModCache(ModCache), - IncludeTimestamps(IncludeTimestamps), + CodeGenOpts(CodeGenOpts), IncludeTimestamps(IncludeTimestamps), BuildingImplicitModule(BuildingImplicitModule), GeneratingReducedBMI(GeneratingReducedBMI) { for (const auto &Ext : Extensions) { diff --git a/clang/lib/Serialization/GeneratePCH.cpp b/clang/lib/Serialization/GeneratePCH.cpp index 77317f0a1db32..f8be0e45078db 100644 --- a/clang/lib/Serialization/GeneratePCH.cpp +++ b/clang/lib/Serialization/GeneratePCH.cpp @@ -25,13 +25,14 @@ using namespace clang; PCHGenerator::PCHGenerator( Preprocessor &PP, ModuleCache &ModCache, StringRef OutputFile, StringRef isysroot, std::shared_ptr Buffer, + const CodeGenOptions &CodeGenOpts, ArrayRef> Extensions, bool AllowASTWithErrors, bool IncludeTimestamps, bool BuildingImplicitModule, bool ShouldCacheASTInMemory, bool GeneratingReducedBMI) : PP(PP), Subject(&PP), OutputFile(OutputFile), isysroot(isysroot.str()), Buffer(std::move(Buffer)), Stream(this->Buffer->Data), - Writer(Stream, this->Buffer->Data, ModCache, Extensions, + Writer(Stream, this->Buffer->Data, ModCache, CodeGenOpts, Extensions, IncludeTimestamps, BuildingImplicitModule, GeneratingReducedBMI), AllowASTWithErrors(AllowASTWithErrors), ShouldCacheASTInMemory(ShouldCacheASTInMemory) { @@ -102,11 +103,12 @@ void PCHGenerator::anchor() {} CXX20ModulesGenerator::CXX20ModulesGenerator(Preprocessor &PP, ModuleCache &ModCache, StringRef OutputFile, + const CodeGenOptions &CodeGenOpts, bool GeneratingReducedBMI, bool AllowASTWithErrors) : PCHGenerator( PP, ModCache, OutputFile, llvm::StringRef(), - std::make_shared(), + std::make_shared(), CodeGenOpts, /*Extensions=*/ArrayRef>(), AllowASTWithErrors, /*IncludeTimestamps=*/false, /*BuildingImplicitModule=*/false, /*ShouldCacheASTInMemory=*/false, diff --git a/clang/unittests/Analysis/MacroExpansionContextTest.cpp b/clang/unittests/Analysis/MacroExpansionContextTest.cpp index 7ed381b399197..9874ea687f3ed 100644 --- a/clang/unittests/Analysis/MacroExpansionContextTest.cpp +++ b/clang/unittests/Analysis/MacroExpansionContextTest.cpp @@ -50,7 +50,6 @@ class MacroExpansionContextTest : public ::testing::Test { DiagnosticsEngine Diags; SourceManager SourceMgr; LangOptions LangOpts; - CodeGenOptions CGOpts; std::shared_ptr TargetOpts; IntrusiveRefCntPtr Target; @@ -63,8 +62,7 @@ class MacroExpansionContextTest : public ::testing::Test { TrivialModuleLoader ModLoader; PreprocessorOptions PPOpts; HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, Target.get()); - Preprocessor PP(PPOpts, Diags, LangOpts, CGOpts, SourceMgr, HeaderInfo, - ModLoader, + Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); PP.Initialize(*Target); diff --git a/clang/unittests/Basic/SourceManagerTest.cpp b/clang/unittests/Basic/SourceManagerTest.cpp index 2565c734ba473..cbe047b5e599a 100644 --- a/clang/unittests/Basic/SourceManagerTest.cpp +++ b/clang/unittests/Basic/SourceManagerTest.cpp @@ -54,7 +54,6 @@ class SourceManagerTest : public ::testing::Test { DiagnosticsEngine Diags; SourceManager SourceMgr; LangOptions LangOpts; - CodeGenOptions CGOpts; std::shared_ptr TargetOpts; IntrusiveRefCntPtr Target; }; @@ -139,8 +138,7 @@ TEST_F(SourceManagerTest, isBeforeInTranslationUnit) { PreprocessorOptions PPOpts; TrivialModuleLoader ModLoader; HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, &*Target); - Preprocessor PP(PPOpts, Diags, LangOpts, CGOpts, SourceMgr, HeaderInfo, - ModLoader, + Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, /*IILookup =*/nullptr, /*OwnsHeaderSearch =*/false); PP.Initialize(*Target); PP.EnterMainSourceFile(); @@ -189,8 +187,7 @@ TEST_F(SourceManagerTest, isBeforeInTranslationUnitWithTokenSplit) { PreprocessorOptions PPOpts; TrivialModuleLoader ModLoader; HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, &*Target); - Preprocessor PP(PPOpts, Diags, LangOpts, CGOpts, SourceMgr, HeaderInfo, - ModLoader, + Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); PP.Initialize(*Target); PP.EnterMainSourceFile(); @@ -464,8 +461,7 @@ TEST_F(SourceManagerTest, ResetsIncludeLocMap) { HeaderSearchOptions HSOpts; PreprocessorOptions PPOpts; HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, &*Target); - Preprocessor PP(PPOpts, Diags, LangOpts, CGOpts, SourceMgr, HeaderInfo, - ModLoader, + Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); PP.Initialize(*Target); PP.EnterMainSourceFile(); @@ -542,8 +538,7 @@ TEST_F(SourceManagerTest, getMacroArgExpandedLocation) { TrivialModuleLoader ModLoader; HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, &*Target); - Preprocessor PP(PPOpts, Diags, LangOpts, CGOpts, SourceMgr, HeaderInfo, - ModLoader, + Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); // Ensure we can get expanded locations in presence of implicit includes. // These are different than normal includes since predefines buffer doesn't @@ -660,8 +655,7 @@ TEST_F(SourceManagerTest, isBeforeInTranslationUnitWithMacroInInclude) { PreprocessorOptions PPOpts; TrivialModuleLoader ModLoader; HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, &*Target); - Preprocessor PP(PPOpts, Diags, LangOpts, CGOpts, SourceMgr, HeaderInfo, - ModLoader, + Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); PP.Initialize(*Target); diff --git a/clang/unittests/Lex/LexerTest.cpp b/clang/unittests/Lex/LexerTest.cpp index f1307d406255c..86df872f6b7df 100644 --- a/clang/unittests/Lex/LexerTest.cpp +++ b/clang/unittests/Lex/LexerTest.cpp @@ -58,8 +58,9 @@ class LexerTest : public ::testing::Test { HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, Target.get()); PreprocessorOptions PPOpts; std::unique_ptr PP = std::make_unique( - PPOpts, Diags, LangOpts, CGOpts, SourceMgr, HeaderInfo, ModLoader, - /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); + PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, + /*IILookup =*/nullptr, + /*OwnsHeaderSearch =*/false); if (!PreDefines.empty()) PP->setPredefines(PreDefines); PP->Initialize(*Target); @@ -106,7 +107,6 @@ class LexerTest : public ::testing::Test { DiagnosticsEngine Diags; SourceManager SourceMgr; LangOptions LangOpts; - CodeGenOptions CGOpts; std::shared_ptr TargetOpts; IntrusiveRefCntPtr Target; std::unique_ptr PP; diff --git a/clang/unittests/Lex/ModuleDeclStateTest.cpp b/clang/unittests/Lex/ModuleDeclStateTest.cpp index c87cdf6ea37c7..6ecba4de3187c 100644 --- a/clang/unittests/Lex/ModuleDeclStateTest.cpp +++ b/clang/unittests/Lex/ModuleDeclStateTest.cpp @@ -77,8 +77,8 @@ class ModuleDeclStateTest : public ::testing::Test { HeaderInfo.emplace(HSOpts, SourceMgr, Diags, LangOpts, Target.get()); - return std::make_unique(PPOpts, Diags, LangOpts, CGOpts, - SourceMgr, *HeaderInfo, ModLoader, + return std::make_unique(PPOpts, Diags, LangOpts, SourceMgr, + *HeaderInfo, ModLoader, /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); } @@ -100,7 +100,6 @@ class ModuleDeclStateTest : public ::testing::Test { std::shared_ptr TargetOpts; IntrusiveRefCntPtr Target; LangOptions LangOpts; - CodeGenOptions CGOpts; TrivialModuleLoader ModLoader; HeaderSearchOptions HSOpts; std::optional HeaderInfo; diff --git a/clang/unittests/Lex/PPCallbacksTest.cpp b/clang/unittests/Lex/PPCallbacksTest.cpp index d35e9b8c48d4e..af86c1888f2c7 100644 --- a/clang/unittests/Lex/PPCallbacksTest.cpp +++ b/clang/unittests/Lex/PPCallbacksTest.cpp @@ -149,7 +149,6 @@ class PPCallbacksTest : public ::testing::Test { DiagnosticsEngine Diags; SourceManager SourceMgr; LangOptions LangOpts; - CodeGenOptions CGOpts; std::shared_ptr TargetOpts; IntrusiveRefCntPtr Target; @@ -201,9 +200,8 @@ class PPCallbacksTest : public ::testing::Test { AddFakeHeader(HeaderInfo, HeaderPath, SystemHeader); PreprocessorOptions PPOpts; - Preprocessor PP(PPOpts, Diags, LangOpts, CGOpts, SourceMgr, HeaderInfo, - ModLoader, /*IILookup=*/nullptr, - /*OwnsHeaderSearch=*/false); + Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, + /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); return InclusionDirectiveCallback(PP)->FilenameRange; } @@ -220,9 +218,8 @@ class PPCallbacksTest : public ::testing::Test { AddFakeHeader(HeaderInfo, HeaderPath, SystemHeader); PreprocessorOptions PPOpts; - Preprocessor PP(PPOpts, Diags, LangOpts, CGOpts, SourceMgr, HeaderInfo, - ModLoader, /*IILookup=*/nullptr, - /*OwnsHeaderSearch=*/false); + Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, + /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); return InclusionDirectiveCallback(PP)->FileType; } @@ -247,8 +244,7 @@ class PPCallbacksTest : public ::testing::Test { llvm::MemoryBuffer::getMemBuffer(SourceText); SourceMgr.setMainFileID(SourceMgr.createFileID(std::move(Buf))); HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, Target.get()); - Preprocessor PP(PPOpts, Diags, LangOpts, CGOpts, SourceMgr, HeaderInfo, - ModLoader, + Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); PP.Initialize(*Target); auto *Callbacks = new CondDirectiveCallbacks; @@ -273,8 +269,7 @@ class PPCallbacksTest : public ::testing::Test { HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, Target.get()); - Preprocessor PP(PPOpts, Diags, LangOpts, CGOpts, SourceMgr, HeaderInfo, - ModLoader, + Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); PP.Initialize(*Target); @@ -304,8 +299,8 @@ class PPCallbacksTest : public ::testing::Test { HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, OpenCLLangOpts, Target.get()); - Preprocessor PP(PPOpts, Diags, OpenCLLangOpts, CGOpts, SourceMgr, - HeaderInfo, ModLoader, /*IILookup=*/nullptr, + Preprocessor PP(PPOpts, Diags, OpenCLLangOpts, SourceMgr, HeaderInfo, + ModLoader, /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); PP.Initialize(*Target); @@ -443,9 +438,8 @@ TEST_F(PPCallbacksTest, FileNotFoundSkipped) { DiagnosticConsumer *DiagConsumer = new DiagnosticConsumer; DiagnosticsEngine FileNotFoundDiags(DiagID, DiagOpts, DiagConsumer); - Preprocessor PP(PPOpts, FileNotFoundDiags, LangOpts, CGOpts, SourceMgr, - HeaderInfo, ModLoader, /*IILookup=*/nullptr, - /*OwnsHeaderSearch=*/false); + Preprocessor PP(PPOpts, FileNotFoundDiags, LangOpts, SourceMgr, HeaderInfo, + ModLoader, /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); PP.Initialize(*Target); class FileNotFoundCallbacks : public PPCallbacks { diff --git a/clang/unittests/Lex/PPConditionalDirectiveRecordTest.cpp b/clang/unittests/Lex/PPConditionalDirectiveRecordTest.cpp index 2ff19281ae262..54c1d020aa0ea 100644 --- a/clang/unittests/Lex/PPConditionalDirectiveRecordTest.cpp +++ b/clang/unittests/Lex/PPConditionalDirectiveRecordTest.cpp @@ -43,7 +43,6 @@ class PPConditionalDirectiveRecordTest : public ::testing::Test { DiagnosticsEngine Diags; SourceManager SourceMgr; LangOptions LangOpts; - CodeGenOptions CGOpts; std::shared_ptr TargetOpts; IntrusiveRefCntPtr Target; }; @@ -76,8 +75,9 @@ TEST_F(PPConditionalDirectiveRecordTest, PPRecAPI) { TrivialModuleLoader ModLoader; HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, Target.get()); PreprocessorOptions PPOpts; - Preprocessor PP(PPOpts, Diags, LangOpts, CGOpts, SourceMgr, HeaderInfo, - ModLoader, /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); + Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, + /*IILookup =*/nullptr, + /*OwnsHeaderSearch =*/false); PP.Initialize(*Target); PPConditionalDirectiveRecord * PPRec = new PPConditionalDirectiveRecord(SourceMgr); diff --git a/clang/unittests/Lex/PPDependencyDirectivesTest.cpp b/clang/unittests/Lex/PPDependencyDirectivesTest.cpp index 2d3ba18928dce..061cb136a552a 100644 --- a/clang/unittests/Lex/PPDependencyDirectivesTest.cpp +++ b/clang/unittests/Lex/PPDependencyDirectivesTest.cpp @@ -45,7 +45,6 @@ class PPDependencyDirectivesTest : public ::testing::Test { DiagnosticsEngine Diags; SourceManager SourceMgr; LangOptions LangOpts; - CodeGenOptions CGOpts; std::shared_ptr TargetOpts; IntrusiveRefCntPtr Target; }; @@ -135,8 +134,9 @@ TEST_F(PPDependencyDirectivesTest, MacroGuard) { HeaderSearchOptions HSOpts; TrivialModuleLoader ModLoader; HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, Target.get()); - Preprocessor PP(PPOpts, Diags, LangOpts, CGOpts, SourceMgr, HeaderInfo, - ModLoader, /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); + Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, + /*IILookup =*/nullptr, + /*OwnsHeaderSearch =*/false); PP.Initialize(*Target); PP.setDependencyDirectivesGetter(GetDependencyDirectives); diff --git a/clang/unittests/Lex/PPMemoryAllocationsTest.cpp b/clang/unittests/Lex/PPMemoryAllocationsTest.cpp index 9fa6fee5124c8..4d83003e28b36 100644 --- a/clang/unittests/Lex/PPMemoryAllocationsTest.cpp +++ b/clang/unittests/Lex/PPMemoryAllocationsTest.cpp @@ -41,7 +41,6 @@ class PPMemoryAllocationsTest : public ::testing::Test { DiagnosticsEngine Diags; SourceManager SourceMgr; LangOptions LangOpts; - CodeGenOptions CGOpts; std::shared_ptr TargetOpts; IntrusiveRefCntPtr Target; }; @@ -71,8 +70,9 @@ TEST_F(PPMemoryAllocationsTest, PPMacroDefinesAllocations) { TrivialModuleLoader ModLoader; HeaderSearch HeaderInfo(HSOpts, SourceMgr, Diags, LangOpts, Target.get()); PreprocessorOptions PPOpts; - Preprocessor PP(PPOpts, Diags, LangOpts, CGOpts, SourceMgr, HeaderInfo, - ModLoader, /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); + Preprocessor PP(PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, + /*IILookup =*/nullptr, + /*OwnsHeaderSearch =*/false); PP.Initialize(*Target); PP.EnterMainSourceFile(); diff --git a/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp b/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp index b0aa73b0a01d4..1e46ee35d5d49 100644 --- a/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp +++ b/clang/unittests/Parse/ParseHLSLRootSignatureTest.cpp @@ -84,8 +84,8 @@ class ParseHLSLRootSignatureTest : public ::testing::Test { HeaderSearch HeaderInfo(SearchOpts, SourceMgr, Diags, LangOpts, Target.get()); auto PP = std::make_unique( - PPOpts, Diags, LangOpts, CGOpts, SourceMgr, HeaderInfo, ModLoader, - /*IILookup=*/nullptr, /*OwnsHeaderSearch=*/false); + PPOpts, Diags, LangOpts, SourceMgr, HeaderInfo, ModLoader, + /*IILookup =*/nullptr, /*OwnsHeaderSearch =*/false); PP->Initialize(*Target); PP->EnterMainSourceFile(); return PP; @@ -99,7 +99,6 @@ class ParseHLSLRootSignatureTest : public ::testing::Test { DiagnosticsEngine Diags; SourceManager SourceMgr; LangOptions LangOpts; - CodeGenOptions CGOpts; PreprocessorOptions PPOpts; std::shared_ptr TargetOpts; IntrusiveRefCntPtr Target;