clang-tools 10.0.0

DispatchOnceNonstaticCheck.cpp
Go to the documentation of this file.
1 //===--- DispatchOnceNonstaticCheck.cpp - clang-tidy ----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "DispatchOnceNonstaticCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/AST/Decl.h"
12 #include "clang/AST/DeclObjC.h"
13 #include "clang/ASTMatchers/ASTMatchFinder.h"
14 #include "clang/ASTMatchers/ASTMatchers.h"
15 #include "clang/Basic/Diagnostic.h"
16
17 using namespace clang::ast_matchers;
18
19 namespace clang {
20 namespace tidy {
21 namespace darwin {
22
23 void DispatchOnceNonstaticCheck::registerMatchers(MatchFinder *Finder) {
24 // Find variables without static or global storage. VarDecls do not include
25 // struct/class members, which are FieldDecls.
26 Finder->addMatcher(
27 varDecl(hasLocalStorage(), hasType(asString("dispatch_once_t")))
28 .bind("non-static-var"),
29 this);
30
31 // Members of structs or classes might be okay, if the use is at static or
32 // global scope. These will be ignored for now. But ObjC ivars can be
33 // flagged immediately, since they cannot be static.
34 Finder->addMatcher(
35 objcIvarDecl(hasType(asString("dispatch_once_t"))).bind("ivar"), this);
36 }
37
38 void DispatchOnceNonstaticCheck::check(const MatchFinder::MatchResult &Result) {
39 if (const auto *VD = Result.Nodes.getNodeAs<VarDecl>("non-static-var")) {
40 if (const auto *PD = dyn_cast<ParmVarDecl>(VD)) {
41 // Catch function/method parameters, as any dispatch_once_t should be
42 // passed by pointer instead.
43 diag(PD->getTypeSpecStartLoc(),
44 "dispatch_once_t variables must have static or global storage "
45 "duration; function parameters should be pointer references");
46 } else {
47 diag(VD->getTypeSpecStartLoc(), "dispatch_once_t variables must have "
48 "static or global storage duration")
49 << FixItHint::CreateInsertion(VD->getTypeSpecStartLoc(), "static ");
50 }
51 }
52
53 if (const auto *D = Result.Nodes.getNodeAs<ObjCIvarDecl>("ivar")) {
54 diag(D->getTypeSpecStartLoc(),
55 "dispatch_once_t variables must have static or global storage "
56 "duration and cannot be Objective-C instance variables");
57 }
58 }
59
60 } // namespace darwin
61 } // namespace tidy
62 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:14
DispatchOnceNonstaticCheck.h
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27