2017-02-14 18:03:27 +08:00
|
|
|
//===--- MisleadingIndentationCheck.cpp - clang-tidy-----------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-02-14 18:03:27 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "MisleadingIndentationCheck.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
|
|
|
|
using namespace clang::ast_matchers;
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace tidy {
|
|
|
|
namespace readability {
|
|
|
|
|
2017-03-17 17:58:30 +08:00
|
|
|
static const IfStmt *getPrecedingIf(const SourceManager &SM,
|
|
|
|
ASTContext *Context, const IfStmt *If) {
|
2021-01-29 07:49:53 +08:00
|
|
|
auto Parents = Context->getParents(*If);
|
|
|
|
if (Parents.size() != 1)
|
2017-03-17 17:58:30 +08:00
|
|
|
return nullptr;
|
2021-01-29 07:49:53 +08:00
|
|
|
if (const auto *PrecedingIf = Parents[0].get<IfStmt>()) {
|
2017-03-17 17:58:30 +08:00
|
|
|
SourceLocation PreviousElseLoc = PrecedingIf->getElseLoc();
|
|
|
|
if (SM.getExpansionLineNumber(PreviousElseLoc) ==
|
|
|
|
SM.getExpansionLineNumber(If->getIfLoc()))
|
|
|
|
return PrecedingIf;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-02-14 18:03:27 +08:00
|
|
|
void MisleadingIndentationCheck::danglingElseCheck(const SourceManager &SM,
|
2017-03-17 17:58:30 +08:00
|
|
|
ASTContext *Context,
|
2017-02-14 18:03:27 +08:00
|
|
|
const IfStmt *If) {
|
|
|
|
SourceLocation IfLoc = If->getIfLoc();
|
|
|
|
SourceLocation ElseLoc = If->getElseLoc();
|
|
|
|
|
|
|
|
if (IfLoc.isMacroID() || ElseLoc.isMacroID())
|
|
|
|
return;
|
|
|
|
|
2018-08-10 06:43:02 +08:00
|
|
|
if (SM.getExpansionLineNumber(If->getThen()->getEndLoc()) ==
|
2017-02-14 18:03:27 +08:00
|
|
|
SM.getExpansionLineNumber(ElseLoc))
|
|
|
|
return;
|
|
|
|
|
2017-03-17 17:58:30 +08:00
|
|
|
// Find location of first 'if' in a 'if else if' chain.
|
2021-01-29 07:49:53 +08:00
|
|
|
for (const auto *PrecedingIf = getPrecedingIf(SM, Context, If); PrecedingIf;
|
2017-03-17 17:58:30 +08:00
|
|
|
PrecedingIf = getPrecedingIf(SM, Context, PrecedingIf))
|
|
|
|
IfLoc = PrecedingIf->getIfLoc();
|
|
|
|
|
2017-02-14 18:03:27 +08:00
|
|
|
if (SM.getExpansionColumnNumber(IfLoc) !=
|
|
|
|
SM.getExpansionColumnNumber(ElseLoc))
|
|
|
|
diag(ElseLoc, "different indentation for 'if' and corresponding 'else'");
|
|
|
|
}
|
|
|
|
|
|
|
|
void MisleadingIndentationCheck::missingBracesCheck(const SourceManager &SM,
|
|
|
|
const CompoundStmt *CStmt) {
|
|
|
|
const static StringRef StmtNames[] = {"if", "for", "while"};
|
2021-01-29 07:49:53 +08:00
|
|
|
for (unsigned int I = 0; I < CStmt->size() - 1; I++) {
|
|
|
|
const Stmt *CurrentStmt = CStmt->body_begin()[I];
|
2017-02-14 18:03:27 +08:00
|
|
|
const Stmt *Inner = nullptr;
|
|
|
|
int StmtKind = 0;
|
|
|
|
|
|
|
|
if (const auto *CurrentIf = dyn_cast<IfStmt>(CurrentStmt)) {
|
|
|
|
StmtKind = 0;
|
|
|
|
Inner =
|
|
|
|
CurrentIf->getElse() ? CurrentIf->getElse() : CurrentIf->getThen();
|
|
|
|
} else if (const auto *CurrentFor = dyn_cast<ForStmt>(CurrentStmt)) {
|
|
|
|
StmtKind = 1;
|
|
|
|
Inner = CurrentFor->getBody();
|
|
|
|
} else if (const auto *CurrentWhile = dyn_cast<WhileStmt>(CurrentStmt)) {
|
|
|
|
StmtKind = 2;
|
|
|
|
Inner = CurrentWhile->getBody();
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isa<CompoundStmt>(Inner))
|
|
|
|
continue;
|
|
|
|
|
2018-08-10 06:42:26 +08:00
|
|
|
SourceLocation InnerLoc = Inner->getBeginLoc();
|
|
|
|
SourceLocation OuterLoc = CurrentStmt->getBeginLoc();
|
2017-02-14 18:03:27 +08:00
|
|
|
|
2019-04-18 00:19:47 +08:00
|
|
|
if (InnerLoc.isInvalid() || InnerLoc.isMacroID() || OuterLoc.isInvalid() ||
|
|
|
|
OuterLoc.isMacroID())
|
|
|
|
continue;
|
|
|
|
|
2017-02-14 18:03:27 +08:00
|
|
|
if (SM.getExpansionLineNumber(InnerLoc) ==
|
|
|
|
SM.getExpansionLineNumber(OuterLoc))
|
|
|
|
continue;
|
|
|
|
|
2021-01-29 07:49:53 +08:00
|
|
|
const Stmt *NextStmt = CStmt->body_begin()[I + 1];
|
2018-08-10 06:42:26 +08:00
|
|
|
SourceLocation NextLoc = NextStmt->getBeginLoc();
|
2017-02-14 18:03:27 +08:00
|
|
|
|
2019-04-18 00:19:47 +08:00
|
|
|
if (NextLoc.isInvalid() || NextLoc.isMacroID())
|
2017-02-14 18:03:27 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (SM.getExpansionColumnNumber(InnerLoc) ==
|
|
|
|
SM.getExpansionColumnNumber(NextLoc)) {
|
|
|
|
diag(NextLoc, "misleading indentation: statement is indented too deeply");
|
|
|
|
diag(OuterLoc, "did you mean this line to be inside this '%0'",
|
|
|
|
DiagnosticIDs::Note)
|
|
|
|
<< StmtNames[StmtKind];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MisleadingIndentationCheck::registerMatchers(MatchFinder *Finder) {
|
2021-03-10 07:06:14 +08:00
|
|
|
Finder->addMatcher(ifStmt(hasElse(stmt())).bind("if"), this);
|
2017-02-14 18:03:27 +08:00
|
|
|
Finder->addMatcher(
|
|
|
|
compoundStmt(has(stmt(anyOf(ifStmt(), forStmt(), whileStmt()))))
|
|
|
|
.bind("compound"),
|
|
|
|
this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MisleadingIndentationCheck::check(const MatchFinder::MatchResult &Result) {
|
|
|
|
if (const auto *If = Result.Nodes.getNodeAs<IfStmt>("if"))
|
2017-03-17 17:58:30 +08:00
|
|
|
danglingElseCheck(*Result.SourceManager, Result.Context, If);
|
2017-02-14 18:03:27 +08:00
|
|
|
|
|
|
|
if (const auto *CStmt = Result.Nodes.getNodeAs<CompoundStmt>("compound"))
|
|
|
|
missingBracesCheck(*Result.SourceManager, CStmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace readability
|
|
|
|
} // namespace tidy
|
|
|
|
} // namespace clang
|