summaryrefslogtreecommitdiff
path: root/compilerplugins/clang
diff options
context:
space:
mode:
authorNoel Grandin <noel.grandin@collabora.co.uk>2019-11-05 15:20:22 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2019-11-06 06:27:51 +0100
commit7b99cdb2d77a3feaf9b782c1e656f2d922e2746e (patch)
tree180459dcd8379ee1a2e89098a4d9eda72abe8f05 /compilerplugins/clang
parent7ee07296a66df29555c9e9a684f24bc68201cb78 (diff)
loplugin:indentation find broken if statements
so I don't read the "then" block as being a sequential statements Change-Id: Ib2004acd3518bd4ebd2246f02a26c2c0a8bbab4c Reviewed-on: https://gerrit.libreoffice.org/82069 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'compilerplugins/clang')
-rw-r--r--compilerplugins/clang/indentation.cxx47
-rw-r--r--compilerplugins/clang/test/indentation.cxx33
2 files changed, 80 insertions, 0 deletions
diff --git a/compilerplugins/clang/indentation.cxx b/compilerplugins/clang/indentation.cxx
index 1b94eac3ec1a..9621ef549bbe 100644
--- a/compilerplugins/clang/indentation.cxx
+++ b/compilerplugins/clang/indentation.cxx
@@ -144,8 +144,10 @@ bool Indentation::VisitCompoundStmt(CompoundStmt const* compoundStmt)
auto stmtLoc = compat::getBeginLoc(stmt);
StringRef macroName;
+ bool partOfMacro = false;
if (SM.isMacroArgExpansion(stmtLoc) || SM.isMacroBodyExpansion(stmtLoc))
{
+ partOfMacro = true;
macroName = Lexer::getImmediateMacroNameForDiagnostics(
stmtLoc, compiler.getSourceManager(), compiler.getLangOpts());
// CPPUNIT_TEST_SUITE/CPPUNIT_TEST/CPPUNIT_TEST_SUITE_END work together, so the one is indented inside the other
@@ -213,6 +215,51 @@ bool Indentation::VisitCompoundStmt(CompoundStmt const* compoundStmt)
//getParentStmt(compoundStmt)->dump();
//stmt->dump();
}
+
+ if (!partOfMacro)
+ if (auto ifStmt = dyn_cast<IfStmt>(stmt))
+ {
+ auto bodyStmt = ifStmt->getThen();
+ if (bodyStmt && !isa<CompoundStmt>(bodyStmt))
+ {
+ stmtLoc = compat::getBeginLoc(bodyStmt);
+ invalid1 = false;
+ invalid2 = false;
+ unsigned bodyColumn = SM.getPresumedColumnNumber(stmtLoc, &invalid1);
+ unsigned bodyLine = SM.getPresumedLineNumber(stmtLoc, &invalid2);
+ if (invalid1 || invalid2)
+ return true;
+
+ if (bodyLine != tmpLine && bodyColumn <= tmpColumn)
+ report(DiagnosticsEngine::Warning, "if body should be indented", stmtLoc);
+ }
+
+ auto elseStmt = ifStmt->getElse();
+ if (elseStmt && !isa<CompoundStmt>(elseStmt) && !isa<IfStmt>(elseStmt))
+ {
+ stmtLoc = compat::getBeginLoc(elseStmt);
+ invalid1 = false;
+ invalid2 = false;
+ unsigned elseColumn = SM.getPresumedColumnNumber(stmtLoc, &invalid1);
+ unsigned elseLine = SM.getPresumedLineNumber(stmtLoc, &invalid2);
+ if (invalid1 || invalid2)
+ return true;
+ if (elseLine != tmpLine && elseColumn <= tmpColumn)
+ report(DiagnosticsEngine::Warning, "else body should be indented", stmtLoc);
+ }
+ if (elseStmt && !isa<CompoundStmt>(bodyStmt))
+ {
+ stmtLoc = ifStmt->getElseLoc();
+ invalid1 = false;
+ invalid2 = false;
+ unsigned elseColumn = SM.getPresumedColumnNumber(stmtLoc, &invalid1);
+ unsigned elseLine = SM.getPresumedLineNumber(stmtLoc, &invalid2);
+ if (invalid1 || invalid2)
+ return true;
+ if (elseLine != tmpLine && elseColumn != tmpColumn)
+ report(DiagnosticsEngine::Warning, "if and else not aligned", stmtLoc);
+ }
+ }
}
return true;
}
diff --git a/compilerplugins/clang/test/indentation.cxx b/compilerplugins/clang/test/indentation.cxx
index 27e858fb319c..e0e25884eebb 100644
--- a/compilerplugins/clang/test/indentation.cxx
+++ b/compilerplugins/clang/test/indentation.cxx
@@ -38,6 +38,39 @@ void top1(int x) {
case 2: foo(); break; // 1expected-error {{statement mis-aligned compared to neighbours [loplugin:indentation]}}
};
+
+ if (x)
+ foo(); // expected-error {{if body should be indented [loplugin:indentation]}}
+
+ if (x)
+ {
+ foo();
+ }
+
+ if (x)
+ ;
+ else
+ foo(); // expected-error {{else body should be indented [loplugin:indentation]}}
+
+ if (x)
+ ;
+ else
+ {
+ foo();
+ }
+
+ if (x)
+ ;
+ else // expected-error {{if and else not aligned [loplugin:indentation]}}
+ foo();
+
+ if (x)
+ {
+ } else
+ {
+ foo();
+ }
+
#if 0
if (x)
foo();