2018-11-12 12:17:35 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* Based on LLVM/Clang.
|
|
|
|
*
|
|
|
|
* This file is distributed under the University of Illinois Open Source
|
|
|
|
* License. See LICENSE.TXT for details.
|
|
|
|
*
|
|
|
|
*/
|
2019-07-17 15:06:43 +02:00
|
|
|
#ifndef LO_CLANG_SHARED_PLUGINS
|
2018-11-12 12:17:35 +02:00
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <set>
|
|
|
|
#include "plugin.hxx"
|
|
|
|
|
|
|
|
/*
|
|
|
|
Check for child statements inside a compound statement that do not share the same indentation.
|
|
|
|
|
|
|
|
TODO if an open-brace starts on a new line by itself, check that it lines up with it's closing-brace
|
|
|
|
TODO else should line up with if
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
class Indentation : public loplugin::FilteringPlugin<Indentation>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit Indentation(loplugin::InstantiationData const& data)
|
|
|
|
: FilteringPlugin(data)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-07-17 15:06:43 +02:00
|
|
|
virtual bool preRun() override
|
2018-11-12 12:17:35 +02:00
|
|
|
{
|
|
|
|
std::string fn(handler.getMainFileName());
|
|
|
|
loplugin::normalizeDotDotInFilePath(fn);
|
|
|
|
// include another file to build a table
|
|
|
|
if (fn == SRCDIR "/sc/source/core/tool/cellkeytranslator.cxx")
|
2019-07-17 15:06:43 +02:00
|
|
|
return false;
|
2018-11-12 12:17:35 +02:00
|
|
|
// weird structure
|
|
|
|
if (fn == SRCDIR "/sc/source/core/tool/compiler.cxx")
|
2019-07-17 15:06:43 +02:00
|
|
|
return false;
|
2018-11-12 12:17:35 +02:00
|
|
|
// looks like lex/yacc output
|
|
|
|
if (fn == SRCDIR "/hwpfilter/source/grammar.cxx")
|
2019-07-17 15:06:43 +02:00
|
|
|
return false;
|
2018-11-12 12:17:35 +02:00
|
|
|
// TODO need to learn to handle attributes like "[[maybe_unused]]"
|
|
|
|
if (fn == SRCDIR "/binaryurp/source/bridge.cxx")
|
2019-07-17 15:06:43 +02:00
|
|
|
return false;
|
2019-02-11 10:58:51 +02:00
|
|
|
// the QEMIT macros
|
2019-02-12 08:54:19 +01:00
|
|
|
if (loplugin::hasPathnamePrefix(fn, SRCDIR "/vcl/qt5/")
|
|
|
|
|| loplugin::isSamePathname(fn, SRCDIR "/vcl/unx/gtk3_kde5/kde5_filepicker_ipc.cxx"))
|
2019-07-17 15:06:43 +02:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void run() override
|
|
|
|
{
|
|
|
|
if (preRun())
|
|
|
|
TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
|
2018-11-12 12:17:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool VisitCompoundStmt(CompoundStmt const*);
|
2019-07-17 15:06:43 +02:00
|
|
|
bool PreTraverseSwitchStmt(SwitchStmt*);
|
|
|
|
void PostTraverseSwitchStmt(SwitchStmt*);
|
2018-11-12 12:17:35 +02:00
|
|
|
bool TraverseSwitchStmt(SwitchStmt*);
|
|
|
|
bool VisitSwitchStmt(SwitchStmt const*);
|
|
|
|
|
|
|
|
private:
|
2019-07-17 15:06:43 +02:00
|
|
|
std::vector<const Stmt*> switchStmtBodies;
|
2018-11-12 12:17:35 +02:00
|
|
|
};
|
|
|
|
|
2019-07-17 15:06:43 +02:00
|
|
|
bool Indentation::PreTraverseSwitchStmt(SwitchStmt* switchStmt)
|
|
|
|
{
|
|
|
|
switchStmtBodies.push_back(switchStmt->getBody());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Indentation::PostTraverseSwitchStmt(SwitchStmt*) { switchStmtBodies.pop_back(); }
|
|
|
|
|
2018-11-12 12:17:35 +02:00
|
|
|
bool Indentation::TraverseSwitchStmt(SwitchStmt* switchStmt)
|
|
|
|
{
|
2019-07-17 15:06:43 +02:00
|
|
|
PreTraverseSwitchStmt(switchStmt);
|
2018-11-12 12:17:35 +02:00
|
|
|
FilteringPlugin::TraverseSwitchStmt(switchStmt);
|
2019-07-17 15:06:43 +02:00
|
|
|
PostTraverseSwitchStmt(switchStmt);
|
2018-11-12 12:17:35 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Indentation::VisitCompoundStmt(CompoundStmt const* compoundStmt)
|
|
|
|
{
|
|
|
|
if (ignoreLocation(compoundStmt))
|
|
|
|
return true;
|
|
|
|
// these are kind of free form
|
2019-07-17 15:06:43 +02:00
|
|
|
if (!switchStmtBodies.empty() && switchStmtBodies.back() == compoundStmt)
|
2018-11-12 12:17:35 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
constexpr unsigned MAX = std::numeric_limits<unsigned>::max();
|
|
|
|
unsigned column = MAX;
|
|
|
|
Stmt const* firstStmt = nullptr;
|
|
|
|
unsigned curLine = MAX;
|
|
|
|
unsigned prevLine = MAX;
|
2019-02-19 21:09:29 +01:00
|
|
|
SourceLocation prevEnd;
|
2018-11-12 12:17:35 +02:00
|
|
|
auto& SM = compiler.getSourceManager();
|
|
|
|
for (auto i = compoundStmt->body_begin(); i != compoundStmt->body_end(); ++i)
|
|
|
|
{
|
|
|
|
auto stmt = *i;
|
2019-02-19 21:09:29 +01:00
|
|
|
auto const actualPrevEnd = prevEnd;
|
|
|
|
prevEnd = compat::getEndLoc(stmt); // compute early, before below `continue`s
|
2018-11-12 12:17:35 +02:00
|
|
|
|
|
|
|
// these show up in macro expansions, not interesting
|
|
|
|
if (isa<NullStmt>(stmt))
|
|
|
|
continue;
|
|
|
|
// these are always weirdly indented
|
|
|
|
if (isa<LabelStmt>(stmt))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto stmtLoc = compat::getBeginLoc(stmt);
|
|
|
|
|
|
|
|
StringRef macroName;
|
|
|
|
if (SM.isMacroArgExpansion(stmtLoc) || SM.isMacroBodyExpansion(stmtLoc))
|
|
|
|
{
|
|
|
|
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
|
|
|
|
if (macroName == "CPPUNIT_TEST_SUITE")
|
|
|
|
continue;
|
|
|
|
// similar thing in dbaccess/
|
|
|
|
if (macroName == "DECL_PROP_IMPL")
|
|
|
|
continue;
|
|
|
|
// similar thing in forms/
|
|
|
|
if (macroName == "DECL_IFACE_PROP_IMPL" || macroName == "DECL_BOOL_PROP_IMPL")
|
|
|
|
continue;
|
2019-02-15 12:13:18 +01:00
|
|
|
#if CLANG_VERSION >= 70000
|
2018-11-12 12:17:35 +02:00
|
|
|
stmtLoc = SM.getExpansionRange(stmtLoc).getBegin();
|
|
|
|
#else
|
|
|
|
stmtLoc = SM.getExpansionRange(stmtLoc).first;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for comment to the left of the statement
|
|
|
|
{
|
|
|
|
const char* p1 = SM.getCharacterData(stmtLoc);
|
|
|
|
--p1;
|
|
|
|
bool foundComment = false;
|
|
|
|
while (*p1 && *p1 != '\n')
|
|
|
|
{
|
|
|
|
if (*p1 == '/')
|
|
|
|
{
|
|
|
|
foundComment = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
--p1;
|
|
|
|
}
|
|
|
|
if (foundComment)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool invalid1 = false;
|
|
|
|
bool invalid2 = false;
|
|
|
|
unsigned tmpColumn = SM.getPresumedColumnNumber(stmtLoc, &invalid1);
|
|
|
|
unsigned tmpLine = SM.getPresumedLineNumber(stmtLoc, &invalid2);
|
|
|
|
if (invalid1 || invalid2)
|
|
|
|
continue;
|
|
|
|
prevLine = curLine;
|
|
|
|
curLine = tmpLine;
|
|
|
|
if (column == MAX)
|
|
|
|
{
|
|
|
|
column = tmpColumn;
|
|
|
|
firstStmt = stmt;
|
|
|
|
}
|
|
|
|
else if (curLine == prevLine)
|
|
|
|
{
|
|
|
|
// ignore multiple statements on same line
|
|
|
|
}
|
|
|
|
else if (column != tmpColumn)
|
|
|
|
{
|
2019-02-19 21:09:29 +01:00
|
|
|
if (containsPreprocessingConditionalInclusion(SourceRange(
|
|
|
|
locationAfterToken(compiler.getSourceManager().getExpansionLoc(actualPrevEnd)),
|
|
|
|
compiler.getSourceManager().getExpansionLoc(compat::getBeginLoc(stmt)))))
|
|
|
|
continue;
|
2018-11-12 12:17:35 +02:00
|
|
|
report(DiagnosticsEngine::Warning, "statement mis-aligned compared to neighbours %0",
|
|
|
|
stmtLoc)
|
|
|
|
<< macroName;
|
|
|
|
report(DiagnosticsEngine::Note, "measured against this one",
|
|
|
|
compat::getBeginLoc(firstStmt));
|
|
|
|
//getParentStmt(compoundStmt)->dump();
|
|
|
|
//stmt->dump();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Indentation::VisitSwitchStmt(SwitchStmt const* switchStmt)
|
|
|
|
{
|
|
|
|
if (ignoreLocation(switchStmt))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
constexpr unsigned MAX = std::numeric_limits<unsigned>::max();
|
|
|
|
unsigned column = MAX;
|
|
|
|
Stmt const* firstStmt = nullptr;
|
|
|
|
unsigned curLine = MAX;
|
|
|
|
unsigned prevLine = MAX;
|
|
|
|
auto& SM = compiler.getSourceManager();
|
|
|
|
auto compoundStmt = dyn_cast<CompoundStmt>(switchStmt->getBody());
|
|
|
|
if (!compoundStmt)
|
|
|
|
return true;
|
|
|
|
for (auto i = compoundStmt->body_begin(); i != compoundStmt->body_end(); ++i)
|
|
|
|
{
|
|
|
|
Stmt const* caseStmt = dyn_cast<CaseStmt>(*i);
|
|
|
|
if (!caseStmt)
|
|
|
|
caseStmt = dyn_cast<DefaultStmt>(*i);
|
|
|
|
if (!caseStmt)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto stmtLoc = compat::getBeginLoc(caseStmt);
|
|
|
|
|
|
|
|
bool invalid1 = false;
|
|
|
|
bool invalid2 = false;
|
|
|
|
unsigned tmpColumn = SM.getPresumedColumnNumber(stmtLoc, &invalid1);
|
|
|
|
unsigned tmpLine = SM.getPresumedLineNumber(stmtLoc, &invalid2);
|
|
|
|
if (invalid1 || invalid2)
|
|
|
|
continue;
|
|
|
|
prevLine = curLine;
|
|
|
|
curLine = tmpLine;
|
|
|
|
if (column == MAX)
|
|
|
|
{
|
|
|
|
column = tmpColumn;
|
|
|
|
firstStmt = caseStmt;
|
|
|
|
}
|
|
|
|
else if (curLine == prevLine)
|
|
|
|
{
|
|
|
|
// ignore multiple statements on same line
|
|
|
|
}
|
|
|
|
else if (column != tmpColumn)
|
|
|
|
{
|
2019-02-11 17:59:59 +00:00
|
|
|
// disable this for now, ends up touching some very large switch statements in sw/ and sc/
|
2018-11-12 12:17:35 +02:00
|
|
|
(void)firstStmt;
|
|
|
|
// report(DiagnosticsEngine::Warning, "statement mis-aligned compared to neighbours",
|
|
|
|
// stmtLoc);
|
|
|
|
// report(DiagnosticsEngine::Note, "measured against this one",
|
|
|
|
// compat::getBeginLoc(firstStmt));
|
|
|
|
//getParentStmt(compoundStmt)->dump();
|
|
|
|
//stmt->dump();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-17 15:06:43 +02:00
|
|
|
loplugin::Plugin::Registration<Indentation> indentation("indentation");
|
2018-11-12 12:17:35 +02:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2019-07-17 15:06:43 +02:00
|
|
|
#endif // LO_CLANG_SHARED_PLUGINS
|
|
|
|
|
2018-11-12 12:17:35 +02:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|