[PATCH] D30841: [clang-tidy] readability-misleading-indentation: fix chained if
Florian Gross via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Fri Mar 10 11:49:30 PST 2017
fgross created this revision.
Herald added a subscriber: JDevlieghere.
Fixed erroneously flagging of chained if statements when styled like this:
if (cond) {
}
else if (cond) {
}
else {
}
https://reviews.llvm.org/D30841
Files:
clang-tidy/readability/MisleadingIndentationCheck.cpp
clang-tidy/readability/MisleadingIndentationCheck.h
test/clang-tidy/readability-misleading-indentation.cpp
Index: test/clang-tidy/readability-misleading-indentation.cpp
===================================================================
--- test/clang-tidy/readability-misleading-indentation.cpp
+++ test/clang-tidy/readability-misleading-indentation.cpp
@@ -76,5 +76,31 @@
{
}
+ if(cond1) {
+ }
+ else if (cond2) {
+ }
+ else {
+ }
+
+ if(cond1) {
+ }
+ else if (cond2) {
+ }
+ else {
+ }
+ // CHECK-MESSAGES: :[[@LINE-2]]:8: warning: different indentation for 'if' and corresponding 'else' [readability-misleading-indentation]
+
+ if (cond1) {
+ if (cond1) {
+ }
+ else if (cond2) {
+ }
+ else {
+ }
+ }
+ else if (cond2) {
+ }
+
BLOCK
}
Index: clang-tidy/readability/MisleadingIndentationCheck.h
===================================================================
--- clang-tidy/readability/MisleadingIndentationCheck.h
+++ clang-tidy/readability/MisleadingIndentationCheck.h
@@ -11,6 +11,7 @@
#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_READABILITY_MISLEADING_INDENTATION_H
#include "../ClangTidy.h"
+#include <map>
namespace clang {
namespace tidy {
@@ -32,6 +33,10 @@
private:
void danglingElseCheck(const SourceManager &SM, const IfStmt *If);
void missingBracesCheck(const SourceManager &SM, const CompoundStmt *CStmt);
+
+ /// Key: Chained If
+ /// Value: Preceding If
+ std::map<const IfStmt *, const IfStmt *> ChainedIfs;
};
} // namespace readability
Index: clang-tidy/readability/MisleadingIndentationCheck.cpp
===================================================================
--- clang-tidy/readability/MisleadingIndentationCheck.cpp
+++ clang-tidy/readability/MisleadingIndentationCheck.cpp
@@ -25,10 +25,20 @@
if (IfLoc.isMacroID() || ElseLoc.isMacroID())
return;
+ if (const auto *ChainedIf = dyn_cast<IfStmt>(If->getElse())) {
+ if (SM.getExpansionLineNumber(ElseLoc) ==
+ SM.getExpansionLineNumber(ChainedIf->getIfLoc()))
+ ChainedIfs.emplace(ChainedIf, If);
+ }
+
if (SM.getExpansionLineNumber(If->getThen()->getLocEnd()) ==
SM.getExpansionLineNumber(ElseLoc))
return;
+ for (auto Iter = ChainedIfs.find(If); Iter != ChainedIfs.end();
+ Iter = ChainedIfs.find(Iter->second))
+ IfLoc = Iter->second->getIfLoc();
+
if (SM.getExpansionColumnNumber(IfLoc) !=
SM.getExpansionColumnNumber(ElseLoc))
diag(ElseLoc, "different indentation for 'if' and corresponding 'else'");
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D30841.91387.patch
Type: text/x-patch
Size: 2428 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170310/a7734743/attachment-0001.bin>
More information about the cfe-commits
mailing list