Skip to content

Commit db71354

Browse files
committed
[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
1 parent 30e5c7e commit db71354

File tree

5 files changed

+15
-5
lines changed

5 files changed

+15
-5
lines changed

clang/docs/LibASTMatchersReference.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2790,7 +2790,7 @@ <h2 id="narrowing-matchers">Narrowing Matchers</h2>
27902790
int *p = 0;
27912791

27922792
If the matcher is use from clang-query, CastKind parameter
2793-
should be passed as a quoted string. e.g., ofKind("CK_NullToPointer").
2793+
should be passed as a quoted string. e.g., hasCastKind("CK_NullToPointer").
27942794
</pre></td></tr>
27952795

27962796

clang/include/clang/ASTMatchers/ASTMatchers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4913,7 +4913,7 @@ AST_POLYMORPHIC_MATCHER_P(hasSourceExpression,
49134913
/// \endcode
49144914
///
49154915
/// If the matcher is use from clang-query, CastKind parameter
4916-
/// should be passed as a quoted string. e.g., ofKind("CK_NullToPointer").
4916+
/// should be passed as a quoted string. e.g., hasCastKind("CK_NullToPointer").
49174917
AST_MATCHER_P(CastExpr, hasCastKind, CastKind, Kind) {
49184918
return Node.getCastKind() == Kind;
49194919
}

clang/lib/ASTMatchers/Dynamic/Marshallers.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,12 @@ llvm::Optional<std::string>
7575
clang::ast_matchers::dynamic::internal::ArgTypeTraits<
7676
clang::CastKind>::getBestGuess(const VariantValue &Value) {
7777
static constexpr llvm::StringRef Allowed[] = {
78-
#define CAST_OPERATION(Name) #Name,
78+
#define CAST_OPERATION(Name) "CK_" #Name,
7979
#include "clang/AST/OperationKinds.def"
8080
};
8181
if (Value.isString())
82-
return ::getBestGuess(Value.getString(), llvm::makeArrayRef(Allowed));
82+
return ::getBestGuess(Value.getString(), llvm::makeArrayRef(Allowed),
83+
"CK_");
8384
return llvm::None;
8485
}
8586

clang/lib/ASTMatchers/Dynamic/Marshallers.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ template <> struct ArgTypeTraits<CastKind> {
171171
private:
172172
static Optional<CastKind> getCastKind(llvm::StringRef AttrKind) {
173173
return llvm::StringSwitch<Optional<CastKind>>(AttrKind)
174-
#define CAST_OPERATION(Name) .Case( #Name, CK_##Name)
174+
#define CAST_OPERATION(Name) .Case("CK_" #Name, CK_##Name)
175175
#include "clang/AST/OperationKinds.def"
176176
.Default(llvm::None);
177177
}

clang/unittests/ASTMatchers/Dynamic/ParserTest.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,15 @@ TEST(ParserTest, FullParserTest) {
221221
EXPECT_FALSE(matches("int x = 1 - false;", M));
222222
EXPECT_FALSE(matches("int x = true - 1;", M));
223223

224+
Code = "implicitCastExpr(hasCastKind(\"CK_IntegralToBoolean\"))";
225+
llvm::Optional<DynTypedMatcher> implicitIntBooleanCast(
226+
Parser::parseMatcherExpression(Code, nullptr, nullptr, &Error));
227+
EXPECT_EQ("", Error.toStringFull());
228+
Matcher<Stmt> MCastStmt =
229+
implicitIntBooleanCast->unconditionalConvertTo<Stmt>();
230+
EXPECT_TRUE(matches("bool X = 1;", MCastStmt));
231+
EXPECT_FALSE(matches("bool X = true;", MCastStmt));
232+
224233
Code = "functionDecl(hasParameter(1, hasName(\"x\")))";
225234
llvm::Optional<DynTypedMatcher> HasParameter(
226235
Parser::parseMatcherExpression(Code, &Error));

0 commit comments

Comments
 (0)