clang-tools 20.0.0git
DynamicStaticInitializersCheck.cpp
Go to the documentation of this file.
1//===--- DynamicStaticInitializersCheck.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
10#include "../utils/FileExtensionsUtils.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13
14using namespace clang::ast_matchers;
15
16namespace clang::tidy::bugprone {
17
18AST_MATCHER(clang::VarDecl, hasConstantDeclaration) {
19 const Expr *Init = Node.getInit();
20 if (Init && !Init->isValueDependent()) {
21 if (Node.isConstexpr())
22 return true;
23 return Node.evaluateValue();
24 }
25 return false;
26}
27
29 StringRef Name, ClangTidyContext *Context)
30 : ClangTidyCheck(Name, Context),
31 HeaderFileExtensions(Context->getHeaderFileExtensions()) {}
32
34 Finder->addMatcher(
35 varDecl(hasGlobalStorage(), unless(hasConstantDeclaration())).bind("var"),
36 this);
37}
38
39void DynamicStaticInitializersCheck::check(const MatchFinder::MatchResult &Result) {
40 const auto *Var = Result.Nodes.getNodeAs<VarDecl>("var");
41 SourceLocation Loc = Var->getLocation();
42 if (!Loc.isValid() || !utils::isPresumedLocInHeaderFile(Loc, *Result.SourceManager,
43 HeaderFileExtensions))
44 return;
45 // If the initializer is a constant expression, then the compiler
46 // doesn't have to dynamically initialize it.
47 diag(Loc, "static variable %0 may be dynamically initialized in this header file")
48 << Var;
49}
50
51} // namespace clang::tidy::bugprone
llvm::SmallString< 256U > Name
SourceLocation Loc
::clang::DynTypedNode Node
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
DynamicStaticInitializersCheck(StringRef Name, ClangTidyContext *Context)
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.
AST_MATCHER(clang::VarDecl, hasConstantDeclaration)
bool isPresumedLocInHeaderFile(SourceLocation Loc, SourceManager &SM, const FileExtensionsSet &HeaderFileExtensions)
Checks whether presumed location of Loc is in header file.