clang-tools 20.0.0git
InitVariablesCheck.cpp
Go to the documentation of this file.
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
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
18using namespace clang::ast_matchers;
19
21
22namespace {
23AST_MATCHER(VarDecl, isLocalVarDecl) { return Node.isLocalVarDecl(); }
24} // namespace
25
27 ClangTidyContext *Context)
28 : ClangTidyCheck(Name, Context),
29 IncludeInserter(Options.getLocalOrGlobal("IncludeStyle",
30 utils::IncludeSorter::IS_LLVM),
31 areDiagsSelfContained()),
32 MathHeader(Options.get("MathHeader", "<math.h>")) {}
33
35 Options.store(Opts, "IncludeStyle", IncludeInserter.getStyle());
36 Options.store(Opts, "MathHeader", MathHeader);
37}
38
39void InitVariablesCheck::registerMatchers(MatchFinder *Finder) {
40 std::string BadDecl = "badDecl";
41 Finder->addMatcher(
42 varDecl(unless(hasInitializer(anything())), unless(isInstantiated()),
43 isLocalVarDecl(), unless(isStaticLocal()), isDefinition(),
44 unless(hasParent(cxxCatchStmt())),
45 optionally(hasParent(declStmt(hasParent(
46 cxxForRangeStmt(hasLoopVariable(varDecl().bind(BadDecl))))))),
47 unless(equalsBoundNode(BadDecl)))
48 .bind("vardecl"),
49 this);
50}
51
52void InitVariablesCheck::registerPPCallbacks(const SourceManager &SM,
53 Preprocessor *PP,
54 Preprocessor *ModuleExpanderPP) {
55 IncludeInserter.registerPreprocessor(PP);
56}
57
58void InitVariablesCheck::check(const MatchFinder::MatchResult &Result) {
59 const auto *MatchedDecl = Result.Nodes.getNodeAs<VarDecl>("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 utils::lexer::findNextTerminator(MatchedDecl->getLocation(),
112 *Result.SourceManager,
113 Result.Context->getLangOpts()),
114 *InitializationString);
115 if (AddMathInclude) {
116 Diagnostic << IncludeInserter.createIncludeInsertion(
117 Source.getFileID(MatchedDecl->getBeginLoc()), MathHeader);
118 }
119 }
120}
121} // namespace clang::tidy::cppcoreguidelines
llvm::SmallString< 256U > Name
DiagnosticCallback Diagnostic
::clang::DynTypedNode Node
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
const LangOptions & getLangOpts() const
Returns the language options from the context.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
InitVariablesCheck(StringRef Name, ClangTidyContext *Context)
void registerPreprocessor(Preprocessor *PP)
Registers this with the Preprocessor PP, must be called before this class is used.
std::optional< FixItHint > createIncludeInsertion(FileID FileID, llvm::StringRef Header)
Creates a Header inclusion directive fixit in the File FileID.
IncludeSorter::IncludeStyle getStyle() const
AST_MATCHER(CXXRecordDecl, hasPublicVirtualOrProtectedNonVirtualDestructor)
SourceLocation findNextTerminator(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts)
Definition: LexerUtils.cpp:84
llvm::StringMap< ClangTidyValue > OptionMap