Debian Codegraph @

llvm-project/clang-tools-extra/clang-tidy/readability/RedundantDeclarationCheck.cpp

raw
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::tidy::readability {
17
18 AST_MATCHER(FunctionDecl, doesDeclarationForceExternallyVisibleDefinition) {
19 return Node.doesDeclarationForceExternallyVisibleDefinition();
20 }
21
22 RedundantDeclarationCheck::RedundantDeclarationCheck(StringRef Name,
23 ClangTidyContext *Context)
24 : ClangTidyCheck(Name, Context),
25 IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", true)) {}
26
27 void RedundantDeclarationCheck::storeOptions(
28 ClangTidyOptions::OptionMap &Opts) {
29 Options.store(Opts, "IgnoreMacros", IgnoreMacros);
30 }
31
32 void RedundantDeclarationCheck::registerMatchers(MatchFinder *Finder) {
33 Finder->addMatcher(
34 namedDecl(anyOf(varDecl(unless(isDefinition())),
35 functionDecl(unless(anyOf(
36 isDefinition(), isDefaulted(),
37 doesDeclarationForceExternallyVisibleDefinition(),
38 hasAncestor(friendDecl()))))))
39 .bind("Decl"),
40 this);
41 }
42
43 void RedundantDeclarationCheck::check(const MatchFinder::MatchResult &Result) {
44 const auto *D = Result.Nodes.getNodeAs<NamedDecl>("Decl");
45 const auto *Prev = D->getPreviousDecl();
46 if (!Prev)
47 return;
48 if (!Prev->getLocation().isValid())
49 return;
50 if (Prev->getLocation() == D->getLocation())
51 return;
52 if (IgnoreMacros &&
53 (D->getLocation().isMacroID() || Prev->getLocation().isMacroID()))
54 return;
55 // Don't complain when the previous declaration is a friend declaration.
56 for (const auto &Parent : Result.Context->getParents(*Prev))
57 if (Parent.get<FriendDecl>())
58 return;
59
60 const SourceManager &SM = *Result.SourceManager;
61
62 const bool DifferentHeaders =
63 !SM.isInMainFile(D->getLocation()) &&
64 !SM.isWrittenInSameFile(Prev->getLocation(), D->getLocation());
65
66 bool MultiVar = false;
67 if (const auto *VD = dyn_cast<VarDecl>(D)) {
68 // Is this a multivariable declaration?
69 for (const auto *Other : VD->getDeclContext()->decls()) {
70 if (Other != D && Other->getBeginLoc() == VD->getBeginLoc()) {
71 MultiVar = true;
72 break;
73 }
74 }
75 }
76
77 SourceLocation EndLoc = Lexer::getLocForEndOfToken(
78 D->getSourceRange().getEnd(), 0, SM, Result.Context->getLangOpts());
79 {
80 auto Diag = diag(D->getLocation(), "redundant %0 declaration") << D;
81 if (!MultiVar && !DifferentHeaders)
82 Diag << FixItHint::CreateRemoval(
83 SourceRange(D->getSourceRange().getBegin(), EndLoc));
84 }
85 diag(Prev->getLocation(), "previously declared here", DiagnosticIDs::Note);
86 }
87 } // namespace clang::tidy::readability