blob: d9c91b230b1f510d039af5a9a137fc680d85cb72 [file] [log] [blame]
Alexander Kornienko477e5d82016-04-08 09:51:06 +00001//===--- InterfacesGlobalInitCheck.cpp - clang-tidy------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Alexander Kornienko477e5d82016-04-08 09:51:06 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "InterfacesGlobalInitCheck.h"
10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12
13using namespace clang::ast_matchers;
14
15namespace clang {
16namespace tidy {
17namespace cppcoreguidelines {
18
19void InterfacesGlobalInitCheck::registerMatchers(MatchFinder *Finder) {
Alexander Kornienko976e0c02018-11-25 02:41:01 +000020 const auto GlobalVarDecl =
21 varDecl(hasGlobalStorage(),
22 hasDeclContext(anyOf(translationUnitDecl(), // Global scope.
23 namespaceDecl(), // Namespace scope.
24 recordDecl())), // Class scope.
25 unless(isConstexpr()));
Alexander Kornienko477e5d82016-04-08 09:51:06 +000026
27 const auto ReferencesUndefinedGlobalVar = declRefExpr(hasDeclaration(
Alexander Kornienko976e0c02018-11-25 02:41:01 +000028 varDecl(GlobalVarDecl, unless(isDefinition())).bind("referencee")));
Alexander Kornienko477e5d82016-04-08 09:51:06 +000029
30 Finder->addMatcher(
Alexander Kornienko976e0c02018-11-25 02:41:01 +000031 varDecl(GlobalVarDecl, isDefinition(),
Alexander Kornienko477e5d82016-04-08 09:51:06 +000032 hasInitializer(expr(hasDescendant(ReferencesUndefinedGlobalVar))))
33 .bind("var"),
34 this);
35}
36
37void InterfacesGlobalInitCheck::check(const MatchFinder::MatchResult &Result) {
38 const auto *const Var = Result.Nodes.getNodeAs<VarDecl>("var");
39 // For now assume that people who write macros know what they're doing.
40 if (Var->getLocation().isMacroID())
41 return;
42 const auto *const Referencee = Result.Nodes.getNodeAs<VarDecl>("referencee");
43 // If the variable has been defined, we're good.
44 const auto *const ReferenceeDef = Referencee->getDefinition();
45 if (ReferenceeDef != nullptr &&
46 Result.SourceManager->isBeforeInTranslationUnit(
47 ReferenceeDef->getLocation(), Var->getLocation())) {
48 return;
49 }
50 diag(Var->getLocation(),
51 "initializing non-local variable with non-const expression depending on "
52 "uninitialized non-local variable %0")
53 << Referencee;
54}
55
56} // namespace cppcoreguidelines
57} // namespace tidy
58} // namespace clang