diff --git a/clang-tools-extra/clang-include-fixer/find-all-symbols/FindAllMacros.h b/clang-tools-extra/clang-include-fixer/find-all-symbols/FindAllMacros.h --- a/clang-tools-extra/clang-include-fixer/find-all-symbols/FindAllMacros.h +++ b/clang-tools-extra/clang-include-fixer/find-all-symbols/FindAllMacros.h @@ -30,17 +30,17 @@ HeaderMapCollector *Collector = nullptr) : Reporter(Reporter), SM(SM), Collector(Collector) {} - void MacroDefined(const Token &MacroNameTok, + void MacroDefined(SourceLocation HashLoc, const Token &MacroNameTok, const MacroDirective *MD) override; void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD, SourceRange Range, const MacroArgs *Args) override; - void Ifdef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDefinition &MD) override; + void Ifdef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) override; - void Ifndef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDefinition &MD) override; + void Ifndef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) override; void EndOfMainFile() override; diff --git a/clang-tools-extra/clang-include-fixer/find-all-symbols/FindAllMacros.cpp b/clang-tools-extra/clang-include-fixer/find-all-symbols/FindAllMacros.cpp --- a/clang-tools-extra/clang-include-fixer/find-all-symbols/FindAllMacros.cpp +++ b/clang-tools-extra/clang-include-fixer/find-all-symbols/FindAllMacros.cpp @@ -32,7 +32,8 @@ SymbolInfo::SymbolKind::Macro, FilePath, {}); } -void FindAllMacros::MacroDefined(const Token &MacroNameTok, +void FindAllMacros::MacroDefined(SourceLocation HashLoc, + const Token &MacroNameTok, const MacroDirective *MD) { if (auto Symbol = CreateMacroSymbol(MacroNameTok, MD->getMacroInfo())) ++FileSymbols[*Symbol].Seen; @@ -51,12 +52,14 @@ MacroUsed(MacroNameTok, MD); } -void FindAllMacros::Ifdef(SourceLocation Loc, const Token &MacroNameTok, +void FindAllMacros::Ifdef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) { MacroUsed(MacroNameTok, MD); } -void FindAllMacros::Ifndef(SourceLocation Loc, const Token &MacroNameTok, +void FindAllMacros::Ifndef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) { MacroUsed(MacroNameTok, MD); } diff --git a/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.h b/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.h --- a/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.h +++ b/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.h @@ -103,22 +103,26 @@ void PragmaAssumeNonNullEnd(SourceLocation Loc) override; void MacroExpands(const Token &MacroNameTok, const MacroDefinition &, SourceRange Range, const MacroArgs *) override; - void MacroDefined(const Token &MacroNameTok, + void MacroDefined(SourceLocation HashLoc, const Token &MacroNameTok, const MacroDirective *MD) override; - void MacroUndefined(const Token &, const MacroDefinition &, + void MacroUndefined(SourceLocation HashLoc, const Token &, + const MacroDefinition &, const MacroDirective *Undef) override; void Defined(const Token &MacroNameTok, const MacroDefinition &, SourceRange Range) override; void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override; - void If(SourceLocation Loc, SourceRange, ConditionValueKind) override; - void Elif(SourceLocation Loc, SourceRange, ConditionValueKind, - SourceLocation) override; - void Ifdef(SourceLocation Loc, const Token &, + void If(SourceLocation HashLoc, SourceLocation Loc, SourceRange, + ConditionValueKind) override; + void Elif(SourceLocation HashLoc, SourceLocation Loc, SourceRange, + ConditionValueKind, SourceLocation) override; + void Ifdef(SourceLocation HashLoc, SourceLocation Loc, const Token &, const MacroDefinition &) override; - void Ifndef(SourceLocation Loc, const Token &, + void Ifndef(SourceLocation HashLoc, SourceLocation Loc, const Token &, const MacroDefinition &) override; - void Else(SourceLocation Loc, SourceLocation) override; - void Endif(SourceLocation Loc, SourceLocation) override; + void Else(SourceLocation HashLoc, SourceLocation Loc, + SourceLocation) override; + void Endif(SourceLocation HashLoc, SourceLocation Loc, + SourceLocation) override; std::unique_ptr Recorder; // Set of all the modules visited. Avoids processing a module more than once. diff --git a/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp b/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp --- a/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp +++ b/clang-tools-extra/clang-tidy/ExpandModularHeadersPPCallbacks.cpp @@ -261,12 +261,14 @@ // FIXME: Figure out whether it's the right location to parse to. parseToLocation(Range.getBegin()); } -void ExpandModularHeadersPPCallbacks::MacroDefined(const Token &MacroNameTok, +void ExpandModularHeadersPPCallbacks::MacroDefined(SourceLocation HashLoc, + const Token &MacroNameTok, const MacroDirective *MD) { parseToLocation(MD->getLocation()); } void ExpandModularHeadersPPCallbacks::MacroUndefined( - const Token &, const MacroDefinition &, const MacroDirective *Undef) { + SourceLocation HashLoc, const Token &, const MacroDefinition &, + const MacroDirective *Undef) { if (Undef) parseToLocation(Undef->getLocation()); } @@ -281,26 +283,32 @@ // FIXME: Figure out whether it's the right location to parse to. parseToLocation(EndifLoc); } -void ExpandModularHeadersPPCallbacks::If(SourceLocation Loc, SourceRange, +void ExpandModularHeadersPPCallbacks::If(SourceLocation HashLoc, + SourceLocation Loc, SourceRange, ConditionValueKind) { parseToLocation(Loc); } -void ExpandModularHeadersPPCallbacks::Elif(SourceLocation Loc, SourceRange, +void ExpandModularHeadersPPCallbacks::Elif(SourceLocation HashLoc, + SourceLocation Loc, SourceRange, ConditionValueKind, SourceLocation) { parseToLocation(Loc); } -void ExpandModularHeadersPPCallbacks::Ifdef(SourceLocation Loc, const Token &, +void ExpandModularHeadersPPCallbacks::Ifdef(SourceLocation HashLoc, + SourceLocation Loc, const Token &, const MacroDefinition &) { parseToLocation(Loc); } -void ExpandModularHeadersPPCallbacks::Ifndef(SourceLocation Loc, const Token &, +void ExpandModularHeadersPPCallbacks::Ifndef(SourceLocation HashLoc, + SourceLocation Loc, const Token &, const MacroDefinition &) { parseToLocation(Loc); } -void ExpandModularHeadersPPCallbacks::Else(SourceLocation Loc, SourceLocation) { +void ExpandModularHeadersPPCallbacks::Else(SourceLocation HashLoc, + SourceLocation Loc, SourceLocation) { parseToLocation(Loc); } -void ExpandModularHeadersPPCallbacks::Endif(SourceLocation Loc, +void ExpandModularHeadersPPCallbacks::Endif(SourceLocation HashLoc, + SourceLocation Loc, SourceLocation) { parseToLocation(Loc); } diff --git a/clang-tools-extra/clang-tidy/bugprone/MacroParenthesesCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/MacroParenthesesCheck.cpp --- a/clang-tools-extra/clang-tidy/bugprone/MacroParenthesesCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/MacroParenthesesCheck.cpp @@ -19,7 +19,7 @@ MacroParenthesesPPCallbacks(Preprocessor *PP, MacroParenthesesCheck *Check) : PP(PP), Check(Check) {} - void MacroDefined(const Token &MacroNameTok, + void MacroDefined(SourceLocation HashLoc, const Token &MacroNameTok, const MacroDirective *MD) override { replacementList(MacroNameTok, MD->getMacroInfo()); argument(MacroNameTok, MD->getMacroInfo()); diff --git a/clang-tools-extra/clang-tidy/cppcoreguidelines/MacroUsageCheck.cpp b/clang-tools-extra/clang-tidy/cppcoreguidelines/MacroUsageCheck.cpp --- a/clang-tools-extra/clang-tidy/cppcoreguidelines/MacroUsageCheck.cpp +++ b/clang-tools-extra/clang-tidy/cppcoreguidelines/MacroUsageCheck.cpp @@ -33,7 +33,7 @@ bool IgnoreCommandLine) : Check(Check), SM(SM), RegExp(RegExpStr), CheckCapsOnly(CapsOnly), IgnoreCommandLineMacros(IgnoreCommandLine) {} - void MacroDefined(const Token &MacroNameTok, + void MacroDefined(SourceLocation HashLoc, const Token &MacroNameTok, const MacroDirective *MD) override { if (SM.isWrittenInBuiltinFile(MD->getLocation()) || MD->getMacroInfo()->isUsedForHeaderGuard() || diff --git a/clang-tools-extra/clang-tidy/google/UpgradeGoogletestCaseCheck.cpp b/clang-tools-extra/clang-tidy/google/UpgradeGoogletestCaseCheck.cpp --- a/clang-tools-extra/clang-tidy/google/UpgradeGoogletestCaseCheck.cpp +++ b/clang-tools-extra/clang-tidy/google/UpgradeGoogletestCaseCheck.cpp @@ -52,13 +52,14 @@ macroUsed(MacroNameTok, MD, Range.getBegin(), CheckAction::Rename); } - void MacroUndefined(const Token &MacroNameTok, const MacroDefinition &MD, + void MacroUndefined(SourceLocation HashLoc, const Token &MacroNameTok, + const MacroDefinition &MD, const MacroDirective *Undef) override { if (Undef != nullptr) macroUsed(MacroNameTok, MD, Undef->getLocation(), CheckAction::Warn); } - void MacroDefined(const Token &MacroNameTok, + void MacroDefined(SourceLocation HashLoc, const Token &MacroNameTok, const MacroDirective *MD) override { if (!ReplacementFound && MD != nullptr) { // We check if the newly defined macro is one of the target replacements. @@ -76,13 +77,13 @@ macroUsed(MacroNameTok, MD, Range.getBegin(), CheckAction::Warn); } - void Ifdef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDefinition &MD) override { + void Ifdef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) override { macroUsed(MacroNameTok, MD, Loc, CheckAction::Warn); } - void Ifndef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDefinition &MD) override { + void Ifndef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) override { macroUsed(MacroNameTok, MD, Loc, CheckAction::Warn); } diff --git a/clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp b/clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp --- a/clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp @@ -124,11 +124,12 @@ } // Keep track of macro definitions that look like enums. - void MacroDefined(const Token &MacroNameTok, + void MacroDefined(SourceLocation HashLoc, const Token &MacroNameTok, const MacroDirective *MD) override; // Undefining an enum-like macro results in the enum set being dropped. - void MacroUndefined(const Token &MacroNameTok, const MacroDefinition &MD, + void MacroUndefined(SourceLocation HashLoc, const Token &MacroNameTok, + const MacroDefinition &MD, const MacroDirective *Undef) override; // Conditional compilation clears any adjacent enum-like macros. @@ -138,42 +139,45 @@ // #if !defined(GUARD) // or // #ifndef GUARD - void If(SourceLocation Loc, SourceRange ConditionRange, + void If(SourceLocation HashLoc, SourceLocation Loc, + SourceRange ConditionRange, ConditionValueKind ConditionValue) override { conditionStart(Loc); checkCondition(ConditionRange); } - void Ifndef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDefinition &MD) override { + void Ifndef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) override { conditionStart(Loc); checkName(MacroNameTok); } - void Ifdef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDefinition &MD) override { + void Ifdef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) override { conditionStart(Loc); checkName(MacroNameTok); } - void Elif(SourceLocation Loc, SourceRange ConditionRange, - ConditionValueKind ConditionValue, SourceLocation IfLoc) override { + void Elif(SourceLocation HashLoc, SourceLocation Loc, + SourceRange ConditionRange, ConditionValueKind ConditionValue, + SourceLocation IfLoc) override { checkCondition(ConditionRange); } - void Elifdef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDefinition &MD) override { + void Elifdef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) override { checkName(MacroNameTok); } - void Elifdef(SourceLocation Loc, SourceRange ConditionRange, - SourceLocation IfLoc) override { - PPCallbacks::Elifdef(Loc, ConditionRange, IfLoc); + void Elifdef(SourceLocation HashLoc, SourceLocation Loc, + SourceRange ConditionRange, SourceLocation IfLoc) override { + PPCallbacks::Elifdef(HashLoc, Loc, ConditionRange, IfLoc); } - void Elifndef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDefinition &MD) override { + void Elifndef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) override { checkName(MacroNameTok); } - void Elifndef(SourceLocation Loc, SourceRange ConditionRange, - SourceLocation IfLoc) override { - PPCallbacks::Elifndef(Loc, ConditionRange, IfLoc); + void Elifndef(SourceLocation HashLoc, SourceLocation Loc, + SourceRange ConditionRange, SourceLocation IfLoc) override { + PPCallbacks::Elifndef(HashLoc, Loc, ConditionRange, IfLoc); } - void Endif(SourceLocation Loc, SourceLocation IfLoc) override; + void Endif(SourceLocation HashLoc, SourceLocation Loc, + SourceLocation IfLoc) override; void PragmaDirective(SourceLocation Loc, PragmaIntroducerKind Introducer) override; @@ -332,7 +336,8 @@ // Any defined but rejected macro is scanned for identifiers that // are to be excluded as enums. -void MacroToEnumCallbacks::MacroDefined(const Token &MacroNameTok, +void MacroToEnumCallbacks::MacroDefined(SourceLocation HashLoc, + const Token &MacroNameTok, const MacroDirective *MD) { // Include guards are never candidates for becoming an enum. if (CurrentFile->GuardScanner == IncludeGuard::IfGuard) { @@ -366,7 +371,8 @@ // Any macro that is undefined removes all adjacent macros from consideration as // an enum and starts a new enum scan. -void MacroToEnumCallbacks::MacroUndefined(const Token &MacroNameTok, +void MacroToEnumCallbacks::MacroUndefined(SourceLocation HashLoc, + const Token &MacroNameTok, const MacroDefinition &MD, const MacroDirective *Undef) { rememberExpressionName(MacroNameTok); @@ -385,7 +391,8 @@ CurrentFile->GuardScanner = IncludeGuard::None; } -void MacroToEnumCallbacks::Endif(SourceLocation Loc, SourceLocation IfLoc) { +void MacroToEnumCallbacks::Endif(SourceLocation HashLoc, SourceLocation Loc, + SourceLocation IfLoc) { // The if directive for the include guard isn't counted in the // ConditionScopes. if (CurrentFile->ConditionScopes == 0 && diff --git a/clang-tools-extra/clang-tidy/readability/DuplicateIncludeCheck.cpp b/clang-tools-extra/clang-tidy/readability/DuplicateIncludeCheck.cpp --- a/clang-tools-extra/clang-tidy/readability/DuplicateIncludeCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/DuplicateIncludeCheck.cpp @@ -50,10 +50,11 @@ StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) override; - void MacroDefined(const Token &MacroNameTok, + void MacroDefined(SourceLocation HashLoc, const Token &MacroNameTok, const MacroDirective *MD) override; - void MacroUndefined(const Token &MacroNameTok, const MacroDefinition &MD, + void MacroUndefined(SourceLocation HashLoc, const Token &MacroNameTok, + const MacroDefinition &MD, const MacroDirective *Undef) override; private: @@ -91,12 +92,14 @@ Files.back().push_back(FileName); } -void DuplicateIncludeCallbacks::MacroDefined(const Token &MacroNameTok, +void DuplicateIncludeCallbacks::MacroDefined(SourceLocation HashLoc, + const Token &MacroNameTok, const MacroDirective *MD) { Files.back().clear(); } -void DuplicateIncludeCallbacks::MacroUndefined(const Token &MacroNameTok, +void DuplicateIncludeCallbacks::MacroUndefined(SourceLocation HashLoc, + const Token &MacroNameTok, const MacroDefinition &MD, const MacroDirective *Undef) { Files.back().clear(); diff --git a/clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp b/clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp --- a/clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp @@ -26,7 +26,8 @@ ElseAfterReturnCheck::ConditionalBranchMap &Collections, const SourceManager &SM) : Collections(Collections), SM(SM) {} - void Endif(SourceLocation Loc, SourceLocation IfLoc) override { + void Endif(SourceLocation HashLoc, SourceLocation Loc, + SourceLocation IfLoc) override { if (!SM.isWrittenInSameFile(Loc, IfLoc)) return; SmallVectorImpl &Collection = Collections[SM.getFileID(Loc)]; diff --git a/clang-tools-extra/clang-tidy/readability/RedundantPreprocessorCheck.cpp b/clang-tools-extra/clang-tidy/readability/RedundantPreprocessorCheck.cpp --- a/clang-tools-extra/clang-tidy/readability/RedundantPreprocessorCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/RedundantPreprocessorCheck.cpp @@ -34,7 +34,8 @@ Preprocessor &PP) : Check(Check), PP(PP) {} - void If(SourceLocation Loc, SourceRange ConditionRange, + void If(SourceLocation HashLoc, SourceLocation Loc, + SourceRange ConditionRange, ConditionValueKind ConditionValue) override { StringRef Condition = Lexer::getSourceText(CharSourceRange::getTokenRange(ConditionRange), @@ -42,7 +43,8 @@ checkMacroRedundancy(Loc, Condition, IfStack, DK_If, DK_If, true); } - void Ifdef(SourceLocation Loc, const Token &MacroNameTok, + void Ifdef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MacroDefinition) override { std::string MacroName = PP.getSpelling(MacroNameTok); checkMacroRedundancy(Loc, MacroName, IfdefStack, DK_Ifdef, DK_Ifdef, true); @@ -50,7 +52,8 @@ false); } - void Ifndef(SourceLocation Loc, const Token &MacroNameTok, + void Ifndef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MacroDefinition) override { std::string MacroName = PP.getSpelling(MacroNameTok); checkMacroRedundancy(Loc, MacroName, IfndefStack, DK_Ifndef, DK_Ifndef, @@ -59,7 +62,8 @@ false); } - void Endif(SourceLocation Loc, SourceLocation IfLoc) override { + void Endif(SourceLocation HashLoc, SourceLocation Loc, + SourceLocation IfLoc) override { if (!IfStack.empty() && IfLoc == IfStack.back().Loc) IfStack.pop_back(); if (!IfdefStack.empty() && IfLoc == IfdefStack.back().Loc) diff --git a/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp b/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp --- a/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp +++ b/clang-tools-extra/clang-tidy/utils/HeaderGuard.cpp @@ -42,8 +42,8 @@ } } - void Ifndef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDefinition &MD) override { + void Ifndef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) override { if (MD) return; @@ -52,14 +52,15 @@ std::make_pair(Loc, MacroNameTok.getLocation()); } - void MacroDefined(const Token &MacroNameTok, + void MacroDefined(SourceLocation HashLoc, const Token &MacroNameTok, const MacroDirective *MD) override { // Record all defined macros. We store the whole token to get info on the // name later. Macros.emplace_back(MacroNameTok, MD->getMacroInfo()); } - void Endif(SourceLocation Loc, SourceLocation IfLoc) override { + void Endif(SourceLocation HashLoc, SourceLocation Loc, + SourceLocation IfLoc) override { // Record all #endif and the corresponding #ifs (including #ifndefs). EndIfs[IfLoc] = Loc; } diff --git a/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp b/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp --- a/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp +++ b/clang-tools-extra/clang-tidy/utils/RenamerClangTidyCheck.cpp @@ -71,7 +71,7 @@ : PP(PP), Check(Check) {} /// MacroDefined calls checkMacro for macros in the main file - void MacroDefined(const Token &MacroNameTok, + void MacroDefined(SourceLocation HashLoc, const Token &MacroNameTok, const MacroDirective *MD) override { if (MD->getMacroInfo()->isBuiltinMacro()) return; diff --git a/clang-tools-extra/clangd/CollectMacros.h b/clang-tools-extra/clangd/CollectMacros.h --- a/clang-tools-extra/clangd/CollectMacros.h +++ b/clang-tools-extra/clangd/CollectMacros.h @@ -51,7 +51,8 @@ InMainFile = isInsideMainFile(Loc, SM); } - void MacroDefined(const Token &MacroName, const MacroDirective *MD) override { + void MacroDefined(SourceLocation HashLoc, const Token &MacroName, + const MacroDirective *MD) override { add(MacroName, MD->getMacroInfo(), /*IsDefinition=*/true); } @@ -60,19 +61,19 @@ add(MacroName, MD.getMacroInfo()); } - void MacroUndefined(const clang::Token &MacroName, + void MacroUndefined(SourceLocation HashLoc, const clang::Token &MacroName, const clang::MacroDefinition &MD, const clang::MacroDirective *Undef) override { add(MacroName, MD.getMacroInfo()); } - void Ifdef(SourceLocation Loc, const Token &MacroName, + void Ifdef(SourceLocation HashLoc, SourceLocation Loc, const Token &MacroName, const MacroDefinition &MD) override { add(MacroName, MD.getMacroInfo()); } - void Ifndef(SourceLocation Loc, const Token &MacroName, - const MacroDefinition &MD) override { + void Ifndef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroName, const MacroDefinition &MD) override { add(MacroName, MD.getMacroInfo()); } diff --git a/clang-tools-extra/clangd/Preamble.cpp b/clang-tools-extra/clangd/Preamble.cpp --- a/clang-tools-extra/clangd/Preamble.cpp +++ b/clang-tools-extra/clangd/Preamble.cpp @@ -269,7 +269,7 @@ InMainFile = SM.isWrittenInMainFile(Loc); } - void MacroDefined(const Token &MacroNameTok, + void MacroDefined(SourceLocation HashLoc, const Token &MacroNameTok, const MacroDirective *MD) override { if (!InMainFile) return; diff --git a/clang-tools-extra/include-cleaner/lib/Record.cpp b/clang-tools-extra/include-cleaner/lib/Record.cpp --- a/clang-tools-extra/include-cleaner/lib/Record.cpp +++ b/clang-tools-extra/include-cleaner/lib/Record.cpp @@ -59,7 +59,8 @@ recordMacroRef(MacroName, *MD.getMacroInfo()); } - void MacroDefined(const Token &MacroName, const MacroDirective *MD) override { + void MacroDefined(SourceLocation HashLoc, const Token &MacroName, + const MacroDirective *MD) override { if (!Active) return; @@ -78,7 +79,8 @@ } } - void MacroUndefined(const Token &MacroName, const MacroDefinition &MD, + void MacroUndefined(SourceLocation HashLoc, const Token &MacroName, + const MacroDefinition &MD, const MacroDirective *) override { if (!Active) return; @@ -86,16 +88,16 @@ recordMacroRef(MacroName, *MI); } - void Ifdef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDefinition &MD) override { + void Ifdef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) override { if (!Active) return; if (const auto *MI = MD.getMacroInfo()) recordMacroRef(MacroNameTok, *MI, RefType::Ambiguous); } - void Ifndef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDefinition &MD) override { + void Ifndef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) override { if (!Active) return; if (const auto *MI = MD.getMacroInfo()) @@ -104,15 +106,15 @@ using PPCallbacks::Elifdef; using PPCallbacks::Elifndef; - void Elifdef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDefinition &MD) override { + void Elifdef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) override { if (!Active) return; if (const auto *MI = MD.getMacroInfo()) recordMacroRef(MacroNameTok, *MI, RefType::Ambiguous); } - void Elifndef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDefinition &MD) override { + void Elifndef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) override { if (!Active) return; if (const auto *MI = MD.getMacroInfo()) diff --git a/clang-tools-extra/modularize/PreprocessorTracker.cpp b/clang-tools-extra/modularize/PreprocessorTracker.cpp --- a/clang-tools-extra/modularize/PreprocessorTracker.cpp +++ b/clang-tools-extra/modularize/PreprocessorTracker.cpp @@ -749,14 +749,18 @@ void Defined(const clang::Token &MacroNameTok, const clang::MacroDefinition &MD, clang::SourceRange Range) override; - void If(clang::SourceLocation Loc, clang::SourceRange ConditionRange, + void If(clang::SourceLocation HashLoc, clang::SourceLocation Loc, + clang::SourceRange ConditionRange, clang::PPCallbacks::ConditionValueKind ConditionResult) override; - void Elif(clang::SourceLocation Loc, clang::SourceRange ConditionRange, + void Elif(clang::SourceLocation HashLoc, clang::SourceLocation Loc, + clang::SourceRange ConditionRange, clang::PPCallbacks::ConditionValueKind ConditionResult, clang::SourceLocation IfLoc) override; - void Ifdef(clang::SourceLocation Loc, const clang::Token &MacroNameTok, + void Ifdef(clang::SourceLocation HashLoc, clang::SourceLocation Loc, + const clang::Token &MacroNameTok, const clang::MacroDefinition &MD) override; - void Ifndef(clang::SourceLocation Loc, const clang::Token &MacroNameTok, + void Ifndef(clang::SourceLocation HashLoc, clang::SourceLocation Loc, + const clang::Token &MacroNameTok, const clang::MacroDefinition &MD) override; private: @@ -1340,26 +1344,29 @@ (MI ? "true" : "false"), PPTracker.getCurrentInclusionPathHandle()); } -void PreprocessorCallbacks::If(clang::SourceLocation Loc, - clang::SourceRange ConditionRange, - clang::PPCallbacks::ConditionValueKind ConditionResult) { +void PreprocessorCallbacks::If( + clang::SourceLocation HashLoc, clang::SourceLocation Loc, + clang::SourceRange ConditionRange, + clang::PPCallbacks::ConditionValueKind ConditionResult) { std::string Unexpanded(getSourceString(PP, ConditionRange)); PPTracker.addConditionalExpansionInstance( PP, PPTracker.getCurrentHeaderHandle(), Loc, clang::tok::pp_if, ConditionResult, Unexpanded, PPTracker.getCurrentInclusionPathHandle()); } -void PreprocessorCallbacks::Elif(clang::SourceLocation Loc, - clang::SourceRange ConditionRange, - clang::PPCallbacks::ConditionValueKind ConditionResult, - clang::SourceLocation IfLoc) { +void PreprocessorCallbacks::Elif( + clang::SourceLocation HashLoc, clang::SourceLocation Loc, + clang::SourceRange ConditionRange, + clang::PPCallbacks::ConditionValueKind ConditionResult, + clang::SourceLocation IfLoc) { std::string Unexpanded(getSourceString(PP, ConditionRange)); PPTracker.addConditionalExpansionInstance( PP, PPTracker.getCurrentHeaderHandle(), Loc, clang::tok::pp_elif, ConditionResult, Unexpanded, PPTracker.getCurrentInclusionPathHandle()); } -void PreprocessorCallbacks::Ifdef(clang::SourceLocation Loc, +void PreprocessorCallbacks::Ifdef(clang::SourceLocation HashLoc, + clang::SourceLocation Loc, const clang::Token &MacroNameTok, const clang::MacroDefinition &MD) { clang::PPCallbacks::ConditionValueKind IsDefined = @@ -1370,7 +1377,8 @@ PPTracker.getCurrentInclusionPathHandle()); } -void PreprocessorCallbacks::Ifndef(clang::SourceLocation Loc, +void PreprocessorCallbacks::Ifndef(clang::SourceLocation HashLoc, + clang::SourceLocation Loc, const clang::Token &MacroNameTok, const clang::MacroDefinition &MD) { clang::PPCallbacks::ConditionValueKind IsNotDefined = diff --git a/clang-tools-extra/pp-trace/PPCallbacksTracker.h b/clang-tools-extra/pp-trace/PPCallbacksTracker.h --- a/clang-tools-extra/pp-trace/PPCallbacksTracker.h +++ b/clang-tools-extra/pp-trace/PPCallbacksTracker.h @@ -127,23 +127,28 @@ void PragmaExecCharsetPop(SourceLocation Loc) override; void MacroExpands(const Token &MacroNameTok, const MacroDefinition &MD, SourceRange Range, const MacroArgs *Args) override; - void MacroDefined(const Token &MacroNameTok, + void MacroDefined(SourceLocation HashLoc, const Token &MacroNameTok, const MacroDirective *MD) override; - void MacroUndefined(const Token &MacroNameTok, const MacroDefinition &MD, + void MacroUndefined(SourceLocation HashLoc, const Token &MacroNameTok, + const MacroDefinition &MD, const MacroDirective *Undef) override; void Defined(const Token &MacroNameTok, const MacroDefinition &MD, SourceRange Range) override; void SourceRangeSkipped(SourceRange Range, SourceLocation EndifLoc) override; - void If(SourceLocation Loc, SourceRange ConditionRange, + void If(SourceLocation HashLoc, SourceLocation Loc, + SourceRange ConditionRange, ConditionValueKind ConditionValue) override; - void Elif(SourceLocation Loc, SourceRange ConditionRange, - ConditionValueKind ConditionValue, SourceLocation IfLoc) override; - void Ifdef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDefinition &MD) override; - void Ifndef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDefinition &MD) override; - void Else(SourceLocation Loc, SourceLocation IfLoc) override; - void Endif(SourceLocation Loc, SourceLocation IfLoc) override; + void Elif(SourceLocation HashLoc, SourceLocation Loc, + SourceRange ConditionRange, ConditionValueKind ConditionValue, + SourceLocation IfLoc) override; + void Ifdef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) override; + void Ifndef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) override; + void Else(SourceLocation HashLoc, SourceLocation Loc, + SourceLocation IfLoc) override; + void Endif(SourceLocation HashLoc, SourceLocation Loc, + SourceLocation IfLoc) override; // Helper functions. diff --git a/clang-tools-extra/pp-trace/PPCallbacksTracker.cpp b/clang-tools-extra/pp-trace/PPCallbacksTracker.cpp --- a/clang-tools-extra/pp-trace/PPCallbacksTracker.cpp +++ b/clang-tools-extra/pp-trace/PPCallbacksTracker.cpp @@ -325,7 +325,8 @@ } // Hook called whenever a macro definition is seen. -void PPCallbacksTracker::MacroDefined(const Token &MacroNameTok, +void PPCallbacksTracker::MacroDefined(SourceLocation HashLoc, + const Token &MacroNameTok, const MacroDirective *MacroDirective) { beginCallback("MacroDefined"); appendArgument("MacroNameTok", MacroNameTok); @@ -333,7 +334,8 @@ } // Hook called whenever a macro #undef is seen. -void PPCallbacksTracker::MacroUndefined(const Token &MacroNameTok, +void PPCallbacksTracker::MacroUndefined(SourceLocation HashLoc, + const Token &MacroNameTok, const MacroDefinition &MacroDefinition, const MacroDirective *Undef) { beginCallback("MacroUndefined"); @@ -359,7 +361,8 @@ } // Hook called whenever an #if is seen. -void PPCallbacksTracker::If(SourceLocation Loc, SourceRange ConditionRange, +void PPCallbacksTracker::If(SourceLocation HashLoc, SourceLocation Loc, + SourceRange ConditionRange, ConditionValueKind ConditionValue) { beginCallback("If"); appendArgument("Loc", Loc); @@ -368,7 +371,8 @@ } // Hook called whenever an #elif is seen. -void PPCallbacksTracker::Elif(SourceLocation Loc, SourceRange ConditionRange, +void PPCallbacksTracker::Elif(SourceLocation HashLoc, SourceLocation Loc, + SourceRange ConditionRange, ConditionValueKind ConditionValue, SourceLocation IfLoc) { beginCallback("Elif"); @@ -379,7 +383,8 @@ } // Hook called whenever an #ifdef is seen. -void PPCallbacksTracker::Ifdef(SourceLocation Loc, const Token &MacroNameTok, +void PPCallbacksTracker::Ifdef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MacroDefinition) { beginCallback("Ifdef"); appendArgument("Loc", Loc); @@ -388,7 +393,8 @@ } // Hook called whenever an #ifndef is seen. -void PPCallbacksTracker::Ifndef(SourceLocation Loc, const Token &MacroNameTok, +void PPCallbacksTracker::Ifndef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MacroDefinition) { beginCallback("Ifndef"); appendArgument("Loc", Loc); @@ -397,14 +403,16 @@ } // Hook called whenever an #else is seen. -void PPCallbacksTracker::Else(SourceLocation Loc, SourceLocation IfLoc) { +void PPCallbacksTracker::Else(SourceLocation HashLoc, SourceLocation Loc, + SourceLocation IfLoc) { beginCallback("Else"); appendArgument("Loc", Loc); appendArgument("IfLoc", IfLoc); } // Hook called whenever an #endif is seen. -void PPCallbacksTracker::Endif(SourceLocation Loc, SourceLocation IfLoc) { +void PPCallbacksTracker::Endif(SourceLocation HashLoc, SourceLocation Loc, + SourceLocation IfLoc) { beginCallback("Endif"); appendArgument("Loc", Loc); appendArgument("IfLoc", IfLoc); diff --git a/clang/include/clang/Lex/PPCallbacks.h b/clang/include/clang/Lex/PPCallbacks.h --- a/clang/include/clang/Lex/PPCallbacks.h +++ b/clang/include/clang/Lex/PPCallbacks.h @@ -299,20 +299,19 @@ const MacroArgs *Args) {} /// Hook called whenever a macro definition is seen. - virtual void MacroDefined(const Token &MacroNameTok, - const MacroDirective *MD) { - } + virtual void MacroDefined(SourceLocation HashLoc, const Token &MacroNameTok, + const MacroDirective *MD) {} /// Hook called whenever a macro \#undef is seen. + /// \param HashLoc The location of the '#' that starts the directive. /// \param MacroNameTok The active Token /// \param MD A MacroDefinition for the named macro. /// \param Undef New MacroDirective if the macro was defined, null otherwise. /// /// MD is released immediately following this callback. - virtual void MacroUndefined(const Token &MacroNameTok, + virtual void MacroUndefined(SourceLocation HashLoc, const Token &MacroNameTok, const MacroDefinition &MD, - const MacroDirective *Undef) { - } + const MacroDirective *Undef) {} /// Hook called whenever the 'defined' operator is seen. /// \param MD The MacroDirective if the name was a macro, null otherwise. @@ -340,84 +339,88 @@ }; /// Hook called whenever an \#if is seen. + /// \param HashLoc The location of the '#' that starts the directive. /// \param Loc the source location of the directive. /// \param ConditionRange The SourceRange of the expression being tested. /// \param ConditionValue The evaluated value of the condition. /// // FIXME: better to pass in a list (or tree!) of Tokens. - virtual void If(SourceLocation Loc, SourceRange ConditionRange, - ConditionValueKind ConditionValue) { - } + virtual void If(SourceLocation HashLoc, SourceLocation Loc, + SourceRange ConditionRange, + ConditionValueKind ConditionValue) {} /// Hook called whenever an \#elif is seen. + /// \param HashLoc The location of the '#' that starts the directive. /// \param Loc the source location of the directive. /// \param ConditionRange The SourceRange of the expression being tested. /// \param ConditionValue The evaluated value of the condition. /// \param IfLoc the source location of the \#if/\#ifdef/\#ifndef directive. // FIXME: better to pass in a list (or tree!) of Tokens. - virtual void Elif(SourceLocation Loc, SourceRange ConditionRange, - ConditionValueKind ConditionValue, SourceLocation IfLoc) { - } + virtual void Elif(SourceLocation HashLoc, SourceLocation Loc, + SourceRange ConditionRange, + ConditionValueKind ConditionValue, SourceLocation IfLoc) {} /// Hook called whenever an \#ifdef is seen. + /// \param HashLoc The location of the '#' that starts the directive. /// \param Loc the source location of the directive. /// \param MacroNameTok Information on the token being tested. /// \param MD The MacroDefinition if the name was a macro, null otherwise. - virtual void Ifdef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDefinition &MD) { - } + virtual void Ifdef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) {} /// Hook called whenever an \#elifdef branch is taken. + /// \param HashLoc The location of the '#' that starts the directive. /// \param Loc the source location of the directive. /// \param MacroNameTok Information on the token being tested. /// \param MD The MacroDefinition if the name was a macro, null otherwise. - virtual void Elifdef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDefinition &MD) { - } + virtual void Elifdef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) {} /// Hook called whenever an \#elifdef is skipped. + /// \param HashLoc The location of the '#' that starts the directive. /// \param Loc the source location of the directive. /// \param ConditionRange The SourceRange of the expression being tested. /// \param IfLoc the source location of the \#if/\#ifdef/\#ifndef directive. // FIXME: better to pass in a list (or tree!) of Tokens. - virtual void Elifdef(SourceLocation Loc, SourceRange ConditionRange, - SourceLocation IfLoc) { - } + virtual void Elifdef(SourceLocation HashLoc, SourceLocation Loc, + SourceRange ConditionRange, SourceLocation IfLoc) {} /// Hook called whenever an \#ifndef is seen. + /// \param HashLoc The location of the '#' that starts the directive. /// \param Loc the source location of the directive. /// \param MacroNameTok Information on the token being tested. /// \param MD The MacroDefiniton if the name was a macro, null otherwise. - virtual void Ifndef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDefinition &MD) { - } + virtual void Ifndef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) {} /// Hook called whenever an \#elifndef branch is taken. + /// \param HashLoc The location of the '#' that starts the directive. /// \param Loc the source location of the directive. /// \param MacroNameTok Information on the token being tested. /// \param MD The MacroDefinition if the name was a macro, null otherwise. - virtual void Elifndef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDefinition &MD) { - } + virtual void Elifndef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) {} /// Hook called whenever an \#elifndef is skipped. + /// \param HashLoc The location of the '#' that starts the directive. /// \param Loc the source location of the directive. /// \param ConditionRange The SourceRange of the expression being tested. /// \param IfLoc the source location of the \#if/\#ifdef/\#ifndef directive. // FIXME: better to pass in a list (or tree!) of Tokens. - virtual void Elifndef(SourceLocation Loc, SourceRange ConditionRange, - SourceLocation IfLoc) { - } + virtual void Elifndef(SourceLocation HashLoc, SourceLocation Loc, + SourceRange ConditionRange, SourceLocation IfLoc) {} /// Hook called whenever an \#else is seen. + /// \param HashLoc The location of the '#' that starts the directive. /// \param Loc the source location of the directive. /// \param IfLoc the source location of the \#if/\#ifdef/\#ifndef directive. - virtual void Else(SourceLocation Loc, SourceLocation IfLoc) { - } + virtual void Else(SourceLocation HashLoc, SourceLocation Loc, + SourceLocation IfLoc) {} /// Hook called whenever an \#endif is seen. + /// \param HashLoc The location of the '#' that starts the directive. /// \param Loc the source location of the directive. /// \param IfLoc the source location of the \#if/\#ifdef/\#ifndef directive. - virtual void Endif(SourceLocation Loc, SourceLocation IfLoc) { - } + virtual void Endif(SourceLocation HashLoc, SourceLocation Loc, + SourceLocation IfLoc) {} }; /// Simple wrapper class for chaining callbacks. @@ -595,17 +598,17 @@ Second->MacroExpands(MacroNameTok, MD, Range, Args); } - void MacroDefined(const Token &MacroNameTok, + void MacroDefined(SourceLocation HashLoc, const Token &MacroNameTok, const MacroDirective *MD) override { - First->MacroDefined(MacroNameTok, MD); - Second->MacroDefined(MacroNameTok, MD); + First->MacroDefined(HashLoc, MacroNameTok, MD); + Second->MacroDefined(HashLoc, MacroNameTok, MD); } - void MacroUndefined(const Token &MacroNameTok, + void MacroUndefined(SourceLocation HashLoc, const Token &MacroNameTok, const MacroDefinition &MD, const MacroDirective *Undef) override { - First->MacroUndefined(MacroNameTok, MD, Undef); - Second->MacroUndefined(MacroNameTok, MD, Undef); + First->MacroUndefined(HashLoc, MacroNameTok, MD, Undef); + Second->MacroUndefined(HashLoc, MacroNameTok, MD, Undef); } void Defined(const Token &MacroNameTok, const MacroDefinition &MD, @@ -620,69 +623,73 @@ } /// Hook called whenever an \#if is seen. - void If(SourceLocation Loc, SourceRange ConditionRange, + void If(SourceLocation HashLoc, SourceLocation Loc, + SourceRange ConditionRange, ConditionValueKind ConditionValue) override { - First->If(Loc, ConditionRange, ConditionValue); - Second->If(Loc, ConditionRange, ConditionValue); + First->If(HashLoc, Loc, ConditionRange, ConditionValue); + Second->If(HashLoc, Loc, ConditionRange, ConditionValue); } /// Hook called whenever an \#elif is seen. - void Elif(SourceLocation Loc, SourceRange ConditionRange, - ConditionValueKind ConditionValue, SourceLocation IfLoc) override { - First->Elif(Loc, ConditionRange, ConditionValue, IfLoc); - Second->Elif(Loc, ConditionRange, ConditionValue, IfLoc); + void Elif(SourceLocation HashLoc, SourceLocation Loc, + SourceRange ConditionRange, ConditionValueKind ConditionValue, + SourceLocation IfLoc) override { + First->Elif(HashLoc, Loc, ConditionRange, ConditionValue, IfLoc); + Second->Elif(HashLoc, Loc, ConditionRange, ConditionValue, IfLoc); } /// Hook called whenever an \#ifdef is seen. - void Ifdef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDefinition &MD) override { - First->Ifdef(Loc, MacroNameTok, MD); - Second->Ifdef(Loc, MacroNameTok, MD); + void Ifdef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) override { + First->Ifdef(HashLoc, Loc, MacroNameTok, MD); + Second->Ifdef(HashLoc, Loc, MacroNameTok, MD); } /// Hook called whenever an \#elifdef is taken. - void Elifdef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDefinition &MD) override { - First->Elifdef(Loc, MacroNameTok, MD); - Second->Elifdef(Loc, MacroNameTok, MD); + void Elifdef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) override { + First->Elifdef(HashLoc, Loc, MacroNameTok, MD); + Second->Elifdef(HashLoc, Loc, MacroNameTok, MD); } /// Hook called whenever an \#elifdef is skipped. - void Elifdef(SourceLocation Loc, SourceRange ConditionRange, - SourceLocation IfLoc) override { - First->Elifdef(Loc, ConditionRange, IfLoc); - Second->Elifdef(Loc, ConditionRange, IfLoc); + void Elifdef(SourceLocation HashLoc, SourceLocation Loc, + SourceRange ConditionRange, SourceLocation IfLoc) override { + First->Elifdef(HashLoc, Loc, ConditionRange, IfLoc); + Second->Elifdef(HashLoc, Loc, ConditionRange, IfLoc); } /// Hook called whenever an \#ifndef is seen. - void Ifndef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDefinition &MD) override { - First->Ifndef(Loc, MacroNameTok, MD); - Second->Ifndef(Loc, MacroNameTok, MD); + void Ifndef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) override { + First->Ifndef(HashLoc, Loc, MacroNameTok, MD); + Second->Ifndef(HashLoc, Loc, MacroNameTok, MD); } /// Hook called whenever an \#elifndef is taken. - void Elifndef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDefinition &MD) override { - First->Elifndef(Loc, MacroNameTok, MD); - Second->Elifndef(Loc, MacroNameTok, MD); + void Elifndef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) override { + First->Elifndef(HashLoc, Loc, MacroNameTok, MD); + Second->Elifndef(HashLoc, Loc, MacroNameTok, MD); } /// Hook called whenever an \#elifndef is skipped. - void Elifndef(SourceLocation Loc, SourceRange ConditionRange, - SourceLocation IfLoc) override { - First->Elifndef(Loc, ConditionRange, IfLoc); - Second->Elifndef(Loc, ConditionRange, IfLoc); + void Elifndef(SourceLocation HashLoc, SourceLocation Loc, + SourceRange ConditionRange, SourceLocation IfLoc) override { + First->Elifndef(HashLoc, Loc, ConditionRange, IfLoc); + Second->Elifndef(HashLoc, Loc, ConditionRange, IfLoc); } /// Hook called whenever an \#else is seen. - void Else(SourceLocation Loc, SourceLocation IfLoc) override { - First->Else(Loc, IfLoc); - Second->Else(Loc, IfLoc); + void Else(SourceLocation HashLoc, SourceLocation Loc, + SourceLocation IfLoc) override { + First->Else(HashLoc, Loc, IfLoc); + Second->Else(HashLoc, Loc, IfLoc); } /// Hook called whenever an \#endif is seen. - void Endif(SourceLocation Loc, SourceLocation IfLoc) override { - First->Endif(Loc, IfLoc); - Second->Endif(Loc, IfLoc); + void Endif(SourceLocation HashLoc, SourceLocation Loc, + SourceLocation IfLoc) override { + First->Endif(HashLoc, Loc, IfLoc); + Second->Endif(HashLoc, Loc, IfLoc); } }; diff --git a/clang/include/clang/Lex/PPConditionalDirectiveRecord.h b/clang/include/clang/Lex/PPConditionalDirectiveRecord.h --- a/clang/include/clang/Lex/PPConditionalDirectiveRecord.h +++ b/clang/include/clang/Lex/PPConditionalDirectiveRecord.h @@ -85,24 +85,28 @@ SourceLocation findConditionalDirectiveRegionLoc(SourceLocation Loc) const; private: - void If(SourceLocation Loc, SourceRange ConditionRange, + void If(SourceLocation HashLoc, SourceLocation Loc, + SourceRange ConditionRange, ConditionValueKind ConditionValue) override; - void Elif(SourceLocation Loc, SourceRange ConditionRange, - ConditionValueKind ConditionValue, SourceLocation IfLoc) override; - void Ifdef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDefinition &MD) override; - void Ifndef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDefinition &MD) override; - void Elifdef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDefinition &MD) override; - void Elifdef(SourceLocation Loc, SourceRange ConditionRange, - SourceLocation IfLoc) override; - void Elifndef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDefinition &MD) override; - void Elifndef(SourceLocation Loc, SourceRange ConditionRange, - SourceLocation IfLoc) override; - void Else(SourceLocation Loc, SourceLocation IfLoc) override; - void Endif(SourceLocation Loc, SourceLocation IfLoc) override; + void Elif(SourceLocation HashLoc, SourceLocation Loc, + SourceRange ConditionRange, ConditionValueKind ConditionValue, + SourceLocation IfLoc) override; + void Ifdef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) override; + void Ifndef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) override; + void Elifdef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) override; + void Elifdef(SourceLocation HashLoc, SourceLocation Loc, + SourceRange ConditionRange, SourceLocation IfLoc) override; + void Elifndef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) override; + void Elifndef(SourceLocation HashLoc, SourceLocation Loc, + SourceRange ConditionRange, SourceLocation IfLoc) override; + void Else(SourceLocation HashLoc, SourceLocation Loc, + SourceLocation IfLoc) override; + void Endif(SourceLocation HashLoc, SourceLocation Loc, + SourceLocation IfLoc) override; }; } // end namespace clang diff --git a/clang/include/clang/Lex/PreprocessingRecord.h b/clang/include/clang/Lex/PreprocessingRecord.h --- a/clang/include/clang/Lex/PreprocessingRecord.h +++ b/clang/include/clang/Lex/PreprocessingRecord.h @@ -522,8 +522,10 @@ 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 MacroDefinition &MD, + void MacroDefined(SourceLocation HashLoc, const Token &Id, + const MacroDirective *MD) override; + void MacroUndefined(SourceLocation HashLoc, const Token &Id, + const MacroDefinition &MD, const MacroDirective *Undef) override; void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, StringRef FileName, bool IsAngled, @@ -531,16 +533,17 @@ OptionalFileEntryRef File, StringRef SearchPath, StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) override; - void Ifdef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDefinition &MD) override; - void Ifndef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDefinition &MD) override; + void Ifdef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) override; + void Ifndef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) override; using PPCallbacks::Elifdef; using PPCallbacks::Elifndef; - void Elifdef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDefinition &MD) override; - void Elifndef(SourceLocation Loc, const Token &MacroNameTok, + void Elifdef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) override; + void Elifndef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) override; /// Hook called whenever the 'defined' operator is seen. diff --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h --- a/clang/include/clang/Lex/Preprocessor.h +++ b/clang/include/clang/Lex/Preprocessor.h @@ -2597,15 +2597,16 @@ void replayPreambleConditionalStack(); // Macro handling. - void HandleDefineDirective(Token &Tok, bool ImmediatelyAfterHeaderGuard); - void HandleUndefDirective(); + void HandleDefineDirective(SourceLocation HashLoc, Token &Tok, + bool ImmediatelyAfterHeaderGuard); + void HandleUndefDirective(SourceLocation HashLoc); // Conditional Inclusion. void HandleIfdefDirective(Token &Result, const Token &HashToken, bool isIfndef, bool ReadAnyTokensBeforeDirective); void HandleIfDirective(Token &IfToken, const Token &HashToken, bool ReadAnyTokensBeforeDirective); - void HandleEndifDirective(Token &EndifToken); + void HandleEndifDirective(Token &EndifToken, const Token &HashToken); void HandleElseDirective(Token &Result, const Token &HashToken); void HandleElifFamilyDirective(Token &ElifToken, const Token &HashToken, tok::PPKeywordKind Kind); diff --git a/clang/lib/CodeGen/MacroPPCallbacks.h b/clang/lib/CodeGen/MacroPPCallbacks.h --- a/clang/lib/CodeGen/MacroPPCallbacks.h +++ b/clang/lib/CodeGen/MacroPPCallbacks.h @@ -106,13 +106,14 @@ SrcMgr::CharacteristicKind FileType) override; /// Hook called whenever a macro definition is seen. - void MacroDefined(const Token &MacroNameTok, + void MacroDefined(SourceLocation HashLoc, const Token &MacroNameTok, const MacroDirective *MD) override; /// Hook called whenever a macro \#undef is seen. /// /// MD is released immediately following this callback. - void MacroUndefined(const Token &MacroNameTok, const MacroDefinition &MD, + void MacroUndefined(SourceLocation HashLoc, const Token &MacroNameTok, + const MacroDefinition &MD, const MacroDirective *Undef) override; }; diff --git a/clang/lib/CodeGen/MacroPPCallbacks.cpp b/clang/lib/CodeGen/MacroPPCallbacks.cpp --- a/clang/lib/CodeGen/MacroPPCallbacks.cpp +++ b/clang/lib/CodeGen/MacroPPCallbacks.cpp @@ -175,7 +175,8 @@ LastHashLoc = HashLoc; } -void MacroPPCallbacks::MacroDefined(const Token &MacroNameTok, +void MacroPPCallbacks::MacroDefined(SourceLocation HashLoc, + const Token &MacroNameTok, const MacroDirective *MD) { IdentifierInfo *Id = MacroNameTok.getIdentifierInfo(); SourceLocation location = getCorrectLocation(MacroNameTok.getLocation()); @@ -188,7 +189,8 @@ Name.str(), Value.str()); } -void MacroPPCallbacks::MacroUndefined(const Token &MacroNameTok, +void MacroPPCallbacks::MacroUndefined(SourceLocation HashLoc, + const Token &MacroNameTok, const MacroDefinition &MD, const MacroDirective *Undef) { IdentifierInfo *Id = MacroNameTok.getIdentifierInfo(); diff --git a/clang/lib/ExtractAPI/ExtractAPIConsumer.cpp b/clang/lib/ExtractAPI/ExtractAPIConsumer.cpp --- a/clang/lib/ExtractAPI/ExtractAPIConsumer.cpp +++ b/clang/lib/ExtractAPI/ExtractAPIConsumer.cpp @@ -241,7 +241,7 @@ Preprocessor &PP) : SM(SM), LCF(LCF), API(API), PP(PP) {} - void MacroDefined(const Token &MacroNameToken, + void MacroDefined(SourceLocation HashLoc, const Token &MacroNameToken, const MacroDirective *MD) override { auto *MacroInfo = MD->getMacroInfo(); @@ -259,7 +259,8 @@ // If a macro gets undefined at some point during preprocessing of the inputs // it means that it isn't an exposed API and we should therefore not add a // macro definition for it. - void MacroUndefined(const Token &MacroNameToken, const MacroDefinition &MD, + void MacroUndefined(SourceLocation HashLoc, const Token &MacroNameToken, + const MacroDefinition &MD, const MacroDirective *Undef) override { // If this macro wasn't previously defined we don't need to do anything // here. diff --git a/clang/lib/Frontend/ASTUnit.cpp b/clang/lib/Frontend/ASTUnit.cpp --- a/clang/lib/Frontend/ASTUnit.cpp +++ b/clang/lib/Frontend/ASTUnit.cpp @@ -909,7 +909,7 @@ public: explicit MacroDefinitionTrackerPPCallbacks(unsigned &Hash) : Hash(Hash) {} - void MacroDefined(const Token &MacroNameTok, + void MacroDefined(SourceLocation HashLoc, const Token &MacroNameTok, const MacroDirective *MD) override { AddDefinedMacroToHash(MacroNameTok, Hash); } diff --git a/clang/lib/Frontend/PrintPreprocessedOutput.cpp b/clang/lib/Frontend/PrintPreprocessedOutput.cpp --- a/clang/lib/Frontend/PrintPreprocessedOutput.cpp +++ b/clang/lib/Frontend/PrintPreprocessedOutput.cpp @@ -215,11 +215,11 @@ void HandleNewlinesInToken(const char *TokStr, unsigned Len); /// MacroDefined - This hook is called whenever a macro definition is seen. - void MacroDefined(const Token &MacroNameTok, + void MacroDefined(SourceLocation HashLoc, const Token &MacroNameTok, const MacroDirective *MD) override; /// MacroUndefined - This hook is called whenever a macro #undef is seen. - void MacroUndefined(const Token &MacroNameTok, + void MacroUndefined(SourceLocation HashLoc, const Token &MacroNameTok, const MacroDefinition &MD, const MacroDirective *Undef) override; @@ -460,7 +460,8 @@ } /// MacroDefined - This hook is called whenever a macro definition is seen. -void PrintPPOutputPPCallbacks::MacroDefined(const Token &MacroNameTok, +void PrintPPOutputPPCallbacks::MacroDefined(SourceLocation HashLoc, + const Token &MacroNameTok, const MacroDirective *MD) { const MacroInfo *MI = MD->getMacroInfo(); // Print out macro definitions in -dD mode and when we have -fdirectives-only @@ -482,7 +483,8 @@ setEmittedDirectiveOnThisLine(); } -void PrintPPOutputPPCallbacks::MacroUndefined(const Token &MacroNameTok, +void PrintPPOutputPPCallbacks::MacroUndefined(SourceLocation HashLoc, + const Token &MacroNameTok, const MacroDefinition &MD, const MacroDirective *Undef) { // Print out macro definitions in -dD mode and when we have -fdirectives-only diff --git a/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp b/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp --- a/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp +++ b/clang/lib/Frontend/Rewrite/InclusionRewriter.cpp @@ -77,10 +77,12 @@ OptionalFileEntryRef File, StringRef SearchPath, StringRef RelativePath, const Module *Imported, SrcMgr::CharacteristicKind FileType) override; - void If(SourceLocation Loc, SourceRange ConditionRange, + void If(SourceLocation HashLoc, SourceLocation Loc, + SourceRange ConditionRange, ConditionValueKind ConditionValue) override; - void Elif(SourceLocation Loc, SourceRange ConditionRange, - ConditionValueKind ConditionValue, SourceLocation IfLoc) override; + void Elif(SourceLocation HashLoc, SourceLocation Loc, + SourceRange ConditionRange, ConditionValueKind ConditionValue, + SourceLocation IfLoc) override; void WriteLineInfo(StringRef Filename, int Line, SrcMgr::CharacteristicKind FileType, StringRef Extra = StringRef()); @@ -196,14 +198,16 @@ LastInclusionLocation = HashLoc; } -void InclusionRewriter::If(SourceLocation Loc, SourceRange ConditionRange, +void InclusionRewriter::If(SourceLocation HashLoc, SourceLocation Loc, + SourceRange ConditionRange, ConditionValueKind ConditionValue) { auto P = IfConditions.insert(std::make_pair(Loc, ConditionValue == CVK_True)); (void)P; assert(P.second && "Unexpected revisitation of the same if directive"); } -void InclusionRewriter::Elif(SourceLocation Loc, SourceRange ConditionRange, +void InclusionRewriter::Elif(SourceLocation HashLoc, SourceLocation Loc, + SourceRange ConditionRange, ConditionValueKind ConditionValue, SourceLocation IfLoc) { auto P = IfConditions.insert(std::make_pair(Loc, ConditionValue == CVK_True)); diff --git a/clang/lib/Index/IndexingAction.cpp b/clang/lib/Index/IndexingAction.cpp --- a/clang/lib/Index/IndexingAction.cpp +++ b/clang/lib/Index/IndexingAction.cpp @@ -36,14 +36,15 @@ Range.getBegin(), *MD.getMacroInfo()); } - void MacroDefined(const Token &MacroNameTok, + void MacroDefined(SourceLocation HashLoc, const Token &MacroNameTok, const MacroDirective *MD) override { IndexCtx->handleMacroDefined(*MacroNameTok.getIdentifierInfo(), MacroNameTok.getLocation(), *MD->getMacroInfo()); } - void MacroUndefined(const Token &MacroNameTok, const MacroDefinition &MD, + void MacroUndefined(SourceLocation HashLoc, const Token &MacroNameTok, + const MacroDefinition &MD, const MacroDirective *Undef) override { if (!MD.getMacroInfo()) // Ignore noop #undef. return; @@ -61,16 +62,16 @@ MacroNameTok.getLocation(), *MD.getMacroInfo()); } - void Ifdef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDefinition &MD) override { + void Ifdef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) override { if (!MD.getMacroInfo()) // Ignore non-existent macro. return; IndexCtx->handleMacroReference(*MacroNameTok.getIdentifierInfo(), MacroNameTok.getLocation(), *MD.getMacroInfo()); } - void Ifndef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDefinition &MD) override { + void Ifndef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) override { if (!MD.getMacroInfo()) // Ignore nonexistent macro. return; IndexCtx->handleMacroReference(*MacroNameTok.getIdentifierInfo(), @@ -80,16 +81,16 @@ using PPCallbacks::Elifdef; using PPCallbacks::Elifndef; - void Elifdef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDefinition &MD) override { + void Elifdef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) override { if (!MD.getMacroInfo()) // Ignore non-existent macro. return; IndexCtx->handleMacroReference(*MacroNameTok.getIdentifierInfo(), MacroNameTok.getLocation(), *MD.getMacroInfo()); } - void Elifndef(SourceLocation Loc, const Token &MacroNameTok, - const MacroDefinition &MD) override { + void Elifndef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) override { if (!MD.getMacroInfo()) // Ignore non-existent macro. return; IndexCtx->handleMacroReference(*MacroNameTok.getIdentifierInfo(), diff --git a/clang/lib/Lex/PPConditionalDirectiveRecord.cpp b/clang/lib/Lex/PPConditionalDirectiveRecord.cpp --- a/clang/lib/Lex/PPConditionalDirectiveRecord.cpp +++ b/clang/lib/Lex/PPConditionalDirectiveRecord.cpp @@ -72,28 +72,32 @@ CondDirectiveLocs.push_back(DirLoc); } -void PPConditionalDirectiveRecord::If(SourceLocation Loc, +void PPConditionalDirectiveRecord::If(SourceLocation HashLoc, + SourceLocation Loc, SourceRange ConditionRange, ConditionValueKind ConditionValue) { addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back())); CondDirectiveStack.push_back(Loc); } -void PPConditionalDirectiveRecord::Ifdef(SourceLocation Loc, +void PPConditionalDirectiveRecord::Ifdef(SourceLocation HashLoc, + SourceLocation Loc, const Token &MacroNameTok, const MacroDefinition &MD) { addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back())); CondDirectiveStack.push_back(Loc); } -void PPConditionalDirectiveRecord::Ifndef(SourceLocation Loc, +void PPConditionalDirectiveRecord::Ifndef(SourceLocation HashLoc, + SourceLocation Loc, const Token &MacroNameTok, const MacroDefinition &MD) { addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back())); CondDirectiveStack.push_back(Loc); } -void PPConditionalDirectiveRecord::Elif(SourceLocation Loc, +void PPConditionalDirectiveRecord::Elif(SourceLocation HashLoc, + SourceLocation Loc, SourceRange ConditionRange, ConditionValueKind ConditionValue, SourceLocation IfLoc) { @@ -101,35 +105,41 @@ CondDirectiveStack.back() = Loc; } -void PPConditionalDirectiveRecord::Elifdef(SourceLocation Loc, const Token &, +void PPConditionalDirectiveRecord::Elifdef(SourceLocation HashLoc, + SourceLocation Loc, const Token &, const MacroDefinition &) { addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back())); CondDirectiveStack.back() = Loc; } -void PPConditionalDirectiveRecord::Elifdef(SourceLocation Loc, SourceRange, +void PPConditionalDirectiveRecord::Elifdef(SourceLocation HashLoc, + SourceLocation Loc, SourceRange, SourceLocation) { addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back())); CondDirectiveStack.back() = Loc; } -void PPConditionalDirectiveRecord::Elifndef(SourceLocation Loc, const Token &, +void PPConditionalDirectiveRecord::Elifndef(SourceLocation HashLoc, + SourceLocation Loc, const Token &, const MacroDefinition &) { addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back())); CondDirectiveStack.back() = Loc; } -void PPConditionalDirectiveRecord::Elifndef(SourceLocation Loc, SourceRange, +void PPConditionalDirectiveRecord::Elifndef(SourceLocation HashLoc, + SourceLocation Loc, SourceRange, SourceLocation) { addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back())); CondDirectiveStack.back() = Loc; } -void PPConditionalDirectiveRecord::Else(SourceLocation Loc, +void PPConditionalDirectiveRecord::Else(SourceLocation HashLoc, + SourceLocation Loc, SourceLocation IfLoc) { addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back())); CondDirectiveStack.back() = Loc; } -void PPConditionalDirectiveRecord::Endif(SourceLocation Loc, +void PPConditionalDirectiveRecord::Endif(SourceLocation HashLoc, + SourceLocation Loc, SourceLocation IfLoc) { addCondDirectiveLoc(CondDirectiveLoc(Loc, CondDirectiveStack.back())); assert(!CondDirectiveStack.empty()); diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp --- a/clang/lib/Lex/PPDirectives.cpp +++ b/clang/lib/Lex/PPDirectives.cpp @@ -596,6 +596,7 @@ assert(Tok.is(tok::hash)); const char *Hashptr = CurLexer->getBufferLocation() - Tok.getLength(); assert(CurLexer->getSourceLocation(Hashptr) == Tok.getLocation()); + SourceLocation LastHashLoc = Tok.getLocation(); // Read the next token, the directive flavor. LexUnexpandedToken(Tok); @@ -677,7 +678,7 @@ endLoc = CheckEndOfDirective("endif"); CurPPLexer->LexingRawMode = true; if (Callbacks) - Callbacks->Endif(Tok.getLocation(), CondInfo.IfLoc); + Callbacks->Endif(LastHashLoc, Tok.getLocation(), CondInfo.IfLoc); break; } else { DiscardUntilEndOfDirective(); @@ -708,7 +709,7 @@ endLoc = CheckEndOfDirective("else"); CurPPLexer->LexingRawMode = true; if (Callbacks) - Callbacks->Else(Tok.getLocation(), CondInfo.IfLoc); + Callbacks->Else(LastHashLoc, Tok.getLocation(), CondInfo.IfLoc); break; } else { DiscardUntilEndOfDirective(); // C99 6.10p4. @@ -745,7 +746,7 @@ CurPPLexer->LexingRawMode = true; if (Callbacks) { Callbacks->Elif( - Tok.getLocation(), DER.ExprRange, + LastHashLoc, Tok.getLocation(), DER.ExprRange, (CondValue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False), CondInfo.IfLoc); } @@ -815,11 +816,11 @@ if (Callbacks) { if (IsElifDef) { - Callbacks->Elifdef(DirectiveToken.getLocation(), MacroNameTok, - MD); + Callbacks->Elifdef(LastHashLoc, DirectiveToken.getLocation(), + MacroNameTok, MD); } else { - Callbacks->Elifndef(DirectiveToken.getLocation(), MacroNameTok, - MD); + Callbacks->Elifndef(LastHashLoc, DirectiveToken.getLocation(), + MacroNameTok, MD); } } // If this condition is true, enter it! @@ -1112,7 +1113,7 @@ SourceLocation HashLoc) { if (const IdentifierInfo *II = Result.getIdentifierInfo()) { if (II->getPPKeywordID() == tok::pp_define) { - return HandleDefineDirective(Result, + return HandleDefineDirective(HashLoc, Result, /*ImmediatelyAfterHeaderGuard=*/false); } if (SkippingUntilPCHThroughHeader && @@ -1232,7 +1233,7 @@ case tok::pp_else: return HandleElseDirective(Result, SavedHash); case tok::pp_endif: - return HandleEndifDirective(Result); + return HandleEndifDirective(Result, SavedHash); // C99 6.10.2 - Source File Inclusion. case tok::pp_include: @@ -1244,9 +1245,10 @@ // C99 6.10.3 - Macro Replacement. case tok::pp_define: - return HandleDefineDirective(Result, ImmediatelyAfterTopLevelIfndef); + return HandleDefineDirective(SavedHash.getLocation(), Result, + ImmediatelyAfterTopLevelIfndef); case tok::pp_undef: - return HandleUndefDirective(); + return HandleUndefDirective(SavedHash.getLocation()); // C99 6.10.4 - Line Control. case tok::pp_line: @@ -3026,7 +3028,8 @@ /// HandleDefineDirective - Implements \#define. This consumes the entire macro /// line then lets the caller lex the next real token. void Preprocessor::HandleDefineDirective( - Token &DefineTok, const bool ImmediatelyAfterHeaderGuard) { + SourceLocation HashLoc, Token &DefineTok, + const bool ImmediatelyAfterHeaderGuard) { ++NumDefined; Token MacroNameTok; @@ -3158,7 +3161,7 @@ // If the callbacks want to know, tell them about the macro definition. if (Callbacks) - Callbacks->MacroDefined(MacroNameTok, MD); + Callbacks->MacroDefined(HashLoc, MacroNameTok, MD); // If we're in MS compatibility mode and the macro being defined is the // assert macro, implicitly add a macro definition for static_assert to work @@ -3180,7 +3183,7 @@ /// HandleUndefDirective - Implements \#undef. /// -void Preprocessor::HandleUndefDirective() { +void Preprocessor::HandleUndefDirective(SourceLocation HashLoc) { ++NumUndefined; Token MacroNameTok; @@ -3215,7 +3218,7 @@ // 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, MD, Undef); + Callbacks->MacroUndefined(HashLoc, MacroNameTok, MD, Undef); if (Undef) appendMacroDirective(II, Undef); @@ -3277,9 +3280,11 @@ if (Callbacks) { if (isIfndef) - Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD); + Callbacks->Ifndef(HashToken.getLocation(), DirectiveTok.getLocation(), + MacroNameTok, MD); else - Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MD); + Callbacks->Ifdef(HashToken.getLocation(), DirectiveTok.getLocation(), + MacroNameTok, MD); } bool RetainExcludedCB = PPOpts->RetainExcludedConditionalBlocks && @@ -3334,7 +3339,7 @@ if (Callbacks) Callbacks->If( - IfToken.getLocation(), DER.ExprRange, + HashToken.getLocation(), IfToken.getLocation(), DER.ExprRange, (ConditionalTrue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False)); bool RetainExcludedCB = PPOpts->RetainExcludedConditionalBlocks && @@ -3360,7 +3365,8 @@ /// HandleEndifDirective - Implements the \#endif directive. /// -void Preprocessor::HandleEndifDirective(Token &EndifToken) { +void Preprocessor::HandleEndifDirective(Token &EndifToken, + const Token &HashToken) { ++NumEndif; // Check that this is the whole directive. @@ -3381,7 +3387,8 @@ "This code should only be reachable in the non-skipping case!"); if (Callbacks) - Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc); + Callbacks->Endif(HashToken.getLocation(), EndifToken.getLocation(), + CondInfo.IfLoc); } /// HandleElseDirective - Implements the \#else directive. @@ -3406,7 +3413,7 @@ if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else); if (Callbacks) - Callbacks->Else(Result.getLocation(), CI.IfLoc); + Callbacks->Else(HashToken.getLocation(), Result.getLocation(), CI.IfLoc); bool RetainExcludedCB = PPOpts->RetainExcludedConditionalBlocks && getSourceManager().isInMainFile(Result.getLocation()); @@ -3473,14 +3480,16 @@ if (Callbacks) { switch (Kind) { case tok::pp_elif: - Callbacks->Elif(ElifToken.getLocation(), ConditionRange, - PPCallbacks::CVK_NotEvaluated, CI.IfLoc); + Callbacks->Elif(HashToken.getLocation(), ElifToken.getLocation(), + ConditionRange, PPCallbacks::CVK_NotEvaluated, CI.IfLoc); break; case tok::pp_elifdef: - Callbacks->Elifdef(ElifToken.getLocation(), ConditionRange, CI.IfLoc); + Callbacks->Elifdef(HashToken.getLocation(), ElifToken.getLocation(), + ConditionRange, CI.IfLoc); break; case tok::pp_elifndef: - Callbacks->Elifndef(ElifToken.getLocation(), ConditionRange, CI.IfLoc); + Callbacks->Elifndef(HashToken.getLocation(), ElifToken.getLocation(), + ConditionRange, CI.IfLoc); break; default: assert(false && "unexpected directive kind"); diff --git a/clang/lib/Lex/PreprocessingRecord.cpp b/clang/lib/Lex/PreprocessingRecord.cpp --- a/clang/lib/Lex/PreprocessingRecord.cpp +++ b/clang/lib/Lex/PreprocessingRecord.cpp @@ -403,7 +403,8 @@ addPreprocessedEntity(new (*this) MacroExpansion(Def, Range)); } -void PreprocessingRecord::Ifdef(SourceLocation Loc, const Token &MacroNameTok, +void PreprocessingRecord::Ifdef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) { // This is not actually a macro expansion but record it as a macro reference. if (MD) @@ -411,7 +412,8 @@ MacroNameTok.getLocation()); } -void PreprocessingRecord::Elifdef(SourceLocation Loc, const Token &MacroNameTok, +void PreprocessingRecord::Elifdef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) { // This is not actually a macro expansion but record it as a macro reference. if (MD) @@ -419,7 +421,8 @@ MacroNameTok.getLocation()); } -void PreprocessingRecord::Ifndef(SourceLocation Loc, const Token &MacroNameTok, +void PreprocessingRecord::Ifndef(SourceLocation HashLoc, SourceLocation Loc, + const Token &MacroNameTok, const MacroDefinition &MD) { // This is not actually a macro expansion but record it as a macro reference. if (MD) @@ -427,7 +430,7 @@ MacroNameTok.getLocation()); } -void PreprocessingRecord::Elifndef(SourceLocation Loc, +void PreprocessingRecord::Elifndef(SourceLocation HashLoc, SourceLocation Loc, const Token &MacroNameTok, const MacroDefinition &MD) { // This is not actually a macro expansion but record it as a macro reference. @@ -458,7 +461,7 @@ addMacroExpansion(Id, MD.getMacroInfo(), Range); } -void PreprocessingRecord::MacroDefined(const Token &Id, +void PreprocessingRecord::MacroDefined(SourceLocation HashLoc, const Token &Id, const MacroDirective *MD) { const MacroInfo *MI = MD->getMacroInfo(); SourceRange R(MI->getDefinitionLoc(), MI->getDefinitionEndLoc()); @@ -468,7 +471,8 @@ MacroDefinitions[MI] = Def; } -void PreprocessingRecord::MacroUndefined(const Token &Id, +void PreprocessingRecord::MacroUndefined(SourceLocation HashLoc, + const Token &Id, const MacroDefinition &MD, const MacroDirective *Undef) { MD.forAllDefinitions([&](MacroInfo *MI) { MacroDefinitions.erase(MI); }); diff --git a/clang/tools/libclang/Indexing.cpp b/clang/tools/libclang/Indexing.cpp --- a/clang/tools/libclang/Indexing.cpp +++ b/clang/tools/libclang/Indexing.cpp @@ -272,11 +272,12 @@ } /// MacroDefined - This hook is called whenever a macro definition is seen. - void MacroDefined(const Token &Id, const MacroDirective *MD) override {} + void MacroDefined(SourceLocation HashLoc, const Token &Id, + const MacroDirective *MD) override {} /// MacroUndefined - This hook is called whenever a macro #undef is seen. /// MI is released immediately following this callback. - void MacroUndefined(const Token &MacroNameTok, + void MacroUndefined(SourceLocation HashLoc, const Token &MacroNameTok, const MacroDefinition &MD, const MacroDirective *UD) override {} diff --git a/clang/unittests/Basic/SourceManagerTest.cpp b/clang/unittests/Basic/SourceManagerTest.cpp --- a/clang/unittests/Basic/SourceManagerTest.cpp +++ b/clang/unittests/Basic/SourceManagerTest.cpp @@ -510,15 +510,15 @@ public: explicit MacroTracker(std::vector &Macros) : Macros(Macros) { } - void MacroDefined(const Token &MacroNameTok, + void MacroDefined(SourceLocation HashLoc, const Token &MacroNameTok, const MacroDirective *MD) override { Macros.push_back(MacroAction(MD->getLocation(), MacroNameTok.getIdentifierInfo()->getName(), MacroAction::kDefinition)); } - void MacroUndefined(const Token &MacroNameTok, + void MacroUndefined(SourceLocation HashLoc, const Token &MacroNameTok, const MacroDefinition &MD, - const MacroDirective *UD) override { + const MacroDirective *UD) override { Macros.push_back( MacroAction(UD ? UD->getLocation() : SourceLocation(), MacroNameTok.getIdentifierInfo()->getName(), diff --git a/clang/unittests/Lex/PPCallbacksTest.cpp b/clang/unittests/Lex/PPCallbacksTest.cpp --- a/clang/unittests/Lex/PPCallbacksTest.cpp +++ b/clang/unittests/Lex/PPCallbacksTest.cpp @@ -75,13 +75,15 @@ std::vector Results; - void If(SourceLocation Loc, SourceRange ConditionRange, + void If(SourceLocation HashLoc, SourceLocation Loc, + SourceRange ConditionRange, ConditionValueKind ConditionValue) override { Results.emplace_back(ConditionRange, ConditionValue); } - void Elif(SourceLocation Loc, SourceRange ConditionRange, - ConditionValueKind ConditionValue, SourceLocation IfLoc) override { + void Elif(SourceLocation HashLoc, SourceLocation Loc, + SourceRange ConditionRange, ConditionValueKind ConditionValue, + SourceLocation IfLoc) override { Results.emplace_back(ConditionRange, ConditionValue); } };