clang-tools  10.0.0
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 "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 
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.checkInitIsICE();
25  }
26  return false;
27 }
28 
29 DynamicStaticInitializersCheck::DynamicStaticInitializersCheck(StringRef Name,
30  ClangTidyContext *Context)
31  : ClangTidyCheck(Name, Context),
32  RawStringHeaderFileExtensions(Options.getLocalOrGlobal(
33  "HeaderFileExtensions", utils::defaultHeaderFileExtensions())) {
34  if (!utils::parseHeaderFileExtensions(RawStringHeaderFileExtensions,
35  HeaderFileExtensions, ',')) {
36  llvm::errs() << "Invalid header file extension: "
37  << RawStringHeaderFileExtensions << "\n";
38  }
39 }
40 
43  Options.store(Opts, "HeaderFileExtensions", RawStringHeaderFileExtensions);
44 }
45 
47  if (!getLangOpts().CPlusPlus || getLangOpts().ThreadsafeStatics)
48  return;
49  Finder->addMatcher(
50  varDecl(hasGlobalStorage(), unless(hasConstantDeclaration())).bind("var"),
51  this);
52 }
53 
54 void DynamicStaticInitializersCheck::check(const MatchFinder::MatchResult &Result) {
55  const auto *Var = Result.Nodes.getNodeAs<VarDecl>("var");
56  SourceLocation Loc = Var->getLocation();
57  if (!Loc.isValid() || !utils::isPresumedLocInHeaderFile(Loc, *Result.SourceManager,
58  HeaderFileExtensions))
59  return;
60  // If the initializer is a constant expression, then the compiler
61  // doesn't have to dynamically initialize it.
62  diag(Loc, "static variable %0 may be dynamically initialized in this header file")
63  << Var;
64 }
65 
66 } // namespace bugprone
67 } // namespace tidy
68 } // namespace clang
SourceLocation Loc
&#39;#&#39; location in the include directive
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
bool parseHeaderFileExtensions(StringRef AllHeaderFileExtensions, HeaderFileExtensionsSet &HeaderFileExtensions, char delimiter)
Parses header file extensions from a semicolon-separated list.
bool isPresumedLocInHeaderFile(SourceLocation Loc, SourceManager &SM, const HeaderFileExtensionsSet &HeaderFileExtensions)
Checks whether presumed location of Loc is in header file.
Base class for all clang-tidy checks.
const LangOptions & getLangOpts() const
Returns the language options from the context.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
StringRef defaultHeaderFileExtensions()
Returns recommended default value for the list of header file extensions.
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
static constexpr llvm::StringLiteral Name
std::map< std::string, std::string > OptionMap
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.