[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang-tools-extra / clang-tidy / readability / RedundantDeclarationCheck.cpp
blob78ba9691c25573d94c1f3714f16ad04c5b7bf681
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 //===----------------------------------------------------------------------===//
9 #include "RedundantDeclarationCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/Lex/Lexer.h"
14 using namespace clang::ast_matchers;
16 namespace clang {
17 namespace tidy {
18 namespace readability {
20 AST_MATCHER(FunctionDecl, doesDeclarationForceExternallyVisibleDefinition) {
21 return Node.doesDeclarationForceExternallyVisibleDefinition();
24 RedundantDeclarationCheck::RedundantDeclarationCheck(StringRef Name,
25 ClangTidyContext *Context)
26 : ClangTidyCheck(Name, Context),
27 IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", true)) {}
29 void RedundantDeclarationCheck::storeOptions(
30 ClangTidyOptions::OptionMap &Opts) {
31 Options.store(Opts, "IgnoreMacros", IgnoreMacros);
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 hasAncestor(friendDecl()))))))
41 .bind("Decl"),
42 this);
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;
62 const SourceManager &SM = *Result.SourceManager;
64 const bool DifferentHeaders =
65 !SM.isInMainFile(D->getLocation()) &&
66 !SM.isWrittenInSameFile(Prev->getLocation(), D->getLocation());
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;
79 SourceLocation EndLoc = Lexer::getLocForEndOfToken(
80 D->getSourceRange().getEnd(), 0, SM, Result.Context->getLangOpts());
82 auto Diag = diag(D->getLocation(), "redundant %0 declaration") << D;
83 if (!MultiVar && !DifferentHeaders)
84 Diag << FixItHint::CreateRemoval(
85 SourceRange(D->getSourceRange().getBegin(), EndLoc));
87 diag(Prev->getLocation(), "previously declared here", DiagnosticIDs::Note);
89 } // namespace readability
90 } // namespace tidy
91 } // namespace clang