Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,11 @@ AST_MATCHER_P(QualType, hasCleanType, Matcher<QualType>, InnerMatcher) {
Finder, Builder);
}

constexpr std::array<StringRef, 2> NameList{
constexpr std::array<StringRef, 2> MakeSmartPtrList{
"::std::make_unique",
"::std::make_shared",
};

Matcher<Expr> constructFrom(Matcher<QualType> TypeMatcher,
Matcher<Expr> ArgumentMatcher) {
return expr(
anyOf(
// construct optional
cxxConstructExpr(argumentCountIs(1U), hasType(TypeMatcher),
hasArgument(0U, ArgumentMatcher)),
// known template methods in std
callExpr(argumentCountIs(1),
callee(functionDecl(
matchers::matchesAnyListedName(NameList),
hasTemplateArgument(0, refersToType(TypeMatcher)))),
hasArgument(0, ArgumentMatcher))),
unless(anyOf(hasAncestor(typeLoc()),
hasAncestor(expr(matchers::hasUnevaluatedContext())))));
}

} // namespace

OptionalValueConversionCheck::OptionalValueConversionCheck(
Expand All @@ -74,7 +57,7 @@ void OptionalValueConversionCheck::registerMatchers(MatchFinder *Finder) {
auto EqualsBoundOptionalType =
qualType(hasCleanType(equalsBoundNode("optional-type")));

auto OptionalDereferenceMatcher = callExpr(
auto OptionalDerefMatcherImpl = callExpr(
anyOf(
cxxOperatorCallExpr(hasOverloadedOperatorName("*"),
hasUnaryOperand(hasType(EqualsBoundOptionalType)))
Expand All @@ -88,11 +71,24 @@ void OptionalValueConversionCheck::registerMatchers(MatchFinder *Finder) {

auto StdMoveCallMatcher =
callExpr(argumentCountIs(1), callee(functionDecl(hasName("::std::move"))),
hasArgument(0, ignoringImpCasts(OptionalDereferenceMatcher)));
hasArgument(0, ignoringImpCasts(OptionalDerefMatcherImpl)));
auto OptionalDerefMatcher =
ignoringImpCasts(anyOf(OptionalDerefMatcherImpl, StdMoveCallMatcher));

Finder->addMatcher(
expr(constructFrom(BindOptionalType,
ignoringImpCasts(anyOf(OptionalDereferenceMatcher,
StdMoveCallMatcher))))
expr(anyOf(
// construct optional
cxxConstructExpr(argumentCountIs(1), hasType(BindOptionalType),
hasArgument(0, OptionalDerefMatcher)),
// known template methods in std
callExpr(
argumentCountIs(1),
callee(functionDecl(
matchers::matchesAnyListedName(MakeSmartPtrList),
hasTemplateArgument(0, refersToType(BindOptionalType)))),
hasArgument(0, OptionalDerefMatcher))),
unless(anyOf(hasAncestor(typeLoc()),
hasAncestor(expr(matchers::hasUnevaluatedContext())))))
.bind("expr"),
this);
}
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/ASTMatchers/ASTMatchers.h
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,7 @@ AST_POLYMORPHIC_MATCHER_P2(
AST_POLYMORPHIC_MATCHER_P(
templateArgumentCountIs,
AST_POLYMORPHIC_SUPPORTED_TYPES(ClassTemplateSpecializationDecl,
VarTemplateSpecializationDecl, FunctionDecl,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd probably be good to update the example code above. Also, can you add test coverage to clang/unittests/ASTMatchers/ASTMatchersNarrowingTest.cpp?

TemplateSpecializationType),
unsigned, N) {
return internal::getTemplateSpecializationArgs(Node).size() == N;
Expand Down
Loading