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"
25 varDecl(hasLocalStorage(), hasType(asString(
"dispatch_once_t")))
26 .bind(
"non-static-var"),
33 objcIvarDecl(hasType(asString(
"dispatch_once_t"))).bind(
"ivar"),
this);
37 if (
const auto *VD = Result.Nodes.getNodeAs<VarDecl>(
"non-static-var")) {
38 if (
const auto *PD = dyn_cast<ParmVarDecl>(VD)) {
41 diag(PD->getTypeSpecStartLoc(),
42 "dispatch_once_t variables must have static or global storage "
43 "duration; function parameters should be pointer references");
45 diag(VD->getTypeSpecStartLoc(),
"dispatch_once_t variables must have "
46 "static or global storage duration")
47 << FixItHint::CreateInsertion(VD->getTypeSpecStartLoc(),
"static ");
51 if (
const auto *D = Result.Nodes.getNodeAs<ObjCIvarDecl>(
"ivar")) {
52 diag(D->getTypeSpecStartLoc(),
53 "dispatch_once_t variables must have static or global storage "
54 "duration and cannot be Objective-C instance variables");
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.