From db71354e4ff13e8f5c6ab38da6339c7583209be0 Mon Sep 17 00:00:00 2001 From: Nathan James Date: Mon, 6 Apr 2020 20:08:21 +0100 Subject: [PATCH] [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 --- clang/docs/LibASTMatchersReference.html | 2 +- clang/include/clang/ASTMatchers/ASTMatchers.h | 2 +- clang/lib/ASTMatchers/Dynamic/Marshallers.cpp | 5 +++-- clang/lib/ASTMatchers/Dynamic/Marshallers.h | 2 +- clang/unittests/ASTMatchers/Dynamic/ParserTest.cpp | 9 +++++++++ 5 files changed, 15 insertions(+), 5 deletions(-) diff --git a/clang/docs/LibASTMatchersReference.html b/clang/docs/LibASTMatchersReference.html index 7490ecd..f5106f0 100644 --- a/clang/docs/LibASTMatchersReference.html +++ b/clang/docs/LibASTMatchersReference.html @@ -2790,7 +2790,7 @@ Example: matches the implicit cast around 0 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"). diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h b/clang/include/clang/ASTMatchers/ASTMatchers.h index 9d7b4dc..070c382 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 de1a978..d45965d 100644 --- a/clang/lib/ASTMatchers/Dynamic/Marshallers.cpp +++ b/clang/lib/ASTMatchers/Dynamic/Marshallers.cpp @@ -75,11 +75,12 @@ llvm::Optional 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 2881552..582a060 100644 --- a/clang/lib/ASTMatchers/Dynamic/Marshallers.h +++ b/clang/lib/ASTMatchers/Dynamic/Marshallers.h @@ -171,7 +171,7 @@ template <> struct ArgTypeTraits { private: static Optional getCastKind(llvm::StringRef AttrKind) { return llvm::StringSwitch>(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 1b553ff..0d3a2d4 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 implicitIntBooleanCast( + Parser::parseMatcherExpression(Code, nullptr, nullptr, &Error)); + EXPECT_EQ("", Error.toStringFull()); + Matcher MCastStmt = + implicitIntBooleanCast->unconditionalConvertTo(); + EXPECT_TRUE(matches("bool X = 1;", MCastStmt)); + EXPECT_FALSE(matches("bool X = true;", MCastStmt)); + Code = "functionDecl(hasParameter(1, hasName(\"x\")))"; llvm::Optional HasParameter( Parser::parseMatcherExpression(Code, &Error)); -- 2.7.4