From 222a7bbf0ded69b369d05dbeeceef0d86cbb0f37 Mon Sep 17 00:00:00 2001 From: Argyrios Kyrtzidis Date: Sat, 8 Dec 2012 02:21:11 +0000 Subject: [PATCH] [Preprocessor] Enhance Ifdef/Ifndef/Defined preprocessor callbacks to also pass a MacroInfo object if the identifier was a macro name. llvm-svn: 169665 --- clang/include/clang/Lex/PPCallbacks.h | 31 +++++++++++++--------- .../clang/Lex/PPConditionalDirectiveRecord.h | 6 +++-- clang/lib/Lex/PPConditionalDirectiveRecord.cpp | 6 +++-- clang/lib/Lex/PPDirectives.cpp | 4 +-- clang/lib/Lex/PPExpressions.cpp | 12 ++++++--- 5 files changed, 38 insertions(+), 21 deletions(-) diff --git a/clang/include/clang/Lex/PPCallbacks.h b/clang/include/clang/Lex/PPCallbacks.h index 9d14447..a32e163 100644 --- a/clang/include/clang/Lex/PPCallbacks.h +++ b/clang/include/clang/Lex/PPCallbacks.h @@ -199,7 +199,8 @@ public: } /// \brief Hook called whenever the 'defined' operator is seen. - virtual void Defined(const Token &MacroNameTok) { + /// \param MI The MacroInfo if the name was a macro, null otherwise. + virtual void Defined(const Token &MacroNameTok, const MacroInfo *MI) { } /// \brief Hook called when a source range is skipped. @@ -228,13 +229,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. - virtual void Ifdef(SourceLocation Loc, const Token &MacroNameTok) { + /// \param MI The MacroInfo if the name was a macro, null otherwise. + virtual void Ifdef(SourceLocation Loc, const Token &MacroNameTok, + const MacroInfo *MI) { } /// \brief Hook called whenever an \#ifndef is seen. /// \param Loc the source location of the directive. /// \param MacroNameTok Information on the token being tested. - virtual void Ifndef(SourceLocation Loc, const Token &MacroNameTok) { + /// \param MI The MacroInfo if the name was a macro, null otherwise. + virtual void Ifndef(SourceLocation Loc, const Token &MacroNameTok, + const MacroInfo *MI) { } /// \brief Hook called whenever an \#else is seen. @@ -362,9 +367,9 @@ public: Second->MacroUndefined(MacroNameTok, MI); } - virtual void Defined(const Token &MacroNameTok) { - First->Defined(MacroNameTok); - Second->Defined(MacroNameTok); + virtual void Defined(const Token &MacroNameTok, const MacroInfo *MI) { + First->Defined(MacroNameTok, MI); + Second->Defined(MacroNameTok, MI); } virtual void SourceRangeSkipped(SourceRange Range) { @@ -386,15 +391,17 @@ public: } /// \brief Hook called whenever an \#ifdef is seen. - virtual void Ifdef(SourceLocation Loc, const Token &MacroNameTok) { - First->Ifdef(Loc, MacroNameTok); - Second->Ifdef(Loc, MacroNameTok); + virtual void Ifdef(SourceLocation Loc, const Token &MacroNameTok, + const MacroInfo *MI) { + First->Ifdef(Loc, MacroNameTok, MI); + Second->Ifdef(Loc, MacroNameTok, MI); } /// \brief Hook called whenever an \#ifndef is seen. - virtual void Ifndef(SourceLocation Loc, const Token &MacroNameTok) { - First->Ifndef(Loc, MacroNameTok); - Second->Ifndef(Loc, MacroNameTok); + virtual void Ifndef(SourceLocation Loc, const Token &MacroNameTok, + const MacroInfo *MI) { + First->Ifndef(Loc, MacroNameTok, MI); + Second->Ifndef(Loc, MacroNameTok, MI); } /// \brief Hook called whenever an \#else is seen. diff --git a/clang/include/clang/Lex/PPConditionalDirectiveRecord.h b/clang/include/clang/Lex/PPConditionalDirectiveRecord.h index d552c27..29d9289 100644 --- a/clang/include/clang/Lex/PPConditionalDirectiveRecord.h +++ b/clang/include/clang/Lex/PPConditionalDirectiveRecord.h @@ -89,8 +89,10 @@ private: virtual void If(SourceLocation Loc, SourceRange ConditionRange); virtual void Elif(SourceLocation Loc, SourceRange ConditionRange, SourceLocation IfLoc); - virtual void Ifdef(SourceLocation Loc, const Token &MacroNameTok); - virtual void Ifndef(SourceLocation Loc, const Token &MacroNameTok); + virtual void Ifdef(SourceLocation Loc, const Token &MacroNameTok, + const MacroInfo *MI); + virtual void Ifndef(SourceLocation Loc, const Token &MacroNameTok, + const MacroInfo *MI); virtual void Else(SourceLocation Loc, SourceLocation IfLoc); virtual void Endif(SourceLocation Loc, SourceLocation IfLoc); }; diff --git a/clang/lib/Lex/PPConditionalDirectiveRecord.cpp b/clang/lib/Lex/PPConditionalDirectiveRecord.cpp index edcde87..063c556 100644 --- a/clang/lib/Lex/PPConditionalDirectiveRecord.cpp +++ b/clang/lib/Lex/PPConditionalDirectiveRecord.cpp @@ -82,13 +82,15 @@ void PPConditionalDirectiveRecord::If(SourceLocation Loc, } void PPConditionalDirectiveRecord::Ifdef(SourceLocation Loc, - const Token &MacroNameTok) { + const Token &MacroNameTok, + const MacroInfo *MI) { addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back())); CondDirectiveStack.push_back(Loc); } void PPConditionalDirectiveRecord::Ifndef(SourceLocation Loc, - const Token &MacroNameTok) { + const Token &MacroNameTok, + const MacroInfo *MI) { addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back())); CondDirectiveStack.push_back(Loc); } diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp index 74a787d..7b5df9c 100644 --- a/clang/lib/Lex/PPDirectives.cpp +++ b/clang/lib/Lex/PPDirectives.cpp @@ -2047,9 +2047,9 @@ void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef, if (Callbacks) { if (isIfndef) - Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok); + Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MI); else - Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok); + Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MI); } // Should we include the stuff contained by this directive? diff --git a/clang/lib/Lex/PPExpressions.cpp b/clang/lib/Lex/PPExpressions.cpp index 868f2dd..9b33c91 100644 --- a/clang/lib/Lex/PPExpressions.cpp +++ b/clang/lib/Lex/PPExpressions.cpp @@ -111,15 +111,21 @@ static bool EvaluateDefined(PPValue &Result, Token &PeekTok, DefinedTracker &DT, Result.Val = II->hasMacroDefinition(); Result.Val.setIsUnsigned(false); // Result is signed intmax_t. + MacroInfo *Macro = 0; // If there is a macro, mark it used. if (Result.Val != 0 && ValueLive) { - MacroInfo *Macro = PP.getMacroInfo(II); + Macro = PP.getMacroInfo(II); PP.markMacroAsUsed(Macro); } // Invoke the 'defined' callback. - if (PPCallbacks *Callbacks = PP.getPPCallbacks()) - Callbacks->Defined(PeekTok); + if (PPCallbacks *Callbacks = PP.getPPCallbacks()) { + MacroInfo *MI = Macro; + // Pass the MacroInfo for the macro name even if the value is dead. + if (!MI && Result.Val != 0) + MI = PP.getMacroInfo(II); + Callbacks->Defined(PeekTok, MI); + } // If we are in parens, ensure we have a trailing ). if (LParenLoc.isValid()) { -- 2.7.4