1 //===--- DynamicStaticInitializersCheck.cpp - clang-tidy ------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "DynamicStaticInitializersCheck.h"
10 #include "../utils/FileExtensionsUtils.h"
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
14 using namespace clang::ast_matchers
;
16 namespace clang::tidy::bugprone
{
18 AST_MATCHER(clang::VarDecl
, hasConstantDeclaration
) {
19 const Expr
*Init
= Node
.getInit();
20 if (Init
&& !Init
->isValueDependent()) {
21 if (Node
.isConstexpr())
23 return Node
.evaluateValue();
28 DynamicStaticInitializersCheck::DynamicStaticInitializersCheck(
29 StringRef Name
, ClangTidyContext
*Context
)
30 : ClangTidyCheck(Name
, Context
),
31 HeaderFileExtensions(Context
->getHeaderFileExtensions()) {}
33 void DynamicStaticInitializersCheck::registerMatchers(MatchFinder
*Finder
) {
35 varDecl(hasGlobalStorage(), unless(hasConstantDeclaration())).bind("var"),
39 void 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
))
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")
51 } // namespace clang::tidy::bugprone