annotate clang-tools-extra/clang-tidy/readability/RedundantControlFlowCheck.cpp @ 266:00f31e85ec16 default tip

Added tag current for changeset 31d058e83c98
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 14 Oct 2023 10:13:55 +0900
parents 1f2b6ac9f198
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===--- RedundantControlFlowCheck.cpp - clang-tidy------------------------===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8
anatofuz
parents:
diff changeset
9 #include "RedundantControlFlowCheck.h"
anatofuz
parents:
diff changeset
10 #include "clang/AST/ASTContext.h"
anatofuz
parents:
diff changeset
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
anatofuz
parents:
diff changeset
12 #include "clang/Lex/Lexer.h"
anatofuz
parents:
diff changeset
13
anatofuz
parents:
diff changeset
14 using namespace clang::ast_matchers;
anatofuz
parents:
diff changeset
15
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
16 namespace clang::tidy::readability {
150
anatofuz
parents:
diff changeset
17
anatofuz
parents:
diff changeset
18 namespace {
anatofuz
parents:
diff changeset
19
anatofuz
parents:
diff changeset
20 const char *const RedundantReturnDiag = "redundant return statement at the end "
anatofuz
parents:
diff changeset
21 "of a function with a void return type";
anatofuz
parents:
diff changeset
22 const char *const RedundantContinueDiag = "redundant continue statement at the "
anatofuz
parents:
diff changeset
23 "end of loop statement";
anatofuz
parents:
diff changeset
24
anatofuz
parents:
diff changeset
25 bool isLocationInMacroExpansion(const SourceManager &SM, SourceLocation Loc) {
anatofuz
parents:
diff changeset
26 return SM.isMacroBodyExpansion(Loc) || SM.isMacroArgExpansion(Loc);
anatofuz
parents:
diff changeset
27 }
anatofuz
parents:
diff changeset
28
anatofuz
parents:
diff changeset
29 } // namespace
anatofuz
parents:
diff changeset
30
anatofuz
parents:
diff changeset
31 void RedundantControlFlowCheck::registerMatchers(MatchFinder *Finder) {
anatofuz
parents:
diff changeset
32 Finder->addMatcher(
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
33 functionDecl(isDefinition(), returns(voidType()),
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
34 hasBody(compoundStmt(hasAnySubstatement(
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
35 returnStmt(unless(has(expr())))))
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
36 .bind("return"))),
150
anatofuz
parents:
diff changeset
37 this);
anatofuz
parents:
diff changeset
38 Finder->addMatcher(
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
39 mapAnyOf(forStmt, cxxForRangeStmt, whileStmt, doStmt)
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
40 .with(hasBody(compoundStmt(hasAnySubstatement(continueStmt()))
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
41 .bind("continue"))),
150
anatofuz
parents:
diff changeset
42 this);
anatofuz
parents:
diff changeset
43 }
anatofuz
parents:
diff changeset
44
anatofuz
parents:
diff changeset
45 void RedundantControlFlowCheck::check(const MatchFinder::MatchResult &Result) {
anatofuz
parents:
diff changeset
46 if (const auto *Return = Result.Nodes.getNodeAs<CompoundStmt>("return"))
anatofuz
parents:
diff changeset
47 checkRedundantReturn(Result, Return);
anatofuz
parents:
diff changeset
48 else if (const auto *Continue =
anatofuz
parents:
diff changeset
49 Result.Nodes.getNodeAs<CompoundStmt>("continue"))
anatofuz
parents:
diff changeset
50 checkRedundantContinue(Result, Continue);
anatofuz
parents:
diff changeset
51 }
anatofuz
parents:
diff changeset
52
anatofuz
parents:
diff changeset
53 void RedundantControlFlowCheck::checkRedundantReturn(
anatofuz
parents:
diff changeset
54 const MatchFinder::MatchResult &Result, const CompoundStmt *Block) {
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
55 CompoundStmt::const_reverse_body_iterator Last = Block->body_rbegin();
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
56 if (const auto *Return = dyn_cast<ReturnStmt>(*Last))
150
anatofuz
parents:
diff changeset
57 issueDiagnostic(Result, Block, Return->getSourceRange(),
anatofuz
parents:
diff changeset
58 RedundantReturnDiag);
anatofuz
parents:
diff changeset
59 }
anatofuz
parents:
diff changeset
60
anatofuz
parents:
diff changeset
61 void RedundantControlFlowCheck::checkRedundantContinue(
anatofuz
parents:
diff changeset
62 const MatchFinder::MatchResult &Result, const CompoundStmt *Block) {
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
63 CompoundStmt::const_reverse_body_iterator Last = Block->body_rbegin();
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
64 if (const auto *Continue = dyn_cast<ContinueStmt>(*Last))
150
anatofuz
parents:
diff changeset
65 issueDiagnostic(Result, Block, Continue->getSourceRange(),
anatofuz
parents:
diff changeset
66 RedundantContinueDiag);
anatofuz
parents:
diff changeset
67 }
anatofuz
parents:
diff changeset
68
anatofuz
parents:
diff changeset
69 void RedundantControlFlowCheck::issueDiagnostic(
anatofuz
parents:
diff changeset
70 const MatchFinder::MatchResult &Result, const CompoundStmt *const Block,
anatofuz
parents:
diff changeset
71 const SourceRange &StmtRange, const char *const Diag) {
anatofuz
parents:
diff changeset
72 SourceManager &SM = *Result.SourceManager;
anatofuz
parents:
diff changeset
73 if (isLocationInMacroExpansion(SM, StmtRange.getBegin()))
anatofuz
parents:
diff changeset
74 return;
anatofuz
parents:
diff changeset
75
anatofuz
parents:
diff changeset
76 CompoundStmt::const_reverse_body_iterator Previous = ++Block->body_rbegin();
anatofuz
parents:
diff changeset
77 SourceLocation Start;
anatofuz
parents:
diff changeset
78 if (Previous != Block->body_rend())
anatofuz
parents:
diff changeset
79 Start = Lexer::findLocationAfterToken(
236
c4bab56944e8 LLVM 16
kono
parents: 221
diff changeset
80 cast<Stmt>(*Previous)->getEndLoc(), tok::semi, SM, getLangOpts(),
150
anatofuz
parents:
diff changeset
81 /*SkipTrailingWhitespaceAndNewLine=*/true);
anatofuz
parents:
diff changeset
82 if (!Start.isValid())
anatofuz
parents:
diff changeset
83 Start = StmtRange.getBegin();
anatofuz
parents:
diff changeset
84 auto RemovedRange = CharSourceRange::getCharRange(
anatofuz
parents:
diff changeset
85 Start, Lexer::findLocationAfterToken(
anatofuz
parents:
diff changeset
86 StmtRange.getEnd(), tok::semi, SM, getLangOpts(),
anatofuz
parents:
diff changeset
87 /*SkipTrailingWhitespaceAndNewLine=*/true));
anatofuz
parents:
diff changeset
88
anatofuz
parents:
diff changeset
89 diag(StmtRange.getBegin(), Diag) << FixItHint::CreateRemoval(RemovedRange);
anatofuz
parents:
diff changeset
90 }
anatofuz
parents:
diff changeset
91
252
1f2b6ac9f198 LLVM16-1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 236
diff changeset
92 } // namespace clang::tidy::readability