[preprocessor] Use MacroDirective in the preprocessor callbacks to make available the

full information about the macro (e.g if it was imported and where).

llvm-svn: 175978
This commit is contained in:
Argyrios Kyrtzidis 2013-02-24 00:05:14 +00:00
parent ecd77a98f0
commit fead64be9b
15 changed files with 113 additions and 98 deletions

View File

@ -26,7 +26,7 @@ namespace clang {
class SourceLocation;
class Token;
class IdentifierInfo;
class MacroInfo;
class MacroDirective;
/// \brief This interface provides a way to observe the actions of the
/// preprocessor as it does its thing.
@ -184,23 +184,25 @@ public:
/// \brief Called by Preprocessor::HandleMacroExpandedIdentifier when a
/// macro invocation is found.
virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo* MI,
virtual void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD,
SourceRange Range) {
}
/// \brief Hook called whenever a macro definition is seen.
virtual void MacroDefined(const Token &MacroNameTok, const MacroInfo *MI) {
virtual void MacroDefined(const Token &MacroNameTok,
const MacroDirective *MD) {
}
/// \brief Hook called whenever a macro \#undef is seen.
///
/// MI is released immediately following this callback.
virtual void MacroUndefined(const Token &MacroNameTok, const MacroInfo *MI) {
/// MD is released immediately following this callback.
virtual void MacroUndefined(const Token &MacroNameTok,
const MacroDirective *MD) {
}
/// \brief Hook called whenever the 'defined' operator is seen.
/// \param MI The MacroInfo if the name was a macro, null otherwise.
virtual void Defined(const Token &MacroNameTok, const MacroInfo *MI) {
/// \param MD The MacroDirective if the name was a macro, null otherwise.
virtual void Defined(const Token &MacroNameTok, const MacroDirective *MD) {
}
/// \brief Hook called when a source range is skipped.
@ -229,17 +231,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 MI The MacroInfo if the name was a macro, null otherwise.
/// \param MD The MacroDirective if the name was a macro, null otherwise.
virtual void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
const MacroInfo *MI) {
const MacroDirective *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 MI The MacroInfo if the name was a macro, null otherwise.
/// \param MD The MacroDirective if the name was a macro, null otherwise.
virtual void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
const MacroInfo *MI) {
const MacroDirective *MD) {
}
/// \brief Hook called whenever an \#else is seen.
@ -351,25 +353,26 @@ public:
Second->PragmaDiagnostic(Loc, Namespace, mapping, Str);
}
virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo* MI,
virtual void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD,
SourceRange Range) {
First->MacroExpands(MacroNameTok, MI, Range);
Second->MacroExpands(MacroNameTok, MI, Range);
First->MacroExpands(MacroNameTok, MD, Range);
Second->MacroExpands(MacroNameTok, MD, Range);
}
virtual void MacroDefined(const Token &MacroNameTok, const MacroInfo *MI) {
First->MacroDefined(MacroNameTok, MI);
Second->MacroDefined(MacroNameTok, MI);
virtual void MacroDefined(const Token &MacroNameTok, const MacroDirective *MD) {
First->MacroDefined(MacroNameTok, MD);
Second->MacroDefined(MacroNameTok, MD);
}
virtual void MacroUndefined(const Token &MacroNameTok, const MacroInfo *MI) {
First->MacroUndefined(MacroNameTok, MI);
Second->MacroUndefined(MacroNameTok, MI);
virtual void MacroUndefined(const Token &MacroNameTok,
const MacroDirective *MD) {
First->MacroUndefined(MacroNameTok, MD);
Second->MacroUndefined(MacroNameTok, MD);
}
virtual void Defined(const Token &MacroNameTok, const MacroInfo *MI) {
First->Defined(MacroNameTok, MI);
Second->Defined(MacroNameTok, MI);
virtual void Defined(const Token &MacroNameTok, const MacroDirective *MD) {
First->Defined(MacroNameTok, MD);
Second->Defined(MacroNameTok, MD);
}
virtual void SourceRangeSkipped(SourceRange Range) {
@ -392,16 +395,16 @@ public:
/// \brief Hook called whenever an \#ifdef is seen.
virtual void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
const MacroInfo *MI) {
First->Ifdef(Loc, MacroNameTok, MI);
Second->Ifdef(Loc, MacroNameTok, MI);
const MacroDirective *MD) {
First->Ifdef(Loc, MacroNameTok, MD);
Second->Ifdef(Loc, MacroNameTok, MD);
}
/// \brief Hook called whenever an \#ifndef is seen.
virtual void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
const MacroInfo *MI) {
First->Ifndef(Loc, MacroNameTok, MI);
Second->Ifndef(Loc, MacroNameTok, MI);
const MacroDirective *MD) {
First->Ifndef(Loc, MacroNameTok, MD);
Second->Ifndef(Loc, MacroNameTok, MD);
}
/// \brief Hook called whenever an \#else is seen.

View File

@ -90,9 +90,9 @@ private:
virtual void Elif(SourceLocation Loc, SourceRange ConditionRange,
SourceLocation IfLoc);
virtual void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
const MacroInfo *MI);
const MacroDirective *MD);
virtual void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
const MacroInfo *MI);
const MacroDirective *MD);
virtual void Else(SourceLocation Loc, SourceLocation IfLoc);
virtual void Endif(SourceLocation Loc, SourceLocation IfLoc);
};

View File

@ -26,6 +26,7 @@
namespace clang {
class IdentifierInfo;
class MacroInfo;
class PreprocessingRecord;
}
@ -557,10 +558,10 @@ namespace clang {
MacroDefinition *findMacroDefinition(const MacroInfo *MI);
private:
virtual void MacroExpands(const Token &Id, const MacroInfo* MI,
virtual void MacroExpands(const Token &Id, const MacroDirective *MD,
SourceRange Range);
virtual void MacroDefined(const Token &Id, const MacroInfo *MI);
virtual void MacroUndefined(const Token &Id, const MacroInfo *MI);
virtual void MacroDefined(const Token &Id, const MacroDirective *MD);
virtual void MacroUndefined(const Token &Id, const MacroDirective *MD);
virtual void InclusionDirective(SourceLocation HashLoc,
const Token &IncludeTok,
StringRef FileName,
@ -571,11 +572,11 @@ namespace clang {
StringRef RelativePath,
const Module *Imported);
virtual void Ifdef(SourceLocation Loc, const Token &MacroNameTok,
const MacroInfo *MI);
const MacroDirective *MD);
virtual void Ifndef(SourceLocation Loc, const Token &MacroNameTok,
const MacroInfo *MI);
const MacroDirective *MD);
/// \brief Hook called whenever the 'defined' operator is seen.
virtual void Defined(const Token &MacroNameTok, const MacroInfo *MI);
virtual void Defined(const Token &MacroNameTok, const MacroDirective *MD);
void addMacroExpansion(const Token &Id, const MacroInfo *MI,
SourceRange Range);

View File

@ -303,10 +303,10 @@ class Preprocessor : public RefCountedBase<Preprocessor> {
struct MacroExpandsInfo {
Token Tok;
MacroInfo *MI;
MacroDirective *MD;
SourceRange Range;
MacroExpandsInfo(Token Tok, MacroInfo *MI, SourceRange Range)
: Tok(Tok), MI(MI), Range(Range) { }
MacroExpandsInfo(Token Tok, MacroDirective *MD, SourceRange Range)
: Tok(Tok), MD(MD), Range(Range) { }
};
SmallVector<MacroExpandsInfo, 2> DelayedMacroExpandsCallbacks;
@ -560,10 +560,10 @@ public:
MacroDirective *getMacroDirectiveHistory(const IdentifierInfo *II) const;
/// \brief Specify a macro for this identifier.
void setMacroDirective(IdentifierInfo *II, MacroInfo *MI,
SourceLocation Loc, bool isImported);
void setMacroDirective(IdentifierInfo *II, MacroInfo *MI) {
setMacroDirective(II, MI, MI->getDefinitionLoc(), false);
MacroDirective *setMacroDirective(IdentifierInfo *II, MacroInfo *MI,
SourceLocation Loc, bool isImported);
MacroDirective *setMacroDirective(IdentifierInfo *II, MacroInfo *MI) {
return setMacroDirective(II, MI, MI->getDefinitionLoc(), false);
}
/// \brief Add a MacroInfo that was loaded from an AST file.
void addLoadedMacroInfo(IdentifierInfo *II, MacroDirective *MD,
@ -1333,7 +1333,7 @@ private:
/// HandleMacroExpandedIdentifier - If an identifier token is read that is to
/// be expanded as a macro, handle it and return the next token as 'Tok'. If
/// the macro should not be expanded return true, otherwise return false.
bool HandleMacroExpandedIdentifier(Token &Tok, MacroInfo *MI);
bool HandleMacroExpandedIdentifier(Token &Tok, MacroDirective *MD);
/// \brief Cache macro expanded tokens for TokenLexers.
//

View File

@ -481,7 +481,7 @@ public:
ARCMTMacroTrackerPPCallbacks(std::vector<SourceLocation> &ARCMTMacroLocs)
: ARCMTMacroLocs(ARCMTMacroLocs) { }
virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo *MI,
virtual void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD,
SourceRange Range) {
if (MacroNameTok.getIdentifierInfo()->getName() == getARCMTMacroName())
ARCMTMacroLocs.push_back(MacroNameTok.getLocation());

View File

@ -847,7 +847,8 @@ class MacroDefinitionTrackerPPCallbacks : public PPCallbacks {
public:
explicit MacroDefinitionTrackerPPCallbacks(unsigned &Hash) : Hash(Hash) { }
virtual void MacroDefined(const Token &MacroNameTok, const MacroInfo *MI) {
virtual void MacroDefined(const Token &MacroNameTok,
const MacroDirective *MD) {
Hash = llvm::HashString(MacroNameTok.getIdentifierInfo()->getName(), Hash);
}
};

View File

@ -160,10 +160,10 @@ public:
void HandleNewlinesInToken(const char *TokStr, unsigned Len);
/// MacroDefined - This hook is called whenever a macro definition is seen.
void MacroDefined(const Token &MacroNameTok, const MacroInfo *MI);
void MacroDefined(const Token &MacroNameTok, const MacroDirective *MD);
/// MacroUndefined - This hook is called whenever a macro #undef is seen.
void MacroUndefined(const Token &MacroNameTok, const MacroInfo *MI);
void MacroUndefined(const Token &MacroNameTok, const MacroDirective *MD);
};
} // end anonymous namespace
@ -317,7 +317,8 @@ void PrintPPOutputPPCallbacks::Ident(SourceLocation Loc, const std::string &S) {
/// MacroDefined - This hook is called whenever a macro definition is seen.
void PrintPPOutputPPCallbacks::MacroDefined(const Token &MacroNameTok,
const MacroInfo *MI) {
const MacroDirective *MD) {
const MacroInfo *MI = MD->getInfo();
// Only print out macro definitions in -dD mode.
if (!DumpDefines ||
// Ignore __FILE__ etc.
@ -329,7 +330,7 @@ void PrintPPOutputPPCallbacks::MacroDefined(const Token &MacroNameTok,
}
void PrintPPOutputPPCallbacks::MacroUndefined(const Token &MacroNameTok,
const MacroInfo *MI) {
const MacroDirective *MD) {
// Only print out macro definitions in -dD mode.
if (!DumpDefines) return;

View File

@ -83,14 +83,14 @@ void PPConditionalDirectiveRecord::If(SourceLocation Loc,
void PPConditionalDirectiveRecord::Ifdef(SourceLocation Loc,
const Token &MacroNameTok,
const MacroInfo *MI) {
const MacroDirective *MD) {
addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
CondDirectiveStack.push_back(Loc);
}
void PPConditionalDirectiveRecord::Ifndef(SourceLocation Loc,
const Token &MacroNameTok,
const MacroInfo *MI) {
const MacroDirective *MD) {
addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back()));
CondDirectiveStack.push_back(Loc);
}

View File

@ -1938,7 +1938,7 @@ void Preprocessor::HandleDefineDirective(Token &DefineTok) {
WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc());
}
setMacroDirective(MacroNameTok.getIdentifierInfo(), MI);
MacroDirective *MD = setMacroDirective(MacroNameTok.getIdentifierInfo(), MI);
assert(!MI->isUsed());
// If we need warning for not using the macro, add its location in the
@ -1952,7 +1952,7 @@ void Preprocessor::HandleDefineDirective(Token &DefineTok) {
// If the callbacks want to know, tell them about the macro definition.
if (Callbacks)
Callbacks->MacroDefined(MacroNameTok, MI);
Callbacks->MacroDefined(MacroNameTok, MD);
}
/// HandleUndefDirective - Implements \#undef.
@ -1977,7 +1977,7 @@ void Preprocessor::HandleUndefDirective(Token &UndefTok) {
// If the callbacks want to know, tell them about the macro #undef.
// Note: no matter if the macro was defined or not.
if (Callbacks)
Callbacks->MacroUndefined(MacroNameTok, MI);
Callbacks->MacroUndefined(MacroNameTok, MD);
// If the macro is not defined, this is a noop undef, just return.
if (MI == 0) return;
@ -2035,7 +2035,8 @@ void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
MacroInfo *MI = getMacroInfo(MII);
MacroDirective *MD = getMacroDirective(MII);
MacroInfo *MI = MD ? MD->getInfo() : 0;
if (CurPPLexer->getConditionalStackDepth() == 0) {
// If the start of a top-level #ifdef and if the macro is not defined,
@ -2055,9 +2056,9 @@ void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef,
if (Callbacks) {
if (isIfndef)
Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MI);
Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD);
else
Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MI);
Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MD);
}
// Should we include the stuff contained by this directive?

View File

@ -111,20 +111,20 @@ 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;
MacroDirective *Macro = 0;
// If there is a macro, mark it used.
if (Result.Val != 0 && ValueLive) {
Macro = PP.getMacroInfo(II);
PP.markMacroAsUsed(Macro);
Macro = PP.getMacroDirective(II);
PP.markMacroAsUsed(Macro->getInfo());
}
// Invoke the 'defined' callback.
if (PPCallbacks *Callbacks = PP.getPPCallbacks()) {
MacroInfo *MI = Macro;
MacroDirective *MD = 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 (!MD && Result.Val != 0)
MD = PP.getMacroDirective(II);
Callbacks->Defined(PeekTok, MD);
}
// If we are in parens, ensure we have a trailing ).

View File

@ -41,10 +41,10 @@ Preprocessor::getMacroDirectiveHistory(const IdentifierInfo *II) const {
return Pos->second;
}
/// setMacroInfo - Specify a macro for this identifier.
///
void Preprocessor::setMacroDirective(IdentifierInfo *II, MacroInfo *MI,
SourceLocation Loc, bool isImported) {
/// \brief Specify a macro for this identifier.
MacroDirective *
Preprocessor::setMacroDirective(IdentifierInfo *II, MacroInfo *MI,
SourceLocation Loc, bool isImported) {
assert(MI && "MacroInfo should be non-zero!");
MacroDirective *MD = AllocateMacroDirective(MI, Loc, isImported);
@ -54,6 +54,8 @@ void Preprocessor::setMacroDirective(IdentifierInfo *II, MacroInfo *MI,
II->setHasMacroDefinition(true);
if (II->isFromAST())
II->setChangedSinceDeserialization();
return MD;
}
void Preprocessor::addLoadedMacroInfo(IdentifierInfo *II, MacroDirective *MD,
@ -304,7 +306,9 @@ bool Preprocessor::isNextPPTokenLParen() {
/// HandleMacroExpandedIdentifier - If an identifier token is read that is to be
/// expanded as a macro, handle it and return the next token as 'Identifier'.
bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
MacroInfo *MI) {
MacroDirective *MD) {
MacroInfo *MI = MD->getInfo();
// If this is a macro expansion in the "#if !defined(x)" line for the file,
// then the macro could expand to different things in other contexts, we need
// to disable the optimization in this case.
@ -312,7 +316,7 @@ bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
// If this is a builtin macro, like __LINE__ or _Pragma, handle it specially.
if (MI->isBuiltinMacro()) {
if (Callbacks) Callbacks->MacroExpands(Identifier, MI,
if (Callbacks) Callbacks->MacroExpands(Identifier, MD,
Identifier.getLocation());
ExpandBuiltinMacro(Identifier);
return false;
@ -365,13 +369,13 @@ bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier,
// MacroExpands callbacks still happen in source order, queue this
// callback to have it happen after the function macro callback.
DelayedMacroExpandsCallbacks.push_back(
MacroExpandsInfo(Identifier, MI, ExpansionRange));
MacroExpandsInfo(Identifier, MD, ExpansionRange));
} else {
Callbacks->MacroExpands(Identifier, MI, ExpansionRange);
Callbacks->MacroExpands(Identifier, MD, ExpansionRange);
if (!DelayedMacroExpandsCallbacks.empty()) {
for (unsigned i=0, e = DelayedMacroExpandsCallbacks.size(); i!=e; ++i) {
MacroExpandsInfo &Info = DelayedMacroExpandsCallbacks[i];
Callbacks->MacroExpands(Info.Tok, Info.MI, Info.Range);
Callbacks->MacroExpands(Info.Tok, Info.MD, Info.Range);
}
DelayedMacroExpandsCallbacks.clear();
}

View File

@ -382,33 +382,34 @@ void PreprocessingRecord::addMacroExpansion(const Token &Id,
}
void PreprocessingRecord::Ifdef(SourceLocation Loc, const Token &MacroNameTok,
const MacroInfo *MI) {
const MacroDirective *MD) {
// This is not actually a macro expansion but record it as a macro reference.
if (MI)
addMacroExpansion(MacroNameTok, MI, MacroNameTok.getLocation());
if (MD)
addMacroExpansion(MacroNameTok, MD->getInfo(), MacroNameTok.getLocation());
}
void PreprocessingRecord::Ifndef(SourceLocation Loc, const Token &MacroNameTok,
const MacroInfo *MI) {
const MacroDirective *MD) {
// This is not actually a macro expansion but record it as a macro reference.
if (MI)
addMacroExpansion(MacroNameTok, MI, MacroNameTok.getLocation());
if (MD)
addMacroExpansion(MacroNameTok, MD->getInfo(), MacroNameTok.getLocation());
}
void PreprocessingRecord::Defined(const Token &MacroNameTok,
const MacroInfo *MI) {
const MacroDirective *MD) {
// This is not actually a macro expansion but record it as a macro reference.
if (MI)
addMacroExpansion(MacroNameTok, MI, MacroNameTok.getLocation());
if (MD)
addMacroExpansion(MacroNameTok, MD->getInfo(), MacroNameTok.getLocation());
}
void PreprocessingRecord::MacroExpands(const Token &Id, const MacroInfo* MI,
void PreprocessingRecord::MacroExpands(const Token &Id,const MacroDirective *MD,
SourceRange Range) {
addMacroExpansion(Id, MI, Range);
addMacroExpansion(Id, MD->getInfo(), Range);
}
void PreprocessingRecord::MacroDefined(const Token &Id,
const MacroInfo *MI) {
const MacroDirective *MD) {
const MacroInfo *MI = MD->getInfo();
SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc());
MacroDefinition *Def
= new (*this) MacroDefinition(Id.getIdentifierInfo(), R);
@ -417,10 +418,10 @@ void PreprocessingRecord::MacroDefined(const Token &Id,
}
void PreprocessingRecord::MacroUndefined(const Token &Id,
const MacroInfo *MI) {
const MacroDirective *MD) {
// Note: MI may be null (when #undef'ining an undefined macro).
if (MI)
MacroDefinitions.erase(MI);
if (MD)
MacroDefinitions.erase(MD->getInfo());
}
void PreprocessingRecord::InclusionDirective(

View File

@ -642,10 +642,11 @@ void Preprocessor::HandleIdentifier(Token &Identifier) {
}
// If this is a macro to be expanded, do it.
if (MacroInfo *MI = getMacroInfo(&II)) {
if (MacroDirective *MD = getMacroDirective(&II)) {
MacroInfo *MI = MD->getInfo();
if (!DisableMacroExpansion) {
if (!Identifier.isExpandDisabled() && MI->isEnabled()) {
if (!HandleMacroExpandedIdentifier(Identifier, MI))
if (!HandleMacroExpandedIdentifier(Identifier, MD))
return;
} else {
// C99 6.10.3.4p2 says that a disabled macro may never again be

View File

@ -281,16 +281,17 @@ public:
}
/// MacroDefined - This hook is called whenever a macro definition is seen.
virtual void MacroDefined(const Token &Id, const MacroInfo *MI) {
virtual void MacroDefined(const Token &Id, const MacroDirective *MD) {
}
/// MacroUndefined - This hook is called whenever a macro #undef is seen.
/// MI is released immediately following this callback.
virtual void MacroUndefined(const Token &MacroNameTok, const MacroInfo *MI) {
virtual void MacroUndefined(const Token &MacroNameTok,
const MacroDirective *MD) {
}
/// MacroExpands - This is called by when a macro invocation is found.
virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo* MI,
virtual void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD,
SourceRange Range) {
}

View File

@ -249,12 +249,13 @@ class MacroTracker : public PPCallbacks {
public:
explicit MacroTracker(std::vector<MacroAction> &Macros) : Macros(Macros) { }
virtual void MacroDefined(const Token &MacroNameTok, const MacroInfo *MI) {
Macros.push_back(MacroAction(MI->getDefinitionLoc(),
virtual void MacroDefined(const Token &MacroNameTok,
const MacroDirective *MD) {
Macros.push_back(MacroAction(MD->getLocation(),
MacroNameTok.getIdentifierInfo()->getName(),
true));
}
virtual void MacroExpands(const Token &MacroNameTok, const MacroInfo* MI,
virtual void MacroExpands(const Token &MacroNameTok, const MacroDirective *MD,
SourceRange Range) {
Macros.push_back(MacroAction(MacroNameTok.getLocation(),
MacroNameTok.getIdentifierInfo()->getName(),