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
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
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
41 void DynamicStaticInitializersCheck::storeOptions(
42 ClangTidyOptions::OptionMap &Opts) {
43 Options.store(Opts, "HeaderFileExtensions", RawStringHeaderFileExtensions);
44 }
45
46 void DynamicStaticInitializersCheck::registerMatchers(MatchFinder *Finder) {
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
Loc
SourceLocation Loc
&#39;#&#39; location in the include directive
Definition: IncludeOrderCheck.cpp:37
clang::ast_matchers
Definition: AbseilMatcher.h:14
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:41
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:54
clang::tidy::utils::parseHeaderFileExtensions
bool parseHeaderFileExtensions(StringRef AllHeaderFileExtensions, HeaderFileExtensionsSet &HeaderFileExtensions, char delimiter)
Parses header file extensions from a semicolon-separated list.
Definition: HeaderFileExtensionsUtils.cpp:41
clang::tidy::utils::isPresumedLocInHeaderFile
bool isPresumedLocInHeaderFile(SourceLocation Loc, SourceManager &SM, const HeaderFileExtensionsSet &HeaderFileExtensions)
Checks whether presumed location of Loc is in header file.
Definition: HeaderFileExtensionsUtils.cpp:25
clang::ast_matchers::AST_MATCHER
AST_MATCHER(Expr, isMacroID)
Definition: PreferIsaOrDynCastInConditionalsCheck.cpp:18
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:47
clang::tidy::ClangTidyCheck::getLangOpts
const LangOptions & getLangOpts() const
Returns the language options from the context.
Definition: ClangTidyCheck.h:188
clang::tidy::bugprone::DynamicStaticInitializersCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: DynamicStaticInitializersCheck.cpp:46
clang::tidy::utils::defaultHeaderFileExtensions
StringRef defaultHeaderFileExtensions()
Returns recommended default value for the list of header file extensions.
Definition: HeaderFileExtensionsUtils.h:40
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:184
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:58
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, std::string > OptionMap
Definition: ClangTidyOptions.h:97
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:77
DynamicStaticInitializersCheck.h
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
Definition: ClangTidyCheck.cpp:21