diff options
Diffstat (limited to 'clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.cpp')
| -rw-r--r-- | clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.cpp | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.cpp b/clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.cpp index d3980b7b4e2..8a6c8c47598 100644 --- a/clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/MisleadingIndentationCheck.cpp @@ -17,7 +17,22 @@ namespace clang { namespace tidy { namespace readability { +static const IfStmt *getPrecedingIf(const SourceManager &SM, + ASTContext *Context, const IfStmt *If) { + auto parents = Context->getParents(*If); + if (parents.size() != 1) + return nullptr; + if (const auto *PrecedingIf = parents[0].get<IfStmt>()) { + SourceLocation PreviousElseLoc = PrecedingIf->getElseLoc(); + if (SM.getExpansionLineNumber(PreviousElseLoc) == + SM.getExpansionLineNumber(If->getIfLoc())) + return PrecedingIf; + } + return nullptr; +} + void MisleadingIndentationCheck::danglingElseCheck(const SourceManager &SM, + ASTContext *Context, const IfStmt *If) { SourceLocation IfLoc = If->getIfLoc(); SourceLocation ElseLoc = If->getElseLoc(); @@ -29,6 +44,11 @@ void MisleadingIndentationCheck::danglingElseCheck(const SourceManager &SM, SM.getExpansionLineNumber(ElseLoc)) return; + // Find location of first 'if' in a 'if else if' chain. + for (auto PrecedingIf = getPrecedingIf(SM, Context, If); PrecedingIf; + PrecedingIf = getPrecedingIf(SM, Context, PrecedingIf)) + IfLoc = PrecedingIf->getIfLoc(); + if (SM.getExpansionColumnNumber(IfLoc) != SM.getExpansionColumnNumber(ElseLoc)) diag(ElseLoc, "different indentation for 'if' and corresponding 'else'"); @@ -92,7 +112,7 @@ void MisleadingIndentationCheck::registerMatchers(MatchFinder *Finder) { void MisleadingIndentationCheck::check(const MatchFinder::MatchResult &Result) { if (const auto *If = Result.Nodes.getNodeAs<IfStmt>("if")) - danglingElseCheck(*Result.SourceManager, If); + danglingElseCheck(*Result.SourceManager, Result.Context, If); if (const auto *CStmt = Result.Nodes.getNodeAs<CompoundStmt>("compound")) missingBracesCheck(*Result.SourceManager, CStmt); |

