diff options
author | Noel Grandin <noel.grandin@collabora.co.uk> | 2021-10-20 12:48:09 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2021-10-20 15:39:38 +0200 |
commit | 283a9790bffa6536f4c26bd31d85f815bc64dd08 (patch) | |
tree | c65ac7fe50fbea809dd765c24edbb47b57adea2b /compilerplugins/clang | |
parent | 18bdf78e156f3cd1e6ccbb3ae28e919583bac70c (diff) |
loplugin:indentation check for indent inside block
look for places where the statements inside a block are
not indented
Change-Id: I0cbfa7e0b6fb194b2aff6fa7e070fb907d70ca2f
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/123885
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'compilerplugins/clang')
-rw-r--r-- | compilerplugins/clang/indentation.cxx | 45 | ||||
-rw-r--r-- | compilerplugins/clang/test/indentation.cxx | 10 |
2 files changed, 41 insertions, 14 deletions
diff --git a/compilerplugins/clang/indentation.cxx b/compilerplugins/clang/indentation.cxx index 345b33a5bd63..2dda32f8174c 100644 --- a/compilerplugins/clang/indentation.cxx +++ b/compilerplugins/clang/indentation.cxx @@ -345,8 +345,8 @@ void Indentation::checkCompoundStmtBraces(const Stmt* parentStmt, const Compound if (invalid1) return; - auto startBraceLoc = compat::getBeginLoc(compoundStmt); - auto endBraceLoc = compat::getEndLoc(compoundStmt); + auto startBraceLoc = compoundStmt->getLBracLoc(); + auto endBraceLoc = compoundStmt->getRBracLoc(); unsigned beginColumn = SM.getPresumedColumnNumber(startBraceLoc, &invalid1); unsigned beginLine = SM.getPresumedLineNumber(startBraceLoc, &invalid2); if (invalid1 || invalid2) @@ -384,21 +384,38 @@ void Indentation::checkCompoundStmtBraces(const Stmt* parentStmt, const Compound endBraceLoc); report(DiagnosticsEngine::Note, "statement beginning here", parentBeginLoc); } + return; } - else + + if (parentColumn != beginColumn) { - if (parentColumn != beginColumn) - { - report(DiagnosticsEngine::Warning, - "start brace not aligned with beginning of parent statement", startBraceLoc); - report(DiagnosticsEngine::Note, "statement beginning here", parentBeginLoc); - } - else if (beginColumn != endColumn) - { - report(DiagnosticsEngine::Warning, "start and end brace not aligned", endBraceLoc); - report(DiagnosticsEngine::Note, "start brace here", startBraceLoc); - } + report(DiagnosticsEngine::Warning, + "start brace not aligned with beginning of parent statement", startBraceLoc); + report(DiagnosticsEngine::Note, "statement beginning here", parentBeginLoc); + } + else if (beginColumn != endColumn) + { + report(DiagnosticsEngine::Warning, "start and end brace not aligned", endBraceLoc); + report(DiagnosticsEngine::Note, "start brace here", startBraceLoc); } + + /** now check that lines inside the compoundstmt are indented */ + if (!compoundStmt->size()) + return; + auto firstStmt = compoundStmt->body_front(); + if (isa<LabelStmt>(firstStmt)) + return; + auto firstStmtLoc = compat::getBeginLoc(firstStmt); + unsigned firstStmtBeginColumn = SM.getPresumedColumnNumber(firstStmtLoc, &invalid1); + if (invalid1) + return; + if (firstStmtBeginColumn > beginColumn) + return; + StringRef fn = getFilenameOfLocation(compiler.getSourceManager().getSpellingLoc(firstStmtLoc)); + // this is doing code generation, so the weird layout is deliberate + if (loplugin::hasPathnamePrefix(fn, SRCDIR "/sc/source/core/opencl/")) + return; + report(DiagnosticsEngine::Warning, "body inside brace not indented", firstStmtLoc); } bool Indentation::VisitSwitchStmt(SwitchStmt const* switchStmt) diff --git a/compilerplugins/clang/test/indentation.cxx b/compilerplugins/clang/test/indentation.cxx index 8ef6d2c03653..71b8c6f61e5a 100644 --- a/compilerplugins/clang/test/indentation.cxx +++ b/compilerplugins/clang/test/indentation.cxx @@ -101,4 +101,14 @@ void attr_bad() { } #endif +void xxx(); +void test5(bool x) +{ + if (x) + { + xxx(); // expected-error {{body inside brace not indented [loplugin:indentation]}} + } +} + + /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ |