clang-tools  11.0.0
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 
9 #include "InitVariablesCheck.h"
10 
11 #include "clang/AST/ASTContext.h"
12 #include "clang/ASTMatchers/ASTMatchFinder.h"
13 #include "clang/Lex/PPCallbacks.h"
14 #include "clang/Lex/Preprocessor.h"
15 
16 using namespace clang::ast_matchers;
17 
18 namespace clang {
19 namespace tidy {
20 namespace 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  IncludeStyle(Options.getLocalOrGlobal("IncludeStyle",
30  utils::IncludeSorter::IS_LLVM)),
31  MathHeader(Options.get("MathHeader", "math.h")) {}
32 
34  Options.store(Opts, "IncludeStyle", IncludeStyle);
35  Options.store(Opts, "MathHeader", MathHeader);
36 }
37 
38 void InitVariablesCheck::registerMatchers(MatchFinder *Finder) {
39  std::string BadDecl = "badDecl";
40  Finder->addMatcher(
41  varDecl(unless(hasInitializer(anything())), unless(isInstantiated()),
42  isLocalVarDecl(), unless(isStaticLocal()), isDefinition(),
43  unless(hasParent(cxxCatchStmt())),
44  optionally(hasParent(declStmt(hasParent(
45  cxxForRangeStmt(hasLoopVariable(varDecl().bind(BadDecl))))))),
46  unless(equalsBoundNode(BadDecl)))
47  .bind("vardecl"),
48  this);
49 }
50 
51 void InitVariablesCheck::registerPPCallbacks(const SourceManager &SM,
52  Preprocessor *PP,
53  Preprocessor *ModuleExpanderPP) {
54  IncludeInserter =
55  std::make_unique<utils::IncludeInserter>(SM, getLangOpts(), IncludeStyle);
56  PP->addPPCallbacks(IncludeInserter->CreatePPCallbacks());
57 }
58 
59 void InitVariablesCheck::check(const MatchFinder::MatchResult &Result) {
60  const auto *MatchedDecl = Result.Nodes.getNodeAs<VarDecl>("vardecl");
61  const ASTContext &Context = *Result.Context;
62  const SourceManager &Source = Context.getSourceManager();
63 
64  // We want to warn about cases where the type name
65  // comes from a macro like this:
66  //
67  // TYPENAME_FROM_MACRO var;
68  //
69  // but not if the entire declaration comes from
70  // one:
71  //
72  // DEFINE_SOME_VARIABLE();
73  //
74  // or if the definition comes from a macro like SWAP
75  // that uses an internal temporary variable.
76  //
77  // Thus check that the variable name does
78  // not come from a macro expansion.
79  if (MatchedDecl->getEndLoc().isMacroID())
80  return;
81 
82  QualType TypePtr = MatchedDecl->getType();
83  const char *InitializationString = nullptr;
84  bool AddMathInclude = false;
85 
86  if (TypePtr->isIntegerType())
87  InitializationString = " = 0";
88  else if (TypePtr->isFloatingType()) {
89  InitializationString = " = NAN";
90  AddMathInclude = true;
91  } else if (TypePtr->isAnyPointerType()) {
92  if (getLangOpts().CPlusPlus11)
93  InitializationString = " = nullptr";
94  else
95  InitializationString = " = NULL";
96  }
97 
98  if (InitializationString) {
99  auto Diagnostic =
100  diag(MatchedDecl->getLocation(), "variable %0 is not initialized")
101  << MatchedDecl
102  << FixItHint::CreateInsertion(
103  MatchedDecl->getLocation().getLocWithOffset(
104  MatchedDecl->getName().size()),
105  InitializationString);
106  if (AddMathInclude) {
107  Diagnostic << IncludeInserter->CreateIncludeInsertion(
108  Source.getFileID(MatchedDecl->getBeginLoc()), MathHeader, false);
109  }
110  }
111 }
112 } // namespace cppcoreguidelines
113 } // namespace tidy
114 } // namespace clang
clang::tidy::cppcoreguidelines::InitVariablesCheck::registerPPCallbacks
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
Definition: InitVariablesCheck.cpp:51
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:114
InitVariablesCheck.h
clang::tidy::ClangTidyCheck::getLangOpts
const LangOptions & getLangOpts() const
Returns the language options from the context.
Definition: ClangTidyCheck.h:475
clang::ast_matchers
Definition: AbseilMatcher.h:14
clang::tidy::cppcoreguidelines::InitVariablesCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: InitVariablesCheck.cpp:59
Diagnostic
DiagnosticCallback Diagnostic
Definition: ConfigCompile.cpp:71
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:471
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:76
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Definition: ClangTidyCheck.cpp:55
clang::ast_matchers::AST_MATCHER
AST_MATCHER(Expr, isMacroID)
Definition: PreferIsaOrDynCastInConditionalsCheck.cpp:19
clang::tidy::bugprone::PP
static Preprocessor * PP
Definition: BadSignalToKillThreadCheck.cpp:29
clang::tidy::cppcoreguidelines::InitVariablesCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: InitVariablesCheck.cpp:33
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::ClangTidyCheck::OptionsView::store
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.
Definition: ClangTidyCheck.cpp:152
clang::tidy::cppcoreguidelines::InitVariablesCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: InitVariablesCheck.cpp:38
clang::tidy::ClangTidyOptions::OptionMap
std::map< std::string, ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:111