clang-tools  14.0.0git
RedundantDeclarationCheck.cpp
Go to the documentation of this file.
1 //===--- RedundantDeclarationCheck.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 "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/Lex/Lexer.h"
13 
14 using namespace clang::ast_matchers;
15 
16 namespace clang {
17 namespace tidy {
18 namespace readability {
19 
20 AST_MATCHER(FunctionDecl, doesDeclarationForceExternallyVisibleDefinition) {
21  return Node.doesDeclarationForceExternallyVisibleDefinition();
22 }
23 
24 RedundantDeclarationCheck::RedundantDeclarationCheck(StringRef Name,
25  ClangTidyContext *Context)
26  : ClangTidyCheck(Name, Context),
27  IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", true)) {}
28 
31  Options.store(Opts, "IgnoreMacros", IgnoreMacros);
32 }
33 
35  Finder->addMatcher(
36  namedDecl(anyOf(varDecl(unless(isDefinition())),
37  functionDecl(unless(anyOf(
38  isDefinition(), isDefaulted(),
39  doesDeclarationForceExternallyVisibleDefinition(),
40  hasParent(friendDecl()))))))
41  .bind("Decl"),
42  this);
43 }
44 
45 void RedundantDeclarationCheck::check(const MatchFinder::MatchResult &Result) {
46  const auto *D = Result.Nodes.getNodeAs<NamedDecl>("Decl");
47  const auto *Prev = D->getPreviousDecl();
48  if (!Prev)
49  return;
50  if (!Prev->getLocation().isValid())
51  return;
52  if (Prev->getLocation() == D->getLocation())
53  return;
54  if (IgnoreMacros &&
55  (D->getLocation().isMacroID() || Prev->getLocation().isMacroID()))
56  return;
57  // Don't complain when the previous declaration is a friend declaration.
58  for (const auto &Parent : Result.Context->getParents(*Prev))
59  if (Parent.get<FriendDecl>())
60  return;
61 
62  const SourceManager &SM = *Result.SourceManager;
63 
64  const bool DifferentHeaders =
65  !SM.isInMainFile(D->getLocation()) &&
66  !SM.isWrittenInSameFile(Prev->getLocation(), D->getLocation());
67 
68  bool MultiVar = false;
69  if (const auto *VD = dyn_cast<VarDecl>(D)) {
70  // Is this a multivariable declaration?
71  for (const auto *Other : VD->getDeclContext()->decls()) {
72  if (Other != D && Other->getBeginLoc() == VD->getBeginLoc()) {
73  MultiVar = true;
74  break;
75  }
76  }
77  }
78 
79  SourceLocation EndLoc = Lexer::getLocForEndOfToken(
80  D->getSourceRange().getEnd(), 0, SM, Result.Context->getLangOpts());
81  {
82  auto Diag = diag(D->getLocation(), "redundant %0 declaration") << D;
83  if (!MultiVar && !DifferentHeaders)
84  Diag << FixItHint::CreateRemoval(
85  SourceRange(D->getSourceRange().getBegin(), EndLoc));
86  }
87  diag(Prev->getLocation(), "previously declared here", DiagnosticIDs::Note);
88 }
89 } // namespace readability
90 } // namespace tidy
91 } // namespace clang
clang::tidy::ClangTidyOptions::OptionMap
llvm::StringMap< ClangTidyValue > OptionMap
Definition: ClangTidyOptions.h:115
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:54
clang::ast_matchers
Definition: AbseilMatcher.h:14
ns1::ns2::D
@ D
Definition: CategoricalFeature.h:3
clang::tidy::readability::RedundantDeclarationCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: RedundantDeclarationCheck.cpp:34
clang::tidy::readability::RedundantDeclarationCheck::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: RedundantDeclarationCheck.cpp:45
clang::tidy::ClangTidyCheck::Options
OptionsView Options
Definition: ClangTidyCheck.h:416
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:71
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:28
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Definition: ClangTidyCheck.cpp:25
clang::ast_matchers::AST_MATCHER
AST_MATCHER(Expr, isMacroID)
Definition: PreferIsaOrDynCastInConditionalsCheck.cpp:19
Parent
const Node * Parent
Definition: ExtractFunction.cpp:152
clang::tidy::readability::RedundantDeclarationCheck::storeOptions
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
Definition: RedundantDeclarationCheck.cpp:29
RedundantDeclarationCheck.h
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::tidy::ClangTidyCheck::OptionsView::store
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.
Definition: ClangTidyCheck.cpp:120