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 Finder->addMatcher(varDecl(unless(hasInitializer(anything())),
33 unless(isInstantiated()), isLocalVarDecl(),
34 unless(isStaticLocal()), isDefinition())
41 Preprocessor *ModuleExpanderPP) {
43 std::make_unique<utils::IncludeInserter>(SM,
getLangOpts(), IncludeStyle);
44 PP->addPPCallbacks(IncludeInserter->CreatePPCallbacks());
48 const auto *MatchedDecl = Result.Nodes.getNodeAs<VarDecl>(
"vardecl");
49 const ASTContext &Context = *Result.Context;
50 const SourceManager &Source = Context.getSourceManager();
67 if (MatchedDecl->getEndLoc().isMacroID())
70 QualType TypePtr = MatchedDecl->getType();
71 const char *InitializationString =
nullptr;
72 bool AddMathInclude =
false;
74 if (TypePtr->isIntegerType())
75 InitializationString =
" = 0";
76 else if (TypePtr->isFloatingType()) {
77 InitializationString =
" = NAN";
78 AddMathInclude =
true;
79 }
else if (TypePtr->isAnyPointerType()) {
81 InitializationString =
" = nullptr";
83 InitializationString =
" = NULL";
86 if (InitializationString) {
88 diag(MatchedDecl->getLocation(),
"variable %0 is not initialized")
90 << FixItHint::CreateInsertion(
91 MatchedDecl->getLocation().getLocWithOffset(
92 MatchedDecl->getName().size()),
93 InitializationString);
95 auto IncludeHint = IncludeInserter->CreateIncludeInsertion(
96 Source.getFileID(MatchedDecl->getBeginLoc()), MathHeader,
false);
98 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.