Yuanfang Chen | f24c1e6 | 2019-08-21 20:00:01 +0000 | [diff] [blame] | 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())) { |
Jonathan Roelofs | 3486cc0 | 2020-02-25 10:41:32 -0700 | [diff] [blame] | 34 | if (!utils::parseFileExtensions(RawStringHeaderFileExtensions, |
Jonathan Roelofs | 47caa69 | 2020-03-04 09:55:03 -0700 | [diff] [blame^] | 35 | HeaderFileExtensions, |
| 36 | utils::defaultFileExtensionDelimiters())) { |
Yuanfang Chen | f24c1e6 | 2019-08-21 20:00:01 +0000 | [diff] [blame] | 37 | llvm::errs() << "Invalid header file extension: " |
| 38 | << RawStringHeaderFileExtensions << "\n"; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | void DynamicStaticInitializersCheck::storeOptions( |
| 43 | ClangTidyOptions::OptionMap &Opts) { |
| 44 | Options.store(Opts, "HeaderFileExtensions", RawStringHeaderFileExtensions); |
| 45 | } |
| 46 | |
| 47 | void DynamicStaticInitializersCheck::registerMatchers(MatchFinder *Finder) { |
Yuanfang Chen | f24c1e6 | 2019-08-21 20:00:01 +0000 | [diff] [blame] | 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 |