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"
29 IncludeInserter(Options.getLocalOrGlobal(
"IncludeStyle",
30 utils::IncludeSorter::IS_LLVM),
31 areDiagsSelfContained()),
32 MathHeader(Options.get(
"MathHeader",
"<math.h>")) {}
40 std::string BadDecl =
"badDecl";
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)))
54 Preprocessor *ModuleExpanderPP) {
59 const auto *MatchedDecl = Result.Nodes.getNodeAs<VarDecl>(
"vardecl");
60 const ASTContext &Context = *Result.Context;
61 const SourceManager &Source = Context.getSourceManager();
64 if (MatchedDecl->isInvalidDecl())
82 if (MatchedDecl->getEndLoc().isMacroID())
85 QualType TypePtr = MatchedDecl->getType();
86 std::optional<const char *> InitializationString;
87 bool AddMathInclude =
false;
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()) {
100 InitializationString =
" = nullptr";
102 InitializationString =
" = NULL";
105 if (InitializationString) {
107 diag(MatchedDecl->getLocation(),
"variable %0 is not initialized")
109 if (*InitializationString !=
nullptr)
112 *Result.SourceManager,
113 Result.Context->getLangOpts()),
114 *InitializationString);
115 if (AddMathInclude) {
117 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)
SourceLocation findNextTerminator(SourceLocation Start, const SourceManager &SM, const LangOptions &LangOpts)
llvm::StringMap< ClangTidyValue > OptionMap