| 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 | #include <optional> |
| 16 | |
| 17 | using namespace clang::ast_matchers; |
| 18 | |
| 19 | namespace clang::tidy::cppcoreguidelines { |
| 20 | |
| 21 | namespace { |
| 22 | AST_MATCHER(VarDecl, isLocalVarDecl) { return Node.isLocalVarDecl(); } |
| 23 | } // namespace |
| 24 | |
| 25 | InitVariablesCheck::InitVariablesCheck(StringRef Name, |
| 26 | ClangTidyContext *Context) |
| 27 | : ClangTidyCheck(Name, Context), |
| 28 | IncludeInserter(Options.getLocalOrGlobal("IncludeStyle", |
| 29 | utils::IncludeSorter::IS_LLVM), |
| 30 | areDiagsSelfContained()), |
| 31 | MathHeader(Options.get("MathHeader", "<math.h>")) {} |
| 32 | |
| 33 | void InitVariablesCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { |
| 34 | Options.store(Opts, "IncludeStyle", IncludeInserter.getStyle()); |
| 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.registerPreprocessor(PP); |
| 55 | } |
| 56 | |
| 57 | void InitVariablesCheck::check(const MatchFinder::MatchResult &Result) { |
| 58 | const auto *MatchedDecl = Result.Nodes.getNodeAs<VarDecl>("vardecl"); |
| 59 | const ASTContext &Context = *Result.Context; |
| 60 | const SourceManager &Source = Context.getSourceManager(); |
| 61 | |
| 62 | // We want to warn about cases where the type name |
| 63 | // comes from a macro like this: |
| 64 | // |
| 65 | // TYPENAME_FROM_MACRO var; |
| 66 | // |
| 67 | // but not if the entire declaration comes from |
| 68 | // one: |
| 69 | // |
| 70 | // DEFINE_SOME_VARIABLE(); |
| 71 | // |
| 72 | // or if the definition comes from a macro like SWAP |
| 73 | // that uses an internal temporary variable. |
| 74 | // |
| 75 | // Thus check that the variable name does |
| 76 | // not come from a macro expansion. |
| 77 | if (MatchedDecl->getEndLoc().isMacroID()) |
| 78 | return; |
| 79 | |
| 80 | QualType TypePtr = MatchedDecl->getType(); |
| 81 | std::optional<const char *> InitializationString; |
| 82 | bool AddMathInclude = false; |
| 83 | |
| 84 | if (TypePtr->isEnumeralType()) |
| 85 | InitializationString = nullptr; |
| 86 | else if (TypePtr->isBooleanType()) |
| 87 | InitializationString = " = false"; |
| 88 | else if (TypePtr->isIntegerType()) |
| 89 | InitializationString = " = 0"; |
| 90 | else if (TypePtr->isFloatingType()) { |
| 91 | InitializationString = " = NAN"; |
| 92 | AddMathInclude = true; |
| 93 | } else if (TypePtr->isAnyPointerType()) { |
| 94 | if (getLangOpts().CPlusPlus11) |
| 95 | InitializationString = " = nullptr"; |
| 96 | else |
| 97 | InitializationString = " = NULL"; |
| 98 | } |
| 99 | |
| 100 | if (InitializationString) { |
| 101 | auto Diagnostic = |
| 102 | diag(MatchedDecl->getLocation(), "variable %0 is not initialized") |
| 103 | << MatchedDecl; |
| 104 | if (*InitializationString != nullptr) |
| 105 | Diagnostic << FixItHint::CreateInsertion( |
| 106 | MatchedDecl->getLocation().getLocWithOffset( |
| 107 | MatchedDecl->getName().size()), |
| 108 | *InitializationString); |
| 109 | if (AddMathInclude) { |
| 110 | Diagnostic << IncludeInserter.createIncludeInsertion( |
| 111 | Source.getFileID(MatchedDecl->getBeginLoc()), MathHeader); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | } // namespace clang::tidy::cppcoreguidelines |