blob: c5b6cc322543cc52542b356b68d9132fabef36b9 [file] [log] [blame]
Daniel Marjamaki399a50c2016-11-01 13:26:15 +00001//===--- RedundantDeclarationCheck.cpp - clang-tidy------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "RedundantDeclarationCheck.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/Lex/Lexer.h"
14
15using namespace clang::ast_matchers;
16
17namespace clang {
18namespace tidy {
19namespace readability {
20
Alexander Kornienkof3321c52017-07-28 12:46:02 +000021RedundantDeclarationCheck::RedundantDeclarationCheck(StringRef Name,
22 ClangTidyContext *Context)
23 : ClangTidyCheck(Name, Context),
24 IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", true)) {}
25
Daniel Marjamaki399a50c2016-11-01 13:26:15 +000026void RedundantDeclarationCheck::registerMatchers(MatchFinder *Finder) {
Alexander Kornienko1e034c32017-05-22 13:58:57 +000027 Finder->addMatcher(
Alexander Kornienkof3321c52017-07-28 12:46:02 +000028 namedDecl(anyOf(varDecl(unless(isDefinition())),
29 functionDecl(unless(anyOf(isDefinition(), isDefaulted(),
30 hasParent(friendDecl()))))))
Alexander Kornienko1e034c32017-05-22 13:58:57 +000031 .bind("Decl"),
32 this);
Daniel Marjamaki399a50c2016-11-01 13:26:15 +000033}
34
35void RedundantDeclarationCheck::check(const MatchFinder::MatchResult &Result) {
Piotr Padlewski08124b12016-12-14 15:29:23 +000036 const auto *D = Result.Nodes.getNodeAs<NamedDecl>("Decl");
Daniel Marjamaki399a50c2016-11-01 13:26:15 +000037 const auto *Prev = D->getPreviousDecl();
38 if (!Prev)
39 return;
Daniel Marjamakiae660452016-11-21 14:29:53 +000040 if (!Prev->getLocation().isValid())
41 return;
Daniel Marjamaki399a50c2016-11-01 13:26:15 +000042 if (Prev->getLocation() == D->getLocation())
43 return;
Alexander Kornienkof3321c52017-07-28 12:46:02 +000044 if (IgnoreMacros &&
45 (D->getLocation().isMacroID() || Prev->getLocation().isMacroID()))
46 return;
47 // Don't complain when the previous declaration is a friend declaration.
48 for (const auto &Parent : Result.Context->getParents(*Prev))
49 if (Parent.get<FriendDecl>())
50 return;
Daniel Marjamaki399a50c2016-11-01 13:26:15 +000051
52 const SourceManager &SM = *Result.SourceManager;
53
54 const bool DifferentHeaders =
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +000055 !SM.isInMainFile(D->getLocation()) &&
56 !SM.isWrittenInSameFile(Prev->getLocation(), D->getLocation());
Daniel Marjamaki399a50c2016-11-01 13:26:15 +000057
58 bool MultiVar = false;
59 if (const auto *VD = dyn_cast<VarDecl>(D)) {
Daniel Marjamaki399a50c2016-11-01 13:26:15 +000060 // Is this a multivariable declaration?
61 for (const auto Other : VD->getDeclContext()->decls()) {
Stephen Kelly43465bf2018-08-09 22:42:26 +000062 if (Other != D && Other->getBeginLoc() == VD->getBeginLoc()) {
Daniel Marjamaki399a50c2016-11-01 13:26:15 +000063 MultiVar = true;
64 break;
65 }
66 }
Daniel Marjamaki399a50c2016-11-01 13:26:15 +000067 }
68
69 SourceLocation EndLoc = Lexer::getLocForEndOfToken(
70 D->getSourceRange().getEnd(), 0, SM, Result.Context->getLangOpts());
71 {
Mandeep Singh Grang7c7ea7d2016-11-08 07:50:19 +000072 auto Diag = diag(D->getLocation(), "redundant %0 declaration") << D;
Daniel Marjamaki399a50c2016-11-01 13:26:15 +000073 if (!MultiVar && !DifferentHeaders)
74 Diag << FixItHint::CreateRemoval(
75 SourceRange(D->getSourceRange().getBegin(), EndLoc));
76 }
77 diag(Prev->getLocation(), "previously declared here", DiagnosticIDs::Note);
78}
79
80} // namespace readability
81} // namespace tidy
82} // namespace clang