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