clang-tools 20.0.0git
UnusedLocalNonTrivialVariableCheck.cpp
Go to the documentation of this file.
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
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
19using namespace clang::ast_matchers;
20using namespace clang::tidy::matchers;
21
22namespace clang::tidy::bugprone {
23
24namespace {
25static constexpr StringRef DefaultIncludeTypeRegex =
26 "::std::.*mutex;::std::future;::std::basic_string;::std::basic_regex;"
27 "::std::basic_istringstream;::std::basic_stringstream;::std::bitset;"
28 "::std::filesystem::path";
29
30AST_MATCHER(VarDecl, isLocalVarDecl) { return Node.isLocalVarDecl(); }
31AST_MATCHER(VarDecl, isReferenced) { return Node.isReferenced(); }
32AST_MATCHER_P(VarDecl, explicitMarkUnused, LangOptions, LangOpts) {
33 // Implementations should not emit a warning that a name-independent
34 // declaration is used or unused.
35 return Node.hasAttr<UnusedAttr>() ||
36 (LangOpts.CPlusPlus26 && Node.isPlaceholderVar(LangOpts));
37}
38AST_MATCHER(Type, isReferenceType) { return Node.isReferenceType(); }
39AST_MATCHER(QualType, isTrivial) {
40 return Node.isTrivialType(Finder->getASTContext()) ||
41 Node.isTriviallyCopyableType(Finder->getASTContext());
42}
43} // namespace
44
46 StringRef Name, ClangTidyContext *Context)
47 : ClangTidyCheck(Name, Context),
48 IncludeTypes(utils::options::parseStringList(
49 Options.get("IncludeTypes", DefaultIncludeTypeRegex))),
50 ExcludeTypes(
51 utils::options::parseStringList(Options.get("ExcludeTypes", ""))) {}
52
55 Options.store(Opts, "IncludeTypes",
57 Options.store(Opts, "ExcludeTypes",
59}
60
62 if (IncludeTypes.empty())
63 return;
64
65 Finder->addMatcher(
66 varDecl(isLocalVarDecl(), unless(isReferenced()),
67 unless(isExceptionVariable()), hasLocalStorage(), isDefinition(),
68 unless(hasType(isReferenceType())), unless(hasType(isTrivial())),
69 unless(explicitMarkUnused(getLangOpts())),
70 hasType(hasUnqualifiedDesugaredType(
71 anyOf(recordType(hasDeclaration(namedDecl(
72 matchesAnyListedName(IncludeTypes),
73 unless(matchesAnyListedName(ExcludeTypes))))),
74 templateSpecializationType(hasDeclaration(namedDecl(
75 matchesAnyListedName(IncludeTypes),
76 unless(matchesAnyListedName(ExcludeTypes)))))))))
77 .bind("var"),
78 this);
79}
80
82 const MatchFinder::MatchResult &Result) {
83 const auto *MatchedDecl = Result.Nodes.getNodeAs<VarDecl>("var");
84 diag(MatchedDecl->getLocation(), "unused local variable %0 of type %1")
85 << MatchedDecl << MatchedDecl->getType();
86}
87
89 const LangOptions &LangOpts) const {
90 return LangOpts.CPlusPlus;
91}
92
93std::optional<TraversalKind>
95 return TK_IgnoreUnlessSpelledInSource;
96}
97
98} // namespace clang::tidy::bugprone
llvm::SmallString< 256U > Name
NodeType Type
::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 check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
bool isLanguageVersionSupported(const LangOptions &LangOpts) const override
Override this to disable registering matchers and PP callbacks if an invalid language version is bein...
std::optional< TraversalKind > getCheckTraversalKind() const override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
AST_MATCHER_P(FunctionDecl, parameterCountGE, unsigned, N)
Matches functions that have at least the specified amount of parameters.
AST_MATCHER(clang::VarDecl, hasConstantDeclaration)
inline ::clang::ast_matchers::internal::Matcher< NamedDecl > matchesAnyListedName(llvm::ArrayRef< StringRef > NameList)
std::string serializeStringList(ArrayRef< StringRef > Strings)
Serialize a sequence of names that can be parsed by parseStringList.
llvm::StringMap< ClangTidyValue > OptionMap