diff options
author | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2013-02-20 00:54:57 +0000 |
---|---|---|
committer | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2013-02-20 00:54:57 +0000 |
commit | 9818a1d443e97677dd3422305de9cc2b1fb2a8c1 (patch) | |
tree | f6b547c4fcc33ff4bae563473287cddfeff4e21c /lib/Lex | |
parent | 206f49966f66ad7cbfe3d37c14fa7e6e7410f3be (diff) |
[preprocessor] Split the MacroInfo class into two separate concepts, MacroInfo class
for the data specific to a macro definition (e.g. what the tokens are), and
MacroDirective class which encapsulates the changes to the "macro namespace"
(e.g. the location where the macro name became active, the location where it was undefined, etc.)
(A MacroDirective always points to a MacroInfo object.)
Usually a macro definition (MacroInfo) is where a macro name becomes active (MacroDirective) but
splitting the concepts allows us to better model the effect of modules to the macro namespace
(also as a bonus it allows better modeling of push_macro/pop_macro #pragmas).
Modules can have their own macro history, separate from the local (current translation unit)
macro history; MacroDirectives will be used to model the macro history (changes to macro namespace).
For example, if "@import A;" imports macro FOO, there will be a new local MacroDirective created
to indicate that "FOO" became active at the import location. Module "A" itself will contain another
MacroDirective in its macro history (at the point of the definition of FOO) and both MacroDirectives
will point to the same MacroInfo object.
Introducing the separation of macro concepts is the first part towards better modeling of module macros.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@175585 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Lex')
-rw-r--r-- | lib/Lex/MacroInfo.cpp | 59 | ||||
-rw-r--r-- | lib/Lex/PPDirectives.cpp | 53 | ||||
-rw-r--r-- | lib/Lex/PPMacroExpansion.cpp | 95 | ||||
-rw-r--r-- | lib/Lex/Pragma.cpp | 17 | ||||
-rw-r--r-- | lib/Lex/Preprocessor.cpp | 11 |
5 files changed, 102 insertions, 133 deletions
diff --git a/lib/Lex/MacroInfo.cpp b/lib/Lex/MacroInfo.cpp index d1875c79d0..ed6cc6edaf 100644 --- a/lib/Lex/MacroInfo.cpp +++ b/lib/Lex/MacroInfo.cpp @@ -17,7 +17,6 @@ using namespace clang; MacroInfo::MacroInfo(SourceLocation DefLoc) : Location(DefLoc), - PreviousDefinition(0), ArgumentList(0), NumArguments(0), IsDefinitionLengthCached(false), @@ -26,54 +25,10 @@ MacroInfo::MacroInfo(SourceLocation DefLoc) IsGNUVarargs(false), IsBuiltinMacro(false), HasCommaPasting(false), - IsFromAST(false), - ChangedAfterLoad(false), IsDisabled(false), IsUsed(false), IsAllowRedefinitionsWithoutWarning(false), - IsWarnIfUnused(false), - IsPublic(true), - IsHidden(false), - IsAmbiguous(false) { -} - -MacroInfo::MacroInfo(const MacroInfo &MI, llvm::BumpPtrAllocator &PPAllocator) - : Location(MI.Location), - EndLocation(MI.EndLocation), - UndefLocation(MI.UndefLocation), - PreviousDefinition(0), - ArgumentList(0), - NumArguments(0), - ReplacementTokens(MI.ReplacementTokens), - DefinitionLength(MI.DefinitionLength), - IsDefinitionLengthCached(MI.IsDefinitionLengthCached), - IsFunctionLike(MI.IsFunctionLike), - IsC99Varargs(MI.IsC99Varargs), - IsGNUVarargs(MI.IsGNUVarargs), - IsBuiltinMacro(MI.IsBuiltinMacro), - HasCommaPasting(MI.HasCommaPasting), - IsFromAST(MI.IsFromAST), - ChangedAfterLoad(MI.ChangedAfterLoad), - IsDisabled(MI.IsDisabled), - IsUsed(MI.IsUsed), - IsAllowRedefinitionsWithoutWarning(MI.IsAllowRedefinitionsWithoutWarning), - IsWarnIfUnused(MI.IsWarnIfUnused), - IsPublic(MI.IsPublic), - IsHidden(MI.IsHidden), - IsAmbiguous(MI.IsAmbiguous) { - setArgumentList(MI.ArgumentList, MI.NumArguments, PPAllocator); -} - -const MacroInfo *MacroInfo::findDefinitionAtLoc(SourceLocation L, - SourceManager &SM) const { - assert(L.isValid() && "SourceLocation is invalid."); - for (const MacroInfo *MI = this; MI; MI = MI->PreviousDefinition) { - if (MI->Location.isInvalid() || // For macros defined on the command line. - SM.isBeforeInTranslationUnit(MI->Location, L)) - return (MI->UndefLocation.isInvalid() || - SM.isBeforeInTranslationUnit(L, MI->UndefLocation)) ? MI : NULL; - } - return NULL; + IsWarnIfUnused(false) { } unsigned MacroInfo::getDefinitionLengthSlow(SourceManager &SM) const { @@ -151,3 +106,15 @@ bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const { return true; } + +const MacroDirective * +MacroDirective::findDirectiveAtLoc(SourceLocation L, SourceManager &SM) const { + assert(L.isValid() && "SourceLocation is invalid."); + for (const MacroDirective *MD = this; MD; MD = MD->Previous) { + if (MD->getLocation().isInvalid() || // For macros defined on the command line. + SM.isBeforeInTranslationUnit(MD->getLocation(), L)) + return (MD->UndefLocation.isInvalid() || + SM.isBeforeInTranslationUnit(L, MD->UndefLocation)) ? MD : NULL; + } + return NULL; +} diff --git a/lib/Lex/PPDirectives.cpp b/lib/Lex/PPDirectives.cpp index c8e1f4fc3e..18250281c1 100644 --- a/lib/Lex/PPDirectives.cpp +++ b/lib/Lex/PPDirectives.cpp @@ -57,10 +57,12 @@ MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) { return MI; } -MacroInfo *Preprocessor::CloneMacroInfo(const MacroInfo &MacroToClone) { - MacroInfo *MI = AllocateMacroInfo(); - new (MI) MacroInfo(MacroToClone, BP); - return MI; +MacroDirective *Preprocessor::AllocateMacroDirective(MacroInfo *MI, + SourceLocation Loc, + bool isImported) { + MacroDirective *MD = BP.Allocate<MacroDirective>(); + new (MD) MacroDirective(MI, Loc, isImported); + return MD; } /// \brief Release the specified MacroInfo to be reused for allocating @@ -1110,22 +1112,22 @@ void Preprocessor::HandleMacroPublicDirective(Token &Tok) { CheckEndOfDirective("__public_macro"); // Okay, we finally have a valid identifier to undef. - MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo()); + MacroDirective *MD = getMacroDirective(MacroNameTok.getIdentifierInfo()); // If the macro is not defined, this is an error. - if (MI == 0) { + if (MD == 0) { Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << MacroNameTok.getIdentifierInfo(); return; } // Note that this macro has now been exported. - MI->setVisibility(/*IsPublic=*/true, MacroNameTok.getLocation()); - + MD->setVisibility(/*IsPublic=*/true, MacroNameTok.getLocation()); + // If this macro definition came from a PCH file, mark it // as having changed since serialization. - if (MI->isFromAST()) - MI->setChangedAfterLoad(); + if (MD->isImported()) + MD->setChangedAfterLoad(); } /// \brief Handle a #private directive. @@ -1141,22 +1143,22 @@ void Preprocessor::HandleMacroPrivateDirective(Token &Tok) { CheckEndOfDirective("__private_macro"); // Okay, we finally have a valid identifier to undef. - MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo()); + MacroDirective *MD = getMacroDirective(MacroNameTok.getIdentifierInfo()); // If the macro is not defined, this is an error. - if (MI == 0) { + if (MD == 0) { Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << MacroNameTok.getIdentifierInfo(); return; } // Note that this macro has now been marked private. - MI->setVisibility(/*IsPublic=*/false, MacroNameTok.getLocation()); - + MD->setVisibility(/*IsPublic=*/false, MacroNameTok.getLocation()); + // If this macro definition came from a PCH file, mark it // as having changed since serialization. - if (MI->isFromAST()) - MI->setChangedAfterLoad(); + if (MD->isImported()) + MD->setChangedAfterLoad(); } //===----------------------------------------------------------------------===// @@ -1918,7 +1920,7 @@ void Preprocessor::HandleDefineDirective(Token &DefineTok) { // Finally, if this identifier already had a macro defined for it, verify that // the macro bodies are identical, and issue diagnostics if they are not. - if (MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo())) { + if (const MacroInfo *OtherMI=getMacroInfo(MacroNameTok.getIdentifierInfo())) { // It is very common for system headers to have tons of macro redefinitions // and for warnings to be disabled in system headers. If this is the case, // then don't bother calling MacroInfo::isIdenticalTo. @@ -1940,7 +1942,7 @@ void Preprocessor::HandleDefineDirective(Token &DefineTok) { WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc()); } - setMacroInfo(MacroNameTok.getIdentifierInfo(), MI); + setMacroDirective(MacroNameTok.getIdentifierInfo(), MI); assert(!MI->isUsed()); // If we need warning for not using the macro, add its location in the @@ -1973,7 +1975,8 @@ void Preprocessor::HandleUndefDirective(Token &UndefTok) { CheckEndOfDirective("undef"); // Okay, we finally have a valid identifier to undef. - MacroInfo *MI = getMacroInfo(MacroNameTok.getIdentifierInfo()); + MacroDirective *MD = getMacroDirective(MacroNameTok.getIdentifierInfo()); + const MacroInfo *MI = MD ? MD->getInfo() : 0; // If the callbacks want to know, tell them about the macro #undef. // Note: no matter if the macro was defined or not. @@ -1989,17 +1992,17 @@ void Preprocessor::HandleUndefDirective(Token &UndefTok) { if (MI->isWarnIfUnused()) WarnUnusedMacroLocs.erase(MI->getDefinitionLoc()); - UndefineMacro(MacroNameTok.getIdentifierInfo(), MI, + UndefineMacro(MacroNameTok.getIdentifierInfo(), MD, MacroNameTok.getLocation()); } -void Preprocessor::UndefineMacro(IdentifierInfo *II, MacroInfo *MI, +void Preprocessor::UndefineMacro(IdentifierInfo *II, MacroDirective *MD, SourceLocation UndefLoc) { - MI->setUndefLoc(UndefLoc); - if (MI->isFromAST()) { - MI->setChangedAfterLoad(); + MD->setUndefLoc(UndefLoc); + if (MD->isImported()) { + MD->setChangedAfterLoad(); if (Listener) - Listener->UndefinedMacro(MI); + Listener->UndefinedMacro(MD); } clearMacroInfo(II); diff --git a/lib/Lex/PPMacroExpansion.cpp b/lib/Lex/PPMacroExpansion.cpp index bda31ed294..3e68fbdf01 100644 --- a/lib/Lex/PPMacroExpansion.cpp +++ b/lib/Lex/PPMacroExpansion.cpp @@ -32,7 +32,8 @@ #include <ctime> using namespace clang; -MacroInfo *Preprocessor::getMacroInfoHistory(const IdentifierInfo *II) const { +MacroDirective * +Preprocessor::getMacroDirectiveHistory(const IdentifierInfo *II) const { assert(II->hadMacroDefinition() && "Identifier has not been not a macro!"); macro_iterator Pos = Macros.find(II); @@ -42,32 +43,32 @@ MacroInfo *Preprocessor::getMacroInfoHistory(const IdentifierInfo *II) const { /// setMacroInfo - Specify a macro for this identifier. /// -void Preprocessor::setMacroInfo(IdentifierInfo *II, MacroInfo *MI) { +void Preprocessor::setMacroDirective(IdentifierInfo *II, MacroInfo *MI, + SourceLocation Loc, bool isImported) { assert(MI && "MacroInfo should be non-zero!"); - assert(MI->getUndefLoc().isInvalid() && - "Undefined macros cannot be registered"); - MacroInfo *&StoredMI = Macros[II]; - MI->setPreviousDefinition(StoredMI); - StoredMI = MI; - II->setHasMacroDefinition(MI->getUndefLoc().isInvalid()); + MacroDirective *MD = AllocateMacroDirective(MI, Loc, isImported); + MacroDirective *&StoredMD = Macros[II]; + MD->setPrevious(StoredMD); + StoredMD = MD; + II->setHasMacroDefinition(true); if (II->isFromAST()) II->setChangedSinceDeserialization(); } -void Preprocessor::addLoadedMacroInfo(IdentifierInfo *II, MacroInfo *MI, - MacroInfo *Hint) { - assert(MI && "Missing macro?"); - assert(MI->isFromAST() && "Macro is not from an AST?"); - assert(!MI->getPreviousDefinition() && "Macro already in chain?"); +void Preprocessor::addLoadedMacroInfo(IdentifierInfo *II, MacroDirective *MD, + MacroDirective *Hint) { + assert(MD && "Missing macro?"); + assert(MD->isImported() && "Macro is not from an AST?"); + assert(!MD->getPrevious() && "Macro already in chain?"); - MacroInfo *&StoredMI = Macros[II]; + MacroDirective *&StoredMD = Macros[II]; // Easy case: this is the first macro definition for this macro. - if (!StoredMI) { - StoredMI = MI; + if (!StoredMD) { + StoredMD = MD; - if (MI->isDefined()) + if (MD->isDefined()) II->setHasMacroDefinition(true); return; } @@ -75,79 +76,79 @@ void Preprocessor::addLoadedMacroInfo(IdentifierInfo *II, MacroInfo *MI, // If this macro is a definition and this identifier has been neither // defined nor undef'd in the current translation unit, add this macro // to the end of the chain of definitions. - if (MI->isDefined() && StoredMI->isFromAST()) { + if (MD->isDefined() && StoredMD->isImported()) { // Simple case: if this is the first actual definition, just put it at // th beginning. - if (!StoredMI->isDefined()) { - MI->setPreviousDefinition(StoredMI); - StoredMI = MI; + if (!StoredMD->isDefined()) { + MD->setPrevious(StoredMD); + StoredMD = MD; II->setHasMacroDefinition(true); return; } // Find the end of the definition chain. - MacroInfo *Prev; - MacroInfo *PrevPrev = StoredMI; - bool Ambiguous = StoredMI->isAmbiguous(); + MacroDirective *Prev; + MacroDirective *PrevPrev = StoredMD; + bool Ambiguous = StoredMD->isAmbiguous(); bool MatchedOther = false; do { Prev = PrevPrev; // If the macros are not identical, we have an ambiguity. - if (!Prev->isIdenticalTo(*MI, *this)) { + if (!Prev->getInfo()->isIdenticalTo(*MD->getInfo(), *this)) { if (!Ambiguous) { Ambiguous = true; - StoredMI->setAmbiguous(true); + StoredMD->setAmbiguous(true); } } else { MatchedOther = true; } - } while ((PrevPrev = Prev->getPreviousDefinition()) && + } while ((PrevPrev = Prev->getPrevious()) && PrevPrev->isDefined()); // If there are ambiguous definitions, and we didn't match any other // definition, then mark us as ambiguous. if (Ambiguous && !MatchedOther) - MI->setAmbiguous(true); + MD->setAmbiguous(true); // Wire this macro information into the chain. - MI->setPreviousDefinition(Prev->getPreviousDefinition()); - Prev->setPreviousDefinition(MI); + MD->setPrevious(Prev->getPrevious()); + Prev->setPrevious(MD); return; } // The macro is not a definition; put it at the end of the list. - MacroInfo *Prev = Hint? Hint : StoredMI; - while (Prev->getPreviousDefinition()) - Prev = Prev->getPreviousDefinition(); - Prev->setPreviousDefinition(MI); + MacroDirective *Prev = Hint? Hint : StoredMD; + while (Prev->getPrevious()) + Prev = Prev->getPrevious(); + Prev->setPrevious(MD); } void Preprocessor::makeLoadedMacroInfoVisible(IdentifierInfo *II, - MacroInfo *MI) { - assert(MI->isFromAST() && "Macro must be from the AST"); + MacroDirective *MD) { + assert(MD->isImported() && "Macro must be from the AST"); - MacroInfo *&StoredMI = Macros[II]; - if (StoredMI == MI) { + MacroDirective *&StoredMD = Macros[II]; + if (StoredMD == MD) { // Easy case: this is the first macro anyway. - II->setHasMacroDefinition(MI->isDefined()); + II->setHasMacroDefinition(MD->isDefined()); return; } // Go find the macro and pull it out of the list. // FIXME: Yes, this is O(N), and making a pile of macros visible or hidden // would be quadratic, but it's extremely rare. - MacroInfo *Prev = StoredMI; - while (Prev->getPreviousDefinition() != MI) - Prev = Prev->getPreviousDefinition(); - Prev->setPreviousDefinition(MI->getPreviousDefinition()); - MI->setPreviousDefinition(0); + MacroDirective *Prev = StoredMD; + while (Prev->getPrevious() != MD) + Prev = Prev->getPrevious(); + Prev->setPrevious(MD->getPrevious()); + MD->setPrevious(0); // Add the macro back to the list. - addLoadedMacroInfo(II, MI); + addLoadedMacroInfo(II, MD); - II->setHasMacroDefinition(StoredMI->isDefined()); + II->setHasMacroDefinition(StoredMD->isDefined()); if (II->isFromAST()) II->setChangedSinceDeserialization(); } @@ -170,7 +171,7 @@ static IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){ // Mark it as being a macro that is builtin. MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation()); MI->setIsBuiltinMacro(); - PP.setMacroInfo(Id, MI); + PP.setMacroDirective(Id, MI); return Id; } diff --git a/lib/Lex/Pragma.cpp b/lib/Lex/Pragma.cpp index 092216aef5..23d088a9fb 100644 --- a/lib/Lex/Pragma.cpp +++ b/lib/Lex/Pragma.cpp @@ -650,17 +650,13 @@ void Preprocessor::HandlePragmaPushMacro(Token &PushMacroTok) { // Get the MacroInfo associated with IdentInfo. MacroInfo *MI = getMacroInfo(IdentInfo); - MacroInfo *MacroCopyToPush = 0; if (MI) { - // Make a clone of MI. - MacroCopyToPush = CloneMacroInfo(*MI); - // Allow the original MacroInfo to be redefined later. MI->setIsAllowRedefinitionsWithoutWarning(true); } // Push the cloned MacroInfo so we can retrieve it later. - PragmaPushMacroInfo[IdentInfo].push_back(MacroCopyToPush); + PragmaPushMacroInfo[IdentInfo].push_back(MI); } /// \brief Handle \#pragma pop_macro. @@ -681,10 +677,10 @@ void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) { PragmaPushMacroInfo.find(IdentInfo); if (iter != PragmaPushMacroInfo.end()) { // Forget the MacroInfo currently associated with IdentInfo. - if (MacroInfo *CurrentMI = getMacroInfo(IdentInfo)) { - if (CurrentMI->isWarnIfUnused()) - WarnUnusedMacroLocs.erase(CurrentMI->getDefinitionLoc()); - UndefineMacro(IdentInfo, CurrentMI, MessageLoc); + if (MacroDirective *CurrentMD = getMacroDirective(IdentInfo)) { + if (CurrentMD->getInfo()->isWarnIfUnused()) + WarnUnusedMacroLocs.erase(CurrentMD->getInfo()->getDefinitionLoc()); + UndefineMacro(IdentInfo, CurrentMD, MessageLoc); } // Get the MacroInfo we want to reinstall. @@ -692,7 +688,8 @@ void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) { if (MacroToReInstall) { // Reinstall the previously pushed macro. - setMacroInfo(IdentInfo, MacroToReInstall); + setMacroDirective(IdentInfo, MacroToReInstall, MessageLoc, + /*isImported=*/false); } else if (IdentInfo->hasMacroDefinition()) { clearMacroInfo(IdentInfo); } diff --git a/lib/Lex/Preprocessor.cpp b/lib/Lex/Preprocessor.cpp index 155074c9f8..8209c3c136 100644 --- a/lib/Lex/Preprocessor.cpp +++ b/lib/Lex/Preprocessor.cpp @@ -306,14 +306,15 @@ StringRef Preprocessor::getLastMacroWithSpelling( StringRef BestSpelling; for (Preprocessor::macro_iterator I = macro_begin(), E = macro_end(); I != E; ++I) { - if (!I->second->isObjectLike()) + if (!I->second->getInfo()->isObjectLike()) continue; - const MacroInfo *MI = I->second->findDefinitionAtLoc(Loc, SourceMgr); - if (!MI) + const MacroDirective * + MD = I->second->findDirectiveAtLoc(Loc, SourceMgr); + if (!MD) continue; - if (!MacroDefinitionEquals(MI, Tokens)) + if (!MacroDefinitionEquals(MD->getInfo(), Tokens)) continue; - SourceLocation Location = I->second->getDefinitionLoc(); + SourceLocation Location = I->second->getInfo()->getDefinitionLoc(); // Choose the macro defined latest. if (BestLocation.isInvalid() || (Location.isValid() && |