|
3 | 3 | #include "clang/AST/CanonicalType.h" |
4 | 4 | #include "clang/AST/DeclCXX.h" |
5 | 5 | #include "clang/ASTMatchers/ASTMatchers.h" |
| 6 | +#include "clang/ASTMatchers/ASTMatchersMacros.h" |
6 | 7 | #include "clang/Basic/OperatorKinds.h" |
7 | 8 |
|
8 | 9 | namespace clang::dataflow { |
@@ -34,33 +35,46 @@ bool hasSmartPointerClassShape(const CXXRecordDecl &RD, bool &HasGet, |
34 | 35 | // there should at least be a const overload as well. |
35 | 36 | if (!MD->isConst() || MD->getNumParams() != 0) |
36 | 37 | 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: { |
49 | 56 | IdentifierInfo *II = MD->getIdentifier(); |
50 | 57 | if (II == nullptr) |
51 | 58 | 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() |
61 | 64 | ->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 | + } |
62 | 73 | } |
63 | 74 | } |
| 75 | + default: |
| 76 | + break; |
| 77 | + } |
64 | 78 | } |
65 | 79 |
|
66 | 80 | if (!HasStar || !HasArrow || StarReturnType != ArrowReturnType) |
@@ -105,26 +119,26 @@ AST_MATCHER(clang::CXXRecordDecl, smartPointerClassWithGetOrValue) { |
105 | 119 |
|
106 | 120 | namespace clang::dataflow { |
107 | 121 |
|
108 | | -ast_matchers::internal::Matcher<Stmt> isSmartPointerLikeOperatorStar() { |
| 122 | +ast_matchers::StatementMatcher isSmartPointerLikeOperatorStar() { |
109 | 123 | return cxxOperatorCallExpr( |
110 | 124 | hasOverloadedOperatorName("*"), |
111 | 125 | callee(cxxMethodDecl(parameterCountIs(0), returns(referenceType()), |
112 | 126 | ofClass(smartPointerClassWithGetOrValue())))); |
113 | 127 | } |
114 | 128 |
|
115 | | -ast_matchers::internal::Matcher<Stmt> isSmartPointerLikeOperatorArrow() { |
| 129 | +ast_matchers::StatementMatcher isSmartPointerLikeOperatorArrow() { |
116 | 130 | return cxxOperatorCallExpr( |
117 | 131 | hasOverloadedOperatorName("->"), |
118 | 132 | callee(cxxMethodDecl(parameterCountIs(0), returns(pointerType()), |
119 | 133 | ofClass(smartPointerClassWithGetOrValue())))); |
120 | 134 | } |
121 | | -ast_matchers::internal::Matcher<Stmt> isSmartPointerLikeValueMethodCall() { |
| 135 | +ast_matchers::StatementMatcher isSmartPointerLikeValueMethodCall() { |
122 | 136 | return cxxMemberCallExpr(callee( |
123 | 137 | cxxMethodDecl(parameterCountIs(0), returns(referenceType()), |
124 | 138 | hasName("value"), ofClass(smartPointerClassWithValue())))); |
125 | 139 | } |
126 | 140 |
|
127 | | -ast_matchers::internal::Matcher<Stmt> isSmartPointerLikeGetMethodCall() { |
| 141 | +ast_matchers::StatementMatcher isSmartPointerLikeGetMethodCall() { |
128 | 142 | return cxxMemberCallExpr(callee( |
129 | 143 | cxxMethodDecl(parameterCountIs(0), returns(pointerType()), hasName("get"), |
130 | 144 | ofClass(smartPointerClassWithGet())))); |
|
0 commit comments