11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/Lex/PPCallbacks.h"
14#include "clang/Lex/Preprocessor.h"
28 IncludeInserter(Options.getLocalOrGlobal(
"IncludeStyle",
29 utils::IncludeSorter::IS_LLVM),
30 areDiagsSelfContained()),
31 MathHeader(Options.get(
"MathHeader",
"<math.h>")) {}
39 std::string BadDecl =
"badDecl";
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)))
53 Preprocessor *ModuleExpanderPP) {
58 const auto *MatchedDecl = Result.Nodes.getNodeAs<VarDecl>(
"vardecl");
59 const ASTContext &Context = *Result.Context;
60 const SourceManager &Source = Context.getSourceManager();
63 if (MatchedDecl->isInvalidDecl())
81 if (MatchedDecl->getEndLoc().isMacroID())
84 QualType TypePtr = MatchedDecl->getType();
85 std::optional<const char *> InitializationString;
86 bool AddMathInclude =
false;
88 if (TypePtr->isEnumeralType())
89 InitializationString =
nullptr;
90 else if (TypePtr->isBooleanType())
91 InitializationString =
" = false";
92 else if (TypePtr->isIntegerType())
93 InitializationString =
" = 0";
94 else if (TypePtr->isFloatingType()) {
95 InitializationString =
" = NAN";
96 AddMathInclude =
true;
97 }
else if (TypePtr->isAnyPointerType()) {
99 InitializationString =
" = nullptr";
101 InitializationString =
" = NULL";
104 if (InitializationString) {
106 diag(MatchedDecl->getLocation(),
"variable %0 is not initialized")
108 if (*InitializationString !=
nullptr)
110 MatchedDecl->getLocation().getLocWithOffset(
111 MatchedDecl->getName().size()),
112 *InitializationString);
113 if (AddMathInclude) {
115 Source.getFileID(MatchedDecl->getBeginLoc()), MathHeader);
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)
llvm::StringMap< ClangTidyValue > OptionMap