| 1 | //===--- UnusedLocalNonTrivialVariableCheck.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 "UnusedLocalNonTrivialVariableCheck.h" |
| 10 | #include "../utils/Matchers.h" |
| 11 | #include "../utils/OptionsUtils.h" |
| 12 | #include "clang/AST/ASTContext.h" |
| 13 | #include "clang/AST/ASTTypeTraits.h" |
| 14 | #include "clang/AST/Type.h" |
| 15 | #include "clang/ASTMatchers/ASTMatchFinder.h" |
| 16 | #include "clang/ASTMatchers/ASTMatchers.h" |
| 17 | #include "clang/ASTMatchers/ASTMatchersMacros.h" |
| 18 | #include "clang/Basic/LangOptions.h" |
| 19 | |
| 20 | using namespace clang::ast_matchers; |
| 21 | using namespace clang::tidy::matchers; |
| 22 | |
| 23 | namespace clang::tidy::bugprone { |
| 24 | |
| 25 | namespace { |
| 26 | static constexpr StringRef DefaultIncludeTypeRegex = |
| 27 | "::std::.*mutex;::std::future;::std::basic_string;::std::basic_regex;" |
| 28 | "::std::basic_istringstream;::std::basic_stringstream;::std::bitset;" |
| 29 | "::std::filesystem::path" ; |
| 30 | |
| 31 | AST_MATCHER(VarDecl, isLocalVarDecl) { return Node.isLocalVarDecl(); } |
| 32 | AST_MATCHER(VarDecl, isReferenced) { return Node.isReferenced(); } |
| 33 | AST_MATCHER(VarDecl, explicitMarkUnused) { |
| 34 | // Implementations should not emit a warning that a name-independent |
| 35 | // declaration is used or unused. |
| 36 | LangOptions const &LangOpts = Finder->getASTContext().getLangOpts(); |
| 37 | return Node.hasAttr<UnusedAttr>() || |
| 38 | (LangOpts.CPlusPlus26 && Node.isPlaceholderVar(LangOpts)); |
| 39 | } |
| 40 | AST_MATCHER(Type, isReferenceType) { return Node.isReferenceType(); } |
| 41 | AST_MATCHER(QualType, isTrivial) { |
| 42 | return Node.isTrivialType(Context: Finder->getASTContext()) || |
| 43 | Node.isTriviallyCopyableType(Context: Finder->getASTContext()); |
| 44 | } |
| 45 | } // namespace |
| 46 | |
| 47 | UnusedLocalNonTrivialVariableCheck::UnusedLocalNonTrivialVariableCheck( |
| 48 | StringRef Name, ClangTidyContext *Context) |
| 49 | : ClangTidyCheck(Name, Context), |
| 50 | IncludeTypes(utils::options::parseStringList( |
| 51 | Option: Options.get(LocalName: "IncludeTypes" , Default: DefaultIncludeTypeRegex))), |
| 52 | ExcludeTypes( |
| 53 | utils::options::parseStringList(Option: Options.get(LocalName: "ExcludeTypes" , Default: "" ))) {} |
| 54 | |
| 55 | void UnusedLocalNonTrivialVariableCheck::storeOptions( |
| 56 | ClangTidyOptions::OptionMap &Opts) { |
| 57 | Options.store(Options&: Opts, LocalName: "IncludeTypes" , |
| 58 | Value: utils::options::serializeStringList(Strings: IncludeTypes)); |
| 59 | Options.store(Options&: Opts, LocalName: "ExcludeTypes" , |
| 60 | Value: utils::options::serializeStringList(Strings: ExcludeTypes)); |
| 61 | } |
| 62 | |
| 63 | void UnusedLocalNonTrivialVariableCheck::registerMatchers(MatchFinder *Finder) { |
| 64 | if (IncludeTypes.empty()) |
| 65 | return; |
| 66 | |
| 67 | Finder->addMatcher( |
| 68 | NodeMatch: varDecl(isLocalVarDecl(), unless(isReferenced()), |
| 69 | unless(isExceptionVariable()), hasLocalStorage(), isDefinition(), |
| 70 | unless(hasType(InnerMatcher: isReferenceType())), unless(hasType(InnerMatcher: isTrivial())), |
| 71 | unless(explicitMarkUnused()), |
| 72 | hasType(InnerMatcher: hasUnqualifiedDesugaredType( |
| 73 | InnerMatcher: anyOf(recordType(hasDeclaration(InnerMatcher: namedDecl( |
| 74 | matchesAnyListedName(NameList: IncludeTypes), |
| 75 | unless(matchesAnyListedName(NameList: ExcludeTypes))))), |
| 76 | templateSpecializationType(hasDeclaration(InnerMatcher: namedDecl( |
| 77 | matchesAnyListedName(NameList: IncludeTypes), |
| 78 | unless(matchesAnyListedName(NameList: ExcludeTypes))))))))) |
| 79 | .bind(ID: "var" ), |
| 80 | Action: this); |
| 81 | } |
| 82 | |
| 83 | void UnusedLocalNonTrivialVariableCheck::check( |
| 84 | const MatchFinder::MatchResult &Result) { |
| 85 | const auto *MatchedDecl = Result.Nodes.getNodeAs<VarDecl>(ID: "var" ); |
| 86 | diag(MatchedDecl->getLocation(), "unused local variable %0 of type %1" ) |
| 87 | << MatchedDecl << MatchedDecl->getType(); |
| 88 | } |
| 89 | |
| 90 | bool UnusedLocalNonTrivialVariableCheck::isLanguageVersionSupported( |
| 91 | const LangOptions &LangOpts) const { |
| 92 | return LangOpts.CPlusPlus; |
| 93 | } |
| 94 | |
| 95 | std::optional<TraversalKind> |
| 96 | UnusedLocalNonTrivialVariableCheck::getCheckTraversalKind() const { |
| 97 | return TK_IgnoreUnlessSpelledInSource; |
| 98 | } |
| 99 | |
| 100 | } // namespace clang::tidy::bugprone |
| 101 | |