11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 18 namespace cppcoreguidelines {
21 AST_MATCHER(VarDecl, isLocalVarDecl) {
return Node.isLocalVarDecl(); }
24 InitVariablesCheck::InitVariablesCheck(StringRef
Name,
27 IncludeStyle(utils::IncludeSorter::parseIncludeStyle(
28 Options.getLocalOrGlobal(
"IncludeStyle",
"llvm"))),
29 MathHeader(Options.get(
"MathHeader",
"math.h")) {}
32 std::string BadDecl =
"badDecl";
34 varDecl(unless(hasInitializer(anything())), unless(isInstantiated()),
35 isLocalVarDecl(), unless(isStaticLocal()), isDefinition(),
36 optionally(hasParent(declStmt(hasParent(
37 cxxForRangeStmt(hasLoopVariable(varDecl().bind(BadDecl))))))),
38 unless(equalsBoundNode(BadDecl)))
45 Preprocessor *ModuleExpanderPP) {
47 std::make_unique<utils::IncludeInserter>(SM,
getLangOpts(), IncludeStyle);
48 PP->addPPCallbacks(IncludeInserter->CreatePPCallbacks());
52 const auto *MatchedDecl = Result.Nodes.getNodeAs<VarDecl>(
"vardecl");
53 const ASTContext &Context = *Result.Context;
54 const SourceManager &Source = Context.getSourceManager();
71 if (MatchedDecl->getEndLoc().isMacroID())
74 QualType TypePtr = MatchedDecl->getType();
75 const char *InitializationString =
nullptr;
76 bool AddMathInclude =
false;
78 if (TypePtr->isIntegerType())
79 InitializationString =
" = 0";
80 else if (TypePtr->isFloatingType()) {
81 InitializationString =
" = NAN";
82 AddMathInclude =
true;
83 }
else if (TypePtr->isAnyPointerType()) {
85 InitializationString =
" = nullptr";
87 InitializationString =
" = NULL";
90 if (InitializationString) {
92 diag(MatchedDecl->getLocation(),
"variable %0 is not initialized")
94 << FixItHint::CreateInsertion(
95 MatchedDecl->getLocation().getLocWithOffset(
96 MatchedDecl->getName().size()),
97 InitializationString);
99 auto IncludeHint = IncludeInserter->CreateIncludeInsertion(
100 Source.getFileID(MatchedDecl->getBeginLoc()), MathHeader,
false);
102 Diagnostic << *IncludeHint;
AST_MATCHER(Expr, isMacroID)
Base class for all clang-tidy checks.
const LangOptions & getLangOpts() const
Returns the language options from the context.
static constexpr llvm::StringLiteral Name
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
Override this to register PPCallbacks in the preprocessor.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.