diff options
author | Noel Grandin <noel.grandin@collabora.co.uk> | 2019-07-16 15:21:10 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2019-07-17 10:42:48 +0200 |
commit | 5f4ae86cfe05f6e4d42c9b106e2f6b0e348e890d (patch) | |
tree | 39c1723981dbacc71c4572521559a50c394a8f75 | |
parent | 8e3f4317823aa83e0ad9e7dc008a1550ef3c9798 (diff) |
make passparamsbyref a shared plugin
Change-Id: I4119122f1bca77ee1424abdd90b0a0956b991704
Reviewed-on: https://gerrit.libreoffice.org/75743
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
-rw-r--r-- | compilerplugins/clang/passparamsbyref.cxx | 60 | ||||
-rw-r--r-- | compilerplugins/clang/sharedvisitor/sharedvisitor.cxx | 54 |
2 files changed, 99 insertions, 15 deletions
diff --git a/compilerplugins/clang/passparamsbyref.cxx b/compilerplugins/clang/passparamsbyref.cxx index 3c083db569a7..af28388c4e79 100644 --- a/compilerplugins/clang/passparamsbyref.cxx +++ b/compilerplugins/clang/passparamsbyref.cxx @@ -6,6 +6,7 @@ * 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/. */ +#ifndef LO_CLANG_SHARED_PLUGINS #include <string> #include <unordered_set> @@ -42,7 +43,10 @@ public: // on the other hand, use a hack of ignoring just the DeclRefExprs nested in // LValueToRValue ImplicitCastExprs when determining whether a param is // bound to a reference: + bool PreTraverseFunctionDecl(FunctionDecl *); + void PostTraverseFunctionDecl(FunctionDecl *); bool TraverseFunctionDecl(FunctionDecl *); + bool PreTraverseImplicitCastExpr(ImplicitCastExpr *); bool TraverseImplicitCastExpr(ImplicitCastExpr *); bool VisitBinAssign(BinaryOperator const *); @@ -55,28 +59,32 @@ private: std::unordered_set<ParmVarDecl const *> mParamExclusions; }; -bool PassParamsByRef::TraverseFunctionDecl(FunctionDecl* functionDecl) +bool PassParamsByRef::PreTraverseFunctionDecl(FunctionDecl* functionDecl) { if (ignoreLocation(functionDecl)) - return true; + return false; if (functionDecl->isDeleted() || functionDecl->isFunctionTemplateSpecialization()) { - return true; + return false; } // only consider base declarations, not overridden ones, or we warn on methods that // are overriding stuff from external libraries const CXXMethodDecl * methodDecl = dyn_cast<CXXMethodDecl>(functionDecl); if (methodDecl && methodDecl->size_overridden_methods() > 0) - return true; + return false; // Only warn on the definition of the function: if (!functionDecl->doesThisDeclarationHaveABody()) - return true; + return false; mbInsideFunctionDecl = true; mParamExclusions.clear(); - bool ret = RecursiveASTVisitor::TraverseFunctionDecl(functionDecl); + return true; +} + +void PassParamsByRef::PostTraverseFunctionDecl(FunctionDecl* functionDecl) +{ mbInsideFunctionDecl = false; auto cxxConstructorDecl = dyn_cast<CXXConstructorDecl>(functionDecl); @@ -124,17 +132,37 @@ bool PassParamsByRef::TraverseFunctionDecl(FunctionDecl* functionDecl) << can->getSourceRange(); } } +} + +bool PassParamsByRef::TraverseFunctionDecl(FunctionDecl* functionDecl) +{ + bool ret = true; + if (PreTraverseFunctionDecl(functionDecl)) + { + ret = RecursiveASTVisitor::TraverseFunctionDecl(functionDecl); + PostTraverseFunctionDecl(functionDecl); + } return ret; } -bool PassParamsByRef::TraverseImplicitCastExpr(ImplicitCastExpr * expr) { +bool PassParamsByRef::PreTraverseImplicitCastExpr(ImplicitCastExpr * expr) +{ if (ignoreLocation(expr)) - return true; - return - (expr->getCastKind() == CK_LValueToRValue - && (dyn_cast<DeclRefExpr>(expr->getSubExpr()->IgnoreParenImpCasts()) - != nullptr)) - || RecursiveASTVisitor::TraverseImplicitCastExpr(expr); + return false; + if (expr->getCastKind() == CK_LValueToRValue + && isa<DeclRefExpr>(expr->getSubExpr()->IgnoreParenImpCasts())) + return false; + return true; +} + +bool PassParamsByRef::TraverseImplicitCastExpr(ImplicitCastExpr * expr) +{ + bool ret = true; + if (PreTraverseImplicitCastExpr(expr)) + { + ret = RecursiveASTVisitor::TraverseImplicitCastExpr(expr); + } + return ret; } bool PassParamsByRef::VisitBinAssign(const BinaryOperator * binaryOperator) @@ -193,8 +221,10 @@ bool PassParamsByRef::isFat(QualType type) { && compiler.getASTContext().getTypeSizeInChars(t2).getQuantity() > 64; } -loplugin::Plugin::Registration< PassParamsByRef > X("passparamsbyref"); +loplugin::Plugin::Registration< PassParamsByRef > passparamsbyref("passparamsbyref"); -} +} // namespace + +#endif // LO_CLANG_SHARED_PLUGINS /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/sharedvisitor/sharedvisitor.cxx b/compilerplugins/clang/sharedvisitor/sharedvisitor.cxx index f3fe66c98fa2..f2b6906541fe 100644 --- a/compilerplugins/clang/sharedvisitor/sharedvisitor.cxx +++ b/compilerplugins/clang/sharedvisitor/sharedvisitor.cxx @@ -26,6 +26,7 @@ #include "../loopvartoosmall.cxx" #include "../overrideparam.cxx" #include "../overridevirtual.cxx" +#include "../passparamsbyref.cxx" #include "../pointerbool.cxx" #include "../privatebase.cxx" #include "../rangedforcopy.cxx" @@ -84,6 +85,7 @@ public: , loopVarTooSmall( nullptr ) , overrideParam( nullptr ) , overrideVirtual( nullptr ) + , passParamsByRef( nullptr ) , pointerBool( nullptr ) , privateBase( nullptr ) , rangedForCopy( nullptr ) @@ -144,6 +146,8 @@ public: overrideParam = nullptr; if( overrideVirtual && !overrideVirtual->preRun()) overrideVirtual = nullptr; + if( passParamsByRef && !passParamsByRef->preRun()) + passParamsByRef = nullptr; if( pointerBool && !pointerBool->preRun()) pointerBool = nullptr; if( privateBase && !privateBase->preRun()) @@ -232,6 +236,8 @@ public: overrideParam->postRun(); if( overrideVirtual ) overrideVirtual->postRun(); + if( passParamsByRef ) + passParamsByRef->postRun(); if( pointerBool ) pointerBool->postRun(); if( privateBase ) @@ -326,6 +332,8 @@ public: overrideParam = static_cast< OverrideParam* >( plugin ); else if( strcmp( name, "overridevirtual" ) == 0 ) overrideVirtual = static_cast< OverrideVirtual* >( plugin ); + else if( strcmp( name, "passparamsbyref" ) == 0 ) + passParamsByRef = static_cast< PassParamsByRef* >( plugin ); else if( strcmp( name, "pointerbool" ) == 0 ) pointerBool = static_cast< PointerBool* >( plugin ); else if( strcmp( name, "privatebase" ) == 0 ) @@ -384,6 +392,17 @@ public: return false; return true; } + bool VisitBinAssign(const class clang::BinaryOperator * arg) + { + if( ignoreLocation( arg )) + return true; + if( passParamsByRef != nullptr ) + { + if( !passParamsByRef->VisitBinAssign( arg )) + passParamsByRef = nullptr; + } + return anyPluginActive(); + } bool VisitBinEQ(const class clang::BinaryOperator * arg) { if( ignoreLocation( arg )) @@ -594,6 +613,11 @@ public: { if( ignoreLocation( arg )) return true; + if( passParamsByRef != nullptr ) + { + if( !passParamsByRef->VisitCXXOperatorCallExpr( arg )) + passParamsByRef = nullptr; + } if( unnecessaryParen != nullptr ) { if( !unnecessaryParen->VisitCXXOperatorCallExpr( arg )) @@ -1102,6 +1126,34 @@ public: unrefFun = saveUnrefFun; return ret; } + bool TraverseFunctionDecl(class clang::FunctionDecl * arg) + { + PassParamsByRef* savePassParamsByRef = passParamsByRef; + if( passParamsByRef != nullptr ) + { + if( !passParamsByRef->PreTraverseFunctionDecl( arg )) + passParamsByRef = nullptr; + } + bool ret = RecursiveASTVisitor::TraverseFunctionDecl( arg ); + if( passParamsByRef != nullptr ) + { + passParamsByRef->PostTraverseFunctionDecl( arg ); + } + passParamsByRef = savePassParamsByRef; + return ret; + } + bool TraverseImplicitCastExpr(class clang::ImplicitCastExpr * arg) + { + PassParamsByRef* savePassParamsByRef = passParamsByRef; + if( passParamsByRef != nullptr ) + { + if( !passParamsByRef->PreTraverseImplicitCastExpr( arg )) + passParamsByRef = nullptr; + } + bool ret = RecursiveASTVisitor::TraverseImplicitCastExpr( arg ); + passParamsByRef = savePassParamsByRef; + return ret; + } bool TraverseInitListExpr(class clang::InitListExpr * arg) { SimplifyConstruct* saveSimplifyConstruct = simplifyConstruct; @@ -1144,6 +1196,7 @@ private: || loopVarTooSmall != nullptr || overrideParam != nullptr || overrideVirtual != nullptr + || passParamsByRef != nullptr || pointerBool != nullptr || privateBase != nullptr || rangedForCopy != nullptr @@ -1187,6 +1240,7 @@ private: LoopVarTooSmall* loopVarTooSmall; OverrideParam* overrideParam; OverrideVirtual* overrideVirtual; + PassParamsByRef* passParamsByRef; PointerBool* pointerBool; PrivateBase* privateBase; RangedForCopy* rangedForCopy; |