[clang] e5ff92e - [clang][NFC] Expand some `auto`s and add another test for matcher `isExpandedFromMacro`.

Yitzhak Mandelbaum via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 4 10:06:39 PST 2020


Author: Yitzhak Mandelbaum
Date: 2020-02-04T13:05:58-05:00
New Revision: e5ff92e049b5b60d208d8b87f5ee693a7464d076

URL: https://github.com/llvm/llvm-project/commit/e5ff92e049b5b60d208d8b87f5ee693a7464d076
DIFF: https://github.com/llvm/llvm-project/commit/e5ff92e049b5b60d208d8b87f5ee693a7464d076.diff

LOG: [clang][NFC] Expand some `auto`s and add another test for matcher `isExpandedFromMacro`.

Summary: Spells out some `auto`s explicitly and adds another test for the matcher `isExpandedFromMacro`.

Reviewers: aaron.ballman

Subscribers: gribozavr, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D73975

Added: 
    

Modified: 
    clang/include/clang/ASTMatchers/ASTMatchers.h
    clang/lib/ASTMatchers/ASTMatchersInternal.cpp
    clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h
index ec969b42c6ae..eadf167dcefe 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -311,10 +311,10 @@ AST_MATCHER_P(clang::Stmt, isExpandedFromMacro, llvm::StringRef, MacroName) {
   // 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;

diff  --git a/clang/lib/ASTMatchers/ASTMatchersInternal.cpp b/clang/lib/ASTMatchers/ASTMatchersInternal.cpp
index 53f6e1d1d278..e8f06358b932 100644
--- a/clang/lib/ASTMatchers/ASTMatchersInternal.cpp
+++ b/clang/lib/ASTMatchers/ASTMatchersInternal.cpp
@@ -603,22 +603,23 @@ static bool isTokenAtLoc(const SourceManager &SM, const LangOptions &LangOpts,
   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();

diff  --git a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
index 2d6ce84b13ff..4b9fce9e3107 100644
--- a/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
+++ b/clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp
@@ -99,6 +99,15 @@ TEST(IsExpandedFromMacro, ShouldMatchObjectMacro) {
   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+


        


More information about the cfe-commits mailing list