Skip to content

Commit e60b494

Browse files
committed
review comments StatementMatcher, use switch
1 parent 5033425 commit e60b494

File tree

2 files changed

+43
-29
lines changed

2 files changed

+43
-29
lines changed

clang/include/clang/Analysis/FlowSensitive/SmartPointerAccessorCaching.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ namespace clang::dataflow {
5353
/// for `std::optional`, we assume the (Matcher, TransferFunction) case
5454
/// with custom handling is ordered early so that these generic cases
5555
/// do not trigger.
56-
ast_matchers::internal::Matcher<Stmt> isSmartPointerLikeOperatorStar();
57-
ast_matchers::internal::Matcher<Stmt> isSmartPointerLikeOperatorArrow();
58-
ast_matchers::internal::Matcher<Stmt> isSmartPointerLikeValueMethodCall();
59-
ast_matchers::internal::Matcher<Stmt> isSmartPointerLikeGetMethodCall();
56+
ast_matchers::StatementMatcher isSmartPointerLikeOperatorStar();
57+
ast_matchers::StatementMatcher isSmartPointerLikeOperatorArrow();
58+
ast_matchers::StatementMatcher isSmartPointerLikeValueMethodCall();
59+
ast_matchers::StatementMatcher isSmartPointerLikeGetMethodCall();
6060

6161
} // namespace clang::dataflow
6262

clang/lib/Analysis/FlowSensitive/SmartPointerAccessorCaching.cpp

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "clang/AST/CanonicalType.h"
44
#include "clang/AST/DeclCXX.h"
55
#include "clang/ASTMatchers/ASTMatchers.h"
6+
#include "clang/ASTMatchers/ASTMatchersMacros.h"
67
#include "clang/Basic/OperatorKinds.h"
78

89
namespace clang::dataflow {
@@ -34,33 +35,46 @@ bool hasSmartPointerClassShape(const CXXRecordDecl &RD, bool &HasGet,
3435
// there should at least be a const overload as well.
3536
if (!MD->isConst() || MD->getNumParams() != 0)
3637
continue;
37-
if (MD->getOverloadedOperator() == OO_Star &&
38-
MD->getReturnType()->isReferenceType()) {
39-
HasStar = true;
40-
StarReturnType = MD->getReturnType()
41-
.getNonReferenceType()
42-
->getCanonicalTypeUnqualified();
43-
} else if (MD->getOverloadedOperator() == OO_Arrow &&
44-
MD->getReturnType()->isPointerType()) {
45-
HasArrow = true;
46-
ArrowReturnType =
47-
MD->getReturnType()->getPointeeType()->getCanonicalTypeUnqualified();
48-
} else {
38+
switch (MD->getOverloadedOperator()) {
39+
case OO_Star:
40+
if (MD->getReturnType()->isReferenceType()) {
41+
HasStar = true;
42+
StarReturnType = MD->getReturnType()
43+
.getNonReferenceType()
44+
->getCanonicalTypeUnqualified();
45+
}
46+
break;
47+
case OO_Arrow:
48+
if (MD->getReturnType()->isPointerType()) {
49+
HasArrow = true;
50+
ArrowReturnType = MD->getReturnType()
51+
->getPointeeType()
52+
->getCanonicalTypeUnqualified();
53+
}
54+
break;
55+
case OO_None: {
4956
IdentifierInfo *II = MD->getIdentifier();
5057
if (II == nullptr)
5158
continue;
52-
if (II->isStr("get") && MD->getReturnType()->isPointerType()) {
53-
HasGet = true;
54-
GetReturnType = MD->getReturnType()
55-
->getPointeeType()
56-
->getCanonicalTypeUnqualified();
57-
} else if (II->isStr("value") && MD->getReturnType()->isReferenceType()) {
58-
HasValue = true;
59-
ValueReturnType = MD->getReturnType()
60-
.getNonReferenceType()
59+
if (II->isStr("get")) {
60+
if (MD->getReturnType()->isPointerType()) {
61+
HasGet = true;
62+
GetReturnType = MD->getReturnType()
63+
->getPointeeType()
6164
->getCanonicalTypeUnqualified();
65+
}
66+
} else if (II->isStr("value")) {
67+
if (MD->getReturnType()->isReferenceType()) {
68+
HasValue = true;
69+
ValueReturnType = MD->getReturnType()
70+
.getNonReferenceType()
71+
->getCanonicalTypeUnqualified();
72+
}
6273
}
6374
}
75+
default:
76+
break;
77+
}
6478
}
6579

6680
if (!HasStar || !HasArrow || StarReturnType != ArrowReturnType)
@@ -105,26 +119,26 @@ AST_MATCHER(clang::CXXRecordDecl, smartPointerClassWithGetOrValue) {
105119

106120
namespace clang::dataflow {
107121

108-
ast_matchers::internal::Matcher<Stmt> isSmartPointerLikeOperatorStar() {
122+
ast_matchers::StatementMatcher isSmartPointerLikeOperatorStar() {
109123
return cxxOperatorCallExpr(
110124
hasOverloadedOperatorName("*"),
111125
callee(cxxMethodDecl(parameterCountIs(0), returns(referenceType()),
112126
ofClass(smartPointerClassWithGetOrValue()))));
113127
}
114128

115-
ast_matchers::internal::Matcher<Stmt> isSmartPointerLikeOperatorArrow() {
129+
ast_matchers::StatementMatcher isSmartPointerLikeOperatorArrow() {
116130
return cxxOperatorCallExpr(
117131
hasOverloadedOperatorName("->"),
118132
callee(cxxMethodDecl(parameterCountIs(0), returns(pointerType()),
119133
ofClass(smartPointerClassWithGetOrValue()))));
120134
}
121-
ast_matchers::internal::Matcher<Stmt> isSmartPointerLikeValueMethodCall() {
135+
ast_matchers::StatementMatcher isSmartPointerLikeValueMethodCall() {
122136
return cxxMemberCallExpr(callee(
123137
cxxMethodDecl(parameterCountIs(0), returns(referenceType()),
124138
hasName("value"), ofClass(smartPointerClassWithValue()))));
125139
}
126140

127-
ast_matchers::internal::Matcher<Stmt> isSmartPointerLikeGetMethodCall() {
141+
ast_matchers::StatementMatcher isSmartPointerLikeGetMethodCall() {
128142
return cxxMemberCallExpr(callee(
129143
cxxMethodDecl(parameterCountIs(0), returns(pointerType()), hasName("get"),
130144
ofClass(smartPointerClassWithGet()))));

0 commit comments

Comments
 (0)