clang-tools 18.0.0git
Go to the documentation of this file.
1//===--- DynamicStaticInitializersCheck.cpp - clang-tidy ------------------===//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
10#include "../utils/FileExtensionsUtils.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
14using namespace clang::ast_matchers;
16namespace clang::tidy::bugprone {
18AST_MATCHER(clang::VarDecl, hasConstantDeclaration) {
19 const Expr *Init = Node.getInit();
20 if (Init && !Init->isValueDependent()) {
21 if (Node.isConstexpr())
22 return true;
23 return Node.evaluateValue();
24 }
25 return false;
29 StringRef Name, ClangTidyContext *Context)
30 : ClangTidyCheck(Name, Context) {
31 std::optional<StringRef> HeaderFileExtensionsOption =
32 Options.get("HeaderFileExtensions");
33 RawStringHeaderFileExtensions =
34 HeaderFileExtensionsOption.value_or(utils::defaultHeaderFileExtensions());
35 if (HeaderFileExtensionsOption) {
36 if (!utils::parseFileExtensions(RawStringHeaderFileExtensions,
37 HeaderFileExtensions,
39 this->configurationDiag("Invalid header file extension: '%0'")
40 << RawStringHeaderFileExtensions;
41 }
42 } else
43 HeaderFileExtensions = Context->getHeaderFileExtensions();
48, "HeaderFileExtensions", RawStringHeaderFileExtensions);
52 Finder->addMatcher(
53 varDecl(hasGlobalStorage(), unless(hasConstantDeclaration())).bind("var"),
54 this);
57void DynamicStaticInitializersCheck::check(const MatchFinder::MatchResult &Result) {
58 const auto *Var = Result.Nodes.getNodeAs<VarDecl>("var");
59 SourceLocation Loc = Var->getLocation();
60 if (!Loc.isValid() || !utils::isPresumedLocInHeaderFile(Loc, *Result.SourceManager,
61 HeaderFileExtensions))
62 return;
63 // If the initializer is a constant expression, then the compiler
64 // doesn't have to dynamically initialize it.
65 diag(Loc, "static variable %0 may be dynamically initialized in this header file")
66 << Var;
69} // namespace clang::tidy::bugprone
llvm::StringRef Name
SourceLocation Loc
::clang::DynTypedNode Node
std::optional< StringRef > get(StringRef LocalName) const
Read a named option from the Context.
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.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
DiagnosticBuilder configurationDiag(StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning) const
Adds a diagnostic to report errors in the check's configuration.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
DynamicStaticInitializersCheck(StringRef Name, ClangTidyContext *Context)
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
AST_MATCHER(clang::VarDecl, hasConstantDeclaration)
bool isPresumedLocInHeaderFile(SourceLocation Loc, SourceManager &SM, const FileExtensionsSet &HeaderFileExtensions)
Checks whether presumed location of Loc is in header file.
StringRef defaultHeaderFileExtensions()
Returns recommended default value for the list of header file extensions.
StringRef defaultFileExtensionDelimiters()
Returns recommended default value for the list of file extension delimiters.
bool parseFileExtensions(StringRef AllFileExtensions, FileExtensionsSet &FileExtensions, StringRef Delimiters)
Parses header file extensions from a semicolon-separated list.
llvm::StringMap< ClangTidyValue > OptionMap