clang-tools  14.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 "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.evaluateValue();
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::parseFileExtensions(RawStringHeaderFileExtensions,
35  HeaderFileExtensions,
37  this->configurationDiag("Invalid header file extension: '%0'")
38  << RawStringHeaderFileExtensions;
39  }
40 }
41 
44  Options.store(Opts, "HeaderFileExtensions", RawStringHeaderFileExtensions);
45 }
46 
48  Finder->addMatcher(
49  varDecl(hasGlobalStorage(), unless(hasConstantDeclaration())).bind("var"),
50  this);
51 }
52 
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
Loc
SourceLocation Loc
Definition: KernelNameRestrictionCheck.cpp:45
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:115
clang::tidy::bugprone::DynamicStaticInitializersCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: DynamicStaticInitializersCheck.cpp:47
clang::tidy::bugprone::DynamicStaticInitializersCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: DynamicStaticInitializersCheck.cpp:53
clang::tidy::bugprone::DynamicStaticInitializersCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: DynamicStaticInitializersCheck.cpp:42
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:54
clang::tidy::utils::isPresumedLocInHeaderFile
bool isPresumedLocInHeaderFile(SourceLocation Loc, SourceManager &SM, const FileExtensionsSet &HeaderFileExtensions)
Checks whether presumed location of Loc is in header file.
Definition: FileExtensionsUtils.cpp:23
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::tidy::utils::parseFileExtensions
bool parseFileExtensions(StringRef AllFileExtensions, FileExtensionsSet &FileExtensions, StringRef Delimiters)
Parses header file extensions from a semicolon-separated list.
Definition: FileExtensionsUtils.cpp:35
DynamicStaticInitializersCheck.h
clang::tidy::utils::defaultFileExtensionDelimiters
StringRef defaultFileExtensionDelimiters()
Returns recommended default value for the list of file extension delimiters.
Definition: FileExtensionsUtils.h:48
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:416
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:71
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Definition: ClangTidyCheck.cpp:25
clang::ast_matchers::AST_MATCHER
AST_MATCHER(Expr, isMacroID)
Definition: PreferIsaOrDynCastInConditionalsCheck.cpp:19
clang::tidy::ClangTidyCheck::configurationDiag
DiagnosticBuilder configurationDiag(StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning) const
Adds a diagnostic to report errors in the check's configuration.
Definition: ClangTidyCheck.cpp:36
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::ClangTidyCheck::OptionsView::store
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.
Definition: ClangTidyCheck.cpp:120
clang::tidy::utils::defaultHeaderFileExtensions
StringRef defaultHeaderFileExtensions()
Returns recommended default value for the list of header file extensions.
Definition: FileExtensionsUtils.h:38