@@ -22,19 +22,20 @@ using namespace clang::ast_matchers;
22
22
23
23
namespace clang ::tidy::modernize {
24
24
25
+ static bool isLockGuardDecl (const NamedDecl *Decl) {
26
+ return Decl->getDeclName ().isIdentifier () &&
27
+ Decl->getName () == " lock_guard" && Decl->isInStdNamespace ();
28
+ }
29
+
25
30
static bool isLockGuard (const QualType &Type) {
26
31
if (const auto *Record = Type->getAs <RecordType>())
27
- if (const RecordDecl *Decl = Record->getDecl ()) {
28
- assert (Decl->getDeclName ().isIdentifier () && " Decl is not identifier" );
29
- return Decl->getName () == " lock_guard" && Decl->isInStdNamespace ();
30
- }
32
+ if (const RecordDecl *Decl = Record->getDecl ())
33
+ return isLockGuardDecl (Decl);
31
34
32
35
if (const auto *TemplateSpecType = Type->getAs <TemplateSpecializationType>())
33
36
if (const TemplateDecl *Decl =
34
- TemplateSpecType->getTemplateName ().getAsTemplateDecl ()) {
35
- assert (Decl->getDeclName ().isIdentifier () && " Decl is not identifier" );
36
- return Decl->getName () == " lock_guard" && Decl->isInStdNamespace ();
37
- }
37
+ TemplateSpecType->getTemplateName ().getAsTemplateDecl ())
38
+ return isLockGuardDecl (Decl);
38
39
39
40
return false ;
40
41
}
@@ -118,48 +119,9 @@ static SourceRange getLockGuardNameRange(const TypeSourceInfo *SourceInfo) {
118
119
TemplateLoc.getLAngleLoc ().getLocWithOffset (-1 ));
119
120
}
120
121
121
- namespace {
122
-
123
- AST_MATCHER_P (CompoundStmt, hasMultiple, ast_matchers::internal::Matcher<Stmt>,
124
- InnerMatcher) {
125
- size_t Count = 0 ;
126
-
127
- for (const Stmt *Stmt : Node.body ())
128
- if (InnerMatcher.matches (*Stmt, Finder, Builder))
129
- Count++;
130
-
131
- return Count > 1 ;
132
- }
133
-
134
- AST_MATCHER_P (CompoundStmt, hasSingle, ast_matchers::internal::Matcher<Stmt>,
135
- InnerMatcher) {
136
- size_t Count = 0 ;
137
- ast_matchers::internal::BoundNodesTreeBuilder Result;
138
-
139
- for (const Stmt *Stmt : Node.body ()) {
140
- ast_matchers::internal::BoundNodesTreeBuilder TB (*Builder);
141
- if (InnerMatcher.matches (*Stmt, Finder, &TB)) {
142
- Count++;
143
- if (Count == 1 )
144
- Result.addMatch (TB);
145
- }
146
- }
147
-
148
- if (Count > 1 ) {
149
- Builder->removeBindings (
150
- [](const ast_matchers::internal::BoundNodesMap &) { return true ; });
151
- return false ;
152
- }
153
-
154
- *Builder = std::move (Result);
155
- return true ;
156
- }
157
-
158
- const StringRef UseScopedLockMessage =
122
+ const static StringRef UseScopedLockMessage =
159
123
" use 'std::scoped_lock' instead of 'std::lock_guard'" ;
160
124
161
- } // namespace
162
-
163
125
UseScopedLockCheck::UseScopedLockCheck (StringRef Name,
164
126
ClangTidyContext *Context)
165
127
: ClangTidyCheck(Name, Context),
@@ -187,14 +149,19 @@ void UseScopedLockCheck::registerMatchers(MatchFinder *Finder) {
187
149
Finder->addMatcher (
188
150
compoundStmt (
189
151
unless (isExpansionInSystemHeader ()),
190
- hasSingle (declStmt (has (LockVarDecl.bind (" lock-decl-single" ))))),
152
+ has (declStmt (has (LockVarDecl)).bind (" lock-decl-single" )),
153
+ unless (has (declStmt (unless (equalsBoundNode (" lock-decl-single" )),
154
+ has (LockVarDecl))))),
191
155
this );
192
156
}
193
157
194
- Finder->addMatcher (compoundStmt (unless (isExpansionInSystemHeader ()),
195
- hasMultiple (declStmt (has (LockVarDecl))))
196
- .bind (" block-multiple" ),
197
- this );
158
+ Finder->addMatcher (
159
+ compoundStmt (unless (isExpansionInSystemHeader ()),
160
+ has (declStmt (has (LockVarDecl)).bind (" lock-decl-multiple" )),
161
+ has (declStmt (unless (equalsBoundNode (" lock-decl-multiple" )),
162
+ has (LockVarDecl))))
163
+ .bind (" block-multiple" ),
164
+ this );
198
165
199
166
if (WarnOnUsingAndTypedef) {
200
167
// Match 'typedef std::lock_guard<std::mutex> Lock'
@@ -222,8 +189,9 @@ void UseScopedLockCheck::registerMatchers(MatchFinder *Finder) {
222
189
}
223
190
224
191
void UseScopedLockCheck::check (const MatchFinder::MatchResult &Result) {
225
- if (const auto *Decl = Result.Nodes .getNodeAs <VarDecl>(" lock-decl-single" )) {
226
- diagOnSingleLock (Decl, Result);
192
+ if (const auto *DS = Result.Nodes .getNodeAs <DeclStmt>(" lock-decl-single" )) {
193
+ llvm::SmallVector<const VarDecl *> Decls = getLockGuardsFromDecl (DS);
194
+ diagOnMultipleLocks ({Decls}, Result);
227
195
return ;
228
196
}
229
197
0 commit comments