1 //===--- InitVariablesCheck.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 "InitVariablesCheck.h"
10 
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 
14 using namespace clang::ast_matchers;
15 
16 namespace clang {
17 namespace tidy {
18 namespace cppcoreguidelines {
19 
20 namespace {
21 AST_MATCHER(VarDecl, isLocalVarDecl) { return Node.isLocalVarDecl(); }
22 } // namespace
23 
24 InitVariablesCheck::InitVariablesCheck(StringRef Name,
25                                        ClangTidyContext *Context)
26     : ClangTidyCheck(Name, Context),
27       IncludeStyle(Options.getLocalOrGlobal("IncludeStyle",
28                                             utils::IncludeSorter::getMapping(),
29                                             utils::IncludeSorter::IS_LLVM)),
30       MathHeader(Options.get("MathHeader", "math.h")) {}
31 
32 void InitVariablesCheck::registerMatchers(MatchFinder *Finder) {
33   std::string BadDecl = "badDecl";
34   Finder->addMatcher(
35       varDecl(unless(hasInitializer(anything())), unless(isInstantiated()),
36               isLocalVarDecl(), unless(isStaticLocal()), isDefinition(),
37               optionally(hasParent(declStmt(hasParent(
38                   cxxForRangeStmt(hasLoopVariable(varDecl().bind(BadDecl))))))),
39               unless(equalsBoundNode(BadDecl)))
40           .bind("vardecl"),
41       this);
42 }
43 
44 void InitVariablesCheck::registerPPCallbacks(const SourceManager &SM,
45                                              Preprocessor *PP,
46                                              Preprocessor *ModuleExpanderPP) {
47   IncludeInserter =
48       std::make_unique<utils::IncludeInserter>(SM, getLangOpts(), IncludeStyle);
49   PP->addPPCallbacks(IncludeInserter->CreatePPCallbacks());
50 }
51 
52 void InitVariablesCheck::check(const MatchFinder::MatchResult &Result) {
53   const auto *MatchedDecl = Result.Nodes.getNodeAs<VarDecl>("vardecl");
54   const ASTContext &Context = *Result.Context;
55   const SourceManager &Source = Context.getSourceManager();
56 
57   // We want to warn about cases where the type name
58   // comes from a macro like this:
59   //
60   // TYPENAME_FROM_MACRO var;
61   //
62   // but not if the entire declaration comes from
63   // one:
64   //
65   // DEFINE_SOME_VARIABLE();
66   //
67   // or if the definition comes from a macro like SWAP
68   // that uses an internal temporary variable.
69   //
70   // Thus check that the variable name does
71   // not come from a macro expansion.
72   if (MatchedDecl->getEndLoc().isMacroID())
73     return;
74 
75   QualType TypePtr = MatchedDecl->getType();
76   const char *InitializationString = nullptr;
77   bool AddMathInclude = false;
78 
79   if (TypePtr->isIntegerType())
80     InitializationString = " = 0";
81   else if (TypePtr->isFloatingType()) {
82     InitializationString = " = NAN";
83     AddMathInclude = true;
84   } else if (TypePtr->isAnyPointerType()) {
85     if (getLangOpts().CPlusPlus11)
86       InitializationString = " = nullptr";
87     else
88       InitializationString = " = NULL";
89   }
90 
91   if (InitializationString) {
92     auto Diagnostic =
93         diag(MatchedDecl->getLocation(), "variable %0 is not initialized")
94         << MatchedDecl
95         << FixItHint::CreateInsertion(
96                MatchedDecl->getLocation().getLocWithOffset(
97                    MatchedDecl->getName().size()),
98                InitializationString);
99     if (AddMathInclude) {
100       Diagnostic << IncludeInserter->CreateIncludeInsertion(
101           Source.getFileID(MatchedDecl->getBeginLoc()), MathHeader, false);
102     }
103   }
104 }
105 
106 } // namespace cppcoreguidelines
107 } // namespace tidy
108 } // namespace clang
109