clang-tools 6.0.0

RedundantControlFlowCheck.cpp
Go to the documentation of this file.
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(
37 isDefinition(), returns(voidType()),
38 has(compoundStmt(hasAnySubstatement(returnStmt(unless(has(expr())))))
39 .bind("return"))),
40 this);
41 auto CompoundContinue =
42 has(compoundStmt(hasAnySubstatement(continueStmt())).bind("continue"));
43 Finder->addMatcher(
44 stmt(anyOf(forStmt(), cxxForRangeStmt(), whileStmt(), doStmt()),
45 CompoundContinue),
46 this);
47 }
48
49 void RedundantControlFlowCheck::check(const MatchFinder::MatchResult &Result) {
50 if (const auto *Return = Result.Nodes.getNodeAs<CompoundStmt>("return"))
51 checkRedundantReturn(Result, Return);
52 else if (const auto *Continue =
53 Result.Nodes.getNodeAs<CompoundStmt>("continue"))
54 checkRedundantContinue(Result, Continue);
55 }
56
57 void RedundantControlFlowCheck::checkRedundantReturn(
58 const MatchFinder::MatchResult &Result, const CompoundStmt *Block) {
59 CompoundStmt::const_reverse_body_iterator last = Block->body_rbegin();
60 if (const auto *Return = dyn_cast<ReturnStmt>(*last))
61 issueDiagnostic(Result, Block, Return->getSourceRange(),
62 RedundantReturnDiag);
63 }
64
65 void RedundantControlFlowCheck::checkRedundantContinue(
66 const MatchFinder::MatchResult &Result, const CompoundStmt *Block) {
67 CompoundStmt::const_reverse_body_iterator last = Block->body_rbegin();
68 if (const auto *Continue = dyn_cast<ContinueStmt>(*last))
69 issueDiagnostic(Result, Block, Continue->getSourceRange(),
70 RedundantContinueDiag);
71 }
72
73 void RedundantControlFlowCheck::issueDiagnostic(
74 const MatchFinder::MatchResult &Result, const CompoundStmt *const Block,
75 const SourceRange &StmtRange, const char *const Diag) {
76 SourceManager &SM = *Result.SourceManager;
77 if (isLocationInMacroExpansion(SM, StmtRange.getBegin()))
78 return;
79
80 CompoundStmt::const_reverse_body_iterator Previous = ++Block->body_rbegin();
81 SourceLocation Start;
82 if (Previous != Block->body_rend())
83 Start = Lexer::findLocationAfterToken(
84 dyn_cast<Stmt>(*Previous)->getLocEnd(), tok::semi, SM, getLangOpts(),
85 /*SkipTrailingWhitespaceAndNewLine=*/true);
86 if (!Start.isValid())
87 Start = StmtRange.getBegin();
88 auto RemovedRange = CharSourceRange::getCharRange(
89 Start, Lexer::findLocationAfterToken(
90 StmtRange.getEnd(), tok::semi, SM, getLangOpts(),
91 /*SkipTrailingWhitespaceAndNewLine=*/true));
92
93 diag(StmtRange.getBegin(), Diag) << FixItHint::CreateRemoval(RemovedRange);
94 }
95
96 } // namespace readability
97 } // namespace tidy
98 } // namespace clang
Loc
SourceLocation Loc
&#39;#&#39; location in the include directive
Definition: IncludeOrderCheck.cpp:36
clang::ast_matchers
Definition: ClangTidyDiagnosticConsumer.h:27
RedundantControlFlowCheck.h
clang
Definition: AndroidTidyModule.cpp:28