diff options
Diffstat (limited to 'clang/lib/Lex')
-rw-r--r-- | clang/lib/Lex/HeaderSearch.cpp | 7 | ||||
-rw-r--r-- | clang/lib/Lex/MacroArgs.cpp | 7 | ||||
-rw-r--r-- | clang/lib/Lex/PPDirectives.cpp | 34 | ||||
-rw-r--r-- | clang/lib/Lex/PPExpressions.cpp | 16 | ||||
-rw-r--r-- | clang/lib/Lex/PPLexerChange.cpp | 2 | ||||
-rw-r--r-- | clang/lib/Lex/PPMacroExpansion.cpp | 87 | ||||
-rw-r--r-- | clang/lib/Lex/Pragma.cpp | 5 | ||||
-rw-r--r-- | clang/lib/Lex/Preprocessor.cpp | 10 |
8 files changed, 90 insertions, 78 deletions
diff --git a/clang/lib/Lex/HeaderSearch.cpp b/clang/lib/Lex/HeaderSearch.cpp index 3e59cdbd186..04cdb0f69f7 100644 --- a/clang/lib/Lex/HeaderSearch.cpp +++ b/clang/lib/Lex/HeaderSearch.cpp @@ -18,6 +18,7 @@ #include "clang/Lex/HeaderSearchOptions.h" #include "clang/Lex/LexDiagnostic.h" #include "clang/Lex/Lexer.h" +#include "clang/Lex/Preprocessor.h" #include "llvm/ADT/APInt.h" #include "llvm/ADT/Hashing.h" #include "llvm/ADT/SmallString.h" @@ -1016,7 +1017,9 @@ void HeaderSearch::MarkFileModuleHeader(const FileEntry *FE, HFI.setHeaderRole(Role); } -bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){ +bool HeaderSearch::ShouldEnterIncludeFile(Preprocessor &PP, + const FileEntry *File, + bool isImport) { ++NumIncluded; // Count # of attempted #includes. // Get information about this file. @@ -1041,7 +1044,7 @@ bool HeaderSearch::ShouldEnterIncludeFile(const FileEntry *File, bool isImport){ // if the macro that guards it is defined, we know the #include has no effect. if (const IdentifierInfo *ControllingMacro = FileInfo.getControllingMacro(ExternalLookup)) - if (ControllingMacro->hasMacroDefinition()) { + if (PP.isMacroDefined(ControllingMacro)) { ++NumMultiIncludeFileOptzn; return false; } diff --git a/clang/lib/Lex/MacroArgs.cpp b/clang/lib/Lex/MacroArgs.cpp index 9967f3f0e49..1c1979d8e83 100644 --- a/clang/lib/Lex/MacroArgs.cpp +++ b/clang/lib/Lex/MacroArgs.cpp @@ -133,12 +133,11 @@ bool MacroArgs::ArgNeedsPreexpansion(const Token *ArgTok, // If there are no identifiers in the argument list, or if the identifiers are // known to not be macros, pre-expansion won't modify it. for (; ArgTok->isNot(tok::eof); ++ArgTok) - if (IdentifierInfo *II = ArgTok->getIdentifierInfo()) { - if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled()) + if (IdentifierInfo *II = ArgTok->getIdentifierInfo()) + if (II->hasMacroDefinition()) // Return true even though the macro could be a function-like macro - // without a following '(' token. + // without a following '(' token, or could be disabled, or not visible. return true; - } return false; } diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp index 718bb5cb89c..6c25bd87a27 100644 --- a/clang/lib/Lex/PPDirectives.cpp +++ b/clang/lib/Lex/PPDirectives.cpp @@ -179,11 +179,13 @@ bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef, return Diag(MacroNameTok, diag::err_defined_macro_name); } - if (isDefineUndef == MU_Undef && II->hasMacroDefinition() && - getMacroInfo(II)->isBuiltinMacro()) { - // Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4 - // and C++ [cpp.predefined]p4], but allow it as an extension. - Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro); + if (isDefineUndef == MU_Undef) { + auto *MI = getMacroInfo(II); + if (MI && MI->isBuiltinMacro()) { + // Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4 + // and C++ [cpp.predefined]p4], but allow it as an extension. + Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro); + } } // If defining/undefining reserved identifier or a keyword, we need to issue @@ -1292,7 +1294,7 @@ void Preprocessor::HandleMacroPublicDirective(Token &Tok) { IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); // Okay, we finally have a valid identifier to undef. - MacroDirective *MD = getMacroDirective(II); + MacroDirective *MD = getLocalMacroDirective(II); // If the macro is not defined, this is an error. if (!MD) { @@ -1319,7 +1321,7 @@ void Preprocessor::HandleMacroPrivateDirective(Token &Tok) { IdentifierInfo *II = MacroNameTok.getIdentifierInfo(); // Okay, we finally have a valid identifier to undef. - MacroDirective *MD = getMacroDirective(II); + MacroDirective *MD = getLocalMacroDirective(II); // If the macro is not defined, this is an error. if (!MD) { @@ -1757,7 +1759,7 @@ void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc, // Ask HeaderInfo if we should enter this #include file. If not, #including // this file will have no effect. - if (!HeaderInfo.ShouldEnterIncludeFile(File, isImport)) { + if (!HeaderInfo.ShouldEnterIncludeFile(*this, File, isImport)) { if (Callbacks) Callbacks->FileSkipped(*File, FilenameTok, FileCharacter); return; @@ -2295,16 +2297,19 @@ void Preprocessor::HandleUndefDirective(Token &UndefTok) { // Check to see if this is the last token on the #undef line. CheckEndOfDirective("undef"); - // Okay, we finally have a valid identifier to undef. - MacroDirective *MD = getMacroDirective(MacroNameTok.getIdentifierInfo()); - const MacroInfo *MI = MD ? MD->getMacroInfo() : nullptr; + // Okay, we have a valid identifier to undef. + auto *II = MacroNameTok.getIdentifierInfo(); // If the callbacks want to know, tell them about the macro #undef. // Note: no matter if the macro was defined or not. - if (Callbacks) + if (Callbacks) { + // FIXME: Tell callbacks about module macros. + MacroDirective *MD = getLocalMacroDirective(II); Callbacks->MacroUndefined(MacroNameTok, MD); + } // If the macro is not defined, this is a noop undef, just return. + const MacroInfo *MI = getMacroInfo(II); if (!MI) return; @@ -2349,8 +2354,7 @@ void Preprocessor::HandleIfdefDirective(Token &Result, bool isIfndef, CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef"); IdentifierInfo *MII = MacroNameTok.getIdentifierInfo(); - MacroDirective *MD = getMacroDirective(MII); - MacroInfo *MI = MD ? MD->getMacroInfo() : nullptr; + MacroInfo *MI = getMacroInfo(MII); if (CurPPLexer->getConditionalStackDepth() == 0) { // If the start of a top-level #ifdef and if the macro is not defined, @@ -2369,6 +2373,8 @@ 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/clang/lib/Lex/PPExpressions.cpp b/clang/lib/Lex/PPExpressions.cpp index a6f16f80b79..9246fb188f1 100644 --- a/clang/lib/Lex/PPExpressions.cpp +++ b/clang/lib/Lex/PPExpressions.cpp @@ -108,15 +108,13 @@ static bool EvaluateDefined(PPValue &Result, Token &PeekTok, DefinedTracker &DT, // Otherwise, we got an identifier, is it defined to something? IdentifierInfo *II = PeekTok.getIdentifierInfo(); - Result.Val = II->hasMacroDefinition(); + Preprocessor::MacroDefinition Macro = PP.getMacroDefinition(II); + Result.Val = !!Macro; Result.Val.setIsUnsigned(false); // Result is signed intmax_t. - MacroDirective *Macro = nullptr; // If there is a macro, mark it used. - if (Result.Val != 0 && ValueLive) { - Macro = PP.getMacroDirective(II); - PP.markMacroAsUsed(Macro->getMacroInfo()); - } + if (Result.Val != 0 && ValueLive) + PP.markMacroAsUsed(Macro.getMacroInfo()); // Save macro token for callback. Token macroToken(PeekTok); @@ -144,10 +142,8 @@ static bool EvaluateDefined(PPValue &Result, Token &PeekTok, DefinedTracker &DT, // Invoke the 'defined' callback. if (PPCallbacks *Callbacks = PP.getPPCallbacks()) { - MacroDirective *MD = Macro; - // Pass the MacroInfo for the macro name even if the value is dead. - if (!MD && Result.Val != 0) - MD = PP.getMacroDirective(II); + // FIXME: Tell callbacks about module macros. + MacroDirective *MD = Macro.getLocalDirective(); Callbacks->Defined(macroToken, MD, SourceRange(beginLoc, PeekTok.getLocation())); } diff --git a/clang/lib/Lex/PPLexerChange.cpp b/clang/lib/Lex/PPLexerChange.cpp index 3f0c2bb2776..dc1b5bed049 100644 --- a/clang/lib/Lex/PPLexerChange.cpp +++ b/clang/lib/Lex/PPLexerChange.cpp @@ -309,7 +309,7 @@ bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) { } if (const IdentifierInfo *DefinedMacro = CurPPLexer->MIOpt.GetDefinedMacro()) { - if (!ControllingMacro->hasMacroDefinition() && + if (!isMacroDefined(ControllingMacro) && DefinedMacro != ControllingMacro && HeaderInfo.FirstTimeLexingFile(FE)) { diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp index 438758c36f6..7aaeb0a7672 100644 --- a/clang/lib/Lex/PPMacroExpansion.cpp +++ b/clang/lib/Lex/PPMacroExpansion.cpp @@ -34,12 +34,11 @@ using namespace clang; MacroDirective * -Preprocessor::getMacroDirectiveHistory(const IdentifierInfo *II) const { - assert(II->hadMacroDefinition() && "Identifier has not been not a macro!"); - +Preprocessor::getLocalMacroDirectiveHistory(const IdentifierInfo *II) const { + if (!II->hadMacroDefinition()) + return nullptr; auto Pos = Macros.find(II); - assert(Pos != Macros.end() && "Identifier macro info is missing!"); - return Pos->second.getLatest(); + return Pos == Macros.end() ? nullptr : Pos->second.getLatest(); } void Preprocessor::appendMacroDirective(IdentifierInfo *II, MacroDirective *MD){ @@ -54,7 +53,7 @@ void Preprocessor::appendMacroDirective(IdentifierInfo *II, MacroDirective *MD){ // Set up the identifier as having associated macro history. II->setHasMacroDefinition(true); - if (!MD->isDefined()) + if (!MD->isDefined() && LeafModuleMacros.find(II) == LeafModuleMacros.end()) II->setHasMacroDefinition(false); if (II->isFromAST() && !MD->isImported()) II->setChangedSinceDeserialization(); @@ -69,7 +68,7 @@ void Preprocessor::setLoadedMacroDirective(IdentifierInfo *II, StoredMD = MD; // Setup the identifier as having associated macro history. II->setHasMacroDefinition(true); - if (!MD->isDefined()) + if (!MD->isDefined() && LeafModuleMacros.find(II) == LeafModuleMacros.end()) II->setHasMacroDefinition(false); } @@ -108,6 +107,8 @@ ModuleMacro *Preprocessor::addModuleMacro(Module *Mod, IdentifierInfo *II, // The new macro is always a leaf macro. LeafMacros.push_back(MM); + // The identifier now has defined macros (that may or may not be visible). + II->setHasMacroDefinition(true); New = true; return MM; @@ -121,7 +122,7 @@ ModuleMacro *Preprocessor::getModuleMacro(Module *Mod, IdentifierInfo *II) { return ModuleMacros.FindNodeOrInsertPos(ID, InsertPos); } -void Preprocessor::updateModuleMacroInfo(IdentifierInfo *II, +void Preprocessor::updateModuleMacroInfo(const IdentifierInfo *II, ModuleMacroInfo &Info) { assert(Info.ActiveModuleMacrosGeneration != MacroVisibilityGeneration && "don't need to update this macro name info"); @@ -156,14 +157,20 @@ void Preprocessor::updateModuleMacroInfo(IdentifierInfo *II, Worklist.push_back(O); } } + // Our reverse postorder walk found the macros in reverse order. + std::reverse(Info.ActiveModuleMacros.begin(), Info.ActiveModuleMacros.end()); // Determine whether the macro name is ambiguous. - Info.IsAmbiguous = false; MacroInfo *MI = nullptr; - bool IsSystemMacro = false; - if (auto *DMD = dyn_cast<DefMacroDirective>(Info.MD)) { - MI = DMD->getInfo(); - IsSystemMacro = SourceMgr.isInSystemHeader(DMD->getLocation()); + bool IsSystemMacro = true; + bool IsAmbiguous = false; + if (auto *MD = Info.MD) { + while (MD && isa<VisibilityMacroDirective>(MD)) + MD = MD->getPrevious(); + if (auto *DMD = dyn_cast_or_null<DefMacroDirective>(MD)) { + MI = DMD->getInfo(); + IsSystemMacro &= SourceMgr.isInSystemHeader(DMD->getLocation()); + } } for (auto *Active : Info.ActiveModuleMacros) { auto *NewMI = Active->getMacroInfo(); @@ -177,13 +184,14 @@ void Preprocessor::updateModuleMacroInfo(IdentifierInfo *II, // // FIXME: Remove the defined-in-system-headers check. clang's limits.h // overrides the system limits.h's macros, so there's no conflict here. - IsSystemMacro &= Active->getOwningModule()->IsSystem; - if (MI && NewMI != MI && !IsSystemMacro && - !MI->isIdenticalTo(*NewMI, *this, /*Syntactically=*/true)) { - Info.IsAmbiguous = true; - break; - } + if (MI && NewMI != MI && + !MI->isIdenticalTo(*NewMI, *this, /*Syntactically=*/true)) + IsAmbiguous = true; + IsSystemMacro &= Active->getOwningModule()->IsSystem || + SourceMgr.isInSystemHeader(NewMI->getDefinitionLoc()); + MI = NewMI; } + Info.IsAmbiguous = IsAmbiguous && !IsSystemMacro; } /// RegisterBuiltinMacro - Register the specified identifier in the identifier @@ -270,10 +278,11 @@ static bool isTrivialSingleTokenExpansion(const MacroInfo *MI, // If the identifier is a macro, and if that macro is enabled, it may be // expanded so it's not a trivial expansion. - if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled() && - // Fast expanding "#define X X" is ok, because X would be disabled. - II != MacroIdent) - return false; + if (auto *ExpansionMI = PP.getMacroInfo(II)) + if (PP.getMacroInfo(II)->isEnabled() && + // Fast expanding "#define X X" is ok, because X would be disabled. + II != MacroIdent) + return false; // If this is an object-like macro invocation, it is safe to trivially expand // it. @@ -336,10 +345,8 @@ 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, - MacroDirective *MD) { - MacroDirective::DefInfo Def = MD->getDefinition(); - assert(Def.isValid()); - MacroInfo *MI = Def.getMacroInfo(); + const MacroDefinition &M) { + MacroInfo *MI = M.getMacroInfo(); // 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 @@ -348,7 +355,8 @@ 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, MD, + // FIXME: Tell callbacks about module macros. + if (Callbacks) Callbacks->MacroExpands(Identifier, M.getLocalDirective(), Identifier.getLocation(), /*Args=*/nullptr); ExpandBuiltinMacro(Identifier); @@ -396,10 +404,13 @@ 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, MD, ExpansionRange)); + MacroExpandsInfo(Identifier, M.getLocalDirective(), ExpansionRange)); } else { - Callbacks->MacroExpands(Identifier, MD, ExpansionRange, Args); + // FIXME: Tell callbacks about module macros. + Callbacks->MacroExpands(Identifier, M.getLocalDirective(), ExpansionRange, + Args); if (!DelayedMacroExpandsCallbacks.empty()) { for (unsigned i=0, e = DelayedMacroExpandsCallbacks.size(); i!=e; ++i) { MacroExpandsInfo &Info = DelayedMacroExpandsCallbacks[i]; @@ -413,20 +424,16 @@ bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier, } // If the macro definition is ambiguous, complain. - if (Def.getDirective()->isAmbiguous()) { + if (M.isAmbiguous()) { Diag(Identifier, diag::warn_pp_ambiguous_macro) << Identifier.getIdentifierInfo(); Diag(MI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_chosen) << Identifier.getIdentifierInfo(); - for (MacroDirective::DefInfo PrevDef = Def.getPreviousDefinition(); - PrevDef && !PrevDef.isUndefined(); - PrevDef = PrevDef.getPreviousDefinition()) { - Diag(PrevDef.getMacroInfo()->getDefinitionLoc(), - diag::note_pp_ambiguous_macro_other) - << Identifier.getIdentifierInfo(); - if (!PrevDef.getDirective()->isAmbiguous()) - break; - } + M.forAllDefinitions([&](const MacroInfo *OtherMI) { + if (OtherMI != MI) + Diag(OtherMI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_other) + << Identifier.getIdentifierInfo(); + }); } // If we started lexing a macro, enter the macro expansion body. diff --git a/clang/lib/Lex/Pragma.cpp b/clang/lib/Lex/Pragma.cpp index 5625842145a..7093182239b 100644 --- a/clang/lib/Lex/Pragma.cpp +++ b/clang/lib/Lex/Pragma.cpp @@ -400,7 +400,7 @@ void Preprocessor::HandlePragmaPoison(Token &PoisonTok) { if (II->isPoisoned()) continue; // If this is a macro identifier, emit a warning. - if (II->hasMacroDefinition()) + if (isMacroDefined(II)) Diag(Tok, diag::pp_poisoning_existing_macro); // Finally, poison it! @@ -590,8 +590,7 @@ void Preprocessor::HandlePragmaPopMacro(Token &PopMacroTok) { PragmaPushMacroInfo.find(IdentInfo); if (iter != PragmaPushMacroInfo.end()) { // Forget the MacroInfo currently associated with IdentInfo. - if (MacroDirective *CurrentMD = getMacroDirective(IdentInfo)) { - MacroInfo *MI = CurrentMD->getMacroInfo(); + if (MacroInfo *MI = getMacroInfo(IdentInfo)) { if (MI->isWarnIfUnused()) WarnUnusedMacroLocs.erase(MI->getDefinitionLoc()); appendMacroDirective(IdentInfo, AllocateUndefMacroDirective(MessageLoc)); diff --git a/clang/lib/Lex/Preprocessor.cpp b/clang/lib/Lex/Preprocessor.cpp index 92ab2af1921..cd00be8adaf 100644 --- a/clang/lib/Lex/Preprocessor.cpp +++ b/clang/lib/Lex/Preprocessor.cpp @@ -73,8 +73,7 @@ Preprocessor::Preprocessor(IntrusiveRefCntPtr<PreprocessorOptions> PPOpts, ModuleImportExpectsIdentifier(false), CodeCompletionReached(0), MainFileDir(nullptr), SkipMainFilePreamble(0, true), CurPPLexer(nullptr), CurDirLookup(nullptr), CurLexerKind(CLK_Lexer), CurSubmodule(nullptr), - Callbacks(nullptr), MacroVisibilityGeneration(0), - MacroArgCache(nullptr), Record(nullptr), + Callbacks(nullptr), MacroArgCache(nullptr), Record(nullptr), MIChainHead(nullptr), DeserialMIChainHead(nullptr) { OwnsHeaderSearch = OwnsHeaders; @@ -108,6 +107,9 @@ Preprocessor::Preprocessor(IntrusiveRefCntPtr<PreprocessorOptions> PPOpts, // We haven't read anything from the external source. ReadMacrosFromExternalSource = false; + // We might already have some macros from an imported module (via a PCH or + // preamble) if modules is enabled. + MacroVisibilityGeneration = LangOpts.Modules ? 1 : 0; // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro. // This gets unpoisoned where it is allowed. @@ -623,8 +625,8 @@ bool Preprocessor::HandleIdentifier(Token &Identifier) { } // If this is a macro to be expanded, do it. - if (MacroDirective *MD = getMacroDirective(&II)) { - MacroInfo *MI = MD->getMacroInfo(); + if (MacroDefinition MD = getMacroDefinition(&II)) { + auto *MI = MD.getMacroInfo(); if (!DisableMacroExpansion) { if (!Identifier.isExpandDisabled() && MI->isEnabled()) { // C99 6.10.3p10: If the preprocessing token immediately after the |