|
6 | 6 | // |
7 | 7 | //===----------------------------------------------------------------------===// |
8 | 8 |
|
| 9 | +#include <iostream> |
| 10 | + |
9 | 11 | #include "MoveSharedPointerContentsCheck.h" |
10 | 12 | #include "../ClangTidyCheck.h" |
11 | 13 | #include "../utils/Matchers.h" |
@@ -52,15 +54,15 @@ void MoveSharedPointerContentsCheck::registerMatchers(MatchFinder *Finder) { |
52 | 54 |
|
53 | 55 | auto resolvedType = callExpr(anyOf( |
54 | 56 | // Resolved type, direct move. |
55 | | - callExpr( |
56 | | - isStdMove, |
57 | | - hasArgument( |
58 | | - 0, |
59 | | - cxxOperatorCallExpr( |
60 | | - hasOverloadedOperatorName("*"), |
61 | | - hasDescendant(declRefExpr(hasType(qualType(isSharedPointer( |
62 | | - matchers::matchesAnyListedName(SharedPointerClasses)))))), |
63 | | - callee(cxxMethodDecl())))) |
| 57 | + callExpr(isStdMove, |
| 58 | + hasArgument( |
| 59 | + 0, cxxOperatorCallExpr( |
| 60 | + hasOverloadedOperatorName("*"), |
| 61 | + hasArgument( |
| 62 | + 0, declRefExpr(hasType(qualType(isSharedPointer( |
| 63 | + matchers::matchesAnyListedName( |
| 64 | + SharedPointerClasses)))))), |
| 65 | + callee(cxxMethodDecl())))) |
64 | 66 | .bind("call"), |
65 | 67 | // Resolved type, move out of get(). |
66 | 68 | callExpr( |
@@ -113,30 +115,23 @@ void MoveSharedPointerContentsCheck::registerMatchers(MatchFinder *Finder) { |
113 | 115 |
|
114 | 116 | void MoveSharedPointerContentsCheck::check( |
115 | 117 | const MatchFinder::MatchResult &Result) { |
116 | | - clang::SourceLocation Loc; |
117 | | - if (const auto *UnresolvedCall = |
118 | | - Result.Nodes.getNodeAs<CallExpr>("unresolved_call"); |
119 | | - UnresolvedCall != nullptr) { |
120 | | - Loc = UnresolvedCall->getBeginLoc(); |
121 | | - } else if (const auto *UnresolvedGetCall = |
122 | | - Result.Nodes.getNodeAs<CallExpr>("unresolved_get_call"); |
123 | | - UnresolvedGetCall != nullptr) { |
124 | | - Loc = UnresolvedGetCall->getBeginLoc(); |
125 | | - } else if (const auto *GetCall = Result.Nodes.getNodeAs<CallExpr>("get_call"); |
126 | | - GetCall != nullptr) { |
127 | | - Loc = GetCall->getBeginLoc(); |
128 | | - } else if (const auto *Call = Result.Nodes.getNodeAs<CallExpr>("call"); |
129 | | - Call != nullptr) { |
130 | | - Loc = Call->getBeginLoc(); |
131 | | - } else { |
132 | | - return; |
| 118 | + const CallExpr *Call = nullptr; |
| 119 | + for (const llvm::StringRef binding : |
| 120 | + {"unresolved_call", "unresolved_get_call", "get_call", "call"}) { |
| 121 | + if (const auto *C = Result.Nodes.getNodeAs<CallExpr>(binding); |
| 122 | + C != nullptr) { |
| 123 | + Call = C; |
| 124 | + break; |
| 125 | + } |
133 | 126 | } |
134 | 127 |
|
135 | | - if (Loc.isValid()) { |
136 | | - diag(Loc, |
137 | | - "don't move the contents out of a shared pointer, as other accessors " |
138 | | - "expect them to remain in a determinate state"); |
| 128 | + if (Call == nullptr && !Call->getBeginLoc().isValid()) { |
| 129 | + return; |
139 | 130 | } |
| 131 | + |
| 132 | + diag(Call->getBeginLoc(), |
| 133 | + "don't move the contents out of a shared pointer, as other accessors " |
| 134 | + "expect them to remain in a determinate state"); |
140 | 135 | } |
141 | 136 |
|
142 | 137 | } // namespace clang::tidy::bugprone |
0 commit comments