clang-tools 10.0.0

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
9 #include "RedundantDeclarationCheck.h"
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
29 void RedundantDeclarationCheck::registerMatchers(MatchFinder *Finder) {
30 Finder->addMatcher(
31 namedDecl(anyOf(varDecl(unless(isDefinition())),
32 functionDecl(unless(anyOf(
33 isDefinition(), isDefaulted(),
34 doesDeclarationForceExternallyVisibleDefinition(),
35 hasParent(friendDecl()))))))
36 .bind("Decl"),
37 this);
38 }
39
40 void RedundantDeclarationCheck::check(const MatchFinder::MatchResult &Result) {
41 const auto *D = Result.Nodes.getNodeAs<NamedDecl>("Decl");
42 const auto *Prev = D->getPreviousDecl();
43 if (!Prev)
44 return;
45 if (!Prev->getLocation().isValid())
46 return;
47 if (Prev->getLocation() == D->getLocation())
48 return;
49 if (IgnoreMacros &&
50 (D->getLocation().isMacroID() || Prev->getLocation().isMacroID()))
51 return;
52 // Don't complain when the previous declaration is a friend declaration.
53 for (const auto &Parent : Result.Context->getParents(*Prev))
54 if (Parent.get<FriendDecl>())
55 return;
56
57 const SourceManager &SM = *Result.SourceManager;
58
59 const bool DifferentHeaders =
60 !SM.isInMainFile(D->getLocation()) &&
61 !SM.isWrittenInSameFile(Prev->getLocation(), D->getLocation());
62
63 bool MultiVar = false;
64 if (const auto *VD = dyn_cast<VarDecl>(D)) {
65 // Is this a multivariable declaration?
66 for (const auto Other : VD->getDeclContext()->decls()) {
67 if (Other != D && Other->getBeginLoc() == VD->getBeginLoc()) {
68 MultiVar = true;
69 break;
70 }
71 }
72 }
73
74 SourceLocation EndLoc = Lexer::getLocForEndOfToken(
75 D->getSourceRange().getEnd(), 0, SM, Result.Context->getLangOpts());
76 {
77 auto Diag = diag(D->getLocation(), "redundant %0 declaration") << D;
78 if (!MultiVar && !DifferentHeaders)
79 Diag << FixItHint::CreateRemoval(
80 SourceRange(D->getSourceRange().getBegin(), EndLoc));
81 }
82 diag(Prev->getLocation(), "previously declared here", DiagnosticIDs::Note);
83 }
84
85 } // namespace readability
86 } // namespace tidy
87 } // namespace clang
clang::ast_matchers
Definition: AbseilMatcher.h:14
Parent
const Node * Parent
Definition: ExtractFunction.cpp:148
clang::ast_matchers::AST_MATCHER
AST_MATCHER(Expr, isMacroID)
Definition: PreferIsaOrDynCastInConditionalsCheck.cpp:18
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidyCheck.h:47
Name
static constexpr llvm::StringLiteral Name
Definition: UppercaseLiteralSuffixCheck.cpp:27
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
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:40
RedundantDeclarationCheck.h
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:77
clang::tidy::ClangTidyCheck::diag
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check&#39;s name.
Definition: ClangTidyCheck.cpp:21
clang::tidy::readability::RedundantDeclarationCheck::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: RedundantDeclarationCheck.cpp:29