10 #include "clang/AST/ASTContext.h" 11 #include "clang/ASTMatchers/ASTMatchFinder.h" 12 #include "clang/Lex/Lexer.h" 18 namespace readability {
22 const char *
const RedundantReturnDiag =
"redundant return statement at the end " 23 "of a function with a void return type";
24 const char *
const RedundantContinueDiag =
"redundant continue statement at the " 25 "end of loop statement";
27 bool isLocationInMacroExpansion(
const SourceManager &SM, SourceLocation
Loc) {
28 return SM.isMacroBodyExpansion(Loc) || SM.isMacroArgExpansion(Loc);
33 void RedundantControlFlowCheck::registerMatchers(MatchFinder *Finder) {
36 isDefinition(), returns(voidType()),
37 has(compoundStmt(hasAnySubstatement(returnStmt(unless(has(expr())))))
40 auto CompoundContinue =
41 has(compoundStmt(hasAnySubstatement(continueStmt())).bind(
"continue"));
43 stmt(anyOf(forStmt(), cxxForRangeStmt(), whileStmt(), doStmt()),
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);
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(),
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);
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()))
79 CompoundStmt::const_reverse_body_iterator Previous = ++Block->body_rbegin();
81 if (Previous != Block->body_rend())
82 Start = Lexer::findLocationAfterToken(
83 dyn_cast<Stmt>(*Previous)->getEndLoc(), tok::semi, SM, getLangOpts(),
86 Start = StmtRange.getBegin();
87 auto RemovedRange = CharSourceRange::getCharRange(
88 Start, Lexer::findLocationAfterToken(
89 StmtRange.getEnd(), tok::semi, SM, getLangOpts(),
92 diag(StmtRange.getBegin(), Diag) << FixItHint::CreateRemoval(RemovedRange);
SourceLocation Loc
'#' location in the include directive
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result