diff --git a/include/clang/Lex/PPCallbacks.h b/include/clang/Lex/PPCallbacks.h index 056c58aa334882a98b44b53e35038d936118e806..a6b8207e7bcfdb3c27bb71cccd3ceccd902359b8 100644 --- a/include/clang/Lex/PPCallbacks.h +++ b/include/clang/Lex/PPCallbacks.h @@ -27,6 +27,7 @@ namespace clang { class SourceLocation; class Token; class IdentifierInfo; + class MacroDefinition; class MacroDirective; class MacroArgs; @@ -238,9 +239,9 @@ public: /// \brief Called by Preprocessor::HandleMacroExpandedIdentifier when a /// macro invocation is found. - virtual void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD, - SourceRange Range, const MacroArgs *Args) { - } + virtual void MacroExpands(const Token &MacroNameTok, + const MacroDefinition &MD, SourceRange Range, + const MacroArgs *Args) {} /// \brief Hook called whenever a macro definition is seen. virtual void MacroDefined(const Token &MacroNameTok, @@ -251,12 +252,12 @@ public: /// /// MD is released immediately following this callback. virtual void MacroUndefined(const Token &MacroNameTok, - const MacroDirective *MD) { + const MacroDefinition &MD) { } /// \brief Hook called whenever the 'defined' operator is seen. /// \param MD The MacroDirective if the name was a macro, null otherwise. - virtual void Defined(const Token &MacroNameTok, const MacroDirective *MD, + virtual void Defined(const Token &MacroNameTok, const MacroDefinition &MD, SourceRange Range) { } @@ -293,17 +294,17 @@ public: /// \brief Hook called whenever an \#ifdef is seen. /// \param Loc the source location of the directive. /// \param MacroNameTok Information on the token being tested. - /// \param MD The MacroDirective if the name was a macro, null otherwise. + /// \param MD The MacroDefinition if the name was a macro, null otherwise. virtual void Ifdef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDirective *MD) { + const MacroDefinition &MD) { } /// \brief Hook called whenever an \#ifndef is seen. /// \param Loc the source location of the directive. /// \param MacroNameTok Information on the token being tested. - /// \param MD The MacroDirective if the name was a macro, null otherwise. + /// \param MD The MacroDefiniton if the name was a macro, null otherwise. virtual void Ifndef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDirective *MD) { + const MacroDefinition &MD) { } /// \brief Hook called whenever an \#else is seen. @@ -434,7 +435,7 @@ public: Second->PragmaWarningPop(Loc); } - void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD, + void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD, SourceRange Range, const MacroArgs *Args) override { First->MacroExpands(MacroNameTok, MD, Range, Args); Second->MacroExpands(MacroNameTok, MD, Range, Args); @@ -446,12 +447,12 @@ public: } void MacroUndefined(const Token &MacroNameTok, - const MacroDirective *MD) override { + const MacroDefinition &MD) override { First->MacroUndefined(MacroNameTok, MD); Second->MacroUndefined(MacroNameTok, MD); } - void Defined(const Token &MacroNameTok, const MacroDirective *MD, + void Defined(const Token &MacroNameTok, const MacroDefinition &MD, SourceRange Range) override { First->Defined(MacroNameTok, MD, Range); Second->Defined(MacroNameTok, MD, Range); @@ -478,14 +479,14 @@ public: /// \brief Hook called whenever an \#ifdef is seen. void Ifdef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDirective *MD) override { + const MacroDefinition &MD) override { First->Ifdef(Loc, MacroNameTok, MD); Second->Ifdef(Loc, MacroNameTok, MD); } /// \brief Hook called whenever an \#ifndef is seen. void Ifndef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDirective *MD) override { + const MacroDefinition &MD) override { First->Ifndef(Loc, MacroNameTok, MD); Second->Ifndef(Loc, MacroNameTok, MD); } diff --git a/include/clang/Lex/PPConditionalDirectiveRecord.h b/include/clang/Lex/PPConditionalDirectiveRecord.h index 00d2d57973590f9a4ec16b8ee2fc26dba31abce9..8c5227561b838c809229535cc7626018787178f3 100644 --- a/include/clang/Lex/PPConditionalDirectiveRecord.h +++ b/include/clang/Lex/PPConditionalDirectiveRecord.h @@ -91,9 +91,9 @@ private: void Elif(SourceLocation Loc, SourceRange ConditionRange, ConditionValueKind ConditionValue, SourceLocation IfLoc) override; void Ifdef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDirective *MD) override; + const MacroDefinition &MD) override; void Ifndef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDirective *MD) override; + const MacroDefinition &MD) override; void Else(SourceLocation Loc, SourceLocation IfLoc) override; void Endif(SourceLocation Loc, SourceLocation IfLoc) override; }; diff --git a/include/clang/Lex/PreprocessingRecord.h b/include/clang/Lex/PreprocessingRecord.h index 5cf74f643804a978f90a3a056eeedb73f5ffa317..53367ab854f2b77f4f874013666a8c7a62e44b9c 100644 --- a/include/clang/Lex/PreprocessingRecord.h +++ b/include/clang/Lex/PreprocessingRecord.h @@ -488,10 +488,10 @@ namespace clang { } private: - void MacroExpands(const Token &Id, const MacroDirective *MD, + void MacroExpands(const Token &Id, const MacroDefinition &MD, SourceRange Range, const MacroArgs *Args) override; void MacroDefined(const Token &Id, const MacroDirective *MD) override; - void MacroUndefined(const Token &Id, const MacroDirective *MD) override; + void MacroUndefined(const Token &Id, const MacroDefinition &MD) override; void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, CharSourceRange FilenameRange, @@ -499,11 +499,11 @@ namespace clang { StringRef RelativePath, const Module *Imported) override; void Ifdef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDirective *MD) override; + const MacroDefinition &MD) override; void Ifndef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDirective *MD) override; + const MacroDefinition &MD) override; /// \brief Hook called whenever the 'defined' operator is seen. - void Defined(const Token &MacroNameTok, const MacroDirective *MD, + void Defined(const Token &MacroNameTok, const MacroDefinition &MD, SourceRange Range) override; void SourceRangeSkipped(SourceRange Range) override; diff --git a/include/clang/Lex/Preprocessor.h b/include/clang/Lex/Preprocessor.h index 07e1beb677d821669bbbbd1d485fd62251969456..3f668e1e277f4cc087d79564f4d1e797ae67c290 100644 --- a/include/clang/Lex/Preprocessor.h +++ b/include/clang/Lex/Preprocessor.h @@ -357,9 +357,9 @@ class Preprocessor : public RefCountedBase<Preprocessor> { struct MacroExpandsInfo { Token Tok; - MacroDirective *MD; + MacroDefinition MD; SourceRange Range; - MacroExpandsInfo(Token Tok, MacroDirective *MD, SourceRange Range) + MacroExpandsInfo(Token Tok, MacroDefinition MD, SourceRange Range) : Tok(Tok), MD(MD), Range(Range) { } }; SmallVector<MacroExpandsInfo, 2> DelayedMacroExpandsCallbacks; diff --git a/lib/ARCMigrate/ARCMT.cpp b/lib/ARCMigrate/ARCMT.cpp index 0a61cfea6c44beae1789b55354d20ea431746d69..f266eaf83968d09bfaf3312e571387186963687e 100644 --- a/lib/ARCMigrate/ARCMT.cpp +++ b/lib/ARCMigrate/ARCMT.cpp @@ -432,7 +432,7 @@ public: ARCMTMacroTrackerPPCallbacks(std::vector<SourceLocation> &ARCMTMacroLocs) : ARCMTMacroLocs(ARCMTMacroLocs) { } - void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD, + void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD, SourceRange Range, const MacroArgs *Args) override { if (MacroNameTok.getIdentifierInfo()->getName() == getARCMTMacroName()) ARCMTMacroLocs.push_back(MacroNameTok.getLocation()); diff --git a/lib/Frontend/PrintPreprocessedOutput.cpp b/lib/Frontend/PrintPreprocessedOutput.cpp index b5e5d0aa037149f76b3c7c4b6383bdbe20f8319b..8ca36489ee465e730db11a5c4a614c33661b4a67 100644 --- a/lib/Frontend/PrintPreprocessedOutput.cpp +++ b/lib/Frontend/PrintPreprocessedOutput.cpp @@ -170,7 +170,7 @@ public: /// MacroUndefined - This hook is called whenever a macro #undef is seen. void MacroUndefined(const Token &MacroNameTok, - const MacroDirective *MD) override; + const MacroDefinition &MD) override; }; } // end anonymous namespace @@ -361,7 +361,7 @@ void PrintPPOutputPPCallbacks::MacroDefined(const Token &MacroNameTok, } void PrintPPOutputPPCallbacks::MacroUndefined(const Token &MacroNameTok, - const MacroDirective *MD) { + const MacroDefinition &MD) { // Only print out macro definitions in -dD mode. if (!DumpDefines) return; diff --git a/lib/Lex/PPConditionalDirectiveRecord.cpp b/lib/Lex/PPConditionalDirectiveRecord.cpp index 99b87a0a15256d7c41215f0f65b1f96eaf72eb25..12a77849b8b30cb184bb8fb535c2ab9b4748e50c 100644 --- a/lib/Lex/PPConditionalDirectiveRecord.cpp +++ b/lib/Lex/PPConditionalDirectiveRecord.cpp @@ -84,14 +84,14 @@ void PPConditionalDirectiveRecord::If(SourceLocation Loc, void PPConditionalDirectiveRecord::Ifdef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDirective *MD) { + const MacroDefinition &MD) { addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back())); CondDirectiveStack.push_back(Loc); } void PPConditionalDirectiveRecord::Ifndef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDirective *MD) { + const MacroDefinition &MD) { addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back())); CondDirectiveStack.push_back(Loc); } diff --git a/lib/Lex/PPDirectives.cpp b/lib/Lex/PPDirectives.cpp index e1a67f9e9d11eda5f6fd4ace16535acef7bb2e4a..d1c8906276152aee4525644d232fbc06de9da24f 100644 --- a/lib/Lex/PPDirectives.cpp +++ b/lib/Lex/PPDirectives.cpp @@ -2293,17 +2293,15 @@ void Preprocessor::HandleUndefDirective(Token &UndefTok) { // Okay, we have a valid identifier to undef. auto *II = MacroNameTok.getIdentifierInfo(); + auto MD = getMacroDefinition(II); // If the callbacks want to know, tell them about the macro #undef. // Note: no matter if the macro was defined or not. - if (Callbacks) { - // FIXME: Tell callbacks about module macros. - MacroDirective *MD = getLocalMacroDirective(II); + if (Callbacks) Callbacks->MacroUndefined(MacroNameTok, MD); - } // If the macro is not defined, this is a noop undef, just return. - const MacroInfo *MI = getMacroInfo(II); + const MacroInfo *MI = MD.getMacroInfo(); if (!MI) return; @@ -2348,7 +2346,8 @@ void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef, CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef"); IdentifierInfo *MII = MacroNameTok.getIdentifierInfo(); - MacroInfo *MI = getMacroInfo(MII); + auto MD = getMacroDefinition(MII); + MacroInfo *MI = MD.getMacroInfo(); if (CurPPLexer->getConditionalStackDepth() == 0) { // If the start of a top-level #ifdef and if the macro is not defined, @@ -2367,8 +2366,6 @@ void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef, markMacroAsUsed(MI); if (Callbacks) { - // FIXME: Tell callbacks about module macros. - MacroDirective *MD = getLocalMacroDirective(MII); if (isIfndef) Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD); else diff --git a/lib/Lex/PPExpressions.cpp b/lib/Lex/PPExpressions.cpp index 411a5012241d7b25254aa0b05eb974d2097358d2..44513023395d4136838aaebae191bb74a9215f4d 100644 --- a/lib/Lex/PPExpressions.cpp +++ b/lib/Lex/PPExpressions.cpp @@ -142,9 +142,7 @@ static bool EvaluateDefined(PPValue &Result, Token &PeekTok, DefinedTracker &DT, // Invoke the 'defined' callback. if (PPCallbacks *Callbacks = PP.getPPCallbacks()) { - // FIXME: Tell callbacks about module macros. - MacroDirective *MD = Macro.getLocalDirective(); - Callbacks->Defined(macroToken, MD, + Callbacks->Defined(macroToken, Macro, SourceRange(beginLoc, PeekTok.getLocation())); } diff --git a/lib/Lex/PPMacroExpansion.cpp b/lib/Lex/PPMacroExpansion.cpp index edeed42c17efd61f61c8c0fb1c5f38c590d10809..5af0205af3764733b7ce5cac508e0c3f0ecb5958 100644 --- a/lib/Lex/PPMacroExpansion.cpp +++ b/lib/Lex/PPMacroExpansion.cpp @@ -419,10 +419,9 @@ bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier, // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially. if (MI->isBuiltinMacro()) { - // FIXME: Tell callbacks about module macros. - if (Callbacks) Callbacks->MacroExpands(Identifier, M.getLocalDirective(), - Identifier.getLocation(), - /*Args=*/nullptr); + if (Callbacks) + Callbacks->MacroExpands(Identifier, M, Identifier.getLocation(), + /*Args=*/nullptr); ExpandBuiltinMacro(Identifier); return true; } @@ -468,13 +467,10 @@ bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier, // reading the function macro arguments. To ensure, in that case, that // MacroExpands callbacks still happen in source order, queue this // callback to have it happen after the function macro callback. - // FIXME: Tell callbacks about module macros. DelayedMacroExpandsCallbacks.push_back( - MacroExpandsInfo(Identifier, M.getLocalDirective(), ExpansionRange)); + MacroExpandsInfo(Identifier, M, ExpansionRange)); } else { - // FIXME: Tell callbacks about module macros. - Callbacks->MacroExpands(Identifier, M.getLocalDirective(), ExpansionRange, - Args); + Callbacks->MacroExpands(Identifier, M, ExpansionRange, Args); if (!DelayedMacroExpandsCallbacks.empty()) { for (unsigned i=0, e = DelayedMacroExpandsCallbacks.size(); i!=e; ++i) { MacroExpandsInfo &Info = DelayedMacroExpandsCallbacks[i]; diff --git a/lib/Lex/PreprocessingRecord.cpp b/lib/Lex/PreprocessingRecord.cpp index fd4bed17797366c4fec2d9072c81e97184875f06..a423041a2d95641106087bb11edfa5fa19efa129 100644 --- a/lib/Lex/PreprocessingRecord.cpp +++ b/lib/Lex/PreprocessingRecord.cpp @@ -381,27 +381,27 @@ void PreprocessingRecord::addMacroExpansion(const Token &Id, } void PreprocessingRecord::Ifdef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDirective *MD) { + const MacroDefinition &MD) { // This is not actually a macro expansion but record it as a macro reference. if (MD) - addMacroExpansion(MacroNameTok, MD->getMacroInfo(), + addMacroExpansion(MacroNameTok, MD.getMacroInfo(), MacroNameTok.getLocation()); } void PreprocessingRecord::Ifndef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDirective *MD) { + const MacroDefinition &MD) { // This is not actually a macro expansion but record it as a macro reference. if (MD) - addMacroExpansion(MacroNameTok, MD->getMacroInfo(), + addMacroExpansion(MacroNameTok, MD.getMacroInfo(), MacroNameTok.getLocation()); } void PreprocessingRecord::Defined(const Token &MacroNameTok, - const MacroDirective *MD, + const MacroDefinition &MD, SourceRange Range) { // This is not actually a macro expansion but record it as a macro reference. if (MD) - addMacroExpansion(MacroNameTok, MD->getMacroInfo(), + addMacroExpansion(MacroNameTok, MD.getMacroInfo(), MacroNameTok.getLocation()); } @@ -409,10 +409,11 @@ void PreprocessingRecord::SourceRangeSkipped(SourceRange Range) { SkippedRanges.push_back(Range); } -void PreprocessingRecord::MacroExpands(const Token &Id,const MacroDirective *MD, +void PreprocessingRecord::MacroExpands(const Token &Id, + const MacroDefinition &MD, SourceRange Range, const MacroArgs *Args) { - addMacroExpansion(Id, MD->getMacroInfo(), Range); + addMacroExpansion(Id, MD.getMacroInfo(), Range); } void PreprocessingRecord::MacroDefined(const Token &Id, @@ -426,10 +427,8 @@ void PreprocessingRecord::MacroDefined(const Token &Id, } void PreprocessingRecord::MacroUndefined(const Token &Id, - const MacroDirective *MD) { - // Note: MI may be null (when #undef'ining an undefined macro). - if (MD) - MacroDefinitions.erase(MD->getMacroInfo()); + const MacroDefinition &MD) { + MD.forAllDefinitions([&](MacroInfo *MI) { MacroDefinitions.erase(MI); }); } void PreprocessingRecord::InclusionDirective( diff --git a/test/Modules/macros.c b/test/Modules/macros.c index 076166966b73a2eda6fce26f748aaeb078aa75a2..7b7b52aa0173c9bc0454722cd232ec658de25f39 100644 --- a/test/Modules/macros.c +++ b/test/Modules/macros.c @@ -1,5 +1,6 @@ // RUN: rm -rf %t // RUN: %clang_cc1 -fmodules -x objective-c -verify -fmodules-cache-path=%t -I %S/Inputs %s +// RUN: %clang_cc1 -fmodules -x objective-c -verify -fmodules-cache-path=%t -I %S/Inputs %s -detailed-preprocessing-record // RUN: %clang_cc1 -fmodules -DLOCAL_VISIBILITY -fmodules-local-submodule-visibility -x objective-c -verify -fmodules-cache-path=%t -I %S/Inputs %s // RUN: not %clang_cc1 -E -fmodules -x objective-c -fmodules-cache-path=%t -I %S/Inputs %s | FileCheck -check-prefix CHECK-PREPROCESSED %s // FIXME: When we have a syntax for modules in C, use that. diff --git a/tools/libclang/Indexing.cpp b/tools/libclang/Indexing.cpp index 20df33e8420c70755e0c44c13359e03a89ab755f..0ede684a18e40c66827d7cf3fe8292cc9354fab8 100644 --- a/tools/libclang/Indexing.cpp +++ b/tools/libclang/Indexing.cpp @@ -284,10 +284,10 @@ public: /// MacroUndefined - This hook is called whenever a macro #undef is seen. /// MI is released immediately following this callback. void MacroUndefined(const Token &MacroNameTok, - const MacroDirective *MD) override {} + const MacroDefinition &MD) override {} /// MacroExpands - This is called by when a macro invocation is found. - void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD, + void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD, SourceRange Range, const MacroArgs *Args) override {} /// SourceRangeSkipped - This hook is called when a source range is skipped. diff --git a/unittests/Basic/SourceManagerTest.cpp b/unittests/Basic/SourceManagerTest.cpp index a151bd5a1f6591763612fb55658ffd9812f4b4ed..494c27a2f1cdfb42eeebaf095208ff4e2f97ab8f 100644 --- a/unittests/Basic/SourceManagerTest.cpp +++ b/unittests/Basic/SourceManagerTest.cpp @@ -263,7 +263,7 @@ public: MacroNameTok.getIdentifierInfo()->getName(), true)); } - void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD, + void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD, SourceRange Range, const MacroArgs *Args) override { Macros.push_back(MacroAction(MacroNameTok.getLocation(), MacroNameTok.getIdentifierInfo()->getName(),