Kaydet (Commit) f76bf262 authored tarafından Noel Grandin's avatar Noel Grandin

new loplugin indentation

look for mixed indentation in compound statements, which makes them hard
to read, and sometimes makes it look like a statement is associated with
a nearby if/for

Change-Id: Ic8429cee1f9a86d938097a4a8769a2bce97b3361
Reviewed-on: https://gerrit.libreoffice.org/63283
Tested-by: Jenkins
Reviewed-by: 's avatarNoel Grandin <noel.grandin@collabora.co.uk>
üst b143e764
/* -*- 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.
*
*/
#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)
{
}
virtual void run() override
{
std::string fn(handler.getMainFileName());
loplugin::normalizeDotDotInFilePath(fn);
// include another file to build a table
if (fn == SRCDIR "/sc/source/core/tool/cellkeytranslator.cxx")
return;
// weird structure
if (fn == SRCDIR "/sc/source/core/tool/compiler.cxx")
return;
// looks like lex/yacc output
if (fn == SRCDIR "/hwpfilter/source/grammar.cxx")
return;
// TODO need to learn to handle attributes like "[[maybe_unused]]"
if (fn == SRCDIR "/binaryurp/source/bridge.cxx")
return;
TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
}
bool VisitCompoundStmt(CompoundStmt const*);
bool TraverseSwitchStmt(SwitchStmt*);
bool VisitSwitchStmt(SwitchStmt const*);
private:
Stmt const* switchStmtBody = nullptr;
};
bool Indentation::TraverseSwitchStmt(SwitchStmt* switchStmt)
{
auto prev = switchStmtBody;
switchStmtBody = switchStmt->getBody();
FilteringPlugin::TraverseSwitchStmt(switchStmt);
switchStmtBody = prev;
return true;
}
bool Indentation::VisitCompoundStmt(CompoundStmt const* compoundStmt)
{
if (ignoreLocation(compoundStmt))
return true;
// these are kind of free form
if (switchStmtBody == compoundStmt)
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();
for (auto i = compoundStmt->body_begin(); i != compoundStmt->body_end(); ++i)
{
auto stmt = *i;
// 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;
#if CLANG_VERSION >= 80000
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)
{
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)
{
// disable this for now, ends up touching some very large switch statemnts in sw/ and sc/
(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;
}
loplugin::Plugin::Registration<Indentation> X("indentation");
} // namespace
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
int foo();
int foo2(int);
#define SOME_MACRO(x) foo2(x)
void top1(int x) {
{
foo(); // expected-note {{measured against this one [loplugin:indentation]}}
foo(); // expected-error {{statement mis-aligned compared to neighbours [loplugin:indentation]}}
}
{
foo(); // expected-note {{measured against this one [loplugin:indentation]}}
SOME_MACRO(1); // expected-error {{statement mis-aligned compared to neighbours SOME_MACRO [loplugin:indentation]}}
}
// no warning expected
{
foo(); foo();
}
// no warning expected
/*xxx*/ foo();
// disable this for now, ends up touching some very large switch statemnts in sw/ and sc/
switch (x)
{
case 1: foo(); break; // 1expected-note {{measured against this one [loplugin:indentation]}}
case 2: foo(); break; // 1expected-error {{statement mis-aligned compared to neighbours [loplugin:indentation]}}
};
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
......@@ -374,7 +374,7 @@ void SfxObjectShell::SetReadOnly()
// medium open mode is adjusted accordingly, and the write lock
// on the file is removed.
if ( !pMedium || IsReadOnlyMedium() )
if ( !pMedium || IsReadOnlyMedium() )
return;
bool bWasROUI = IsReadOnly();
......
......@@ -32,6 +32,7 @@ $(eval $(call gb_CompilerTest_add_exception_objects,compilerplugins_clang, \
compilerplugins/clang/test/faileddyncast \
compilerplugins/clang/test/finalprotected \
compilerplugins/clang/test/flatten \
compilerplugins/clang/test/indentation \
compilerplugins/clang/test/loopvartoosmall \
compilerplugins/clang/test/oncevar \
compilerplugins/clang/test/oslendian-1 \
......
......@@ -143,7 +143,7 @@ int result = 32;
static unsigned int get_unaligned_uint(const unsigned char* cursor)
{
unsigned int result;
unsigned int result;
memcpy(&result, cursor, sizeof(unsigned int));
return result;
......@@ -168,9 +168,9 @@ struct pool
static void* pool_take_extent(struct pool* pool, int allocate)
{
unsigned int size = 0;
void* extent;
void* data = NULL;
unsigned int size = 0;
void* extent;
void* data = NULL;
if(pool->extent)
{
......@@ -214,7 +214,7 @@ void* data = NULL;
*/
static struct pool* pool_create(int size_elem, int primary, int secondary)
{
struct pool* pool;
struct pool* pool;
assert(primary > 0);
assert(secondary >= 0);
......@@ -237,8 +237,8 @@ struct pool* pool;
static void pool_destroy(struct pool* pool)
{
void* extent;
void* next;
void* extent;
void* next;
if(pool != NULL)
{
......@@ -255,7 +255,7 @@ void* next;
static void* pool_alloc(struct pool* pool)
{
void* data;
void* data;
data = pool->head_free;
if(data == NULL)
......@@ -442,7 +442,7 @@ static void hash_destroy(struct hash* hash)
static struct hash* hash_create(unsigned int size)
{
struct hash* hash;
struct hash* hash;
assert(size > 0);
hash = calloc(1, sizeof(struct hash));
......@@ -481,12 +481,12 @@ struct hash* hash;
static void hash_resize(struct hash* hash)
{
unsigned int old_size = hash->size;
unsigned int hashed;
struct hash_elem* hash_elem;
struct hash_elem* next;
struct hash_elem** array;
unsigned int i;
unsigned int old_size = hash->size;
unsigned int hashed;
struct hash_elem* hash_elem;
struct hash_elem* next;
struct hash_elem** array;
unsigned int i;
hash->size = (old_size << 1) + 1;
/* we really should avoid to get there... so print a message to alert of the condition */
......@@ -538,9 +538,9 @@ static int compare_key(struct hash const * hash, const char* a, const char* b, i
*/
static int hash_store(struct hash* hash, const char* key, int key_len)
{
unsigned int hashed;
struct hash_elem* hash_elem;
int cost = 0;
unsigned int hashed;
struct hash_elem* hash_elem;
int cost = 0;
(void) cost;
hashed = hash_compute(hash, key, key_len);
......@@ -583,7 +583,7 @@ int cost = 0;
static int file_stat(const char* name, struct stat* buffer_stat, int* rc)
{
int rc_local = 0;
int rc_local = 0;
rc_local = stat(name, buffer_stat);
if (rc_local < 0)
......@@ -595,8 +595,8 @@ int rc_local = 0;
static off_t file_get_size(const char* name, int* rc)
{
struct stat buffer_stat;
off_t size = -1;
struct stat buffer_stat;
off_t size = -1;
if (!file_stat(name, &buffer_stat, rc))
{
......@@ -619,10 +619,10 @@ static size_t file_load_buffer_count = 0;
static char* file_load(const char* name, off_t* size, int* return_rc)
{
off_t local_size = 0;
int rc = 0;
char* buffer = NULL;
int fd;
off_t local_size = 0;
int rc = 0;
char* buffer = NULL;
int fd;
assert(name != NULL);
......@@ -657,7 +657,7 @@ int fd;
}
else
{
ssize_t i;
ssize_t i;
REDO:
i = read(fd, buffer, (size_t)(*size));
......@@ -873,9 +873,9 @@ static char * eat_space_at_end(char * end)
static char* phony_content_buffer;
static char* generate_phony_line(char const * phony_target, char const * extension)
{
char const * src;
char* dest;
char* last_dot = NULL;
char const * src;
char* dest;
char* last_dot = NULL;
//fprintf(stderr, "generate_phony_line called with phony_target %s and extension %s\n", phony_target, extension);
for(dest = phony_content_buffer+work_dir_len+1, src = phony_target; *src != 0; ++src, ++dest)
{
......@@ -898,7 +898,7 @@ char* last_dot = NULL;
static int generate_phony_file(char* fn, char const * content)
{
FILE* depfile;
FILE* depfile;
depfile = fopen(fn, "w");
if(!depfile)
{
......@@ -914,17 +914,17 @@ FILE* depfile;
static int process(struct hash* dep_hash, char* fn)
{
int rc;
char* buffer;
char* end;
char* cursor;
char* cursor_out;
char* base;
char* created_line = NULL;
char* src_relative;
int continuation = 0;
char last_ns = 0;
off_t size;
int rc;
char* buffer;
char* end;
char* cursor;
char* cursor_out;
char* base;
char* created_line = NULL;
char* src_relative;
int continuation = 0;
char last_ns = 0;
off_t size;
buffer = file_load(fn, &size, &rc);
if(!rc)
......@@ -1129,13 +1129,13 @@ static int get_var(char **var, const char *name)
int main(int argc, char** argv)
{
int rc = 0;
off_t in_list_size = 0;
char* in_list;
char* in_list_cursor;
char* in_list_base;
struct hash* dep_hash = NULL;
const char *env_str;
int rc = 0;
off_t in_list_size = 0;
char* in_list;
char* in_list_cursor;
char* in_list_base;
struct hash* dep_hash = NULL;
const char *env_str;
if(argc < 2)
{
......
......@@ -1824,6 +1824,7 @@ compilerplugins/clang/test/externvar.hxx
compilerplugins/clang/test/faileddyncast.cxx
compilerplugins/clang/test/finalprotected.cxx
compilerplugins/clang/test/flatten.cxx
compilerplugins/clang/test/indentation.cxx
compilerplugins/clang/test/loopvartoosmall.cxx
compilerplugins/clang/test/oncevar.cxx
compilerplugins/clang/test/oslendian-1.cxx
......
......@@ -928,7 +928,7 @@ void ElementDescriptor::readDataAwareAttr( OUString const & rAttrName )
try
{
Reference< beans::XPropertySet > xConvertor( xFac->createInstance( "com.sun.star.table.CellAddressConversion" ), uno::UNO_QUERY );
Reference< beans::XPropertySet > xBindable( xBinding->getValueBinding(), UNO_QUERY );
Reference< beans::XPropertySet > xBindable( xBinding->getValueBinding(), UNO_QUERY );
if ( xBindable.is() )
{
table::CellAddress aAddress;
......
......@@ -72,9 +72,9 @@ Reference< xml::input::XElement > Frame::startChildElement(
void Frame::endElement()
{
if ( !m_xContainer.is() )
if ( !m_xContainer.is() )
m_xContainer.set( m_xImport->_xDialogModelFactory->createInstance( "com.sun.star.awt.UnoFrameModel" ), UNO_QUERY );
Reference< beans::XPropertySet > xProps( m_xContainer, UNO_QUERY_THROW );
Reference< beans::XPropertySet > xProps( m_xContainer, UNO_QUERY_THROW );
// m_xImport is what we need to add to ( e.g. the dialog in this case )
ControlImportContext ctx( m_xImport.get(), xProps, getControlId( _xAttributes ) );
......@@ -118,7 +118,7 @@ Reference< xml::input::XElement > MultiPage::startChildElement(
// Create new DialogImport for this container
DialogImport* pMultiPageImport = new DialogImport( *m_xImport );
pMultiPageImport->_xDialogModel = m_xContainer;
pMultiPageImport->_xDialogModel = m_xContainer;
return new BulletinBoardElement( rLocalName, xAttributes, this, pMultiPageImport );
}
else
......@@ -130,7 +130,7 @@ Reference< xml::input::XElement > MultiPage::startChildElement(
void MultiPage::endElement()
{
Reference< beans::XPropertySet > xProps( m_xContainer, UNO_QUERY_THROW );
Reference< beans::XPropertySet > xProps( m_xContainer, UNO_QUERY_THROW );
// m_xImport is what we need to add to ( e.g. the dialog in this case )
ControlImportContext ctx( m_xImport.get(), xProps, getControlId( _xAttributes ));
......@@ -148,7 +148,7 @@ void MultiPage::endElement()
ctx.importDefaults( 0, 0, _xAttributes ); // inherited from BulletinBoardElement
ctx.importLongProperty("MultiPageValue" , "value", _xAttributes );
ctx.importBooleanProperty( "Decoration", "withtabs", _xAttributes) ;
ctx.importBooleanProperty( "Decoration", "withtabs", _xAttributes) ;
ctx.importEvents( _events );
// avoid ring-reference:
// vector< event elements > holding event elements holding this (via _pParent)
......@@ -171,7 +171,7 @@ Reference< xml::input::XElement > Page::startChildElement(
{
DialogImport* pPageImport = new DialogImport( *m_xImport );
pPageImport->_xDialogModel = m_xContainer;
pPageImport->_xDialogModel = m_xContainer;
return new BulletinBoardElement( rLocalName, xAttributes, this, pPageImport );
}
else
......@@ -183,7 +183,7 @@ Reference< xml::input::XElement > Page::startChildElement(
void Page::endElement()
{
Reference< beans::XPropertySet > xProps( m_xContainer, UNO_QUERY_THROW );
Reference< beans::XPropertySet > xProps( m_xContainer, UNO_QUERY_THROW );
ControlImportContext ctx( m_xImport.get(), xProps, getControlId( _xAttributes ));
......
......@@ -1766,7 +1766,7 @@ Reference< xml::sax::XDocumentHandler > importDialogModel(
// single set of styles and stylenames apply to all containers
std::shared_ptr< std::vector< OUString > > pStyleNames( new std::vector< OUString > );
std::shared_ptr< std::vector< css::uno::Reference< css::xml::input::XElement > > > pStyles( new std::vector< css::uno::Reference< css::xml::input::XElement > > );
return ::xmlscript::createDocumentHandler(
return ::xmlscript::createDocumentHandler(
static_cast< xml::input::XRoot * >(
new DialogImport( xContext, xDialogModel, pStyleNames, pStyles, xDocument ) ) );
}
......
......@@ -96,11 +96,11 @@ CertificateContainer::addCertificate( const OUString & url, const OUString & cer
{
certMap.emplace( url, certificate_name );
//remember that the cert is trusted
if (trust)
certTrustMap.emplace( url, certificate_name );
//remember that the cert is trusted
if (trust)
certTrustMap.emplace( url, certificate_name );
return true;
return true;
}
::security::CertificateContainerStatus
......
......@@ -608,7 +608,7 @@ void DigitalSignaturesDialog::ImplFillSignaturesBox()
if ( bSigValid )
{
bSigValid = DocumentSignatureHelper::checkIfAllFilesAreSigned(
bSigValid = DocumentSignatureHelper::checkIfAllFilesAreSigned(
aElementsToBeVerified, rInfo, mode);
if( bSigValid )
......
......@@ -415,7 +415,7 @@ cssu::Reference< css::io::XInputStream >
* xInputStream - the XInputStream interface
******************************************************************************/
{
cssu::Reference< css::io::XInputStream > xObjectInputStream;
cssu::Reference< css::io::XInputStream > xObjectInputStream;
SAL_WARN_IF( !m_xUriBinding.is(), "xmlsecurity.helper", "Need XUriBinding!" );
......
......@@ -164,7 +164,7 @@ void XSecController::addStreamReference(
bool isBinary,
sal_Int32 nDigestID )
{
SignatureReferenceType type = (isBinary?SignatureReferenceType::BINARYSTREAM:SignatureReferenceType::XMLSTREAM);
SignatureReferenceType type = (isBinary?SignatureReferenceType::BINARYSTREAM:SignatureReferenceType::XMLSTREAM);
if (m_vInternalSignatureInformations.empty())
{
......@@ -178,7 +178,7 @@ void XSecController::addStreamReference(
/*
* get the input stream
*/
cssu::Reference< css::io::XInputStream > xObjectInputStream
cssu::Reference< css::io::XInputStream > xObjectInputStream
= getObjectInputStream( ouUri );
if ( xObjectInputStream.is() )
......
......@@ -176,7 +176,7 @@ struct InitNSSInitialize
bInitialized = nsscrypto_initialize( m_xContext, bNSSInit );
if (bNSSInit)
atexit(nsscrypto_finalize );
return & bInitialized;
return & bInitialized;
}
};
......
......@@ -554,17 +554,17 @@ verifyCertificate( const Reference< csss::XCertificate >& aCert,
nullptr /* nickname */,
PR_FALSE /* isPerm */,
PR_TRUE /* copyDER */);
if (!certTmp)
{
if (!certTmp)
{
SAL_INFO("xmlsecurity.xmlsec", "Failed to add a temporary certificate: " << intermediateCerts[i]->getIssuerName());
}
else
{
}
else
{
SAL_INFO("xmlsecurity.xmlsec", "Added temporary certificate: " <<
(certTmp->subjectName ? certTmp->subjectName : ""));
vecTmpNSSCertificates.push_back(certTmp);
}
}
}
......
......@@ -210,7 +210,7 @@ SAL_CALL XMLSignature_NssImpl::validate(
throw RuntimeException() ;
}
setErrorRecorder();
setErrorRecorder();
sal_Int32 nSecurityEnvironment = aSecurityCtx->getSecurityEnvironmentNumber();
sal_Int32 i;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment