[PATCH] D73975: [clang][NFC] Expand some `auto`s and add another test for matcher `isExpandedFromMacro`.
Yitzhak Mandelbaum via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Tue Feb 4 10:12:44 PST 2020
This revision was automatically updated to reflect the committed changes.
Closed by commit rGe5ff92e049b5: [clang][NFC] Expand some `auto`s and add another test for matcher… (authored by ymandel).
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D73975/new/
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;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D73975.242367.patch
Type: text/x-patch
Size: 3259 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200204/84eda822/attachment-0001.bin>
More information about the cfe-commits
mailing list