11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 18 namespace readability {
21 ASTContext *Context,
const IfStmt *If) {
22 auto parents = Context->getParents(*If);
23 if (parents.size() != 1)
25 if (
const auto *PrecedingIf = parents[0].get<IfStmt>()) {
26 SourceLocation PreviousElseLoc = PrecedingIf->getElseLoc();
27 if (SM.getExpansionLineNumber(PreviousElseLoc) ==
28 SM.getExpansionLineNumber(If->getIfLoc()))
34 void MisleadingIndentationCheck::danglingElseCheck(
const SourceManager &SM,
37 SourceLocation IfLoc = If->getIfLoc();
38 SourceLocation ElseLoc = If->getElseLoc();
40 if (IfLoc.isMacroID() || ElseLoc.isMacroID())
43 if (SM.getExpansionLineNumber(If->getThen()->getEndLoc()) ==
44 SM.getExpansionLineNumber(ElseLoc))
48 for (
auto PrecedingIf =
getPrecedingIf(SM, Context, If); PrecedingIf;
50 IfLoc = PrecedingIf->getIfLoc();
52 if (SM.getExpansionColumnNumber(IfLoc) !=
53 SM.getExpansionColumnNumber(ElseLoc))
54 diag(ElseLoc,
"different indentation for 'if' and corresponding 'else'");
57 void MisleadingIndentationCheck::missingBracesCheck(
const SourceManager &SM,
58 const CompoundStmt *CStmt) {
59 const static StringRef StmtNames[] = {
"if",
"for",
"while"};
60 for (
unsigned int i = 0; i < CStmt->size() - 1; i++) {
61 const Stmt *CurrentStmt = CStmt->body_begin()[i];
62 const Stmt *Inner =
nullptr;
65 if (
const auto *CurrentIf = dyn_cast<IfStmt>(CurrentStmt)) {
68 CurrentIf->getElse() ? CurrentIf->getElse() : CurrentIf->getThen();
69 }
else if (
const auto *CurrentFor = dyn_cast<ForStmt>(CurrentStmt)) {
71 Inner = CurrentFor->getBody();
72 }
else if (
const auto *CurrentWhile = dyn_cast<WhileStmt>(CurrentStmt)) {
74 Inner = CurrentWhile->getBody();
79 if (isa<CompoundStmt>(Inner))
82 SourceLocation InnerLoc = Inner->getBeginLoc();
83 SourceLocation OuterLoc = CurrentStmt->getBeginLoc();
85 if (SM.getExpansionLineNumber(InnerLoc) ==
86 SM.getExpansionLineNumber(OuterLoc))
89 const Stmt *NextStmt = CStmt->body_begin()[i + 1];
90 SourceLocation NextLoc = NextStmt->getBeginLoc();
92 if (InnerLoc.isMacroID() || OuterLoc.isMacroID() || NextLoc.isMacroID())
95 if (SM.getExpansionColumnNumber(InnerLoc) ==
96 SM.getExpansionColumnNumber(NextLoc)) {
97 diag(NextLoc,
"misleading indentation: statement is indented too deeply");
98 diag(OuterLoc,
"did you mean this line to be inside this '%0'",
100 << StmtNames[StmtKind];
105 void MisleadingIndentationCheck::registerMatchers(MatchFinder *Finder) {
106 Finder->addMatcher(ifStmt(hasElse(stmt())).bind(
"if"),
this);
108 compoundStmt(has(stmt(anyOf(ifStmt(), forStmt(), whileStmt()))))
113 void MisleadingIndentationCheck::check(
const MatchFinder::MatchResult &
Result) {
114 if (
const auto *If = Result.Nodes.getNodeAs<IfStmt>(
"if"))
115 danglingElseCheck(*Result.SourceManager, Result.Context, If);
117 if (
const auto *CStmt = Result.Nodes.getNodeAs<CompoundStmt>(
"compound"))
118 missingBracesCheck(*Result.SourceManager, CStmt);
static const IfStmt * getPrecedingIf(const SourceManager &SM, ASTContext *Context, const IfStmt *If)
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//