Aaron Ballman | c3975b7 | 2016-02-01 15:31:15 +0000 | [diff] [blame^] | 1 | //===--- RedundantControlFlowCheck.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 "RedundantControlFlowCheck.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 | namespace { |
| 22 | |
| 23 | const char *const RedundantReturnDiag = "redundant return statement at the end " |
| 24 | "of a function with a void return type"; |
| 25 | const char *const RedundantContinueDiag = "redundant continue statement at the " |
| 26 | "end of loop statement"; |
| 27 | |
| 28 | bool isLocationInMacroExpansion(const SourceManager &SM, SourceLocation Loc) { |
| 29 | return SM.isMacroBodyExpansion(Loc) || SM.isMacroArgExpansion(Loc); |
| 30 | } |
| 31 | |
| 32 | } // namespace |
| 33 | |
| 34 | void RedundantControlFlowCheck::registerMatchers(MatchFinder *Finder) { |
| 35 | Finder->addMatcher( |
| 36 | functionDecl(isDefinition(), returns(voidType()), |
| 37 | has(compoundStmt(hasAnySubstatement(returnStmt( |
| 38 | unless(has(expr()))))).bind("return"))), |
| 39 | this); |
| 40 | auto CompoundContinue = |
| 41 | has(compoundStmt(hasAnySubstatement(continueStmt())).bind("continue")); |
| 42 | Finder->addMatcher( |
| 43 | stmt(anyOf(forStmt(), cxxForRangeStmt(), whileStmt(), doStmt()), |
| 44 | CompoundContinue), |
| 45 | this); |
| 46 | } |
| 47 | |
| 48 | void RedundantControlFlowCheck::check(const MatchFinder::MatchResult &Result) { |
| 49 | if (const auto *Return = Result.Nodes.getNodeAs<CompoundStmt>("return")) |
| 50 | checkRedundantReturn(Result, Return); |
| 51 | else if (const auto *Continue = |
| 52 | Result.Nodes.getNodeAs<CompoundStmt>("continue")) |
| 53 | checkRedundantContinue(Result, Continue); |
| 54 | } |
| 55 | |
| 56 | void RedundantControlFlowCheck::checkRedundantReturn( |
| 57 | const MatchFinder::MatchResult &Result, const CompoundStmt *Block) { |
| 58 | CompoundStmt::const_reverse_body_iterator last = Block->body_rbegin(); |
| 59 | if (const auto *Return = dyn_cast<ReturnStmt>(*last)) |
| 60 | issueDiagnostic(Result, Block, Return->getSourceRange(), |
| 61 | RedundantReturnDiag); |
| 62 | } |
| 63 | |
| 64 | void RedundantControlFlowCheck::checkRedundantContinue( |
| 65 | const MatchFinder::MatchResult &Result, const CompoundStmt *Block) { |
| 66 | CompoundStmt::const_reverse_body_iterator last = Block->body_rbegin(); |
| 67 | if (const auto *Continue = dyn_cast<ContinueStmt>(*last)) |
| 68 | issueDiagnostic(Result, Block, Continue->getSourceRange(), |
| 69 | RedundantContinueDiag); |
| 70 | } |
| 71 | |
| 72 | void RedundantControlFlowCheck::issueDiagnostic( |
| 73 | const MatchFinder::MatchResult &Result, const CompoundStmt *const Block, |
| 74 | const SourceRange &StmtRange, const char *const Diag) { |
| 75 | SourceManager &SM = *Result.SourceManager; |
| 76 | if (isLocationInMacroExpansion(SM, StmtRange.getBegin())) |
| 77 | return; |
| 78 | |
| 79 | CompoundStmt::const_reverse_body_iterator Previous = ++Block->body_rbegin(); |
| 80 | SourceLocation Start; |
| 81 | if (Previous != Block->body_rend()) |
| 82 | Start = Lexer::findLocationAfterToken( |
| 83 | dyn_cast<Stmt>(*Previous)->getLocEnd(), tok::semi, SM, |
| 84 | Result.Context->getLangOpts(), |
| 85 | /*SkipTrailingWhitespaceAndNewLine=*/true); |
| 86 | else |
| 87 | Start = StmtRange.getBegin(); |
| 88 | auto RemovedRange = CharSourceRange::getCharRange( |
| 89 | Start, |
| 90 | Lexer::findLocationAfterToken(StmtRange.getEnd(), tok::semi, SM, |
| 91 | Result.Context->getLangOpts(), |
| 92 | /*SkipTrailingWhitespaceAndNewLine=*/true)); |
| 93 | |
| 94 | diag(StmtRange.getBegin(), Diag) << FixItHint::CreateRemoval(RemovedRange); |
| 95 | } |
| 96 | |
| 97 | } // namespace readability |
| 98 | } // namespace tidy |
| 99 | } // namespace clang |