Daniel Marjamaki | 399a50c | 2016-11-01 13:26:15 +0000 | [diff] [blame] | 1 | //===--- 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 | |
| 15 | using namespace clang::ast_matchers; |
| 16 | |
| 17 | namespace clang { |
| 18 | namespace tidy { |
| 19 | namespace readability { |
| 20 | |
| 21 | void RedundantDeclarationCheck::registerMatchers(MatchFinder *Finder) { |
Alexander Kornienko | 1e034c3 | 2017-05-22 13:58:57 +0000 | [diff] [blame^] | 22 | Finder->addMatcher( |
| 23 | namedDecl( |
| 24 | anyOf(varDecl(unless(isDefinition())), |
| 25 | functionDecl(unless(anyOf(isDefinition(), isDefaulted()))))) |
| 26 | .bind("Decl"), |
| 27 | this); |
Daniel Marjamaki | 399a50c | 2016-11-01 13:26:15 +0000 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | void RedundantDeclarationCheck::check(const MatchFinder::MatchResult &Result) { |
Piotr Padlewski | 08124b1 | 2016-12-14 15:29:23 +0000 | [diff] [blame] | 31 | const auto *D = Result.Nodes.getNodeAs<NamedDecl>("Decl"); |
Daniel Marjamaki | 399a50c | 2016-11-01 13:26:15 +0000 | [diff] [blame] | 32 | const auto *Prev = D->getPreviousDecl(); |
| 33 | if (!Prev) |
| 34 | return; |
Daniel Marjamaki | ae66045 | 2016-11-21 14:29:53 +0000 | [diff] [blame] | 35 | if (!Prev->getLocation().isValid()) |
| 36 | return; |
Daniel Marjamaki | 399a50c | 2016-11-01 13:26:15 +0000 | [diff] [blame] | 37 | if (Prev->getLocation() == D->getLocation()) |
| 38 | return; |
| 39 | |
| 40 | const SourceManager &SM = *Result.SourceManager; |
| 41 | |
| 42 | const bool DifferentHeaders = |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 43 | !SM.isInMainFile(D->getLocation()) && |
| 44 | !SM.isWrittenInSameFile(Prev->getLocation(), D->getLocation()); |
Daniel Marjamaki | 399a50c | 2016-11-01 13:26:15 +0000 | [diff] [blame] | 45 | |
| 46 | bool MultiVar = false; |
| 47 | if (const auto *VD = dyn_cast<VarDecl>(D)) { |
Daniel Marjamaki | 399a50c | 2016-11-01 13:26:15 +0000 | [diff] [blame] | 48 | // Is this a multivariable declaration? |
| 49 | for (const auto Other : VD->getDeclContext()->decls()) { |
| 50 | if (Other != D && Other->getLocStart() == VD->getLocStart()) { |
| 51 | MultiVar = true; |
| 52 | break; |
| 53 | } |
| 54 | } |
Daniel Marjamaki | 399a50c | 2016-11-01 13:26:15 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | SourceLocation EndLoc = Lexer::getLocForEndOfToken( |
| 58 | D->getSourceRange().getEnd(), 0, SM, Result.Context->getLangOpts()); |
| 59 | { |
Mandeep Singh Grang | 7c7ea7d | 2016-11-08 07:50:19 +0000 | [diff] [blame] | 60 | auto Diag = diag(D->getLocation(), "redundant %0 declaration") << D; |
Daniel Marjamaki | 399a50c | 2016-11-01 13:26:15 +0000 | [diff] [blame] | 61 | if (!MultiVar && !DifferentHeaders) |
| 62 | Diag << FixItHint::CreateRemoval( |
| 63 | SourceRange(D->getSourceRange().getBegin(), EndLoc)); |
| 64 | } |
| 65 | diag(Prev->getLocation(), "previously declared here", DiagnosticIDs::Note); |
| 66 | } |
| 67 | |
| 68 | } // namespace readability |
| 69 | } // namespace tidy |
| 70 | } // namespace clang |