ymandel created this revision. ymandel added a reviewer: aaron.ballman. Herald added a project: clang.
Spells out some `auto`s explicitly and adds another test for the matcher `isExpandedFromMacro`. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D73975 Files: clang/include/clang/ASTMatchers/ASTMatchers.h clang/lib/ASTMatchers/ASTMatchersInternal.cpp clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp Index: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp =================================================================== --- clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp +++ clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp @@ -99,6 +99,15 @@ EXPECT_TRUE(matches(input, binaryOperator(isExpandedFromMacro("PLUS")))); } +TEST(IsExpandedFromMacro, ShouldMatchFromCommandLine) { + std::string input = R"cc( + void Test() { FOUR_PLUS_FOUR; } + )cc"; + EXPECT_TRUE(matchesConditionally(input, + binaryOperator(isExpandedFromMacro("FOUR_PLUS_FOUR")), + true, {"-std=c++11", "-DFOUR_PLUS_FOUR=4+4"})); +} + TEST(IsExpandedFromMacro, ShouldNotMatchBeginOnly) { std::string input = R"cc( #define ONE_PLUS 1+ Index: clang/lib/ASTMatchers/ASTMatchersInternal.cpp =================================================================== --- clang/lib/ASTMatchers/ASTMatchersInternal.cpp +++ clang/lib/ASTMatchers/ASTMatchersInternal.cpp @@ -603,22 +603,23 @@ bool Invalid = false; // Since `Loc` may point into an expansion buffer, which has no corresponding // source, we need to look at the spelling location to read the actual source. - StringRef TokenText = clang::Lexer::getSpelling( - SM.getSpellingLoc(Loc), Buffer, SM, LangOpts, &Invalid); + StringRef TokenText = Lexer::getSpelling(SM.getSpellingLoc(Loc), Buffer, SM, + LangOpts, &Invalid); return !Invalid && Text == TokenText; } llvm::Optional<SourceLocation> getExpansionLocOfMacro(StringRef MacroName, SourceLocation Loc, const ASTContext &Context) { - auto& SM = Context.getSourceManager(); - const auto& LangOpts = Context.getLangOpts(); + auto &SM = Context.getSourceManager(); + const LangOptions &LangOpts = Context.getLangOpts(); while (Loc.isMacroID()) { - auto Expansion = SM.getSLocEntry(SM.getFileID(Loc)).getExpansion(); + SrcMgr::ExpansionInfo Expansion = + SM.getSLocEntry(SM.getFileID(Loc)).getExpansion(); if (Expansion.isMacroArgExpansion()) // Check macro argument for an expansion of the given macro. For example, // `F(G(3))`, where `MacroName` is `G`. - if (auto ArgLoc = getExpansionLocOfMacro( + if (llvm::Optional<SourceLocation> ArgLoc = getExpansionLocOfMacro( MacroName, Expansion.getSpellingLoc(), Context)) return ArgLoc; Loc = Expansion.getExpansionLocStart(); Index: clang/include/clang/ASTMatchers/ASTMatchers.h =================================================================== --- clang/include/clang/ASTMatchers/ASTMatchers.h +++ clang/include/clang/ASTMatchers/ASTMatchers.h @@ -311,10 +311,10 @@ // Verifies that the statement' beginning and ending are both expanded from // the same instance of the given macro. auto& Context = Finder->getASTContext(); - auto B = + llvm::Optional<SourceLocation> B = internal::getExpansionLocOfMacro(MacroName, Node.getBeginLoc(), Context); if (!B) return false; - auto E = + llvm::Optional<SourceLocation> E = internal::getExpansionLocOfMacro(MacroName, Node.getEndLoc(), Context); if (!E) return false; return *B == *E;
Index: clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp =================================================================== --- clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp +++ clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp @@ -99,6 +99,15 @@ EXPECT_TRUE(matches(input, binaryOperator(isExpandedFromMacro("PLUS")))); } +TEST(IsExpandedFromMacro, ShouldMatchFromCommandLine) { + std::string input = R"cc( + void Test() { FOUR_PLUS_FOUR; } + )cc"; + EXPECT_TRUE(matchesConditionally(input, + binaryOperator(isExpandedFromMacro("FOUR_PLUS_FOUR")), + true, {"-std=c++11", "-DFOUR_PLUS_FOUR=4+4"})); +} + TEST(IsExpandedFromMacro, ShouldNotMatchBeginOnly) { std::string input = R"cc( #define ONE_PLUS 1+ Index: clang/lib/ASTMatchers/ASTMatchersInternal.cpp =================================================================== --- clang/lib/ASTMatchers/ASTMatchersInternal.cpp +++ clang/lib/ASTMatchers/ASTMatchersInternal.cpp @@ -603,22 +603,23 @@ bool Invalid = false; // Since `Loc` may point into an expansion buffer, which has no corresponding // source, we need to look at the spelling location to read the actual source. - StringRef TokenText = clang::Lexer::getSpelling( - SM.getSpellingLoc(Loc), Buffer, SM, LangOpts, &Invalid); + StringRef TokenText = Lexer::getSpelling(SM.getSpellingLoc(Loc), Buffer, SM, + LangOpts, &Invalid); return !Invalid && Text == TokenText; } llvm::Optional<SourceLocation> getExpansionLocOfMacro(StringRef MacroName, SourceLocation Loc, const ASTContext &Context) { - auto& SM = Context.getSourceManager(); - const auto& LangOpts = Context.getLangOpts(); + auto &SM = Context.getSourceManager(); + const LangOptions &LangOpts = Context.getLangOpts(); while (Loc.isMacroID()) { - auto Expansion = SM.getSLocEntry(SM.getFileID(Loc)).getExpansion(); + SrcMgr::ExpansionInfo Expansion = + SM.getSLocEntry(SM.getFileID(Loc)).getExpansion(); if (Expansion.isMacroArgExpansion()) // Check macro argument for an expansion of the given macro. For example, // `F(G(3))`, where `MacroName` is `G`. - if (auto ArgLoc = getExpansionLocOfMacro( + if (llvm::Optional<SourceLocation> ArgLoc = getExpansionLocOfMacro( MacroName, Expansion.getSpellingLoc(), Context)) return ArgLoc; Loc = Expansion.getExpansionLocStart(); Index: clang/include/clang/ASTMatchers/ASTMatchers.h =================================================================== --- clang/include/clang/ASTMatchers/ASTMatchers.h +++ clang/include/clang/ASTMatchers/ASTMatchers.h @@ -311,10 +311,10 @@ // Verifies that the statement' beginning and ending are both expanded from // the same instance of the given macro. auto& Context = Finder->getASTContext(); - auto B = + llvm::Optional<SourceLocation> B = internal::getExpansionLocOfMacro(MacroName, Node.getBeginLoc(), Context); if (!B) return false; - auto E = + llvm::Optional<SourceLocation> E = internal::getExpansionLocOfMacro(MacroName, Node.getEndLoc(), Context); if (!E) return false; return *B == *E;
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits