• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
9 #include "DynamicStaticInitializersCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 
13 using namespace clang::ast_matchers;
14 
15 namespace clang {
16 namespace tidy {
17 namespace bugprone {
18 
AST_MATCHER(clang::VarDecl,hasConstantDeclaration)19 AST_MATCHER(clang::VarDecl, hasConstantDeclaration) {
20   const Expr *Init = Node.getInit();
21   if (Init && !Init->isValueDependent()) {
22     if (Node.isConstexpr())
23       return true;
24     return Node.evaluateValue();
25   }
26   return false;
27 }
28 
DynamicStaticInitializersCheck(StringRef Name,ClangTidyContext * Context)29 DynamicStaticInitializersCheck::DynamicStaticInitializersCheck(StringRef Name,
30                                                                ClangTidyContext *Context)
31     : ClangTidyCheck(Name, Context),
32       RawStringHeaderFileExtensions(Options.getLocalOrGlobal(
33         "HeaderFileExtensions", utils::defaultHeaderFileExtensions())) {
34   if (!utils::parseFileExtensions(RawStringHeaderFileExtensions,
35                                   HeaderFileExtensions,
36                                   utils::defaultFileExtensionDelimiters())) {
37     this->configurationDiag("Invalid header file extension: '%0'")
38         << RawStringHeaderFileExtensions;
39   }
40 }
41 
storeOptions(ClangTidyOptions::OptionMap & Opts)42 void DynamicStaticInitializersCheck::storeOptions(
43     ClangTidyOptions::OptionMap &Opts) {
44   Options.store(Opts, "HeaderFileExtensions", RawStringHeaderFileExtensions);
45 }
46 
registerMatchers(MatchFinder * Finder)47 void DynamicStaticInitializersCheck::registerMatchers(MatchFinder *Finder) {
48   Finder->addMatcher(
49       varDecl(hasGlobalStorage(), unless(hasConstantDeclaration())).bind("var"),
50       this);
51 }
52 
check(const MatchFinder::MatchResult & Result)53 void DynamicStaticInitializersCheck::check(const MatchFinder::MatchResult &Result) {
54   const auto *Var = Result.Nodes.getNodeAs<VarDecl>("var");
55   SourceLocation Loc = Var->getLocation();
56   if (!Loc.isValid() || !utils::isPresumedLocInHeaderFile(Loc, *Result.SourceManager,
57                                                           HeaderFileExtensions))
58     return;
59   // If the initializer is a constant expression, then the compiler
60   // doesn't have to dynamically initialize it.
61   diag(Loc, "static variable %0 may be dynamically initialized in this header file")
62     << Var;
63 }
64 
65 } // namespace bugprone
66 } // namespace tidy
67 } // namespace clang
68