11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 13 #include "clang/Lex/Lexer.h" 19 namespace readability {
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";
28 bool isLocationInMacroExpansion(
const SourceManager &SM, SourceLocation
Loc) {
29 return SM.isMacroBodyExpansion(Loc) || SM.isMacroArgExpansion(Loc);
34 void RedundantControlFlowCheck::registerMatchers(MatchFinder *Finder) {
37 isDefinition(), returns(voidType()),
38 has(compoundStmt(hasAnySubstatement(returnStmt(unless(has(expr())))))
41 auto CompoundContinue =
42 has(compoundStmt(hasAnySubstatement(continueStmt())).bind(
"continue"));
44 stmt(anyOf(forStmt(), cxxForRangeStmt(), whileStmt(), doStmt()),
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);
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(),
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);
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()))
80 CompoundStmt::const_reverse_body_iterator Previous = ++Block->body_rbegin();
82 if (Previous != Block->body_rend())
83 Start = Lexer::findLocationAfterToken(
84 dyn_cast<Stmt>(*Previous)->getEndLoc(), tok::semi, SM, getLangOpts(),
87 Start = StmtRange.getBegin();
88 auto RemovedRange = CharSourceRange::getCharRange(
89 Start, Lexer::findLocationAfterToken(
90 StmtRange.getEnd(), tok::semi, SM, getLangOpts(),
93 diag(StmtRange.getBegin(), Diag) << FixItHint::CreateRemoval(RemovedRange);
SourceLocation Loc
'#' location in the include directive
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//