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
AST_MATCHER(FunctionDecl,doesDeclarationForceExternallyVisibleDefinition)20 AST_MATCHER(FunctionDecl, doesDeclarationForceExternallyVisibleDefinition) {
21 return Node.doesDeclarationForceExternallyVisibleDefinition();
22 }
23
RedundantDeclarationCheck(StringRef Name,ClangTidyContext * Context)24 RedundantDeclarationCheck::RedundantDeclarationCheck(StringRef Name,
25 ClangTidyContext *Context)
26 : ClangTidyCheck(Name, Context),
27 IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", true)) {}
28
storeOptions(ClangTidyOptions::OptionMap & Opts)29 void RedundantDeclarationCheck::storeOptions(
30 ClangTidyOptions::OptionMap &Opts) {
31 Options.store(Opts, "IgnoreMacros", IgnoreMacros);
32 }
33
registerMatchers(MatchFinder * Finder)34 void RedundantDeclarationCheck::registerMatchers(MatchFinder *Finder) {
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
check(const MatchFinder::MatchResult & Result)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
92