[Libreoffice-commits] core.git: 2 commits - basctl/source comphelper/source compilerplugins/clang i18nlangtag/source include/comphelper include/i18nlangtag include/ucbhelper solenv/CompilerTest_compilerplugins_clang.mk ucbhelper/source
Noel Grandin
noel.grandin at collabora.co.uk
Wed Oct 25 06:32:37 UTC 2017
basctl/source/basicide/scriptdocument.cxx | 2
comphelper/source/container/embeddedobjectcontainer.cxx | 8 -
comphelper/source/misc/backupfilehelper.cxx | 4
comphelper/source/misc/syntaxhighlight.cxx | 2
comphelper/source/property/propertystatecontainer.cxx | 2
comphelper/source/xml/ofopxmlhelper.cxx | 4
compilerplugins/clang/dodgyswitch.cxx | 77 ++++++++++++++++
compilerplugins/clang/test/dodgyswitch.cxx | 30 ++++++
i18nlangtag/source/languagetag/languagetag.cxx | 24 +++-
include/comphelper/MasterPropertySet.hxx | 2
include/comphelper/backupfilehelper.hxx | 2
include/comphelper/embeddedobjectcontainer.hxx | 8 -
include/comphelper/propertystatecontainer.hxx | 4
include/comphelper/proxyaggregation.hxx | 2
include/comphelper/stillreadwriteinteraction.hxx | 2
include/comphelper/syntaxhighlight.hxx | 2
include/i18nlangtag/languagetag.hxx | 3
include/ucbhelper/content.hxx | 2
include/ucbhelper/interactionrequest.hxx | 2
include/ucbhelper/resultset.hxx | 6 -
solenv/CompilerTest_compilerplugins_clang.mk | 1
ucbhelper/source/client/content.cxx | 4
ucbhelper/source/provider/interactionrequest.cxx | 2
ucbhelper/source/provider/resultset.cxx | 10 +-
24 files changed, 161 insertions(+), 44 deletions(-)
New commits:
commit 57c5f980835f834f6ea96c6f5ca841b5372aa61d
Author: Noel Grandin <noel.grandin at collabora.co.uk>
Date: Tue Oct 24 13:02:58 2017 +0200
loplugin:constmethod in comphelper,ucbhelper
Change-Id: I27a860fbbedd2174c60c199af18cae76e02abc25
Reviewed-on: https://gerrit.libreoffice.org/43759
Tested-by: Jenkins <ci at libreoffice.org>
Reviewed-by: Noel Grandin <noel.grandin at collabora.co.uk>
diff --git a/comphelper/source/container/embeddedobjectcontainer.cxx b/comphelper/source/container/embeddedobjectcontainer.cxx
index 520d37e1f8c4..49d033b54007 100644
--- a/comphelper/source/container/embeddedobjectcontainer.cxx
+++ b/comphelper/source/container/embeddedobjectcontainer.cxx
@@ -226,12 +226,12 @@ OUString EmbeddedObjectContainer::CreateUniqueObjectName()
return aStr;
}
-uno::Sequence < OUString > EmbeddedObjectContainer::GetObjectNames()
+uno::Sequence < OUString > EmbeddedObjectContainer::GetObjectNames() const
{
return comphelper::mapKeysToSequence(pImpl->maObjectContainer);
}
-bool EmbeddedObjectContainer::HasEmbeddedObjects()
+bool EmbeddedObjectContainer::HasEmbeddedObjects() const
{
return pImpl->maObjectContainer.size() != 0;
}
@@ -247,7 +247,7 @@ bool EmbeddedObjectContainer::HasEmbeddedObject( const OUString& rName )
return xAccess->hasByName(rName);
}
-bool EmbeddedObjectContainer::HasEmbeddedObject( const uno::Reference < embed::XEmbeddedObject >& xObj )
+bool EmbeddedObjectContainer::HasEmbeddedObject( const uno::Reference < embed::XEmbeddedObject >& xObj ) const
{
for( const auto& rObj : pImpl->maObjectContainer )
{
@@ -266,7 +266,7 @@ bool EmbeddedObjectContainer::HasInstantiatedEmbeddedObject( const OUString& rNa
return ( aIt != pImpl->maObjectContainer.end() );
}
-OUString EmbeddedObjectContainer::GetEmbeddedObjectName( const css::uno::Reference < css::embed::XEmbeddedObject >& xObj )
+OUString EmbeddedObjectContainer::GetEmbeddedObjectName( const css::uno::Reference < css::embed::XEmbeddedObject >& xObj ) const
{
for( const auto& rObj : pImpl->maObjectContainer )
{
diff --git a/comphelper/source/misc/backupfilehelper.cxx b/comphelper/source/misc/backupfilehelper.cxx
index a6a4e5ec9e8e..614789a3bcbc 100644
--- a/comphelper/source/misc/backupfilehelper.cxx
+++ b/comphelper/source/misc/backupfilehelper.cxx
@@ -1653,7 +1653,7 @@ namespace
}
}
- bool empty()
+ bool empty() const
{
return maPackedFileEntryVector.empty();
}
@@ -1940,7 +1940,7 @@ namespace comphelper
return bDidPop;
}
- bool BackupFileHelper::isPopPossibleExtensionInfo()
+ bool BackupFileHelper::isPopPossibleExtensionInfo() const
{
bool bPopPossible(false);
diff --git a/comphelper/source/misc/syntaxhighlight.cxx b/comphelper/source/misc/syntaxhighlight.cxx
index 0557a3f95b4a..131fa704ec86 100644
--- a/comphelper/source/misc/syntaxhighlight.cxx
+++ b/comphelper/source/misc/syntaxhighlight.cxx
@@ -712,7 +712,7 @@ void SyntaxHighlighter::getHighlightPortions(const OUString& rLine,
m_tokenizer->getHighlightPortions( rLine, portions );
}
-HighlighterLanguage SyntaxHighlighter::GetLanguage()
+HighlighterLanguage SyntaxHighlighter::GetLanguage() const
{
return m_tokenizer->aLanguage;
}
diff --git a/comphelper/source/property/propertystatecontainer.cxx b/comphelper/source/property/propertystatecontainer.cxx
index d06c3f190443..a2e1245fe57c 100644
--- a/comphelper/source/property/propertystatecontainer.cxx
+++ b/comphelper/source/property/propertystatecontainer.cxx
@@ -149,7 +149,7 @@ namespace comphelper
}
- PropertyState OPropertyStateContainer::getPropertyStateByHandle( sal_Int32 _nHandle )
+ PropertyState OPropertyStateContainer::getPropertyStateByHandle( sal_Int32 _nHandle ) const
{
// simply compare the current and the default value
Any aCurrentValue; getFastPropertyValue( aCurrentValue, _nHandle );
diff --git a/comphelper/source/xml/ofopxmlhelper.cxx b/comphelper/source/xml/ofopxmlhelper.cxx
index 0d4e164a2f66..205b8883c0d3 100644
--- a/comphelper/source/xml/ofopxmlhelper.cxx
+++ b/comphelper/source/xml/ofopxmlhelper.cxx
@@ -68,7 +68,7 @@ class OFOPXMLHelper_Impl
public:
- css::uno::Sequence< css::uno::Sequence< css::beans::StringPair > > const & GetParsingResult();
+ css::uno::Sequence< css::uno::Sequence< css::beans::StringPair > > const & GetParsingResult() const;
explicit OFOPXMLHelper_Impl( sal_uInt16 nFormat ); // must not be created directly
@@ -269,7 +269,7 @@ OFOPXMLHelper_Impl::OFOPXMLHelper_Impl( sal_uInt16 nFormat )
{
}
-uno::Sequence< uno::Sequence< beans::StringPair > > const & OFOPXMLHelper_Impl::GetParsingResult()
+uno::Sequence< uno::Sequence< beans::StringPair > > const & OFOPXMLHelper_Impl::GetParsingResult() const
{
if ( m_aElementsSeq.size() )
throw uno::RuntimeException(); // the parsing has still not finished!
diff --git a/i18nlangtag/source/languagetag/languagetag.cxx b/i18nlangtag/source/languagetag/languagetag.cxx
index 057718620374..1e65fe593f6d 100644
--- a/i18nlangtag/source/languagetag/languagetag.cxx
+++ b/i18nlangtag/source/languagetag/languagetag.cxx
@@ -1010,16 +1010,25 @@ LanguageTag::ImplPtr LanguageTag::registerImpl() const
}
-LanguageTag::ImplPtr const & LanguageTag::getImpl() const
+LanguageTagImpl const * LanguageTag::getImpl() const
{
if (!mpImpl)
{
mpImpl = registerImpl();
syncVarsFromRawImpl();
}
- return mpImpl;
+ return mpImpl.get();
}
+LanguageTagImpl * LanguageTag::getImpl()
+{
+ if (!mpImpl)
+ {
+ mpImpl = registerImpl();
+ syncVarsFromRawImpl();
+ }
+ return mpImpl.get();
+}
void LanguageTag::resetVars()
{
@@ -1277,8 +1286,7 @@ bool LanguageTagImpl::synCanonicalize()
void LanguageTag::syncFromImpl()
{
- ImplPtr xImpl = getImpl();
- LanguageTagImpl* pImpl = xImpl.get();
+ LanguageTagImpl* pImpl = getImpl();
bool bRegister = ((mbInitializedBcp47 && maBcp47 != pImpl->maBcp47) ||
(mbInitializedLangID && mnLangID != pImpl->mnLangID));
SAL_INFO_IF( bRegister, "i18nlangtag",
@@ -1829,7 +1837,7 @@ OUString const & LanguageTagImpl::getLanguage() const
OUString LanguageTag::getLanguage() const
{
- ImplPtr pImpl = getImpl();
+ LanguageTagImpl const* pImpl = getImpl();
if (pImpl->mbCachedLanguage)
return pImpl->maCachedLanguage;
OUString aRet( pImpl->getLanguage());
@@ -1851,7 +1859,7 @@ OUString const & LanguageTagImpl::getScript() const
OUString LanguageTag::getScript() const
{
- ImplPtr pImpl = getImpl();
+ LanguageTagImpl const* pImpl = getImpl();
if (pImpl->mbCachedScript)
return pImpl->maCachedScript;
OUString aRet( pImpl->getScript());
@@ -1887,7 +1895,7 @@ OUString const & LanguageTagImpl::getCountry() const
OUString LanguageTag::getCountry() const
{
- ImplPtr pImpl = getImpl();
+ LanguageTagImpl const* pImpl = getImpl();
if (pImpl->mbCachedCountry)
return pImpl->maCachedCountry;
OUString aRet( pImpl->getCountry());
@@ -1915,7 +1923,7 @@ OUString const & LanguageTagImpl::getVariants() const
OUString LanguageTag::getVariants() const
{
- ImplPtr pImpl = getImpl();
+ LanguageTagImpl const * pImpl = getImpl();
if (pImpl->mbCachedVariants)
return pImpl->maCachedVariants;
OUString aRet( pImpl->getVariants());
diff --git a/include/comphelper/MasterPropertySet.hxx b/include/comphelper/MasterPropertySet.hxx
index 937832488b78..44472d4c3977 100644
--- a/include/comphelper/MasterPropertySet.hxx
+++ b/include/comphelper/MasterPropertySet.hxx
@@ -39,7 +39,7 @@ namespace comphelper
bool mbInit;
SlaveData ( ChainablePropertySet *pSlave);
- bool IsInit () { return mbInit;}
+ bool IsInit () const { return mbInit;}
void SetInit ( bool bInit) { mbInit = bInit; }
};
}
diff --git a/include/comphelper/backupfilehelper.hxx b/include/comphelper/backupfilehelper.hxx
index 68835b866452..6f3445870625 100644
--- a/include/comphelper/backupfilehelper.hxx
+++ b/include/comphelper/backupfilehelper.hxx
@@ -137,7 +137,7 @@ namespace comphelper
* isPopPossibleExtensionInfo is the specialized version for ExtensionInfo
*/
bool isPopPossible();
- bool isPopPossibleExtensionInfo();
+ bool isPopPossibleExtensionInfo() const;
/** tries to execute a restore. Will overwrite the base file
* in that case and take one version off the 'stack' of copies.
diff --git a/include/comphelper/embeddedobjectcontainer.hxx b/include/comphelper/embeddedobjectcontainer.hxx
index eb4cd1308911..d838af94a3c1 100644
--- a/include/comphelper/embeddedobjectcontainer.hxx
+++ b/include/comphelper/embeddedobjectcontainer.hxx
@@ -80,18 +80,18 @@ public:
OUString CreateUniqueObjectName();
// get a list of object names that have been added so far
- css::uno::Sequence < OUString > GetObjectNames();
+ css::uno::Sequence < OUString > GetObjectNames() const;
// check for existence of objects at all
- bool HasEmbeddedObjects();
+ bool HasEmbeddedObjects() const;
// check existence of an object - either by identity or by name
bool HasEmbeddedObject( const OUString& );
- bool HasEmbeddedObject( const css::uno::Reference < css::embed::XEmbeddedObject >& );
+ bool HasEmbeddedObject( const css::uno::Reference < css::embed::XEmbeddedObject >& ) const;
bool HasInstantiatedEmbeddedObject( const OUString& );
// get the object name of an object - this is the persist name if the object has persistence
- OUString GetEmbeddedObjectName( const css::uno::Reference < css::embed::XEmbeddedObject >& );
+ OUString GetEmbeddedObjectName( const css::uno::Reference < css::embed::XEmbeddedObject >& ) const;
// retrieve an embedded object by name that either has been added already or is available in the container storage
css::uno::Reference<css::embed::XEmbeddedObject> GetEmbeddedObject(const OUString&, OUString const* pBaseURL = nullptr);
diff --git a/include/comphelper/propertystatecontainer.hxx b/include/comphelper/propertystatecontainer.hxx
index 1585f85c3ec6..f2f04410d732 100644
--- a/include/comphelper/propertystatecontainer.hxx
+++ b/include/comphelper/propertystatecontainer.hxx
@@ -74,14 +74,14 @@ namespace comphelper
<p>Already implemented by this base class, no need to override</p>
@precond <arg>_nHandle</arg> is a valid property handle
*/
- css::beans::PropertyState getPropertyStateByHandle( sal_Int32 _nHandle );
+ css::beans::PropertyState getPropertyStateByHandle( sal_Int32 _nHandle ) const;
/** set the property denoted by the given handle to its default value
<p>Already implemented by this base class, no need to override</p>
@precond <arg>_nHandle</arg> is a valid property handle
*/
- void setPropertyToDefaultByHandle( sal_Int32 _nHandle );
+ void setPropertyToDefaultByHandle( sal_Int32 _nHandle );
/** get the default value for the property denoted by the given handle
diff --git a/include/comphelper/proxyaggregation.hxx b/include/comphelper/proxyaggregation.hxx
index 465d3e243666..585d81010931 100644
--- a/include/comphelper/proxyaggregation.hxx
+++ b/include/comphelper/proxyaggregation.hxx
@@ -88,7 +88,7 @@ namespace comphelper
css::uno::Reference< css::uno::XComponentContext > m_xContext;
protected:
- const css::uno::Reference< css::uno::XComponentContext >& getComponentContext()
+ const css::uno::Reference< css::uno::XComponentContext >& getComponentContext() const
{
return m_xContext;
}
diff --git a/include/comphelper/stillreadwriteinteraction.hxx b/include/comphelper/stillreadwriteinteraction.hxx
index 375a23590bcb..11b0f12c4e69 100644
--- a/include/comphelper/stillreadwriteinteraction.hxx
+++ b/include/comphelper/stillreadwriteinteraction.hxx
@@ -45,7 +45,7 @@ public:
void resetInterceptions();
void resetErrorStates();
- bool wasWriteError() { return (m_bUsed && m_bHandledByMySelf);}
+ bool wasWriteError() const { return (m_bUsed && m_bHandledByMySelf);}
private:
css::uno::Reference< css::task::XInteractionHandler > m_xAuxiliaryHandler;
diff --git a/include/comphelper/syntaxhighlight.hxx b/include/comphelper/syntaxhighlight.hxx
index 76e87124bdc8..c8faf2211bfc 100644
--- a/include/comphelper/syntaxhighlight.hxx
+++ b/include/comphelper/syntaxhighlight.hxx
@@ -81,7 +81,7 @@ public:
void getHighlightPortions( const OUString& rLine,
std::vector<HighlightPortion>& pPortions ) const;
- HighlighterLanguage GetLanguage();
+ HighlighterLanguage GetLanguage() const;
};
#endif
diff --git a/include/i18nlangtag/languagetag.hxx b/include/i18nlangtag/languagetag.hxx
index 5088756e4691..42da7cc88179 100644
--- a/include/i18nlangtag/languagetag.hxx
+++ b/include/i18nlangtag/languagetag.hxx
@@ -544,7 +544,8 @@ private:
mutable bool mbInitializedLangID : 1;
bool mbIsFallback : 1;
- ImplPtr const & getImpl() const;
+ LanguageTagImpl* getImpl();
+ LanguageTagImpl const* getImpl() const;
ImplPtr registerImpl() const;
void syncFromImpl();
void syncVarsFromRawImpl() const;
diff --git a/include/ucbhelper/content.hxx b/include/ucbhelper/content.hxx
index eec9d2d26926..3d48b70915f0 100644
--- a/include/ucbhelper/content.hxx
+++ b/include/ucbhelper/content.hxx
@@ -663,7 +663,7 @@ public:
bool bMajorVersion = false,
const OUString & rCommentVersion = OUString( ),
OUString* pResultURL = nullptr,
- const OUString & rDocumentId = OUString( ) );
+ const OUString & rDocumentId = OUString( ) ) const;
/**
* This method lock the resource.
diff --git a/include/ucbhelper/interactionrequest.hxx b/include/ucbhelper/interactionrequest.hxx
index 7c2a5f565cad..5481797ff4b5 100644
--- a/include/ucbhelper/interactionrequest.hxx
+++ b/include/ucbhelper/interactionrequest.hxx
@@ -548,7 +548,7 @@ public:
// XAuthFallback
virtual void SAL_CALL setCode( const OUString& code ) override;
/// @throws css::uno::RuntimeException
- const OUString& SAL_CALL getCode();
+ const OUString& SAL_CALL getCode() const;
};
diff --git a/include/ucbhelper/resultset.hxx b/include/ucbhelper/resultset.hxx
index a71afddabb5f..906b7c9af103 100644
--- a/include/ucbhelper/resultset.hxx
+++ b/include/ucbhelper/resultset.hxx
@@ -259,7 +259,7 @@ public:
* @param rEvt is a property change event.
*/
void propertyChanged(
- const css::beans::PropertyChangeEvent& rEvt );
+ const css::beans::PropertyChangeEvent& rEvt ) const;
/**
* This method should be called by the data supplier for the result set
@@ -283,7 +283,7 @@ public:
* @return a sequence of properties.
*/
const css::uno::Sequence< css::beans::Property >&
- getProperties();
+ getProperties() const;
/**
* This method returns the environment to use for interactions, progress
@@ -292,7 +292,7 @@ public:
* @return an environment or an empty reference.
*/
const css::uno::Reference< css::ucb::XCommandEnvironment >&
- getEnvironment();
+ getEnvironment() const;
};
diff --git a/ucbhelper/source/client/content.cxx b/ucbhelper/source/client/content.cxx
index cf80b313ff2d..8b5591dfcd58 100644
--- a/ucbhelper/source/client/content.cxx
+++ b/ucbhelper/source/client/content.cxx
@@ -174,7 +174,7 @@ public:
const OUString& getURL() const;
Reference< XContent > getContent();
Reference< XCommandProcessor > getCommandProcessor();
- Reference< XComponentContext > const & getComponentContext()
+ Reference< XComponentContext > const & getComponentContext() const
{ assert(m_xCtx.is()); return m_xCtx; }
Any executeCommand( const Command& rCommand );
@@ -944,7 +944,7 @@ bool Content::transferContent( const Content& rSourceContent,
bool bMajorVersion,
const OUString & rVersionComment,
OUString* pResultURL,
- const OUString & rDocumentId )
+ const OUString & rDocumentId ) const
{
Reference< XUniversalContentBroker > pBroker(
UniversalContentBroker::create( m_xImpl->getComponentContext() ) );
diff --git a/ucbhelper/source/provider/interactionrequest.cxx b/ucbhelper/source/provider/interactionrequest.cxx
index edb7010aeaad..b95f96df5a5a 100644
--- a/ucbhelper/source/provider/interactionrequest.cxx
+++ b/ucbhelper/source/provider/interactionrequest.cxx
@@ -784,7 +784,7 @@ void SAL_CALL InteractionAuthFallback::setCode( const OUString& code )
m_aCode = code;
}
-const OUString& SAL_CALL InteractionAuthFallback::getCode( )
+const OUString& SAL_CALL InteractionAuthFallback::getCode() const
{
return m_aCode;
}
diff --git a/ucbhelper/source/provider/resultset.cxx b/ucbhelper/source/provider/resultset.cxx
index 522748442c02..780a2dc0fcdb 100644
--- a/ucbhelper/source/provider/resultset.cxx
+++ b/ucbhelper/source/provider/resultset.cxx
@@ -88,7 +88,7 @@ class PropertySetInfo :
private:
bool queryProperty(
- const OUString& aName, beans::Property& rProp );
+ const OUString& aName, beans::Property& rProp ) const;
public:
PropertySetInfo(
@@ -1362,7 +1362,7 @@ void SAL_CALL ResultSet::removeVetoableChangeListener(
// Non-interface methods.
-void ResultSet::propertyChanged( const beans::PropertyChangeEvent& rEvt )
+void ResultSet::propertyChanged( const beans::PropertyChangeEvent& rEvt ) const
{
if ( !m_pImpl->m_pPropertyChangeListeners )
return;
@@ -1427,14 +1427,14 @@ void ResultSet::rowCountFinal()
}
-const uno::Sequence< beans::Property >& ResultSet::getProperties()
+const uno::Sequence< beans::Property >& ResultSet::getProperties() const
{
return m_pImpl->m_aProperties;
}
const uno::Reference< css::ucb::XCommandEnvironment >&
-ResultSet::getEnvironment()
+ResultSet::getEnvironment() const
{
return m_pImpl->m_xEnv;
}
@@ -1537,7 +1537,7 @@ sal_Bool SAL_CALL PropertySetInfo::hasPropertyByName(
bool PropertySetInfo::queryProperty(
- const OUString& aName, beans::Property& rProp )
+ const OUString& aName, beans::Property& rProp ) const
{
sal_Int32 nCount = m_pProps->getLength();
const beans::Property* pProps = m_pProps->getConstArray();
commit 80a30219c4c553ff22979b73dd97a8869d87e488
Author: Noel Grandin <noel.grandin at collabora.co.uk>
Date: Tue Oct 24 12:22:20 2017 +0200
new loplugin:dodgyswitch
and fix bug in ScriptDocument::getTitle
which has been there since
commit e304ba66f4aba5cc55612508b5738a1ed26a7904
Date: Thu Mar 15 14:59:30 2007 +0000
INTEGRATION: CWS basmgr02 (1.1.2); FILE ADDED
plugin is off by default since it uses expensive parentStmt() calls
Change-Id: Id0f16baec48e0381e0083594d7e59b58b023da2f
Reviewed-on: https://gerrit.libreoffice.org/43750
Tested-by: Jenkins <ci at libreoffice.org>
Reviewed-by: Noel Grandin <noel.grandin at collabora.co.uk>
diff --git a/basctl/source/basicide/scriptdocument.cxx b/basctl/source/basicide/scriptdocument.cxx
index 08522afcc985..733cebdbb679 100644
--- a/basctl/source/basicide/scriptdocument.cxx
+++ b/basctl/source/basicide/scriptdocument.cxx
@@ -1475,6 +1475,7 @@ namespace basctl
case LibraryType::All: aTitle = IDEResId(RID_STR_USERMACROSDIALOGS); break;
default:
break;
+ }
}
break;
case LIBRARY_LOCATION_SHARE:
@@ -1494,7 +1495,6 @@ namespace basctl
break;
default:
break;
- }
}
return aTitle;
diff --git a/compilerplugins/clang/dodgyswitch.cxx b/compilerplugins/clang/dodgyswitch.cxx
new file mode 100644
index 000000000000..b731e30419f9
--- /dev/null
+++ b/compilerplugins/clang/dodgyswitch.cxx
@@ -0,0 +1,77 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * 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/.
+ */
+
+#include <cassert>
+#include <string>
+#include <iostream>
+#include <fstream>
+#include <set>
+#include "plugin.hxx"
+
+namespace {
+
+class DodgySwitch:
+ public RecursiveASTVisitor<DodgySwitch>, public loplugin::Plugin
+{
+public:
+ explicit DodgySwitch(InstantiationData const & data): Plugin(data) {}
+
+ virtual void run() override
+ {
+ TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
+ }
+
+ bool VisitDefaultStmt(DefaultStmt const * );
+ bool VisitCaseStmt(CaseStmt const * );
+private:
+ bool IsParentSwitch(Stmt const * );
+};
+
+bool DodgySwitch::VisitDefaultStmt(DefaultStmt const * defaultStmt)
+{
+ if (ignoreLocation(defaultStmt))
+ return true;
+ if (!IsParentSwitch(defaultStmt))
+ report(
+ DiagnosticsEngine::Warning, "default statement not directly under switch",
+ defaultStmt->getLocStart())
+ << defaultStmt->getSourceRange();
+ return true;
+}
+
+bool DodgySwitch::VisitCaseStmt(CaseStmt const * caseStmt)
+{
+ if (ignoreLocation(caseStmt))
+ return true;
+ if (!IsParentSwitch(caseStmt))
+ {
+ //parentStmt(parentStmt(caseStmt))->dump();
+ report(
+ DiagnosticsEngine::Warning, "case statement not directly under switch",
+ caseStmt->getLocStart())
+ << caseStmt->getSourceRange();
+ }
+ return true;
+}
+
+bool DodgySwitch::IsParentSwitch(Stmt const * stmt)
+{
+ auto parent = parentStmt(stmt);
+ if (isa<CaseStmt>(parent) || isa<DefaultStmt>(parent)) // daisy chain
+ return true;
+ auto compoundStmt = dyn_cast<CompoundStmt>(parent);
+ if (!compoundStmt)
+ return false;
+ return isa<SwitchStmt>(parentStmt(compoundStmt));
+}
+
+loplugin::Plugin::Registration< DodgySwitch > X("dodgyswitch", false);
+
+}
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/compilerplugins/clang/test/dodgyswitch.cxx b/compilerplugins/clang/test/dodgyswitch.cxx
new file mode 100644
index 000000000000..3a61ed388c2b
--- /dev/null
+++ b/compilerplugins/clang/test/dodgyswitch.cxx
@@ -0,0 +1,30 @@
+/* -*- 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/.
+ */
+
+#include <sal/types.h>
+
+int foo();
+
+int main() {
+ switch (foo())
+ {
+ case 1: { break; }
+ case 2: {
+ SAL_FALLTHROUGH;
+ {
+ case 3: // expected-error {{case statement not directly under switch [loplugin:dodgyswitch]}}
+ break;
+ }
+ default: // expected-error {{default statement not directly under switch [loplugin:dodgyswitch]}}
+ break;
+ }
+ }
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
diff --git a/solenv/CompilerTest_compilerplugins_clang.mk b/solenv/CompilerTest_compilerplugins_clang.mk
index 97a870d9c296..4d9772fc4665 100644
--- a/solenv/CompilerTest_compilerplugins_clang.mk
+++ b/solenv/CompilerTest_compilerplugins_clang.mk
@@ -18,6 +18,7 @@ $(eval $(call gb_CompilerTest_add_exception_objects,compilerplugins_clang, \
$(if $(filter-out INTEL,$(CPU)),compilerplugins/clang/test/convertuintptr) \
compilerplugins/clang/test/cppunitassertequals \
compilerplugins/clang/test/datamembershadow \
+ compilerplugins/clang/test/dodgyswitch \
compilerplugins/clang/test/droplong \
compilerplugins/clang/test/externvar \
compilerplugins/clang/test/expressionalwayszero \
More information about the Libreoffice-commits
mailing list