diff --git a/clang/include/clang/Lex/MacroInfo.h b/clang/include/clang/Lex/MacroInfo.h index 19a706216d509..cd47a7048f2c6 100644 --- a/clang/include/clang/Lex/MacroInfo.h +++ b/clang/include/clang/Lex/MacroInfo.h @@ -583,6 +583,11 @@ class ModuleMacro : public llvm::FoldingSetNode { unsigned getNumOverridingMacros() const { return NumOverriddenBy; } }; +struct ModuleMacroInfo { + ArrayRef ActiveModuleMacros = {}; + bool IsAmbiguous = false; +}; + /// A description of the current definition of a macro. /// /// The definition of a macro comprises a set of (at least one) defining @@ -593,9 +598,9 @@ class MacroDefinition { public: MacroDefinition() = default; - MacroDefinition(DefMacroDirective *MD, ArrayRef MMs, - bool IsAmbiguous) - : LatestLocalAndAmbiguous(MD, IsAmbiguous), ModuleMacros(MMs) {} + MacroDefinition(DefMacroDirective *MD, ModuleMacroInfo Info) + : LatestLocalAndAmbiguous(MD, Info.IsAmbiguous), + ModuleMacros(Info.ActiveModuleMacros) {} /// Determine whether there is a definition of this macro. explicit operator bool() const { diff --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h index c0c94b7ea4101..eb9e46e7cee89 100644 --- a/clang/include/clang/Lex/Preprocessor.h +++ b/clang/include/clang/Lex/Preprocessor.h @@ -874,7 +874,7 @@ class Preprocessor { SmallVector DelayedMacroExpandsCallbacks; /// Information about a name that has been used to define a module macro. - struct ModuleMacroInfo { + struct FullModuleMacroInfo { /// The most recent macro directive for this identifier. MacroDirective *MD; @@ -891,15 +891,15 @@ class Preprocessor { /// The module macros that are overridden by this macro. llvm::TinyPtrVector OverriddenMacros; - ModuleMacroInfo(MacroDirective *MD) : MD(MD) {} + FullModuleMacroInfo(MacroDirective *MD) : MD(MD) {} }; /// The state of a macro for an identifier. class MacroState { - mutable llvm::PointerUnion State; + mutable llvm::PointerUnion State; - ModuleMacroInfo *getModuleInfo(Preprocessor &PP, - const IdentifierInfo *II) const { + FullModuleMacroInfo *getFullModuleInfo(Preprocessor &PP, + const IdentifierInfo *II) const { if (II->isOutOfDate()) PP.updateOutOfDateIdentifier(*II); // FIXME: Find a spare bit on IdentifierInfo and store a @@ -910,10 +910,10 @@ class Preprocessor { !PP.CurSubmoduleState->VisibleModules.getGeneration()) return nullptr; - auto *Info = dyn_cast_if_present(State); + auto *Info = dyn_cast_if_present(State); if (!Info) { Info = new (PP.getPreprocessorAllocator()) - ModuleMacroInfo(cast(State)); + FullModuleMacroInfo(cast(State)); State = Info; } @@ -939,32 +939,27 @@ class Preprocessor { } ~MacroState() { - if (auto *Info = dyn_cast_if_present(State)) - Info->~ModuleMacroInfo(); + if (auto *Info = dyn_cast_if_present(State)) + Info->~FullModuleMacroInfo(); } MacroDirective *getLatest() const { - if (auto *Info = dyn_cast_if_present(State)) + if (auto *Info = dyn_cast_if_present(State)) return Info->MD; return cast(State); } void setLatest(MacroDirective *MD) { - if (auto *Info = dyn_cast_if_present(State)) + if (auto *Info = dyn_cast_if_present(State)) Info->MD = MD; else State = MD; } - bool isAmbiguous(Preprocessor &PP, const IdentifierInfo *II) const { - auto *Info = getModuleInfo(PP, II); - return Info ? Info->IsAmbiguous : false; - } - - ArrayRef - getActiveModuleMacros(Preprocessor &PP, const IdentifierInfo *II) const { - if (auto *Info = getModuleInfo(PP, II)) - return Info->ActiveModuleMacros; + ModuleMacroInfo getModuleInfo(Preprocessor &PP, + const IdentifierInfo *II) const { + if (auto *Info = getFullModuleInfo(PP, II)) + return ModuleMacroInfo{Info->ActiveModuleMacros, Info->IsAmbiguous}; return {}; } @@ -977,7 +972,7 @@ class Preprocessor { } void overrideActiveModuleMacros(Preprocessor &PP, IdentifierInfo *II) { - if (auto *Info = getModuleInfo(PP, II)) { + if (auto *Info = getFullModuleInfo(PP, II)) { Info->OverriddenMacros.insert(Info->OverriddenMacros.end(), Info->ActiveModuleMacros.begin(), Info->ActiveModuleMacros.end()); @@ -987,19 +982,19 @@ class Preprocessor { } ArrayRef getOverriddenMacros() const { - if (auto *Info = dyn_cast_if_present(State)) + if (auto *Info = dyn_cast_if_present(State)) return Info->OverriddenMacros; return {}; } void setOverriddenMacros(Preprocessor &PP, ArrayRef Overrides) { - auto *Info = dyn_cast_if_present(State); + auto *Info = dyn_cast_if_present(State); if (!Info) { if (Overrides.empty()) return; Info = new (PP.getPreprocessorAllocator()) - ModuleMacroInfo(cast(State)); + FullModuleMacroInfo(cast(State)); State = Info; } Info->OverriddenMacros.clear(); @@ -1423,8 +1418,7 @@ class Preprocessor { while (isa_and_nonnull(MD)) MD = MD->getPrevious(); return MacroDefinition(dyn_cast_or_null(MD), - S.getActiveModuleMacros(*this, II), - S.isAmbiguous(*this, II)); + S.getModuleInfo(*this, II)); } MacroDefinition getMacroDefinitionAtLoc(const IdentifierInfo *II, @@ -1437,9 +1431,7 @@ class Preprocessor { if (auto *MD = S.getLatest()) DI = MD->findDirectiveAtLoc(Loc, getSourceManager()); // FIXME: Compute the set of active module macros at the specified location. - return MacroDefinition(DI.getDirective(), - S.getActiveModuleMacros(*this, II), - S.isAmbiguous(*this, II)); + return MacroDefinition(DI.getDirective(), S.getModuleInfo(*this, II)); } /// Given an identifier, return its latest non-imported MacroDirective @@ -2619,7 +2611,8 @@ class Preprocessor { /// Update the set of active module macros and ambiguity flag for a module /// macro name. - void updateModuleMacroInfo(const IdentifierInfo *II, ModuleMacroInfo &Info); + void updateModuleMacroInfo(const IdentifierInfo *II, + FullModuleMacroInfo &Info); DefMacroDirective *AllocateDefMacroDirective(MacroInfo *MI, SourceLocation Loc); diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp index 20b8fe585a007..5fd3512d2f45c 100644 --- a/clang/lib/Lex/PPMacroExpansion.cpp +++ b/clang/lib/Lex/PPMacroExpansion.cpp @@ -176,7 +176,7 @@ ModuleMacro *Preprocessor::getModuleMacro(Module *Mod, } void Preprocessor::updateModuleMacroInfo(const IdentifierInfo *II, - ModuleMacroInfo &Info) { + FullModuleMacroInfo &Info) { assert(Info.ActiveModuleMacrosGeneration != CurSubmoduleState->VisibleModules.getGeneration() && "don't need to update this macro name info"); @@ -264,7 +264,9 @@ void Preprocessor::dumpMacroInfo(const IdentifierInfo *II) { State = &Pos->second; llvm::errs() << "MacroState " << State << " " << II->getNameStart(); - if (State && State->isAmbiguous(*this, II)) + const auto ModuleInfo = + State ? State->getModuleInfo(*this, II) : ModuleMacroInfo{}; + if (ModuleInfo.IsAmbiguous) llvm::errs() << " ambiguous"; if (State && !State->getOverriddenMacros().empty()) { llvm::errs() << " overrides"; @@ -281,10 +283,8 @@ void Preprocessor::dumpMacroInfo(const IdentifierInfo *II) { } // Dump module macros. - llvm::DenseSet Active; - for (auto *MM : State ? State->getActiveModuleMacros(*this, II) - : ArrayRef()) - Active.insert(MM); + llvm::DenseSet Active(llvm::from_range, + ModuleInfo.ActiveModuleMacros); llvm::DenseSet Visited; llvm::SmallVector Worklist(Leaf); while (!Worklist.empty()) {