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