blob: 59993cba34520f00d8628f81c13be26ff6521fd1 [file] [log] [blame]
Alexander Kornienko477e5d82016-04-08 09:51:06 +00001//===--- InterfacesGlobalInitCheck.cpp - clang-tidy------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "InterfacesGlobalInitCheck.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13
14using namespace clang::ast_matchers;
15
16namespace clang {
17namespace tidy {
18namespace cppcoreguidelines {
19
20void InterfacesGlobalInitCheck::registerMatchers(MatchFinder *Finder) {
Alexander Kornienko976e0c02018-11-25 02:41:01 +000021 const auto GlobalVarDecl =
22 varDecl(hasGlobalStorage(),
23 hasDeclContext(anyOf(translationUnitDecl(), // Global scope.
24 namespaceDecl(), // Namespace scope.
25 recordDecl())), // Class scope.
26 unless(isConstexpr()));
Alexander Kornienko477e5d82016-04-08 09:51:06 +000027
28 const auto ReferencesUndefinedGlobalVar = declRefExpr(hasDeclaration(
Alexander Kornienko976e0c02018-11-25 02:41:01 +000029 varDecl(GlobalVarDecl, unless(isDefinition())).bind("referencee")));
Alexander Kornienko477e5d82016-04-08 09:51:06 +000030
31 Finder->addMatcher(
Alexander Kornienko976e0c02018-11-25 02:41:01 +000032 varDecl(GlobalVarDecl, isDefinition(),
Alexander Kornienko477e5d82016-04-08 09:51:06 +000033 hasInitializer(expr(hasDescendant(ReferencesUndefinedGlobalVar))))
34 .bind("var"),
35 this);
36}
37
38void InterfacesGlobalInitCheck::check(const MatchFinder::MatchResult &Result) {
39 const auto *const Var = Result.Nodes.getNodeAs<VarDecl>("var");
40 // For now assume that people who write macros know what they're doing.
41 if (Var->getLocation().isMacroID())
42 return;
43 const auto *const Referencee = Result.Nodes.getNodeAs<VarDecl>("referencee");
44 // If the variable has been defined, we're good.
45 const auto *const ReferenceeDef = Referencee->getDefinition();
46 if (ReferenceeDef != nullptr &&
47 Result.SourceManager->isBeforeInTranslationUnit(
48 ReferenceeDef->getLocation(), Var->getLocation())) {
49 return;
50 }
51 diag(Var->getLocation(),
52 "initializing non-local variable with non-const expression depending on "
53 "uninitialized non-local variable %0")
54 << Referencee;
55}
56
57} // namespace cppcoreguidelines
58} // namespace tidy
59} // namespace clang