[clang] db71354 - [ASTMatchers] Fixed CastKind being parsed incorrectly for dynamic matchers
Nathan James via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 9 07:09:52 PDT 2020
Author: Nathan James
Date: 2020-04-09T15:09:45+01:00
New Revision: db71354e4ff13e8f5c6ab38da6339c7583209be0
URL: https://github.com/llvm/llvm-project/commit/db71354e4ff13e8f5c6ab38da6339c7583209be0
DIFF: https://github.com/llvm/llvm-project/commit/db71354e4ff13e8f5c6ab38da6339c7583209be0.diff
LOG: [ASTMatchers] Fixed CastKind being parsed incorrectly for dynamic matchers
Summary: Requires hasCastKind arguments to have `CK_` prefixed to bring it in line with the documentation and other matchers that take enumerations.
Reviewers: klimek, aaron.ballman
Reviewed By: aaron.ballman
Subscribers: cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D77503
Added:
Modified:
clang/docs/LibASTMatchersReference.html
clang/include/clang/ASTMatchers/ASTMatchers.h
clang/lib/ASTMatchers/Dynamic/Marshallers.cpp
clang/lib/ASTMatchers/Dynamic/Marshallers.h
clang/unittests/ASTMatchers/Dynamic/ParserTest.cpp
Removed:
################################################################################
diff --git a/clang/docs/LibASTMatchersReference.html b/clang/docs/LibASTMatchersReference.html
index 7490ecd89186..f5106f0e0ba2 100644
--- a/clang/docs/LibASTMatchersReference.html
+++ b/clang/docs/LibASTMatchersReference.html
@@ -2790,7 +2790,7 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
int *p = 0;
If the matcher is use from clang-query, CastKind parameter
-should be passed as a quoted string. e.g., ofKind("CK_NullToPointer").
+should be passed as a quoted string. e.g., hasCastKind("CK_NullToPointer").
</pre></td></tr>
diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h
index 9d7b4dcaacfd..070c38295ba7 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -4913,7 +4913,7 @@ AST_POLYMORPHIC_MATCHER_P(hasSourceExpression,
/// \endcode
///
/// If the matcher is use from clang-query, CastKind parameter
-/// should be passed as a quoted string. e.g., ofKind("CK_NullToPointer").
+/// should be passed as a quoted string. e.g., hasCastKind("CK_NullToPointer").
AST_MATCHER_P(CastExpr, hasCastKind, CastKind, Kind) {
return Node.getCastKind() == Kind;
}
diff --git a/clang/lib/ASTMatchers/Dynamic/Marshallers.cpp b/clang/lib/ASTMatchers/Dynamic/Marshallers.cpp
index de1a97814e42..d45965d013cf 100644
--- a/clang/lib/ASTMatchers/Dynamic/Marshallers.cpp
+++ b/clang/lib/ASTMatchers/Dynamic/Marshallers.cpp
@@ -75,11 +75,12 @@ llvm::Optional<std::string>
clang::ast_matchers::dynamic::internal::ArgTypeTraits<
clang::CastKind>::getBestGuess(const VariantValue &Value) {
static constexpr llvm::StringRef Allowed[] = {
-#define CAST_OPERATION(Name) #Name,
+#define CAST_OPERATION(Name) "CK_" #Name,
#include "clang/AST/OperationKinds.def"
};
if (Value.isString())
- return ::getBestGuess(Value.getString(), llvm::makeArrayRef(Allowed));
+ return ::getBestGuess(Value.getString(), llvm::makeArrayRef(Allowed),
+ "CK_");
return llvm::None;
}
diff --git a/clang/lib/ASTMatchers/Dynamic/Marshallers.h b/clang/lib/ASTMatchers/Dynamic/Marshallers.h
index 288155217d5c..582a060842b9 100644
--- a/clang/lib/ASTMatchers/Dynamic/Marshallers.h
+++ b/clang/lib/ASTMatchers/Dynamic/Marshallers.h
@@ -171,7 +171,7 @@ template <> struct ArgTypeTraits<CastKind> {
private:
static Optional<CastKind> getCastKind(llvm::StringRef AttrKind) {
return llvm::StringSwitch<Optional<CastKind>>(AttrKind)
-#define CAST_OPERATION(Name) .Case( #Name, CK_##Name)
+#define CAST_OPERATION(Name) .Case("CK_" #Name, CK_##Name)
#include "clang/AST/OperationKinds.def"
.Default(llvm::None);
}
diff --git a/clang/unittests/ASTMatchers/Dynamic/ParserTest.cpp b/clang/unittests/ASTMatchers/Dynamic/ParserTest.cpp
index 1b553ffe73ed..0d3a2d4ce776 100644
--- a/clang/unittests/ASTMatchers/Dynamic/ParserTest.cpp
+++ b/clang/unittests/ASTMatchers/Dynamic/ParserTest.cpp
@@ -221,6 +221,15 @@ TEST(ParserTest, FullParserTest) {
EXPECT_FALSE(matches("int x = 1 - false;", M));
EXPECT_FALSE(matches("int x = true - 1;", M));
+ Code = "implicitCastExpr(hasCastKind(\"CK_IntegralToBoolean\"))";
+ llvm::Optional<DynTypedMatcher> implicitIntBooleanCast(
+ Parser::parseMatcherExpression(Code, nullptr, nullptr, &Error));
+ EXPECT_EQ("", Error.toStringFull());
+ Matcher<Stmt> MCastStmt =
+ implicitIntBooleanCast->unconditionalConvertTo<Stmt>();
+ EXPECT_TRUE(matches("bool X = 1;", MCastStmt));
+ EXPECT_FALSE(matches("bool X = true;", MCastStmt));
+
Code = "functionDecl(hasParameter(1, hasName(\"x\")))";
llvm::Optional<DynTypedMatcher> HasParameter(
Parser::parseMatcherExpression(Code, &Error));
More information about the cfe-commits
mailing list