From 940b4d1848e8c70ab7642901a68594e8016caffc Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 27 Apr 2024 18:51:28 +0200 Subject: Adding upstream version 1:7.0.4. Signed-off-by: Daniel Baumann --- compilerplugins/clang/store/README | 3 + compilerplugins/clang/store/badvectorinit.cxx | 214 +++++++++ compilerplugins/clang/store/bodynotinblock.cxx | 147 ++++++ compilerplugins/clang/store/bodynotinblock.hxx | 38 ++ compilerplugins/clang/store/cascadingassignop.cxx | 94 ++++ compilerplugins/clang/store/cascadingassignop.hxx | 38 ++ compilerplugins/clang/store/cascadingcondop.cxx | 86 ++++ compilerplugins/clang/store/cascadingcondop.hxx | 38 ++ .../clang/store/changefunctioncalls.cxx | 91 ++++ compilerplugins/clang/store/constantfunction.cxx | 506 +++++++++++++++++++++ compilerplugins/clang/store/deadclass.cxx | 69 +++ compilerplugins/clang/store/defaultparams.cxx | 128 ++++++ compilerplugins/clang/store/deletedspecial.cxx | 153 +++++++ compilerplugins/clang/store/derivedclass.cxx | 70 +++ compilerplugins/clang/store/findoncontainer.cxx | 77 ++++ compilerplugins/clang/store/fpcomparison.cxx | 386 ++++++++++++++++ compilerplugins/clang/store/lclstaticfix.cxx | 54 +++ compilerplugins/clang/store/lclstaticfix.hxx | 33 ++ compilerplugins/clang/store/manualrefcount.cxx | 323 +++++++++++++ .../clang/store/paintmethodconversion.cxx | 94 ++++ .../clang/store/postfixincrementfix.cxx | 132 ++++++ .../clang/store/postfixincrementfix.hxx | 38 ++ compilerplugins/clang/store/refassign.cxx | 152 +++++++ .../clang/store/removeforwardstringdecl.cxx | 78 ++++ .../clang/store/removeforwardstringdecl.hxx | 35 ++ compilerplugins/clang/store/removevirtuals.cxx | 150 ++++++ compilerplugins/clang/store/returnbyref.cxx | 138 ++++++ compilerplugins/clang/store/returnunique.cxx | 83 ++++ compilerplugins/clang/store/revisibility.cxx | 89 ++++ compilerplugins/clang/store/rtlconstasciimacro.cxx | 144 ++++++ compilerplugins/clang/store/sfxitemsetrewrite.cxx | 419 +++++++++++++++++ compilerplugins/clang/store/stdexception.cxx | 188 ++++++++ compilerplugins/clang/store/stylepolice.cxx | 196 ++++++++ .../clang/store/svstreamoutputoperators.cxx | 223 +++++++++ compilerplugins/clang/store/test/deadclass.cxx | 14 + compilerplugins/clang/store/tutorial/tutorial1.cxx | 68 +++ compilerplugins/clang/store/tutorial/tutorial1.hxx | 38 ++ .../clang/store/tutorial/tutorial1_example.cxx | 21 + compilerplugins/clang/store/tutorial/tutorial2.cxx | 95 ++++ compilerplugins/clang/store/tutorial/tutorial2.hxx | 38 ++ .../clang/store/tutorial/tutorial2_example.cxx | 18 + compilerplugins/clang/store/tutorial/tutorial3.cxx | 77 ++++ compilerplugins/clang/store/tutorial/tutorial3.hxx | 40 ++ compilerplugins/clang/store/unusedcode.cxx | 77 ++++ compilerplugins/clang/store/valueof.cxx | 150 ++++++ 45 files changed, 5343 insertions(+) create mode 100644 compilerplugins/clang/store/README create mode 100644 compilerplugins/clang/store/badvectorinit.cxx create mode 100644 compilerplugins/clang/store/bodynotinblock.cxx create mode 100644 compilerplugins/clang/store/bodynotinblock.hxx create mode 100644 compilerplugins/clang/store/cascadingassignop.cxx create mode 100644 compilerplugins/clang/store/cascadingassignop.hxx create mode 100644 compilerplugins/clang/store/cascadingcondop.cxx create mode 100644 compilerplugins/clang/store/cascadingcondop.hxx create mode 100644 compilerplugins/clang/store/changefunctioncalls.cxx create mode 100644 compilerplugins/clang/store/constantfunction.cxx create mode 100644 compilerplugins/clang/store/deadclass.cxx create mode 100644 compilerplugins/clang/store/defaultparams.cxx create mode 100644 compilerplugins/clang/store/deletedspecial.cxx create mode 100644 compilerplugins/clang/store/derivedclass.cxx create mode 100644 compilerplugins/clang/store/findoncontainer.cxx create mode 100644 compilerplugins/clang/store/fpcomparison.cxx create mode 100644 compilerplugins/clang/store/lclstaticfix.cxx create mode 100644 compilerplugins/clang/store/lclstaticfix.hxx create mode 100644 compilerplugins/clang/store/manualrefcount.cxx create mode 100644 compilerplugins/clang/store/paintmethodconversion.cxx create mode 100644 compilerplugins/clang/store/postfixincrementfix.cxx create mode 100644 compilerplugins/clang/store/postfixincrementfix.hxx create mode 100644 compilerplugins/clang/store/refassign.cxx create mode 100644 compilerplugins/clang/store/removeforwardstringdecl.cxx create mode 100644 compilerplugins/clang/store/removeforwardstringdecl.hxx create mode 100644 compilerplugins/clang/store/removevirtuals.cxx create mode 100644 compilerplugins/clang/store/returnbyref.cxx create mode 100644 compilerplugins/clang/store/returnunique.cxx create mode 100644 compilerplugins/clang/store/revisibility.cxx create mode 100644 compilerplugins/clang/store/rtlconstasciimacro.cxx create mode 100644 compilerplugins/clang/store/sfxitemsetrewrite.cxx create mode 100644 compilerplugins/clang/store/stdexception.cxx create mode 100644 compilerplugins/clang/store/stylepolice.cxx create mode 100644 compilerplugins/clang/store/svstreamoutputoperators.cxx create mode 100644 compilerplugins/clang/store/test/deadclass.cxx create mode 100644 compilerplugins/clang/store/tutorial/tutorial1.cxx create mode 100644 compilerplugins/clang/store/tutorial/tutorial1.hxx create mode 100644 compilerplugins/clang/store/tutorial/tutorial1_example.cxx create mode 100644 compilerplugins/clang/store/tutorial/tutorial2.cxx create mode 100644 compilerplugins/clang/store/tutorial/tutorial2.hxx create mode 100644 compilerplugins/clang/store/tutorial/tutorial2_example.cxx create mode 100644 compilerplugins/clang/store/tutorial/tutorial3.cxx create mode 100644 compilerplugins/clang/store/tutorial/tutorial3.hxx create mode 100644 compilerplugins/clang/store/unusedcode.cxx create mode 100644 compilerplugins/clang/store/valueof.cxx (limited to 'compilerplugins/clang/store') diff --git a/compilerplugins/clang/store/README b/compilerplugins/clang/store/README new file mode 100644 index 000000000..b56254412 --- /dev/null +++ b/compilerplugins/clang/store/README @@ -0,0 +1,3 @@ +This plugin actions are not used. They are still kept in case they would be useful again +(they can be activated again by simply moving them back in the clang/ source directory) +or simply as a reference when writing new plugins. diff --git a/compilerplugins/clang/store/badvectorinit.cxx b/compilerplugins/clang/store/badvectorinit.cxx new file mode 100644 index 000000000..4ab086d9e --- /dev/null +++ b/compilerplugins/clang/store/badvectorinit.cxx @@ -0,0 +1,214 @@ +/* -*- 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 +#include +#include +#include +#include +#include "plugin.hxx" + +/** + +Comments from Bjoern Michaelsen: + +Killing the 1-argument vector fill constructor: + + std::vector< basebmp::Color > aDevPal(2); + +in general is probably a Good Thing(tm). It can just be too misleading. +Requiring at least the explicit two-value fill constructor for the rare cases where +someone wants a filled vector isn't too much to ask and less prone to +misunderstandings: + + std::vector< basebmp::Color > aDevPal(2, basebmp::Color(0,0,0)); + +Although that _still_ might be misleading[1], so turning all those into the +somewhat longer, but more explicit: + + std::vector< basebmp::Color > aDevPal; + aDevPal.reserve(2); + aDevPal.push_back(...); + ... + +> So I suppose the check would be for a size reservation on a vector +> followed by push_back - rather than some array indexing - does that make +> sense ? or did I go crazy ;-) + +Yes, in general you want neither of the above forms. Preferably instead of +e.g.: + + std::vector< basebmp::Color > aDevPal(2); + aDevPal[0] = basebmp::Color( 0, 0, 0 ); + aDevPal[1] = basebmp::Color( 0xff, 0xff, 0xff ); + +you would -- if possible -- simply: + + std::vector< basebmp::Color > aDevPal{ + basebmp::Color( 0, 0, 0 ), + basebmp::Color( 0xff, 0xff, 0xff ) }; + +and only for complex cases, where you do not have the elements statically +available, something like: + + std::vector< foo > vFoos; + vFoos.reserve(vInput.size()); + std::transform(std::back_inserter(vFoos), + vInput.begin(), + vInput.end(), + [] (decltype(vInput)::value_type aInputValue) { return do_something(aInputValue); }); + +see also: +https://skyfromme.wordpress.com/2015/03/02/50-ways-to-fill-your-vector/ +https://skyfromme.wordpress.com/2015/03/12/following-the-white-rabbit/ +(tl;dr: Use initializer lists to fill vectors when possible). + +Best, + +Bjoern + +[1] Well, except that: + std::vector(3, 0) + is doing something different from: + std::vector{3, 0} + just to make things more interesting. But hey, that's C++ for you. + But that wart exists for the 1-arg ctor too -- yet another reason to kill that. +*/ + +namespace { + + +class BadVectorInit: + public loplugin::FilteringPlugin +{ +public: + explicit BadVectorInit(InstantiationData const & data): FilteringPlugin(data) {} + + virtual void run() override + { + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + } + + bool VisitCXXConstructExpr(const CXXConstructExpr* ); + bool TraverseFunctionDecl(FunctionDecl* ); + bool VisitCXXMemberCallExpr(const CXXMemberCallExpr* ); +private: + StringRef getFilename(SourceLocation loc); + std::set suspectSet; +}; + +bool BadVectorInit::TraverseFunctionDecl(FunctionDecl* decl) +{ + bool ret = RecursiveASTVisitor::TraverseFunctionDecl(decl); + suspectSet.clear(); + return ret; +} + +StringRef BadVectorInit::getFilename(SourceLocation loc) +{ + SourceLocation spellingLocation = compiler.getSourceManager().getSpellingLoc(loc); + StringRef name { getFilenameOfLocation(spellingLocation) }; + return name; +} + +bool BadVectorInit::VisitCXXMemberCallExpr(const CXXMemberCallExpr* expr) +{ + if (suspectSet.empty() || ignoreLocation( expr )) + return true; + + // need to exclude some false positives + StringRef aFileName = getFilename(expr->getLocStart()); + if (aFileName == SRCDIR "/framework/source/services/autorecovery.cxx" + || aFileName == SRCDIR "/vcl/source/opengl/OpenGLHelper.cxx" + || aFileName == SRCDIR "/vcl/source/gdi/gdimtf.cxx" + ) + { + return true; + } + + const FunctionDecl* functionDecl = expr->getDirectCallee(); + if (!functionDecl) + return true; + if (functionDecl->getNameAsString().find("push_back") == string::npos) + return true; + const DeclRefExpr* declExpr = dyn_cast(expr->getImplicitObjectArgument()); + if (!declExpr) + return true; + const VarDecl* varDecl = dyn_cast(declExpr->getDecl()); + if (!varDecl) + return true; + varDecl = varDecl->getCanonicalDecl(); + if (suspectSet.find(varDecl) == suspectSet.end()) + return true; + report( + DiagnosticsEngine::Warning, + "calling push_back after using sized constructor", + expr->getLocStart()) + << expr->getSourceRange(); + report( + DiagnosticsEngine::Note, + "on this var", + varDecl->getLocStart()) + << varDecl->getSourceRange(); + + return true; +} + +bool BadVectorInit::VisitCXXConstructExpr(const CXXConstructExpr* expr) +{ + if (ignoreLocation( expr )) + return true; + + const CXXConstructorDecl *consDecl = expr->getConstructor(); + consDecl = consDecl->getCanonicalDecl(); + + // The default constructor can potentially have a parameter, e.g. + // in glibcxx-debug the default constructor is: + // explicit vector(const _Allocator& __a = _Allocator()) + if (consDecl->param_size() == 0 || consDecl->isDefaultConstructor()) + return true; + + std::string aParentName = consDecl->getParent()->getQualifiedNameAsString(); + if (aParentName.find("vector") == string::npos && aParentName.find("deque") == string::npos) + return true; + + // ignore the copy/move constructors, and those taking an initializer_list + // etc.: + if (consDecl->isCopyConstructor() || consDecl->isMoveConstructor()) + return true; + const ParmVarDecl* pParam = consDecl->getParamDecl(0); + std::string aParam1 = pParam->getOriginalType().getAsString(); + if (aParam1.find("initializer_list") != string::npos + || aParam1.find("iterator") != string::npos) + return true; + + // found a call to the 1-arg vector constructor, now look for the VarDecl it belongs to + + const Stmt* parent = expr; + do { + parent = parentStmt(parent); + if (!parent) break; + if (isa(parent)) + { + const DeclStmt* declStmt = dyn_cast(parent); + const Decl* decl = declStmt->getSingleDecl(); + if (decl && isa(decl)) + suspectSet.insert(dyn_cast(decl)->getCanonicalDecl()); + break; + } + } while (true); + + return true; +} + +loplugin::Plugin::Registration< BadVectorInit > X("badvectorinit", true); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/bodynotinblock.cxx b/compilerplugins/clang/store/bodynotinblock.cxx new file mode 100644 index 000000000..ca4f90499 --- /dev/null +++ b/compilerplugins/clang/store/bodynotinblock.cxx @@ -0,0 +1,147 @@ +/* -*- 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 "bodynotinblock.hxx" + +namespace loplugin +{ + +/* +This is a compile check. + +Check for two statements that are both indented to look like a body of if/while/for +but are not inside a compound statement and thus the second one is unrelated. + +For example: + + if( a != 0 ) + b = 2; + c = 3; + +Here either both statements should be inside {} or the second statement in indented wrong. +*/ + +BodyNotInBlock::BodyNotInBlock( const InstantiationData& data ) + : Plugin( data ) + { + } + +void BodyNotInBlock::run() + { + TraverseDecl( compiler.getASTContext().getTranslationUnitDecl()); + } + +bool BodyNotInBlock::VisitIfStmt( const IfStmt* stmt ) + { + if( ignoreLocation( stmt )) + return true; + checkBody( stmt->getThen(), stmt->getIfLoc(), 0, stmt->getElse() != NULL ); + checkBody( stmt->getElse(), stmt->getElseLoc(), 0 ); + return true; + } + +bool BodyNotInBlock::VisitWhileStmt( const WhileStmt* stmt ) + { + if( ignoreLocation( stmt )) + return true; + checkBody( stmt->getBody(), stmt->getWhileLoc(), 1 ); + return true; + } + +bool BodyNotInBlock::VisitForStmt( const ForStmt* stmt ) + { + if( ignoreLocation( stmt )) + return true; + checkBody( stmt->getBody(), stmt->getForLoc(), 2 ); + return true; + } + +bool BodyNotInBlock::VisitCXXForRangeStmt( const CXXForRangeStmt* stmt ) + { + if( ignoreLocation( stmt )) + return true; + checkBody( stmt->getBody(), stmt->getForLoc(), 2 ); + return true; + } + +void BodyNotInBlock::checkBody( const Stmt* body, SourceLocation stmtLocation, int stmtType, bool dontGoUp ) + { + if( body == NULL ) + return; + // TODO: If the if/else/while/for comes from a macro expansion, ignore it completely for + // now. The code below could assume everything is in the same place (and thus also column) + // and give a false warning. Moreover some macros are rather loosely written and would + // result in poor formatting. To be evaluated later, maybe this could be handled + // including macro expansion. + if( stmtLocation.isMacroID()) + return; + if( dyn_cast< CompoundStmt >( body )) + return; // if body is a compound statement, then it is in {} + const Stmt* previousParent = parentStmt( body ); // Here the statement itself. + // Find the next statement (in source position) after 'body'. + for(;;) + { + const Stmt* parent = parentStmt( previousParent ); + if( parent == NULL ) + break; + for( ConstStmtIterator it = parent->child_begin(); + it != parent->child_end(); + ) + { + if( *it == previousParent ) // found grand(grand...)parent + { + // get next statement after our (grand...)parent + ++it; + while( it != parent->child_end() && *it == NULL ) + ++it; // skip empty ones (missing 'else' bodies for example) + if( it != parent->child_end()) + { + bool invalid1, invalid2; + unsigned bodyColumn = compiler.getSourceManager() + .getPresumedColumnNumber( body->getLocStart(), &invalid1 ); + unsigned nextStatementColumn = compiler.getSourceManager() + .getPresumedColumnNumber( (*it)->getLocStart(), &invalid2 ); + if( invalid1 || invalid2 ) + return; + if( bodyColumn == nextStatementColumn ) + { + report( DiagnosticsEngine::Warning, + "statement aligned as second statement in %select{if|while|for}0 body but not in a statement block", + (*it)->getLocStart()) << stmtType; + report( DiagnosticsEngine::Note, + "%select{if|while|for}0 body statement is here", + body->getLocStart()) << stmtType; + } + return; + } + // else we need to go higher to find the next statement + } + else + ++it; + } + // If going up would mean leaving a {} block, stop, because the } should + // make it visible the two statements are not in the same body. + if( dyn_cast< CompoundStmt >( parent )) + return; + // If the body to be checked is a body of an if statement that has also + // an else part, don't go up, the else is after the body and should make + // it clear the body does not continue there. + if( dontGoUp ) + return; + previousParent = parent; + } + } + +static Plugin::Registration< BodyNotInBlock > X( "bodynotinblock" ); + +} // namespace + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/bodynotinblock.hxx b/compilerplugins/clang/store/bodynotinblock.hxx new file mode 100644 index 000000000..e5f0dc49f --- /dev/null +++ b/compilerplugins/clang/store/bodynotinblock.hxx @@ -0,0 +1,38 @@ +/* -*- 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. + * + */ + +#ifndef BODYNOTINBLOCK_H +#define BODYNOTINBLOCK_H + +#include "plugin.hxx" + +namespace loplugin +{ + +class BodyNotInBlock + : public loplugin::FilteringPlugin + { + public: + explicit BodyNotInBlock( const InstantiationData& data ); + virtual void run() override; + bool VisitIfStmt( const IfStmt* stmt ); + bool VisitWhileStmt( const WhileStmt* stmt ); + bool VisitForStmt( const ForStmt* stmt ); + bool VisitCXXForRangeStmt( const CXXForRangeStmt* stmt ); + private: + void checkBody( const Stmt* body, SourceLocation stmtLocation, int stmtType, bool dontGoUp = false ); + }; + +} // namespace + +#endif // BODYNOTINBLOCK_H + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/cascadingassignop.cxx b/compilerplugins/clang/store/cascadingassignop.cxx new file mode 100644 index 000000000..a1098ee06 --- /dev/null +++ b/compilerplugins/clang/store/cascadingassignop.cxx @@ -0,0 +1,94 @@ +/* -*- 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 "cascadingassignop.hxx" + +/* +This is a compile check. + +It checks for complex statements with conditional operators in conditional +operators, which are error prone, e.g. + Thing foo = IsBar() ? ( IsBaz() ? b1 : b2 ) : b3; + +However, it finds 556 cases in sw/source alone, thus likely needs some more +restricting, e.g. by checking for multiline conditional operator statements or +a certain length in characters (but that needs the Context/SourceManager, which +I haven't played with yet). +*/ + +// the value is rather arbitrary, but code above this number of stmts begins to +// be smelly +static const int stmtlimit = 20; + +namespace loplugin +{ + +struct WalkCounter +{ + int stmtcount; + bool cascading; + bool conditionals; +}; + +// Ctor, nothing special, pass the argument(s). +CascadingAssignOp::CascadingAssignOp( const InstantiationData& data ) + : FilteringPlugin( data ) +{ +} + +// Perform the actual action. +void CascadingAssignOp::run() +{ + // Traverse the whole AST of the translation unit (i.e. examine the whole source file). + // The Clang AST helper class will call VisitReturnStmt for every return statement. + TraverseDecl( compiler.getASTContext().getTranslationUnitDecl()); +} + +void CascadingAssignOp::Walk( const Stmt* stmt, WalkCounter& c ) +{ + for(Stmt::const_child_iterator it = stmt->child_begin(); it != stmt->child_end(); ++it) + { + ++c.stmtcount; + const BinaryOperator* binop = dyn_cast< BinaryOperator >( *it ); + if ( binop ) + { + if ( (binop->isAssignmentOp() || binop->isCompoundAssignmentOp())) + c.cascading = true; + if ( dyn_cast< AbstractConditionalOperator >( binop ) || binop->isLogicalOp()) + c.conditionals = true; + } + Walk(*it, c); + } +} + +bool CascadingAssignOp::VisitStmt( const Stmt* stmt ) +{ + const BinaryOperator* binop = dyn_cast< BinaryOperator >( stmt ); + if ( binop && (binop->isAssignmentOp() || binop->isCompoundAssignmentOp())) + { + WalkCounter c = { 0, false, false }; + Walk(binop, c); + if(c.cascading && c.conditionals && c.stmtcount >= stmtlimit) + { + std::string msg("cascading assign operator mixing in conditionals, complexity: "); + msg.append(std::to_string(c.stmtcount)); + report( DiagnosticsEngine::Warning, msg, binop->getLocStart()); + } + } + return true; +} + +// Register the plugin action with the LO plugin handling. +static Plugin::Registration< CascadingAssignOp > X( "cascadingassignop" ); + +} // namespace loplugin + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/cascadingassignop.hxx b/compilerplugins/clang/store/cascadingassignop.hxx new file mode 100644 index 000000000..26080c004 --- /dev/null +++ b/compilerplugins/clang/store/cascadingassignop.hxx @@ -0,0 +1,38 @@ +/* -*- 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. + * + */ + +#ifndef CASCADINGCONDOP_H +#define CASCADINGCONDOP_H + +#include "plugin.hxx" + +namespace loplugin +{ + +struct WalkCounter; + +// The class implementing the plugin action. +class CascadingAssignOp + // Inherits from the Clang class that will allow examining the Clang AST tree (i.e. syntax tree). + : public FilteringPlugin< CascadingAssignOp > + { + public: + CascadingAssignOp( const InstantiationData& data ); + virtual void run() override; + void Walk( const Stmt* stmt, WalkCounter& c ); + bool VisitStmt( const Stmt* stmt ); + }; + +} // namespace loplugin + +#endif // CASCADINGCONDOP_H + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/cascadingcondop.cxx b/compilerplugins/clang/store/cascadingcondop.cxx new file mode 100644 index 000000000..f2687e769 --- /dev/null +++ b/compilerplugins/clang/store/cascadingcondop.cxx @@ -0,0 +1,86 @@ +/* -*- 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 "cascadingcondop.hxx" + +/* +This is a compile check. + +It checks for complex statements with conditional operators in conditional +operators, which are error prone, e.g. + Thing foo = IsBar() ? ( IsBaz() ? b1 : b2 ) : b3; + +However, it finds 556 cases in sw/source alone, thus likely needs some more +restricting, e.g. by checking for multiline conditional operator statements or +a certain length in characters (but that needs the Context/SourceManager, which +I haven't played with yet). +*/ + +// the value is rather arbitrary, but code above this number of stmts begins to +// be smelly +static const int stmtlimit = 50; + +namespace loplugin +{ + +struct WalkCounter +{ + int stmtcount; + bool cascading; +}; + +// Ctor, nothing special, pass the argument(s). +CascadingCondOp::CascadingCondOp( const InstantiationData& data ) + : FilteringPlugin( data ) +{ +} + +// Perform the actual action. +void CascadingCondOp::run() +{ + // Traverse the whole AST of the translation unit (i.e. examine the whole source file). + // The Clang AST helper class will call VisitReturnStmt for every return statement. + TraverseDecl( compiler.getASTContext().getTranslationUnitDecl()); +} + +void CascadingCondOp::Walk( const Stmt* stmt, WalkCounter& c ) +{ + for(Stmt::const_child_iterator it = stmt->child_begin(); it != stmt->child_end(); ++it) + { + ++c.stmtcount; + if ( dyn_cast< ConditionalOperator >( *it )) + c.cascading = true; + Walk(*it, c); + } +} + +bool CascadingCondOp::VisitStmt( const Stmt* stmt ) +{ + if ( const ConditionalOperator* condop = dyn_cast< ConditionalOperator >( stmt )) + { + WalkCounter c = { 0, false }; + Walk(condop, c); + if(c.cascading && c.stmtcount >= stmtlimit) + { + std::string msg("cascading conditional operator, complexity: "); + msg.append(std::to_string(c.stmtcount)); + report( DiagnosticsEngine::Warning, msg, condop->getLocStart()); + } + } + return true; +} + +// Register the plugin action with the LO plugin handling. +static Plugin::Registration< CascadingCondOp > X( "cascadingcondop" ); + +} // namespace loplugin + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/cascadingcondop.hxx b/compilerplugins/clang/store/cascadingcondop.hxx new file mode 100644 index 000000000..f2c995064 --- /dev/null +++ b/compilerplugins/clang/store/cascadingcondop.hxx @@ -0,0 +1,38 @@ +/* -*- 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. + * + */ + +#ifndef CASCADINGCONDOP_H +#define CASCADINGCONDOP_H + +#include "plugin.hxx" + +namespace loplugin +{ + +struct WalkCounter; + +// The class implementing the plugin action. +class CascadingCondOp + // Inherits from the Clang class that will allow examining the Clang AST tree (i.e. syntax tree). + : public FilteringPlugin< CascadingCondOp > + { + public: + CascadingCondOp( const InstantiationData& data ); + virtual void run() override; + void Walk( const Stmt* stmt, WalkCounter& c ); + bool VisitStmt( const Stmt* stmt ); + }; + +} // namespace loplugin + +#endif // CASCADINGCONDOP_H + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/changefunctioncalls.cxx b/compilerplugins/clang/store/changefunctioncalls.cxx new file mode 100644 index 000000000..9f5390a21 --- /dev/null +++ b/compilerplugins/clang/store/changefunctioncalls.cxx @@ -0,0 +1,91 @@ +/* -*- 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. + * + */ + +/* +This is a rewriter. + +Changes all calls to a specific function (after it's been renamed or its +arguments have changed). + +This specific example checks for calls to function 'void bar(unsigned int)' +and adds '+ 10' to the argument (as plain text, so if the argument is a more +complex expression, operator precedence may mean the result is actually different). + +This can be easily adjusted for different modifications to a function: +- replace CallExpr with CXXOperatorCallExpr or CXXMemberCallExpr +- check different names or arguments +- change getDirectCallee() to getCallee() +- etc. +*/ + +#include "plugin.hxx" +#include "check.hxx" + +namespace loplugin +{ + +class ChangeFunctionCalls + : public loplugin::FilteringRewritePlugin< ChangeFunctionCalls > + { + public: + explicit ChangeFunctionCalls( CompilerInstance& compiler, Rewriter& rewriter ); + virtual void run() override; + bool VisitCallExpr( const CallExpr* call ); + }; + +ChangeFunctionCalls::ChangeFunctionCalls( CompilerInstance& compiler, Rewriter& rewriter ) + : FilteringRewritePlugin( compiler, rewriter ) + { + } + +void ChangeFunctionCalls::run() + { + TraverseDecl( compiler.getASTContext().getTranslationUnitDecl()); + } + +bool ChangeFunctionCalls::VisitCallExpr( const CallExpr* call ) + { + if( ignoreLocation( call )) + return true; + // Using getDirectCallee() here means that we find only calls + // that call the function directly (i.e. not using a pointer, for example). + // Use getCallee() to include also those : + // if( const FunctionDecl* func = dyn_cast_or_null< FunctionDecl >( call->getCalleeDecl())) + if( const FunctionDecl* func = call->getDirectCallee()) + { + // so first check fast details like number of arguments or the (unqualified) + // name before checking the fully qualified name. + // See FunctionDecl for all the API about the function. + if( func->getNumParams() == 1 && func->getIdentifier() != NULL + && ( func->getName() == "bar" )) + { + auto qt = loplugin::DeclCheck(func); + if( qt.Function("bar").GlobalNamespace() ) + { + // Further checks about arguments. Check mainly ParmVarDecl, VarDecl, + // ValueDecl and QualType for Clang API details. + string arg0 = func->getParamDecl( 0 )->getType().getAsString(); + if( arg0 == "unsigned int" ) + { + insertTextAfterToken( call->getArg( 0 )->getLocEnd(), " + 10" ); + report( DiagnosticsEngine::Warning, "found", call->getLocStart()); + } + } + } + } + return true; + } + +static Plugin::Registration< ChangeFunctionCalls > X( "changefunctioncalls" ); + +} // namespace + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/constantfunction.cxx b/compilerplugins/clang/store/constantfunction.cxx new file mode 100644 index 000000000..a29556723 --- /dev/null +++ b/compilerplugins/clang/store/constantfunction.cxx @@ -0,0 +1,506 @@ +/* -*- 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 "plugin.hxx" +#include + +/* + Look for member functions that merely return a compile-time constant, or they are empty, and can thus + be either removed, or converted into a constant. + + This mostly tends to happen as a side-effect of other cleanups. +*/ +namespace { + +class ConstantFunction: + public loplugin::FilteringPlugin +{ + StringRef getFilename(const FunctionDecl* functionDecl); +public: + explicit ConstantFunction(InstantiationData const & data): FilteringRewritePlugin(data) {} + + void run() override + { + // these files crash clang-3.5 somewhere in the isEvaluatable/EvaluateAsXXX stuff +/* FileID mainFileID = compiler.getSourceManager().getMainFileID(); + if (strstr(compiler.getSourceManager().getFileEntryForID(mainFileID)->getName(), "bootstrapfixture.cxx") != 0) { + return; + } + if (strstr(compiler.getSourceManager().getFileEntryForID(mainFileID)->getName(), "gtk3gtkinst.cxx") != 0) { + return; + }*/ + + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + } + + bool VisitFunctionDecl(const FunctionDecl *); +}; + +StringRef ConstantFunction::getFilename(const FunctionDecl* functionDecl) +{ + SourceLocation spellingLocation = compiler.getSourceManager().getSpellingLoc(functionDecl->getCanonicalDecl()->getNameInfo().getLoc()); + StringRef name { getFilenameOfLocation(spellingLocation) }; + return name; +} + +static bool startsWith(const std::string& rStr, const char* pSubStr) { + return rStr.compare(0, strlen(pSubStr), pSubStr) == 0; +} + +bool ConstantFunction::VisitFunctionDecl(const FunctionDecl * pFunctionDecl) { + if (ignoreLocation(pFunctionDecl)) { + return true; + } + if (!pFunctionDecl->hasBody()) { + return true; + } + if (!pFunctionDecl->isThisDeclarationADefinition()) { + return true; + } + // stuff declared extern-C is almost always used as a some kind of callback + if (pFunctionDecl->isExternC()) { + return true; + } + if (pFunctionDecl->isConstexpr()) { + return true; + } + if (pFunctionDecl->isMain()) { + return true; + } + + StringRef aFileName = getFilename(pFunctionDecl); + + // various tests in here are empty stubs under Linux + if (aFileName.startswith(SRCDIR "/sal/qa/")) { + return true; + } + // lots of empty stuff here where it looks like someone is still going to "fill in the blanks" + if (aFileName.startswith(SRCDIR "/basegfx/test/")) { + return true; + } + // bridges has some weird stuff in it... + if (aFileName.startswith(SRCDIR "/bridges/")) { + return true; + } + // dummy implementation of DDE, since it is only active on Windows + if (aFileName == SRCDIR "/svl/unx/source/svdde/ddedummy.cxx" + || aFileName == SRCDIR "/include/svl/svdde.hxx") { + return true; + } + // fancy templates at work here + if (aFileName == SRCDIR "/vcl/source/gdi/bmpfast.cxx") { + return true; + } + // bunch of stuff used as callbacks here + if (aFileName == SRCDIR "/vcl/generic/glyphs/gcach_layout.cxx") { + return true; + } + // salplug runtime-loading mechanism at work + if (aFileName == SRCDIR "/vcl/inc/salinst.hxx") { + return true; + } + // lots of callbacks here + if (aFileName == SRCDIR "/extensions/source/plugin/unx/npnapi.cxx") { + return true; + } + // vcl/unx/gtk3 re-using vcl/unx/gtk: + if (aFileName.find("/../../gtk/") != std::string::npos) { + return true; + } + // used by code generated by python + if (aFileName == SRCDIR "/writerfilter/source/ooxml/OOXMLFastContextHandler.hxx") { + return true; + } + // this test just test the include of some headers + if (aFileName == SRCDIR "/officecfg/qa/cppheader.cxx") { + return true; + } + // just ignore this for now, people furiously hacking in there + if (startsWith(aFileName, SRCDIR "/libreofficekit")) { + return true; + } + + + const CXXMethodDecl *pCXXMethodDecl = dyn_cast(pFunctionDecl); + if (pCXXMethodDecl) { + if (pCXXMethodDecl->isVirtual()) { + return true; + } + // static with inline body will be optimised at compile-time to a constant anyway + if (pCXXMethodDecl->isStatic() && (pCXXMethodDecl->hasInlineBody() || pCXXMethodDecl->isInlineSpecified())) { + return true; + } + // this catches some stuff in templates + if (pFunctionDecl->hasAttr()) { + return true; + } + } + // a free function with an inline body will be optimised at compile-time to a constant anyway + if (!pCXXMethodDecl && pFunctionDecl->isInlineSpecified()) { + return true; + } + if (isa(pFunctionDecl) || isa(pFunctionDecl) || isa(pFunctionDecl)) { + return true; + } + if (isInUnoIncludeFile(pFunctionDecl)) { + return true; + } + + switch (pFunctionDecl->getOverloadedOperator()) { + case OO_Delete: + case OO_EqualEqual: + case OO_Call: + return true; + default: + break; + } + + std::string aFunctionName = pFunctionDecl->getQualifiedNameAsString(); + + // something to do with dynamic loading in sal/textenc/textenc.cxx + if (aFunctionName == "thisModule") { + return true; + } + // an empty stub under certain conditions, sal/osl/unx/thread.cxx + if (aFunctionName == "osl_thread_priority_init_Impl") { + return true; + } + // a pointer to this function is taken and passed to an underlying API, cppu/source/uno/lbenv.cxx + if (aFunctionName == "defenv_dispose") { + return true; + } + // a pointer to this function is taken and passed to an underlying API, cppuhelper/source/exc_thrower.cxx + if (aFunctionName == "ExceptionThrower_acquire_release_nop") { + return true; + } + // used as a callback, /vcl/source/filter/jpeg/JpegReader.cxx + if (aFunctionName == "term_source") { + return true; + } + // only valid for windows, extensions/source/update/check/updatecheck.cxx + if (aFunctionName == "(anonymous namespace)::UpdateCheckThread::hasInternetConnection") { + return true; + } + // used as callback, extensions/source/plugin/unx/npwrap.cxx + if (aFunctionName == "plugin_x_error_handler" || aFunctionName == "noClosure") { + return true; + } + // used as callback, sax/source/expatwrap/sax_expat.cxx + if (aFunctionName == "(anonymous namespace)::SaxExpatParser_Impl::callbackUnknownEncoding") { + return true; + } + // used as callback, i18npool/source/textconversion/textconversion.cxx + if (aFunctionName == "com::sun::star::i18n::nullFunc") { + return true; + } + // used as callback, xmloff/source/text/txtparae.cxx + if (aFunctionName == "(anonymous namespace)::lcl_TextContentsUnfiltered") { + return true; + } + // template magic, include/canvas/verifyinput.hxx + if (aFunctionName == "canvas::tools::verifyInput") { + return true; + } + // template magic, cppcanvas/source/mtfrenderer/implrenderer.cxx + if (aFunctionName == "cppcanvas::internal::(anonymous namespace)::AreaQuery::result") { + return true; + } + // callback, drawinglayer/source/dumper/XShapeDumper. + if (aFunctionName == "(anonymous namespace)::closeCallback") { + return true; + } + // callback, basic/source/runtime/runtime.cxx + if (aFunctionName == "SbiRuntime::StepNOP") { + return true; + } + // DLL stuff, only used on windows, basic/source/runtime/dllmgr.hxx + if (aFunctionName == "SbiDllMgr::FreeDll") { + return true; + } + // only used on Windows, basic/source/sbx/sbxdec.cxx + if (aFunctionName == "SbxDecimal::neg" || aFunctionName == "SbxDecimal::isZero") { + return true; + } + // used as a callback, include/sfx2/shell.hxx + if (aFunctionName == "SfxShell::EmptyExecStub" || aFunctionName == "SfxShell::EmptyStateStub" + || aFunctionName == "SfxShell::VerbState") { + return true; + } + // SFX_IMPL_POS_CHILDWINDOW_WITHID macro + if (aFunctionName.find("GetChildWindowId") != std::string::npos) { + return true; + } + // SFX_IMPL_SUPERCLASS_INTERFACE macro + if (aFunctionName.find("InitInterface_Impl") != std::string::npos) { + return true; + } + // callback, vcl/unx/generic/app/sm.cxx + if (aFunctionName == "IgnoreIceIOErrors" || aFunctionName == "IgnoreIceErrors") { + return true; + } + // callback, vcl/unx/gtk/a11y/atkcomponent.cxx + if (aFunctionName == "component_wrapper_get_mdi_zorder") { + return true; + } + // callback, vcl/unx/gtk/a11y/atkaction.cxx + if (aFunctionName == "action_wrapper_set_description") { + return true; + } + // callback, vcl/unx/gtk/a11y/atkutil.cxx + if (aFunctionName == "ooo_atk_util_get_toolkit_version" || aFunctionName == "ooo_atk_util_get_toolkit_name") { + return true; + } + // callback, vcl/unx/gtk/a11y/atktextattributes.cxx + if (aFunctionName == "InvalidValue") { + return true; + } + // callback, vcl/unx/gtk/a11y/atktable.cxx + if (aFunctionName == "table_wrapper_set_summary" || aFunctionName == "table_wrapper_set_row_header" + || aFunctionName == "table_wrapper_set_row_description" + || aFunctionName == "table_wrapper_set_column_header" + || aFunctionName == "table_wrapper_set_column_description" + || aFunctionName == "table_wrapper_set_caption") { + return true; + } + // callbacks, vcl/unx/gtk/window/gtksalframe.cxx + if (startsWith(aFunctionName, "GtkSalFrame::IMHandler::signal")) { + return true; + } + // callbacks, vcl/unx/gtk/window/glomenu.cxx + if (startsWith(aFunctionName, "g_lo_menu_is_mutable")) { + return true; + } + // only contains code for certain versions of GTK, /vcl/unx/gtk/window/gtksalframe.cx + if (aFunctionName == "GtkSalFrame::AllocateFrame") { + return true; + } + // only valid for Windows, embeddedobj/source/msole/olemisc.cxx + if (aFunctionName == "OleEmbeddedObject::GetRidOfComponent") { + return true; + } + // callback, svx/source/accessibility/ShapeTypeHandler.cxx + if (aFunctionName == "accessibility::CreateEmptyShapeReference") { + return true; + } + // chart2/source/view/main/AbstractShapeFactory.cxx + if (aFunctionName == "chart::(anonymous namespace)::thisModule") { + return true; + } + // chart2/source/tools/InternalData.cxx + if (aFunctionName == "chart::InternalData::dump") { + return true; + } + // hwpfilter/ + if (aFunctionName == "debug" || aFunctionName == "token_debug") { + return true; + } + // callback, sdext/source/presenter/PresenterFrameworkObserver.cxx + if (aFunctionName == "sdext::presenter::PresenterFrameworkObserver::True") { + return true; + } + // callback, sw/source/core/doc/tblrwcl.cxx + if (aFunctionName == "lcl_DelOtherBox") { + return true; + } + // callback, sw/source/filter/ww8/ww8par.cxx + if (aFunctionName == "SwWW8ImplReader::Read_Majority") { + return true; + } + // callback, sw/source/filter/ww8/ww8par5.cxx + if (aFunctionName == "SwWW8ImplReader::Read_F_Shape") { + return true; + } + // called from SDI file, I don't know what that stuff is about, sd/source/ui/slidesorter/shell/SlideSorterViewShell.cx + if (aFunctionName == "sd::slidesorter::SlideSorterViewShell::ExecStatusBar" + || aFunctionName == "sd::OutlineViewShell::ExecStatusBar") { + return true; + } + // only used in debug mode, sd/source/filter/ppt/pptinanimations.cxx + if (startsWith(aFunctionName, "ppt::AnimationImporter::dump")) { + return true; + } + // only used in ENABLE_SDREMOTE_BLUETOOTH mode, sd/source/ui/dlg/tpoption.cx + if (aFunctionName == "SdTpOptionsMisc::SetImpressMode") { + return true; + } + // template magic, sc/source/ui/docshell/datastream.cxx + if (startsWith(aFunctionName, "sc::(anonymous namespace)::CSVHandler::")) { + return true; + } + // called from SDI file, I don't know what that stuff is about, sc/source/ui/view/cellsh4.cxx + if (aFunctionName == "ScCellShell::GetStateCursor") { + return true; + } + // template magic, sc/source/filter/excel/xepivot.cxx + if (aFunctionName == "XclExpPivotCache::SaveXml") { + return true; + } + // template magic, sc/source/filter/html/htmlpars.cxx + if (startsWith(aFunctionName, "(anonymous namespace)::CSSHandler::")) { + return true; + } + // callbacks, sc/source/filter/oox/formulaparser.cxx + if (startsWith(aFunctionName, "oox::xls::BiffFormulaParserImpl::import")) { + return true; + } + // template magic, sc/qa/unit/helper/csv_handler.hxx + if (startsWith(aFunctionName, "csv_handler::") || startsWith(aFunctionName, "conditional_format_handler::")) { + return true; + } + // template magic, slideshow/source/inc/listenercontainer.hxx + if (startsWith(aFunctionName, "slideshow::internal::EmptyBase::EmptyClearableGuard::")) { + return true; + } + // callback, scripting/source/vbaevents/eventhelper.cxx + if (aFunctionName == "ApproveAll") { + return true; + } + // only on WNT, basic/qa/cppunit/test_vba.cx + if (aFunctionName == "(anonymous namespace)::VBATest::testMiscOLEStuff") { + return true; + } + // GtkSalFrame::TriggerPaintEvent() is only compiled under certain versions of GTK + if (aFunctionName == "GtkSalFrame::TriggerPaintEvent") { + return true; + } + if (aFunctionName == "SwVectorModifyBase::dumpAsXml") { + return true; + } + // vcl/unx/gtk3 re-using vcl/unx/gtk: + if (aFunctionName == "DeInitAtkBridge" + || aFunctionName == "GtkData::initNWF" + || aFunctionName == "GtkSalFrame::EnsureAppMenuWatch" + || aFunctionName == "InitAtkBridge") + { + return true; + } + if (aFunctionName == "sc::AlignedAllocator::operator!=") { + return true; + } + if (aFunctionName == "clipboard_owner_init") { + return true; + } + // returns sizeof(struct) vcl/source/gdi/dibtools.cxx + if (aFunctionName == "getDIBV5HeaderSize") { + return true; + } + // windows only + if (aFunctionName == "InitAccessBridge") { + return true; + } + // callbacks + if (aFunctionName == "disabled_initSystray" || aFunctionName == "disabled_deInitSystray") { + return true; + } + // behind a BREAKPAD option + if (aFunctionName == "desktop::(anonymous namespace)::crashReportInfoExists") { + return true; + } + // LOK stuff + if (aFunctionName == "doc_getTileMode") { + return true; + } + // apparently this will be useful at sometime in the future + if (aFunctionName == "LocaleDataWrapper::getCurrZeroChar") { + return true; + } + // marked with TODO + if (aFunctionName == "oglcanvas::TextLayout::draw") { + return true; + } + // called from the .sdi files + if (aFunctionName == "SfxObjectShell::StateView_Impl") { + return true; + } + // gtk callback + if (aFunctionName == "GtkSalFrame::signalVisibility") { + return true; + } + // platform-version-dependent code + if (aFunctionName == "(anonymous namespace)::ACTIVE_TAB") { + return true; + } + // SMIL callbacks + if (aFunctionName == "boost::sp_scalar_constructor_hook" || aFunctionName == "boost::sp_scalar_destructor_hook") { + return true; + } + + + + + std::string aImmediateMacro = ""; + if (compiler.getSourceManager().isMacroBodyExpansion(pFunctionDecl->getLocStart()) ) { + StringRef name { Lexer::getImmediateMacroName( + pFunctionDecl->getLocStart(), compiler.getSourceManager(), compiler.getLangOpts()) }; + aImmediateMacro = name; + if (name.startswith("IMPL_LINK_") ) + { + return true; + } + } + + const CompoundStmt *pCompoundStmt = dyn_cast(pFunctionDecl->getBody()); + bool bEmptyBody = false; + if (pCompoundStmt) { + if (pCompoundStmt->size() > 1) { + return true; + } + if (pCompoundStmt->size() > 0) { + const ReturnStmt *pReturnStmt = dyn_cast(*pCompoundStmt->body_begin()); + if (!pReturnStmt) { + return true; + } + if (const UnaryOperator* unaryOp = dyn_cast(pReturnStmt->getRetValue())) { + if (unaryOp->getOpcode() == UO_AddrOf) { + return true; + } + } + if (pReturnStmt->getRetValue() != nullptr) { + // && !pReturnStmt->getRetValue()->isEvaluatable(compiler.getASTContext())) { + bool aBoolResult; + llvm::APSInt aIntResult; + if (pReturnStmt->getRetValue()->isTypeDependent() + || (!pReturnStmt->getRetValue()->EvaluateAsBooleanCondition(aBoolResult, compiler.getASTContext()) + && !pReturnStmt->getRetValue()->EvaluateAsInt(aIntResult, compiler.getASTContext()))) + { + return true; + } + } + } else { + bEmptyBody = true; + } + } + + std::string aMessage = "this "; + aMessage += pCXXMethodDecl ? "method" : "function"; + if (bEmptyBody) { + aMessage += " is empty and should be removed, " + aFunctionName; + } else { + aMessage += " returns a constant value and should be converted to a constant " + "or to static inline, " + aFunctionName + ", " + aImmediateMacro; + } + report( + DiagnosticsEngine::Warning, + aMessage, + pFunctionDecl->getLocStart()) + << pFunctionDecl->getSourceRange(); + if (pFunctionDecl != pFunctionDecl->getCanonicalDecl()) + report( + DiagnosticsEngine::Note, + aMessage, + pFunctionDecl->getCanonicalDecl()->getLocStart()) + << pFunctionDecl->getCanonicalDecl()->getSourceRange(); + return true; +} + +loplugin::Plugin::Registration X("constantfunction"); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/deadclass.cxx b/compilerplugins/clang/store/deadclass.cxx new file mode 100644 index 000000000..f055d6de2 --- /dev/null +++ b/compilerplugins/clang/store/deadclass.cxx @@ -0,0 +1,69 @@ +/* -*- 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 "plugin.hxx" + +namespace { + +class DeadClass: + public loplugin::FilteringPlugin +{ +public: + explicit DeadClass(InstantiationData const & data): FilteringPlugin(data) {} + + void run() override; + + bool VisitCXXRecordDecl(CXXRecordDecl const *); +}; + +void DeadClass::run() { + if (compiler.getLangOpts().CPlusPlus) { + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + } +} + +bool DeadClass::VisitCXXRecordDecl(CXXRecordDecl const * decl) { + if (ignoreLocation(decl) || !decl->isThisDeclarationADefinition()) + return true; + if (decl->needsImplicitDefaultConstructor()) + return true; + if (decl->getDescribedClassTemplate()) + return true; + if (isa(decl)) + return true; + int otherCnt = 0; + int copyMoveCnt = 0; + for (auto i = decl->ctor_begin(); i != decl->ctor_end(); ++i) { + if (!i->isUserProvided()) + continue; + if (i->isCopyOrMoveConstructor()) + copyMoveCnt++; + else + otherCnt++; + } + if (otherCnt == 0 && copyMoveCnt > 0) + { + report( + DiagnosticsEngine::Warning, + "class has only copy/move constructors, must be dead", + decl->getLocStart()) + << decl->getSourceRange(); + for (auto i = decl->ctor_begin(); i != decl->ctor_end(); ++i) { + if (i->isDeleted()) + continue; + } + } + return true; +} + +loplugin::Plugin::Registration X("deadclass"); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/defaultparams.cxx b/compilerplugins/clang/store/defaultparams.cxx new file mode 100644 index 000000000..cb533cb19 --- /dev/null +++ b/compilerplugins/clang/store/defaultparams.cxx @@ -0,0 +1,128 @@ +/* -*- 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 +#include + +#include "plugin.hxx" + +// Find places where we call a method with values == the values specified in the parameter defaults. +// i.e. where the code might as well not specify anything. + +namespace { + +class DefaultParams: + public loplugin::FilteringPlugin +{ +public: + explicit DefaultParams(InstantiationData const & data): FilteringPlugin(data) {} + + virtual void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); } + + bool VisitCallExpr(CallExpr * callExpr); +private: + bool evaluate(const Expr* expr, APSInt& x); +}; + +bool DefaultParams::VisitCallExpr(CallExpr * callExpr) { + if (ignoreLocation(callExpr)) { + return true; + } + if (callExpr->getDirectCallee() == nullptr) { + return true; + } + const FunctionDecl* functionDecl = callExpr->getDirectCallee()->getCanonicalDecl(); + auto n = functionDecl->getNumParams(); + if (n == 0 || !functionDecl->getParamDecl(n - 1)->hasDefaultArg()) { + return true; + } + assert(callExpr->getNumArgs() <= n); // can be < in template code + for (unsigned i = callExpr->getNumArgs(); i != 0;) { + --i; + Expr* arg = callExpr->getArg(i); + if (arg->isDefaultArgument()) { + continue; + } + // ignore this, it seems to trigger an infinite recursion + if (isa(arg)) + break; + const ParmVarDecl* parmVarDecl = functionDecl->getParamDecl(i); + if (!parmVarDecl->hasDefaultArg() + || parmVarDecl->hasUninstantiatedDefaultArg()) + { + break; + } + const Expr* defaultArgExpr = parmVarDecl->getDefaultArg(); + if (!defaultArgExpr) { + break; + } + bool found = false; + if (defaultArgExpr->isNullPointerConstant(compiler.getASTContext(), Expr::NPC_NeverValueDependent) + && arg->isNullPointerConstant(compiler.getASTContext(), Expr::NPC_NeverValueDependent)) + { + found = true; + } + if (!found) + { + APSInt x1, x2; + if (evaluate(defaultArgExpr, x1) && evaluate(arg, x2) && x1 == x2) + { + found = true; + } + } + // catch params with defaults like "= OUString()" + if (!found + && isa(arg) + && isa(defaultArgExpr)) + { + const CXXBindTemporaryExpr* strippedArg = dyn_cast_or_null(arg->IgnoreParenCasts()); + if (strippedArg && isa(strippedArg->getSubExpr()) + && dyn_cast(strippedArg->getSubExpr())->getNumArgs() == 0) + { + found = true; + } + } + if (!found) + break; + // Ignore CPPUNIT, it's macros contain some stuff that triggers us + StringRef aFileName = getFilenameOfLocation(compiler.getSourceManager().getSpellingLoc(parmVarDecl->getLocStart())); + if (aFileName.find("include/cppunit") != std::string::npos) + break; + report( + DiagnosticsEngine::Warning, + "not necessary to pass this argument, it defaults to the same value", + arg->getSourceRange().getBegin()) + << arg->getSourceRange(); + report( + DiagnosticsEngine::Note, + "default method parameter declaration here", + parmVarDecl->getSourceRange().getBegin()) + << parmVarDecl->getSourceRange(); + } + return true; +} + +bool DefaultParams::evaluate(const Expr* expr, APSInt& x) +{ + if (isa(expr)) { + x = 0; + return true; + } + if (expr->EvaluateAsInt(x, compiler.getASTContext())) + { + return true; + } + return false; +} + +loplugin::Plugin::Registration< DefaultParams > X("defaultparams"); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/deletedspecial.cxx b/compilerplugins/clang/store/deletedspecial.cxx new file mode 100644 index 000000000..bb6eb5cc6 --- /dev/null +++ b/compilerplugins/clang/store/deletedspecial.cxx @@ -0,0 +1,153 @@ +/* -*- 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 +#include +#include + +#include "plugin.hxx" + +// Second-guess that certain private special member function declarations for +// which no definition can be found are left undefined to prevent them from +// being implicitly declared. Such situations are better expressed by marking +// the function as = delete (it e.g. helps compilers determine +// whether class members are unused if all of a class's member definitions are +// seen in a compilation unit). (Default constructors for classes with multiple +// constructors are exempted as they would not be implicitly declared. +// Destructors are exempted because it is likely that a destructor is defined +// private on purpose.) + +namespace { + +CXXRecordDecl const * getClass(CXXMethodDecl const * decl) { + CXXRecordDecl const * cls = dyn_cast(decl->getDeclContext()); + assert(cls != nullptr); + return cls; +} + +class DeletedSpecial: + public loplugin::FilteringPlugin +{ +public: + explicit DeletedSpecial(InstantiationData const & data): FilteringPlugin(data) {} + + virtual void run() override; + + bool VisitCXXMethodDecl(CXXMethodDecl const * decl); + +private: + bool whitelist( + CXXMethodDecl const * decl, std::string const & name, + std::string const & path); +}; + +void DeletedSpecial::run() { + if (compiler.getLangOpts().CPlusPlus + && compiler.getPreprocessor().getIdentifierInfo( + "LIBO_INTERNAL_ONLY")->hasMacroDefinition()) + { + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + } +} + +bool DeletedSpecial::VisitCXXMethodDecl(CXXMethodDecl const * decl) { + if (ignoreLocation(decl) || !decl->isFirstDecl() || decl->isDefined() + || decl->isDefaulted() || decl->getAccess() != AS_private) + { + return true; + } + std::string desc; + if (decl->isCopyAssignmentOperator()) { + if (whitelist(decl, "ImpGraphic", "vcl/inc/impgraph.hxx") + || whitelist(decl, "SwSubFont", "sw/source/core/inc/swfont.hxx")) + { + return true; + } + desc = "copy assignment operator"; + } else if (decl->isMoveAssignmentOperator()) { + desc = "move assignment operator"; + } else { + CXXConstructorDecl const * ctor = dyn_cast(decl); + CXXRecordDecl const * cls = getClass(decl); + if (ctor != nullptr && ctor->isCopyConstructor()) { + if (whitelist(decl, "ImpGraphic", "vcl/inc/impgraph.hxx") + || whitelist(decl, "SbMethod", "include/basic/sbmeth.hxx") + || whitelist(decl, "ScDBCollection::NamedDBs", "sc/inc/dbdata.hxx") + || whitelist(decl, "ScDrawPage", "sc/inc/drawpage.hxx") + || whitelist(decl, "SmEditSource", "starmath/source/accessibility.hxx") + || whitelist(decl, "SwChartDataSequence", "sw/inc/unochart.hxx") + || whitelist(decl, "SwDPage", "sw/inc/dpage.hxx") + || whitelist(decl, "SwRedlineExtraData_Format", "sw/inc/redline.hxx") + || whitelist(decl, "SwRedlineExtraData_FormattingChanges", "sw/inc/redline.hxx") + || whitelist(decl, "SwTextAPIEditSource", "sw/source/core/inc/textapi.hxx") + || whitelist(decl, "XclImpBiff5Decrypter", "sc/source/filter/inc/xistream.hxx") + || whitelist(decl, "XclImpBiff8Decrypter", "sc/source/filter/inc/xistream.hxx") + || whitelist(decl, "configmgr::LocalizedPropertyNode", "configmgr/source/localizedpropertynode.hxx") + || whitelist(decl, "configmgr::LocalizedValueNode", "configmgr/source/localizedvaluenode.hxx") + || whitelist(decl, "configmgr::PropertyNode", "configmgr/source/propertynode.hxx") + || whitelist(decl, "oox::xls::BiffDecoder_RCF", "sc/source/filter/inc/biffcodec.hxx") + || whitelist(decl, "oox::xls::BiffDecoder_XOR", "sc/source/filter/inc/biffcodec.hxx") + || whitelist(decl, "rptui::OReportPage", "reportdesign/inc/RptPage.hxx")) + { + return true; + } + desc = "copy constructor"; + } else if (ctor != nullptr && ctor->isMoveConstructor()) { + desc = "move constructor"; + } else if (ctor != nullptr && ctor->isDefaultConstructor() + && std::distance(cls->ctor_begin(), cls->ctor_end()) == 1) + { + if (whitelist(decl, "AquaA11yFocusListener", "vcl/osx/a11yfocuslistener.hxx") + || whitelist(decl, "DocTemplLocaleHelper", "sfx2/source/doc/doctemplateslocal.hxx") + || whitelist(decl, "ScViewDataTable", "sc/source/filter/excel/../../ui/inc/viewdata.hxx") + || whitelist(decl, "ScViewDataTable", "sc/source/ui/inc/viewdata.hxx") + || whitelist(decl, "SwLineInfo", "sw/source/core/text/inftxt.hxx") + || whitelist(decl, "XRenderPeer", "vcl/unx/generic/gdi/xrender_peer.hxx") + || whitelist(decl, "desktop::DispatchWatcher", "desktop/source/app/dispatchwatcher.hxx") + || whitelist(decl, "desktop::RequestHandler", "desktop/source/app/officeipcthread.hxx") + || whitelist(decl, "desktop::RequestHandler", "desktop/source/lib/../app/officeipcthread.hxx") + || whitelist(decl, "sd::DiscoveryService", "sd/source/ui/remotecontrol/DiscoveryService.hxx") + || whitelist(decl, "sd::IconCache", "sd/source/ui/inc/tools/IconCache.hxx") + || whitelist(decl, "sd::RemoteServer", "sd/source/ui/inc/RemoteServer.hxx") + || whitelist(decl, "sd::slidesorter::cache::PageCacheManager", "sd/source/ui/slidesorter/inc/cache/SlsPageCacheManager.hxx") + || whitelist(decl, "framework::CommandInfoProvider", "include/framework/commandinfoprovider.hxx") + || whitelist(decl, "vcl::SettingsConfigItem", "vcl/inc/configsettings.hxx") + || whitelist(decl, "writerfilter::ooxml::OOXMLFactory", "writerfilter/source/ooxml/OOXMLFactory.hxx")) + { + return true; + } + desc = "default constructor"; + } else { + return true; + } + } + report( + DiagnosticsEngine::Warning, + ("private %0 is not defined at least in this compilation unit, maybe it" + " should be marked as deleted?"), + decl->getLocation()) + << desc << decl->getSourceRange(); + return true; +} + +bool DeletedSpecial::whitelist( + CXXMethodDecl const * decl, std::string const & name, + std::string const & path) +{ + return getClass(decl)->getQualifiedNameAsString() == name + && (getFilenameOfLocation( + compiler.getSourceManager().getSpellingLoc(decl->getLocation())) + == SRCDIR "/" + path); +} + +loplugin::Plugin::Registration X("deletedspecial", true); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/derivedclass.cxx b/compilerplugins/clang/store/derivedclass.cxx new file mode 100644 index 000000000..a1df0c253 --- /dev/null +++ b/compilerplugins/clang/store/derivedclass.cxx @@ -0,0 +1,70 @@ +/* -*- 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 "plugin.hxx" + +namespace { + +class DerivedClass: + public loplugin::FilteringPlugin +{ +public: + explicit DerivedClass(InstantiationData const & data): + FilteringPlugin(data) {} + + virtual void run() override + { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); } + + bool VisitCXXRecordDecl(CXXRecordDecl const * decl); +}; + +bool BaseCheck(const CXXRecordDecl *BaseDefinition, void *BaseClassName) { + // print warning about deriving from this classes + // the name has to contain namespace, e.g. foo::bar::ClassName + const char *BaseClasses[] = { + "Dialog", + "ProgressBar", + "SfxToolBoxControl", + "StatusBar", + 0, + }; + for (int i = 0; BaseClasses[i]; i++) + if (BaseDefinition->getQualifiedNameAsString().compare(BaseClasses[i]) == 0) { + *(const char **)BaseClassName = BaseClasses[i]; + return false; + } + return true; +} + +bool DerivedClass::VisitCXXRecordDecl(CXXRecordDecl const * decl) { + const char *BaseClassName = 0; + // checking for decl->hasDefinition() avoids crash in decl->forallBases + if (decl->hasDefinition() && + // not sure what hasAnyDependentBases() does, + // but it avoids classes we don't want, e.g. WeakAggComponentImplHelper1 + !decl->hasAnyDependentBases() && + !decl->forallBases(BaseCheck, &BaseClassName)) { + string warning_msg("class %0 derives from "); + // no idea how BaseClassName can be 0 sometimes... + if (BaseClassName) + warning_msg += BaseClassName; + report( + DiagnosticsEngine::Warning, + warning_msg, + decl->getLocStart()) + << decl->getQualifiedNameAsString() << decl->getSourceRange(); + } + return true; +} + +loplugin::Plugin::Registration X("derivedclass"); + +} + +/* vim:set shiftwidth=4 softtabstop=4 tabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/findoncontainer.cxx b/compilerplugins/clang/store/findoncontainer.cxx new file mode 100644 index 000000000..09f51187a --- /dev/null +++ b/compilerplugins/clang/store/findoncontainer.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 +#include + +#include "plugin.hxx" + +// Look for places calling std::find on a standard container where it should be using the container find method, which +// is more efficient. +// +// This lives in /store because the implementation is a hack and is highly dependent on the inwards +// of the libc++ library on the machine it runs on. +// + +namespace { + +class FindOnContainer: + public loplugin::FilteringPlugin +{ +public: + explicit FindOnContainer(InstantiationData const & data): FilteringPlugin(data) {} + + virtual void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); } + + bool VisitCallExpr(const CallExpr * expr); +}; + +bool FindOnContainer::VisitCallExpr(const CallExpr * expr) { + if (ignoreLocation(expr)) { + return true; + } + FunctionDecl const * fdecl = expr->getDirectCallee(); + if (fdecl == nullptr) { + return true; + } + std::string qname { fdecl->getQualifiedNameAsString() }; + if (qname == "std::find") + { + std::string tname = expr->getArg(0)->getType().getAsString(); + if (tname.find("std::_List_iterator") != std::string::npos + || tname.find("std::_List_const_iterator") != std::string::npos + || tname.find("std::vector") != std::string::npos + || tname.find("std::_Deque_iterator") != std::string::npos + || tname == "const int *" + || tname == "struct std::_Bit_const_iterator" + || tname == "const rtl::OUString *" + || tname == "class rtl::OUString *" + || tname == "const class rtl::OUString *" + || tname == "const sal_Int8 *" + || tname == "const sal_Int32 *" + || tname == "sal_Int32 *" + || tname == "sal_uInt16 *" ) + { + return true; + } + expr->dump(); + report( + DiagnosticsEngine::Warning, + ("rather use the more specific find method " + tname), + expr->getExprLoc()); + return true; + } + return true; +} + +loplugin::Plugin::Registration< FindOnContainer > X("findoncontainer"); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/fpcomparison.cxx b/compilerplugins/clang/store/fpcomparison.cxx new file mode 100644 index 000000000..faaec0110 --- /dev/null +++ b/compilerplugins/clang/store/fpcomparison.cxx @@ -0,0 +1,386 @@ +/* -*- 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 +#include +#include +#include +#include + +#include "check.hxx" +#include "plugin.hxx" + +/** +comparing floating point numbers using == or != is a bad idea. +*/ + +namespace { + +class FpComparison: + public loplugin::FilteringPlugin +{ +public: + explicit FpComparison(loplugin::InstantiationData const & data): + FilteringPlugin(data) {} + + virtual void run() override + { + vclFloatDevicePixel = compiler.getPreprocessor() + .getIdentifierInfo("VCL_FLOAT_DEVICE_PIXEL")->hasMacroDefinition(); + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + } + + bool VisitBinaryOperator(const BinaryOperator* ); + bool TraverseFunctionDecl(FunctionDecl* ); + bool TraverseCXXMethodDecl(CXXMethodDecl* ); +private: + bool ignore(FunctionDecl* ); + enum class EState { None, TraverseProcess, TraverseIgnore }; + EState meState = EState::None; + bool vclFloatDevicePixel; +}; + +bool FpComparison::TraverseFunctionDecl(FunctionDecl* function) +{ + bool bIgnore = ignore(function); + meState = bIgnore ? EState::TraverseIgnore : EState::TraverseProcess; + bool bRet = RecursiveASTVisitor::TraverseFunctionDecl(function); + meState = EState::None; + return bRet; +} + +bool FpComparison::TraverseCXXMethodDecl(CXXMethodDecl* function) +{ + bool bIgnore = ignore(function); + meState = bIgnore ? EState::TraverseIgnore : EState::TraverseProcess; + bool bRet = RecursiveASTVisitor::TraverseCXXMethodDecl(function); + meState = EState::None; + return bRet; +} + +bool FpComparison::ignore(FunctionDecl* function) +{ + if (ignoreLocation(function)) { + return true; + } + // we assume that these modules know what they are doing with FP stuff + StringRef aFileName = getFilenameOfLocation(compiler.getSourceManager().getSpellingLoc(function->getLocStart())); + if (loplugin::hasPathnamePrefix(aFileName, SRCDIR "/sc/")) { + return true; + } + if (!function->doesThisDeclarationHaveABody()) { + return true; + } + // Ignore operator== and operator!= + if (function->getOverloadedOperator() == OO_EqualEqual + || function->getOverloadedOperator() == OO_ExclaimEqual) { + return true; + } + // ignore known good functions + loplugin::DeclCheck dc(function); + if ((dc.Function("approxEqual").Namespace("math").Namespace("rtl") + .GlobalNamespace()) + || dc.Function("doubleToString").AnonymousNamespace().GlobalNamespace() + || dc.Function("stringToDouble").AnonymousNamespace().GlobalNamespace() + || dc.Function("rtl_math_round").GlobalNamespace() + || dc.Function("rtl_math_approxEqual").GlobalNamespace() + || dc.Function("rtl_math_approxValue").GlobalNamespace() + || dc.Function("rtl_math_asinh").GlobalNamespace() + || dc.Function("rtl_math_acosh").GlobalNamespace() + || dc.Function("_equalSequence").Namespace("cppu").GlobalNamespace() + // cppu/source/uno/eq.hxx + || dc.Function("_equalData").Namespace("cppu").GlobalNamespace() + // cppu/source/uno/eq.hxx + || dc.Function("equalFont").Namespace("xmlscript").GlobalNamespace() + // xmlscript/source/xmldlg_imexp/xmldlg_export.cxx + || (dc.Function("initialize").Class("Impl2").AnonymousNamespace() + .GlobalNamespace()) + // testtools/source/bridgetest/constructors.cxx + || (dc.Function("initialize").Class("Impl").AnonymousNamespace() + .GlobalNamespace()) + // testtools/source/bridgetest/constructors.cxx + || dc.Function("lok_approxEqual").AnonymousNamespace().GlobalNamespace() + // libreofficekit/source/gtk/lokdocview.cxx + // These might need fixing: + || (dc.Function("getSmallestDistancePointToPolygon").Namespace("utils") + .Namespace("basegfx").GlobalNamespace()) + // basegfx/source/polygon/b2dpolygontools.cxx + || (dc.Function("getSmallestDistancePointToPolyPolygon") + .Namespace("utils").Namespace("basegfx").GlobalNamespace()) + // basegfx/source/polygon/b2dpolypolygontools.cxx + || dc.Function("performTest").Namespace("bridge_test").GlobalNamespace() + // testtools/source/bridgetest/bridgetest.cxx + || dc.Function("equals").Namespace("bridge_test").GlobalNamespace() + || (dc.Function("lcl_getNANInsteadDBL_MIN").AnonymousNamespace() + .GlobalNamespace()) + // chart2/source/controller/chartapiwrapper/ChartDataWrapper.cxx + || dc.Function("compareSubstring").Class("ChapterCollator").Namespace("i18npool").GlobalNamespace() + || dc.Function("setDateTime").Class("Calendar_gregorian").Namespace("i18npool").GlobalNamespace() + || dc.Function("setLocalDateTime").Class("Calendar_gregorian").Namespace("i18npool").GlobalNamespace() + //TODO: + || dc.Function("ImpCheckCondition").Class("SvNumberformat").GlobalNamespace() + || dc.Function("GetTimeFormat").Class("SvNumberFormatter").GlobalNamespace() + || dc.Function("GuessDateTimeFormat").Class("SvNumberFormatter").GlobalNamespace() + || dc.Function("GetEditFormat").Class("SvNumberFormatter").GlobalNamespace() + || dc.Function("getSmallestDistancePointToBezierSegment").Class("B2DCubicBezier").Namespace("basegfx").GlobalNamespace() + || dc.Function("getLength").Class("B3DVector").Namespace("basegfx").GlobalNamespace() + || dc.Function("getXZLength").Class("B3DVector").Namespace("basegfx").GlobalNamespace() + || dc.Function("getYZLength").Class("B3DVector").Namespace("basegfx").GlobalNamespace() + || dc.Function("impSolve").Class("solver").AnonymousNamespace().Namespace("basegfx").GlobalNamespace() + || dc.Function("getB2DPolyPolygon").Class("solver").AnonymousNamespace().Namespace("basegfx").GlobalNamespace() + || dc.Function("getNormal").Class("CoordinateDataArray3D").GlobalNamespace() + || dc.Function("append").Class("ImplPolygon").AnonymousNamespace().Namespace("basegfx").GlobalNamespace() + || dc.Function("finish").Class("ImplPolygon").AnonymousNamespace().Namespace("basegfx").GlobalNamespace() + || dc.Function("convertMeasure").Class("Converter").Namespace("sax").GlobalNamespace() + || dc.Function("convertDouble").Class("Converter").Namespace("sax").GlobalNamespace() + || dc.Function("convertDuration").Class("Converter").Namespace("sax").GlobalNamespace() + || dc.Function("Frustum").Class("B3dTransformationSet").GlobalNamespace() + || dc.Function("Ortho").Class("B3dTransformationSet").GlobalNamespace() + || dc.Function("SetRatio").Class("B3dTransformationSet").GlobalNamespace() + || dc.Function("SetDeviceRectangle").Class("B3dTransformationSet").GlobalNamespace() + || dc.Function("HSBtoRGB").Class("Color").GlobalNamespace() + || dc.Function("Normalize").Class("Vector2D").Namespace("tools").GlobalNamespace() + || dc.Function("setAcceleration").Class("AnimationNode").Namespace("animcore").GlobalNamespace() + || dc.Function("setDecelerate").Class("AnimationNode").Namespace("animcore").GlobalNamespace() + || dc.Function("setVolume").Class("AnimationNode").Namespace("animcore").GlobalNamespace() + || dc.Function("setIterateInterval").Class("AnimationNode").Namespace("animcore").GlobalNamespace() + || dc.Function("createElement").Struct("Style").Namespace("xmlscript").GlobalNamespace() + || dc.Function("set_property").Class("Window").Namespace("vcl").GlobalNamespace() + || dc.Function("Adjust").Class("Bitmap").GlobalNamespace() + || dc.Function("ImplWriteActions").Class("SVMConverter").GlobalNamespace() + || dc.Function("Mirror").Class("GDIMetaFile").GlobalNamespace() + || dc.Function("Adjust").Class("GDIMetaFile").GlobalNamespace() + || dc.Function("Scale").Class("MetaCommentAction").GlobalNamespace() + || dc.Function("ImplGetGamma").Class("PNGReaderImpl").Namespace("vcl").GlobalNamespace() + || dc.Function("playMetafile").Class("PDFWriterImpl").Namespace("vcl").GlobalNamespace() + || dc.Function("invert").Class("Matrix3").Namespace("vcl").GlobalNamespace() + || dc.Function("emitTilings").Class("PDFWriterImpl").Namespace("vcl").GlobalNamespace() + || dc.Function("drawHorizontalGlyphs").Class("PDFWriterImpl").Namespace("vcl").GlobalNamespace() + || dc.Function("ImplWrite").Class("EMFWriter").GlobalNamespace() + || dc.Function("WriteRecords").Class("WMFWriter").GlobalNamespace() + || dc.Function("convertOneTypeEsc").Class("CffSubsetterContext").GlobalNamespace() + || dc.Function("ApplyGlyphTransform").Class("FreetypeFont").GlobalNamespace() + || dc.Function("JoinVerticalClipRectangles").Class("PrinterGfx").Namespace("psp").GlobalNamespace() + || dc.Function("PSSetLineWidth").Class("PrinterGfx").Namespace("psp").GlobalNamespace() + || dc.Function("DrawEPS").Class("PrinterGfx").Namespace("psp").GlobalNamespace() + || dc.Function("addDrawRectangle").Class("RenderList").GlobalNamespace() + || dc.Function("addDrawPolyPolygon").Class("RenderList").GlobalNamespace() + || dc.Function("addDrawPolyLine").Class("RenderList").GlobalNamespace() + || dc.Function("ApplyMatrix").Class("OpenGLProgram").GlobalNamespace() + || dc.Function("GuessWidth").Class("BorderWidthImpl").GlobalNamespace() + || dc.Function("ImplSetValue").Class("FormattedField").GlobalNamespace() + || dc.Function("IsAdjusted").Class("GraphicAttr").GlobalNamespace() + || dc.Operator(OO_Call).Struct("SpriteWeakOrder").Namespace("canvas").GlobalNamespace() + || dc.Function("setAlpha").Class("CanvasCustomSpriteHelper").Namespace("canvas").GlobalNamespace() + || dc.Function("setPriority").Class("CanvasCustomSpriteHelper").Namespace("canvas").GlobalNamespace() + || dc.Function("createFillGraphicAttribute").Class("SdrFillGraphicAttribute").Namespace("attribute").Namespace("drawinglayer").GlobalNamespace() + || dc.Function("create2DDecomposition").Class("ScenePrimitive2D").Namespace("primitive2d").Namespace("drawinglayer").GlobalNamespace() + || dc.Function("createAtom").Class("SvgLinearGradientPrimitive2D").Namespace("primitive2d").Namespace("drawinglayer").GlobalNamespace() + || dc.Function("createAtom").Class("SvgRadialGradientPrimitive2D").Namespace("primitive2d").Namespace("drawinglayer").GlobalNamespace() + || dc.Function("FoldConstantsBinaryNode").Class("SbiExprNode").GlobalNamespace() + || dc.Function("Format").Class("SbxValue").GlobalNamespace() + || dc.Function("Compare").Class("SbxValue").GlobalNamespace() + || dc.Function("SelectPlayToolBoxItem").Class("MediaControlBase").Namespace("avmedia").GlobalNamespace() + || dc.Function("convertDateTime").Class("SvXMLUnitConverter").GlobalNamespace() + || dc.Function("exportAudio").Class("AnimationsExporterImpl").Namespace("xmloff").GlobalNamespace() + || dc.Function("StartElement").Class("XMLEnhancedCustomShapeContext").GlobalNamespace() + || dc.Function("SetString").Class("SdXMLImExTransform2D").GlobalNamespace() + || dc.Function("SetString").Class("SdXMLImExTransform3D").GlobalNamespace() + || dc.Function("ExportPart_Impl").Class("SvXMLNumFmtExport").GlobalNamespace() + || dc.Function("AddNumber").Class("SvXMLNumFormatContext").GlobalNamespace() + || dc.Function("GetColorData").Class("EnhancedCustomShape2d").GlobalNamespace() + || dc.Function("AdaptObjColor").Class("EnhancedCustomShape2d").GlobalNamespace() + || dc.Function("RotateScene").Class("E3dScene").GlobalNamespace() + || dc.Function("createViewIndependentPrimitive2DSequence").Class("ViewContactOfSdrCaptionObj").Namespace("contact").Namespace("sdr").GlobalNamespace() + || dc.Function("PostItemChange").Class("E3dSceneProperties").Namespace("properties").Namespace("sdr").GlobalNamespace() + || dc.Function("NbcRotate").Class("SdrObject").GlobalNamespace() + || dc.Function("TakeObjNameSingul").Class("SdrPathObj").GlobalNamespace() + || dc.Function("NbcInsPoint").Class("SdrPathObj").GlobalNamespace() + || dc.Function("setValue").Class("Cell").Namespace("table").Namespace("sdr").GlobalNamespace() + || dc.Function("implSetDepth").Class("ExtrusionDepthWindow").Namespace("svx").GlobalNamespace() + || dc.Function("PointsToBezier").Class("XPolygon").GlobalNamespace() + || dc.Function("SetPosition").Class("Svx3DLightControl").GlobalNamespace() + || dc.Function("SetRotation").Class("Svx3DLightControl").GlobalNamespace() + || dc.Function("PlayToolBoxSelectHdl").Class("MediaPlaybackPanel").Namespace("sidebar").Namespace("svx").GlobalNamespace() + || dc.Function("CreateGraphicProperties").Class("EscherPropertyContainer").GlobalNamespace() + || dc.Function("renderSprite").Class("CanvasCustomSprite").Namespace("oglcanvas").GlobalNamespace() + || dc.Operator(OO_Call).Struct("SpriteComparator").AnonymousNamespace().Namespace("oglcanvas").GlobalNamespace() + || dc.Function("isHorizontalAxis").Class("TickFactory2D").Namespace("chart").GlobalNamespace() + || dc.Function("isVerticalAxis").Class("TickFactory2D").Namespace("chart").GlobalNamespace() + || dc.Function("getDistanceAxisTickToText").Class("TickFactory2D").Namespace("chart").GlobalNamespace() + || dc.Function("calculateExplicitIncrementAndScaleForLogarithmic").Class("ScaleAutomatism").Namespace("chart").GlobalNamespace() + || dc.Function("calculateExplicitIncrementAndScaleForLinear").Class("ScaleAutomatism").Namespace("chart").GlobalNamespace() + || dc.Function("makeTickmarkPropertiesForComplexCategories").Struct("AxisProperties").Namespace("chart").GlobalNamespace() + || dc.Function("createShapes").Class("BarChart").Namespace("chart").GlobalNamespace() + || dc.Function("transform").Class("Linear3DTransformation").Namespace("chart").GlobalNamespace() + || dc.Function("CalculateCubicSplines").Class("SplineCalculater").Namespace("chart").GlobalNamespace() + || dc.Function("setDiagramPositioning").Class("DiagramHelper").Namespace("chart").GlobalNamespace() + || dc.Function("centerGrow").Class("RelativePositionHelper").Namespace("chart").GlobalNamespace() + || dc.Function("pushToPropMap").Struct("FillProperties").Namespace("drawingml").Namespace("oox").GlobalNamespace() + || dc.Function("convertFromProperties").Class("AxFontDataModel").Namespace("ole").Namespace("oox").GlobalNamespace() + || dc.Function("isNotANumber").Class("ChartDataWrapper").Namespace("wrapper").Namespace("chart").GlobalNamespace() + || dc.Function("Reset").Class("ErrorBarResources").Namespace("chart").GlobalNamespace() + || dc.Function("ApplySpecialItem").Class("AxisItemConverter").Namespace("wrapper").Namespace("chart").GlobalNamespace() + || dc.Function("ApplySpecialItem").Class("DataPointItemConverter").Namespace("wrapper").Namespace("chart").GlobalNamespace() + || dc.Function("ApplySpecialItem").Class("TitleItemConverter").Namespace("wrapper").Namespace("chart").GlobalNamespace() + || dc.Function("ApplySpecialItem").Class("TextLabelItemConverter").Namespace("wrapper").Namespace("chart").GlobalNamespace() + || dc.Function("operate").Class("OOp_COMPARE").Namespace("file").Namespace("connectivity").GlobalNamespace() + || dc.Function("Write").Class("ORTFImportExport").Namespace("dbaui").GlobalNamespace() + || dc.Function("appendRow").Class("ORTFImportExport").Namespace("dbaui").GlobalNamespace() + || dc.Function("WriteCell").Class("OHTMLImportExport").Namespace("dbaui").GlobalNamespace() + || dc.Function("getBold").Class("VbaFontBase").GlobalNamespace() + || dc.Function("ModifyHdl").Class("SaneDlg").GlobalNamespace() + || dc.Function("EstablishNumericOption").Class("SaneDlg").GlobalNamespace() + || dc.Function("translatePropertiesToItems").Class("ControlCharacterDialog").Namespace("pcr").GlobalNamespace() + || dc.Function("writeMatrix").Class("Tag").Namespace("swf").GlobalNamespace() + || dc.Function("Impl_writeActions").Class("Writer").Namespace("swf").GlobalNamespace() + || dc.Function("Impl_quadBezierApprox").Class("Writer").Namespace("swf").GlobalNamespace() + || dc.Function("hasGradientOpacity").Struct("AnnotatingVisitor").AnonymousNamespace().Namespace("svgi").GlobalNamespace() + || dc.Function("getOdfColor").Struct("AnnotatingVisitor").AnonymousNamespace().Namespace("svgi").GlobalNamespace() + || dc.Function("writeStyle").Struct("AnnotatingVisitor").AnonymousNamespace().Namespace("svgi").GlobalNamespace() + || dc.Operator(OO_Call).Struct("ShapeWritingVisitor").AnonymousNamespace().Namespace("svgi").GlobalNamespace() + || dc.Function("SvgDashArray2Odf").Struct("OfficeStylesWritingVisitor").Namespace("svgi").GlobalNamespace() + || dc.Function("ImplWriteMask").Class("SVGActionWriter").GlobalNamespace() + || dc.Function("Factor").Class("FormulaCompiler").Namespace("formula").GlobalNamespace() + || dc.Function("setDateTime").Class("Calendar_gregorian").Namespace("i18n").Namespace("star").Namespace("sun").Namespace("com").GlobalNamespace() + || dc.Function("setLocalDateTime").Class("Calendar_gregorian").Namespace("i18n").Namespace("star").Namespace("sun").Namespace("com").GlobalNamespace() + || dc.Function("compareSubstring").Class("ChapterCollator").Namespace("i18n").Namespace("star").Namespace("sun").Namespace("com").GlobalNamespace() + || dc.Function("ToXml").Class("XFPadding").GlobalNamespace() + || dc.Function("Equal").Class("XFCellStyle").GlobalNamespace() + || dc.Function("Equal").Class("XFParaStyle").GlobalNamespace() + || dc.Function("GetAnimationEffect").Class("EffectMigration").Namespace("sd").GlobalNamespace() + || dc.Function("SetAnimationSpeed").Class("EffectMigration").Namespace("sd").GlobalNamespace() + || dc.Function("GetAnimationSpeed").Class("EffectMigration").Namespace("sd").GlobalNamespace() + || dc.Function("calculateIterateDuration").Class("CustomAnimationEffect").Namespace("sd").GlobalNamespace() + || dc.Function("setDuration").Class("CustomAnimationEffect").Namespace("sd").GlobalNamespace() + || dc.Function("replaceNode").Class("CustomAnimationEffect").Namespace("sd").GlobalNamespace() + || dc.Function("setIterateInterval").Class("CustomAnimationEffect").Namespace("sd").GlobalNamespace() + || dc.Function("append").Class("EffectSequenceHelper").Namespace("sd").GlobalNamespace() + || dc.Function("replace").Class("EffectSequenceHelper").Namespace("sd").GlobalNamespace() + || dc.Function("createTextGroupParagraphEffects").Class("EffectSequenceHelper").Namespace("sd").GlobalNamespace() + || dc.Function("setTextGrouping").Class("EffectSequenceHelper").Namespace("sd").GlobalNamespace() + || dc.Function("setTextGroupingAuto").Class("EffectSequenceHelper").Namespace("sd").GlobalNamespace() + || dc.Function("SetPresentationPenWidth").Class("SdOptionsMisc").GlobalNamespace() + || dc.Function("VarLook").Class("SwCalc").GlobalNamespace() + || dc.Function("Prim").Class("SwCalc").GlobalNamespace() + || dc.Function("keycompare").Struct("SwSortElement").GlobalNamespace() + || dc.Function("FormatValue").Class("SwDBField").GlobalNamespace() + || dc.Function("Evaluate").Class("SwDBField").GlobalNamespace() + || dc.Function("GetValue").Class("SwTableBox").GlobalNamespace() + || dc.Function("Modify").Class("SwTableBoxFormat").GlobalNamespace() + || dc.Function("isNotANumber").Class("SwXTextTable").GlobalNamespace() + || dc.Function("GetNextToken").Class("CSS1Parser").GlobalNamespace() + || dc.Function("update").Class("FontStylePropertyBox").Namespace("sd").GlobalNamespace() + || dc.Function("implMenuSelectHdl").Class("FontStylePropertyBox").Namespace("sd").GlobalNamespace() + || dc.Function("update").Class("CustomAnimationEffectTabPage").Namespace("sd").GlobalNamespace() + || dc.Function("update").Class("CustomAnimationDurationTabPage").Namespace("sd").GlobalNamespace() + || dc.Function("update").Class("CustomAnimationTextAnimTabPage").Namespace("sd").GlobalNamespace() + || dc.Function("FillCalcWithMergeData").Class("SwDBManager").GlobalNamespace() + || dc.Function("compareWith").Struct("TransitionEffect").Namespace("impl").Namespace("sd").GlobalNamespace() + || dc.Function("changeSelection").Class("CustomAnimationPane").Namespace("sd").GlobalNamespace() + || dc.Function("ContextMenuHdl").Class("SlideshowImpl").Namespace("sd").GlobalNamespace() + || dc.Function("FormatBox").Class("DocxAttributeOutput").GlobalNamespace() + || dc.Function("GetString").Class("Complex").Namespace("analysis").Namespace("sca").GlobalNamespace() + || dc.Function("getDelta").Class("AnalysisAddIn").GlobalNamespace() + || dc.Function("DataToDoc").Class("SwInsertDBColAutoPilot").GlobalNamespace() + || dc.Function("convertAnimateValue").Class("AnimationExporter").Namespace("ppt").GlobalNamespace() + || dc.Function("GetId").Class("PPTExBulletProvider").GlobalNamespace() + || dc.Function("SetThumbPosition").Class("PresenterScrollBar").Namespace("presenter").Namespace("sdext").GlobalNamespace() + || dc.Function("SetTotalSize").Class("PresenterScrollBar").Namespace("presenter").Namespace("sdext").GlobalNamespace() + || dc.Function("SetThumbSize").Class("PresenterScrollBar").Namespace("presenter").Namespace("sdext").GlobalNamespace() + || dc.Function("Layout").Class("PresenterNotesView").Namespace("presenter").Namespace("sdext").GlobalNamespace() + || dc.Function("compare").Struct("lessThanShape").Class("Shape").Namespace("internal").Namespace("slideshow").GlobalNamespace() + || dc.Operator(OO_Call).Struct("lessThanArea").Class("HyperlinkArea").Namespace("internal").Namespace("slideshow").GlobalNamespace() + || dc.Function("viewsChanged").Class("PointerSymbol").Namespace("internal").Namespace("slideshow").GlobalNamespace() + || dc.Function("RetrieveAttrs").Struct("SmXMLContext_Helper").GlobalNamespace() + || dc.Function("CompareImpl").Class("SortedResultSet").GlobalNamespace() + || dc.Function("emit").Struct("PDFNumber").Namespace("pdfparse").GlobalNamespace() + || dc.Function("visit").Class("DrawXmlOptimizer").Namespace("pdfi").GlobalNamespace() + || dc.Function("optimizeTextElements").Class("DrawXmlOptimizer").Namespace("pdfi").GlobalNamespace() + || dc.Function("init").Class("DrawXmlFinalizer").Namespace("pdfi").GlobalNamespace() + || dc.Function("visit").Class("DrawXmlFinalizer").Namespace("pdfi").GlobalNamespace() + || dc.Function("GetState").Class("OReportController").Namespace("rptui").GlobalNamespace() + || dc.Function("isFormatCommandEnabled").Class("OReportController").Namespace("rptui").GlobalNamespace() + || dc.Function("resolveUnderlines").Struct("PageElement").Namespace("pdfi").GlobalNamespace() + || dc.Function("visit").Class("WriterXmlOptimizer").Namespace("pdfi").GlobalNamespace() + || dc.Function("optimizeTextElements").Class("WriterXmlOptimizer").Namespace("pdfi").GlobalNamespace() + || dc.Function("drawGlyphs").Class("PDFIProcessor").Namespace("pdfi").GlobalNamespace() + || dc.Function("LTypeToDXFLineInfo").Class("DXF2GDIMetaFile").GlobalNamespace() + || dc.Function("DrawEntities").Class("DXF2GDIMetaFile").GlobalNamespace() + || dc.Function("ImplWriteActions").Class("PSWriter").GlobalNamespace() + || dc.Function("ImplWriteLineInfo").Class("PSWriter").GlobalNamespace() + || dc.Function("ImplInsert").Class("CGMBitmap").GlobalNamespace() + || dc.Function("GetNext").Class("CGMBitmap").GlobalNamespace() + || dc.Function("drawPolyLine").Class("X11SalGraphicsImpl").GlobalNamespace() + || dc.Function("testRefresh").Class("XDataPilotTable").Namespace("apitest").GlobalNamespace() + || dc.Function("testTitleManualLayoutXLSX").Class("Chart2ExportTest").GlobalNamespace() + || dc.Function("testPlotAreaManualLayoutXLSX").Class("Chart2ExportTest").GlobalNamespace() + || dc.Function("testLegendManualLayoutXLSX").Class("Chart2ExportTest").GlobalNamespace() + || dc.Function("SetScreenNumber").Class("AquaSalFrame").GlobalNamespace() + || (vclFloatDevicePixel + && (dc.Function("Justify").Class("GenericSalLayout").GlobalNamespace() + || dc.Function("AdjustLayout").Class("MultiSalLayout").GlobalNamespace())) + // vcl/headless/svpgdi.cxx, ba4a124b0c0c66fd275f5147d55eeec27ce78da9: + || dc.Function("drawAlphaBitmap").Class("SvpSalGraphics").GlobalNamespace() + || dc.Function("drawMask").Class("SvpSalGraphics").GlobalNamespace() + || dc.Function("renderSource").GlobalNamespace()) + { + return true; + } +// cout << "xxx " + function->getQualifiedNameAsString() << endl; + return false; +} + +bool isZeroConstant(ASTContext& context, const Expr* expr) +{ + if (!expr->getType()->isFloatingType()) { + return false; + } + // prevent clang crash + if (!context.getLangOpts().CPlusPlus) { + return false; + } + APValue result; + if (!expr->isCXX11ConstantExpr(context, &result)) { + return false; + } + assert(result.isFloat()); + return result.getFloat().isZero(); +} +bool FpComparison::VisitBinaryOperator(const BinaryOperator* binaryOp) +{ + if (meState != EState::TraverseProcess || ignoreLocation(binaryOp)) { + return true; + } + if (binaryOp->getOpcode() != BO_EQ && binaryOp->getOpcode() != BO_NE) { + return true; + } + // comparison with zero is valid + if (isZeroConstant(compiler.getASTContext(), binaryOp->getLHS()) + || isZeroConstant(compiler.getASTContext(), binaryOp->getRHS())) + { + return true; + } + QualType LHSStrippedType = binaryOp->getLHS()->IgnoreParenImpCasts()->getType(); + QualType RHSStrippedType = binaryOp->getRHS()->IgnoreParenImpCasts()->getType(); + if (LHSStrippedType->isFloatingType() && RHSStrippedType->isFloatingType()) { + report( + DiagnosticsEngine::Warning, "floating-point comparison", + binaryOp->getSourceRange().getBegin()) + << binaryOp->getSourceRange(); + } + return true; +} + + +loplugin::Plugin::Registration< FpComparison > X("fpcomparison", true); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/lclstaticfix.cxx b/compilerplugins/clang/store/lclstaticfix.cxx new file mode 100644 index 000000000..01e4171fc --- /dev/null +++ b/compilerplugins/clang/store/lclstaticfix.cxx @@ -0,0 +1,54 @@ +/* -*- 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 "lclstaticfix.hxx" + +/* +This is a rewriter. + +Check all lcl_ functions and prepend static if needed. +*/ + +namespace loplugin +{ + +LclStaticFix::LclStaticFix( CompilerInstance& compiler, Rewriter& rewriter ) + : FilteringRewritePlugin( compiler, rewriter ) + { + } + +void LclStaticFix::run() + { + TraverseDecl( compiler.getASTContext().getTranslationUnitDecl()); + } + +bool LclStaticFix::VisitFunctionDecl( const FunctionDecl* declaration ) + { + if( ignoreLocation( declaration )) + return true; + if( declaration->isCXXClassMember()) + return true; + if( declaration->getStorageClass() == SC_Static ) + return true; + string name = declaration->getQualifiedNameAsString(); + if( name.find( "::" ) != string::npos ) + return true; + if( name.compare( 0, 4, "lcl_" ) != 0 ) + return true; + insertText( declaration->getLocStart(), "static " ); + return true; + } + +static Plugin::Registration< LclStaticFix > X( "lclstaticfix" ); + +} // namespace + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/lclstaticfix.hxx b/compilerplugins/clang/store/lclstaticfix.hxx new file mode 100644 index 000000000..e15420e63 --- /dev/null +++ b/compilerplugins/clang/store/lclstaticfix.hxx @@ -0,0 +1,33 @@ +/* -*- 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. + * + */ + +#ifndef LCLSTATICFIX_H +#define LCLSTATICFIX_H + +#include "plugin.hxx" + +namespace loplugin +{ + +class LclStaticFix + : public loplugin::FilteringRewritePlugin< LclStaticFix > + { + public: + explicit LclStaticFix( CompilerInstance& compiler, Rewriter& rewriter ); + virtual void run() override; + bool VisitFunctionDecl( const FunctionDecl* declaration ); + }; + +} // namespace + +#endif // POSTFIXINCREMENTFIX_H + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/manualrefcount.cxx b/compilerplugins/clang/store/manualrefcount.cxx new file mode 100644 index 000000000..c8085904f --- /dev/null +++ b/compilerplugins/clang/store/manualrefcount.cxx @@ -0,0 +1,323 @@ +/* -*- 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 +#include +#include +#include +#include + +#include +#include "compat.hxx" +#include "plugin.hxx" +#include "check.hxx" + +/** + Look for calls to the ref-counting methods acquire()/release(), which should only be called by classes like rtl::Reference. +*/ + +namespace { + +class ManualRefCount: + public RecursiveASTVisitor, public loplugin::Plugin +{ +public: + explicit ManualRefCount(InstantiationData const & data): Plugin(data) {} + + virtual void run() override + { + StringRef fn( compiler.getSourceManager().getFileEntryForID( + compiler.getSourceManager().getMainFileID())->getName() ); + + // old code, no point in updating + if (loplugin::isSamePathname(fn, SRCDIR "/store/source/store.cxx")) + return; + +// TODO ----------------------------- + if (loplugin::isSamePathname(fn, SRCDIR "/registry/source/registry.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/registry/source/regimpl.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/registry/source/reflread.cxx")) + return; + // TODO MenuAttributes::CreateAttribute + if (loplugin::isSamePathname(fn, SRCDIR "/framework/source/fwe/xml/menuconfiguration.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/sw/source/uibase/app/apphdl.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/dbaccess/source/core/dataaccess/ModelImpl.cxx")) + return; + // need a better replacement for vcl::EventPoster + if (loplugin::isSamePathname(fn, SRCDIR "/svtools/source/misc/acceleratorexecute.cxx")) + return; + // PostUserEvent stuff + if (loplugin::isSamePathname(fn, SRCDIR "/toolkit/source/awt/vclxwindow.cxx")) + return; + // playing games with pointers passed into combobox entries + if (loplugin::isSamePathname(fn, SRCDIR "/cui/source/customize/cfgutil.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/cui/source/customize/cfg.cxx")) + return; +// END TODO ----------------------------- + + // can't fix these without breaking stable ABI + if (fn.startswith(SRCDIR "/sal/")) + return; + if (fn.startswith(SRCDIR "/salhelper/")) + return; + if (fn.startswith(SRCDIR "/cppu/")) + return; + if (fn.startswith(SRCDIR "/cppuhelper/")) + return; + if (fn.startswith(SRCDIR "/bridges/")) + return; + + // lots of magic here + if (fn.startswith(SRCDIR "/stoc/")) + return; + if (fn.startswith(SRCDIR "/testtools/")) + return; + + // mutex games + if (loplugin::isSamePathname(fn, SRCDIR "/vcl/source/app/scheduler.cxx")) + return; + // opengl games + if (loplugin::isSamePathname(fn, SRCDIR "/vcl/source/app/svdata.cxx")) + return; + + // passing the pointer through PostUserEvent + if (loplugin::isSamePathname(fn, SRCDIR "/avmedia/source/gstreamer/gstplayer.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/svx/source/form/fmscriptingenv.cxx")) + return; + + // thread games + if (loplugin::isSamePathname(fn, SRCDIR "/io/source/stm/opump.cxx")) + return; + + // ??? no idea what this code is up to + if (loplugin::isSamePathname(fn, SRCDIR "/extensions/source/scanner/scanunx.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/stoc/source/invocation_adapterfactory/iafactory.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/fpicker/source/office/asyncfilepicker.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/forms/source/component/FormComponent.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/ucb/source/ucp/file/bc.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/ucb/source/ucp/file/filprp.cxx")) + return; + // calling release() ? + if (loplugin::isSamePathname(fn, SRCDIR "/toolkit/source/helper/accessibilityclient.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/svtools/source/misc/svtaccessiblefactory.cxx")) + return; + + // implementing css::uno::XInterface + if (loplugin::isSamePathname(fn, SRCDIR "/sd/source/ui/animations/motionpathtag.cxx")) + return; + // UNO factory methods + if (fn.startswith(SRCDIR "/comphelper/")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/linguistic/source/convdiclist.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/linguistic/source/dlistimp.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/linguistic/source/gciterator.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/linguistic/source/lngsvcmgr.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/linguistic/source/lngopt.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/vcl/unx/generic/gdi/gcach_xpeer.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/dbaccess/source/ui/dlg/dbwizsetup.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/dbaccess/source/ui/dlg/dbwizsetup.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/lingucomponent/source/hyphenator/hyphen/hyphenimp.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/lingucomponent/source/spellcheck/spell/sspellimp.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/lingucomponent/source/thesaurus/libnth/nthesimp.cxx")) + return; + + + // some kind of complicated listener nonsense + if (loplugin::isSamePathname(fn, SRCDIR "/sd/source/ui/framework/tools/FrameworkHelper.cxx")) + return; + // more listener nonsense + if (loplugin::isSamePathname(fn, SRCDIR "/sw/source/uibase/uno/unomailmerge.cxx")) + return; + // playing games with it's listener list + if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/ui/unoobj/cellsuno.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/ui/unoobj/chart2uno.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/ui/unoobj/dapiuno.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/ui/unoobj/datauno.cxx")) + return; + if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/ui/unoobj/linkuno.cxx")) + return; + // PostUserEvent + if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/ui/vba/vbaeventshelper.cxx")) + return; + // thread holding itself + if (loplugin::isSamePathname(fn, SRCDIR "/forms/source/component/EventThread.cxx")) + return; + + + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + } + + bool shouldVisitTemplateInstantiations () const { return true; } + + bool VisitCXXMemberCallExpr(const CXXMemberCallExpr *); + bool TraverseCXXRecordDecl(CXXRecordDecl *); + bool TraverseCXXMethodDecl(CXXMethodDecl *); + bool TraverseFunctionDecl(FunctionDecl *); + bool TraverseCXXConstructorDecl(CXXConstructorDecl *); + bool TraverseCXXDestructorDecl(CXXDestructorDecl *); + bool TraverseCXXConversionDecl(CXXConversionDecl *); + bool TraverseClassTemplateSpecializationDecl(ClassTemplateSpecializationDecl *); + bool TraverseLinkageSpecDecl(LinkageSpecDecl *); +private: + bool ignoreCallerClass(CXXRecordDecl*); +}; + +bool ManualRefCount::TraverseCXXMethodDecl(CXXMethodDecl* cxxMethodDecl) +{ + if (ignoreCallerClass(cxxMethodDecl->getParent())) + return true; + // disambiguating forwarding methods for XInterface subclasses + if (cxxMethodDecl->getIdentifier() && (cxxMethodDecl->getName() == "acquire" || cxxMethodDecl->getName() == "release")) + return true; + return RecursiveASTVisitor::TraverseCXXMethodDecl(cxxMethodDecl); +} + +bool ManualRefCount::TraverseFunctionDecl(FunctionDecl* functionDecl) +{ + auto tc = loplugin::DeclCheck(functionDecl); + if (tc.Function("make_shared_from_UNO").Namespace("comphelper").GlobalNamespace()) + return true; + return RecursiveASTVisitor::TraverseFunctionDecl(functionDecl); +} + +bool ManualRefCount::TraverseCXXConstructorDecl(CXXConstructorDecl* cxxMethodDecl) +{ + if (ignoreCallerClass(cxxMethodDecl->getParent())) + return true; + return RecursiveASTVisitor::TraverseCXXMethodDecl(cxxMethodDecl); +} + +bool ManualRefCount::TraverseCXXDestructorDecl(CXXDestructorDecl*) +{ + // just ignore destructors, tons of places like to call acquire() on themselves in their destructor + // supposedly to prevent recursively calling the destructor + return true; +} +bool ManualRefCount::TraverseCXXConversionDecl(CXXConversionDecl* cxxMethodDecl) +{ + if (ignoreCallerClass(cxxMethodDecl->getParent())) + return true; + return RecursiveASTVisitor::TraverseCXXMethodDecl(cxxMethodDecl); +} +bool ManualRefCount::TraverseCXXRecordDecl(CXXRecordDecl* cxxRecordDecl) +{ + if (ignoreCallerClass(cxxRecordDecl)) + return true; + return RecursiveASTVisitor::TraverseCXXRecordDecl(cxxRecordDecl); +} + +bool ManualRefCount::TraverseClassTemplateSpecializationDecl(ClassTemplateSpecializationDecl* templateDecl) +{ + if (ignoreCallerClass(templateDecl)) + return true; + return RecursiveASTVisitor::TraverseClassTemplateSpecializationDecl(templateDecl); +} + +bool ManualRefCount::TraverseLinkageSpecDecl(LinkageSpecDecl *) +{ + // ignore methods inside "extern ""C""" blocks, these are normally UNO constructors, which + // are required to raise the reference count before returning + return true; +} + +bool ManualRefCount::ignoreCallerClass(CXXRecordDecl* cxxRecordDecl) +{ + auto tc = loplugin::TypeCheck(cxxRecordDecl); + return + tc.Class("Reference").Namespace("rtl").GlobalNamespace() + || tc.Class("cow_wrapper").Namespace("o3tl").GlobalNamespace() + || tc.Class("Reference").Namespace("uno").Namespace("star").Namespace("sun").Namespace("com").GlobalNamespace() + || tc.Class("ShareGuard").Namespace("framework").GlobalNamespace() + || tc.Class("ControlModelLock").Namespace("frm").GlobalNamespace() + || tc.Struct("ReleaseFunc").Namespace("detail").Namespace("comphelper").GlobalNamespace() + // TODO no idea what this is up to + || tc.Class("SfxModelSubComponent").GlobalNamespace() + || tc.Class("OSubComponent").Namespace("mysqlc").Namespace("connectivity").GlobalNamespace() + || tc.Class("OSubComponent").Namespace("connectivity").GlobalNamespace() + // TODO do we really need this? + || tc.Class("ShareableMutex").Namespace("framework").GlobalNamespace() + || tc.Class("ObservableThread").GlobalNamespace() + ; +} + +bool ManualRefCount::VisitCXXMemberCallExpr(const CXXMemberCallExpr* cxxMemberCallExpr) +{ + if (ignoreLocation(cxxMemberCallExpr)) + return true; + if (isInUnoIncludeFile(compiler.getSourceManager().getSpellingLoc(cxxMemberCallExpr->getLocStart()))) + return true; + + // first, use some heuristics to find the right kind of acquire()/release() calls + CXXMethodDecl const * calleeMethodDecl = cxxMemberCallExpr->getMethodDecl(); + if (!calleeMethodDecl || !calleeMethodDecl->getIdentifier()) + return true; + if (calleeMethodDecl->getName() != "acquire" && calleeMethodDecl->getName() != "release") + return true; + if (calleeMethodDecl->getNumParams() != 0) + return true; + // std::unique_ptr::release() and similar methods + if (calleeMethodDecl->getName() == "release" && loplugin::TypeCheck(calleeMethodDecl->getReturnType()).Pointer()) + return true; + + // these are OK + auto calleeRecordTC = loplugin::TypeCheck(calleeMethodDecl->getParent()); + if (calleeRecordTC.Struct("ResourceHolder").Namespace("store").GlobalNamespace()) + return true; + if (calleeRecordTC.Class("Module").Namespace("osl").GlobalNamespace()) + return true; + if (calleeRecordTC.Class("Mutex").Namespace("osl").GlobalNamespace()) + return true; + if (calleeRecordTC.Class("multi_type_vector").Namespace("mdds").GlobalNamespace()) + return true; + +// while (calleeMethodDecl->size_overridden_methods() > 0) +// calleeMethodDecl = *calleeMethodDecl->begin_overridden_methods(); +// auto tc2 = loplugin::TypeCheck(calleeMethodDecl->getParent()); +// if (tc2.Class("XInterface").Namespace("uno").Namespace("star").Namespace("sun").Namespace("com").GlobalNamespace()) +// return true; + +std::cout << calleeMethodDecl->getParent()->getQualifiedNameAsString() << std::endl; + report( + DiagnosticsEngine::Warning, "call to acquire/release", + cxxMemberCallExpr->getLocStart()) + << cxxMemberCallExpr->getSourceRange(); + return true; +} + + +loplugin::Plugin::Registration< ManualRefCount > X("manualrefcount", true); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/paintmethodconversion.cxx b/compilerplugins/clang/store/paintmethodconversion.cxx new file mode 100644 index 000000000..f8c63d4b9 --- /dev/null +++ b/compilerplugins/clang/store/paintmethodconversion.cxx @@ -0,0 +1,94 @@ +/* -*- 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 "clang/Lex/Lexer.h" + +#include "plugin.hxx" +#include +#include + +/** + * Rewrites all Paint method on subclasses of vcl::Window to include RenderContext& as parameter. + * + * run as: make COMPILER_PLUGIN_TOOL=paintmethodconversion UPDATE_FILES=all FORCE_COMPILE_ALL=1 + */ + +namespace +{ + +bool baseCheckNotWindowSubclass(const CXXRecordDecl* aBaseDefinition, void* /*pInput*/) +{ + if (aBaseDefinition && aBaseDefinition->getQualifiedNameAsString() == "vcl::Window") + { + return false; + } + return true; +} + +bool isDerivedFromWindow(const CXXRecordDecl* decl) { + if (!decl) + return false; + // skip vcl::Window + if (decl->getQualifiedNameAsString() == "vcl::Window") + return false; + if (!decl->forallBases(baseCheckNotWindowSubclass, nullptr, true)) + return true; + + return false; +} + +class PaintMethodConversion: public loplugin::FilteringRewritePlugin +{ +public: + explicit PaintMethodConversion(InstantiationData const& data): + FilteringRewritePlugin(data) + {} + + virtual void run() override + { + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + } + + bool TraverseCXXMethodDecl(const CXXMethodDecl* methodDeclaration) + { + if (!rewriter) + return true; + + if (methodDeclaration->getNameAsString() != "Paint") + return true; + + if (!isDerivedFromWindow(methodDeclaration->getParent())) + { + return true; + } + + unsigned int nNoOfParameters = methodDeclaration->getNumParams(); + + if (nNoOfParameters == 1) // we expect only one parameter Paint(Rect&) + { + const ParmVarDecl* parameterDecl = methodDeclaration->getParamDecl(0); + if (methodDeclaration->hasBody()) + { + rewriter->InsertText(parameterDecl->getLocStart(), "vcl::RenderContext& /*rRenderContext*/, ", true, true); + } + else + { + rewriter->InsertText(parameterDecl->getLocStart(), "vcl::RenderContext& rRenderContext, ", true, true); + } + } + return true; + } + +}; + +loplugin::Plugin::Registration X("paintmethodconversion", true); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/postfixincrementfix.cxx b/compilerplugins/clang/store/postfixincrementfix.cxx new file mode 100644 index 000000000..eba6f35b5 --- /dev/null +++ b/compilerplugins/clang/store/postfixincrementfix.cxx @@ -0,0 +1,132 @@ +/* -*- 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 "postfixincrementfix.hxx" + +/* +This is a rewriter. + +Change all postfix ++ operators of non-trivial types to prefix if possible. +*/ + +namespace loplugin +{ + +PostfixIncrementFix::PostfixIncrementFix( const InstantiationData& data ) + : FilteringRewritePlugin( data ) + { + } + +void PostfixIncrementFix::run() + { + TraverseDecl( compiler.getASTContext().getTranslationUnitDecl()); + } + +bool PostfixIncrementFix::VisitCXXOperatorCallExpr( const CXXOperatorCallExpr* op ) + { + if( ignoreLocation( op )) + return true; + // postfix ++ has two arguments (the operand and the hidden extra int) + if( op->getOperator() == OO_PlusPlus && op->getNumArgs() == 2 ) + fixPostfixOperator( op ); + // For primitive types it would be UnaryOperatorExpr, but probably no good reason to change those. + return true; + } + +void PostfixIncrementFix::fixPostfixOperator( const CXXOperatorCallExpr* op ) + { + if( !canChangePostfixToPrefix( op, op )) + return; + if( !shouldDoChange( op->getArg( 0 ))) + return; + // Adding spaces around the moved ++ should not be necessary + // (there might a problem with e.g. a+b++ -> a+++b (i.e. a++ +b), + // but we don't change such expressions). + if( insertText( op->getLocStart(), "++" )) // insert is intentionally first, in case it fails + removeText( op->getCallee()->getSourceRange()); + } + +bool PostfixIncrementFix::canChangePostfixToPrefix( const Stmt* stmt , const CXXOperatorCallExpr* op ) + { + const Stmt* parent = parentStmt( stmt ); + if( parent == NULL ) + return true; + // check if foo++ can be safely replaced by ++foo + switch( parent->getStmtClass()) + { + case Stmt::CompoundStmtClass: + return true; + // these mean somebody is going to use it + case Stmt::ImplicitCastExprClass: + case Stmt::MaterializeTemporaryExprClass: + case Stmt::BinaryOperatorClass: + case Stmt::UnaryOperatorClass: + case Stmt::CallExprClass: + case Stmt::CXXOperatorCallExprClass: + return false; + case Stmt::CXXBindTemporaryExprClass: + // tricky, it may just mean the temporary will be cleaned up + // (by ExprWithCleanups), ignore and go up + return canChangePostfixToPrefix( parent, op ); + case Stmt::ExprWithCleanupsClass: + // cleanup of a temporary, should be harmless (if the use + // of the postfix ++ operator here relies on the fact that + // the dtor for the object will be called, that's pretty insane + // code). Ignore and go up. + return canChangePostfixToPrefix( parent, op ); + case Stmt::ParenExprClass: // parentheses, go up + return canChangePostfixToPrefix( parent, op ); + case Stmt::IfStmtClass: + // cannot be changed in condition, can be changed in statements + return cast< IfStmt >( parent )->getCond() != stmt; + case Stmt::WhileStmtClass: + return cast< WhileStmt >( parent )->getCond() != stmt; + case Stmt::DoStmtClass: + return cast< DoStmt >( parent )->getCond() != stmt; + case Stmt::ForStmtClass: + return cast< ForStmt >( parent )->getCond() != stmt; + default: + report( DiagnosticsEngine::Fatal, "cannot analyze operator++ (plugin needs fixing)", + op->getLocStart()) << parent->getSourceRange(); + parent->dump(); + return false; + } + } + +bool PostfixIncrementFix::shouldDoChange( const Expr* operand ) + { + // TODO Changing 'a->b++' to '++a->b' is technically the same, but the latter probably looks confusing, + // so either avoid that, or add parentheses. Avoid for now. + const Expr* expr = const_cast< Expr* >( operand )->IgnoreImplicit(); // does not have const version + switch( expr->getStmtClass()) + { + case Stmt::ParenExprClass: + return true; // there are already parentheses, ok to move the ++ + case Stmt::MemberExprClass: + return false; // ++a->b , avoid + case Stmt::DeclRefExprClass: + return true; + default: + { + report( DiagnosticsEngine::Fatal, "cannot analyze operator++ (plugin needs fixing)", + expr->getLocStart()) << operand->getSourceRange(); + expr->dump(); + operand->dump(); + return false; + } + } + } + +static Plugin::Registration< PostfixIncrementFix > X( "postfixincrementfix" ); + +} // namespace + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/postfixincrementfix.hxx b/compilerplugins/clang/store/postfixincrementfix.hxx new file mode 100644 index 000000000..6cc2470e6 --- /dev/null +++ b/compilerplugins/clang/store/postfixincrementfix.hxx @@ -0,0 +1,38 @@ +/* -*- 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. + * + */ + +#ifndef POSTFIXINCREMENTFIX_H +#define POSTFIXINCREMENTFIX_H + +#include "plugin.hxx" + +namespace loplugin +{ + +class PostfixIncrementFix + : public loplugin::FilteringRewritePlugin< PostfixIncrementFix > + { + public: + explicit PostfixIncrementFix( const InstantiationData& data ); + virtual void run() override; + bool VisitCXXOperatorCallExpr( const CXXOperatorCallExpr* op ); + private: + void fixPostfixOperator( const CXXOperatorCallExpr* op ); + void fixPostfixOperators( const Stmt* stmt ); + bool canChangePostfixToPrefix( const Stmt* stmt, const CXXOperatorCallExpr* op ); + bool shouldDoChange( const Expr* op ); + }; + +} // namespace + +#endif // POSTFIXINCREMENTFIX_H + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/refassign.cxx b/compilerplugins/clang/store/refassign.cxx new file mode 100644 index 000000000..27471a6ae --- /dev/null +++ b/compilerplugins/clang/store/refassign.cxx @@ -0,0 +1,152 @@ +/* -*- 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/. + */ +#ifndef LO_CLANG_SHARED_PLUGINS + +#include +#include +#include +#include +#include + +#include + +#include "check.hxx" +#include "compat.hxx" +#include "plugin.hxx" + +/** + * Look for a mistake I made (a lot) at one point where we assign a reference to a reference var, which + * does not do at all what I thought. + */ + +namespace +{ +class RefAssign : public loplugin::FilteringPlugin +{ +public: + explicit RefAssign(loplugin::InstantiationData const& data) + : FilteringPlugin(data) + { + } + + virtual bool preRun() override + { + StringRef fn(handler.getMainFileName()); + if (loplugin::isSamePathname(fn, SRCDIR "/comphelper/source/misc/syntaxhighlight.cxx")) + return false; + if (loplugin::isSamePathname(fn, SRCDIR "/svl/source/numbers/zformat.cxx")) + return false; + if (loplugin::isSamePathname(fn, SRCDIR "/tools/source/memtools/multisel.cxx")) + return false; + if (loplugin::isSamePathname(fn, SRCDIR "/tools/source/generic/point.cxx")) + return false; + if (loplugin::isSamePathname(fn, SRCDIR "/vcl/source/control/edit.cxx")) + return false; + if (loplugin::isSamePathname(fn, SRCDIR "/vcl/source/control/fmtfield.cxx")) + return false; + if (loplugin::isSamePathname(fn, SRCDIR "/vcl/source/control/field.cxx")) + return false; + if (loplugin::isSamePathname(fn, SRCDIR "/vcl/source/control/field2.cxx")) + return false; + if (loplugin::isSamePathname(fn, SRCDIR "/vcl/source/edit/textview.cxx")) + return false; + if (loplugin::isSamePathname(fn, SRCDIR "/vcl/source/edit/vclmedit.cxx")) + return false; + if (loplugin::isSamePathname(fn, SRCDIR "/editeng/source/editeng/editdoc.cxx")) + return false; + if (loplugin::isSamePathname(fn, SRCDIR "/editeng/source/editeng/impedit2.cxx")) + return false; + if (loplugin::isSamePathname(fn, SRCDIR "/svx/source/dialog/svxruler.cxx")) + return false; + if (loplugin::isSamePathname(fn, SRCDIR "/sw/source/uibase/ribbar/inputwin.cxx")) + return false; + if (loplugin::isSamePathname(fn, SRCDIR "/sw/source/core/text/txtftn.cxx")) + return false; + + return true; + } + + virtual void run() override + { + if (preRun()) + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + } + + bool VisitBinaryOperator(BinaryOperator const*); + // causes crashes in CallExpr::getReturnType + bool TraverseFunctionTemplateDecl(FunctionTemplateDecl*) { return true; } + bool TraverseClassTemplateDecl(ClassTemplateDecl*) { return true; } + +private: + clang::QualType ExtractType(Expr const*); +}; + +bool RefAssign::VisitBinaryOperator(BinaryOperator const* binaryOp) +{ + if (ignoreLocation(binaryOp)) + return true; + if (binaryOp->getOpcode() != BO_Assign) + return true; + + // ignore assigning to/from an element of a collection + if (isa(binaryOp->getLHS()->IgnoreParenImpCasts())) + return true; + if (isa(binaryOp->getRHS()->IgnoreParenImpCasts())) + return true; + + // if we are assigning to a parameter we probably mean it + if (auto declRefExpr = dyn_cast(binaryOp->getLHS()->IgnoreParenImpCasts())) + if (declRefExpr->getDecl() && isa(declRefExpr->getDecl())) + return true; + + if (auto callExpr = dyn_cast(binaryOp->getRHS()->IgnoreParenImpCasts())) + if (auto functionDecl = dyn_cast_or_null(callExpr->getCalleeDecl())) + if (functionDecl->getIdentifier() + && (functionDecl->getName() == "min" || functionDecl->getName() == "max")) + return true; + + auto lhsType = ExtractType(binaryOp->getLHS()); + auto rhsType = ExtractType(binaryOp->getRHS()); + if (!loplugin::TypeCheck(lhsType).LvalueReference()) + return true; + if (!loplugin::TypeCheck(rhsType).LvalueReference()) + return true; + binaryOp->dump(); + report(DiagnosticsEngine::Warning, + "assigning a %0 to a var of type %1 probably does not do what you think", + compat::getBeginLoc(binaryOp)) + << rhsType << lhsType << binaryOp->getSourceRange(); + return true; +} + +clang::QualType RefAssign::ExtractType(Expr const* expr) +{ + expr = expr->IgnoreParenImpCasts(); + if (auto declReflExpr = dyn_cast(expr)) + { + if (auto varDecl = dyn_cast(declReflExpr->getDecl())) + return varDecl->getType(); + } + else if (auto callExpr = dyn_cast(expr)) + { + if (callExpr->isTypeDependent()) + return {}; + // callExpr->dump(); + return callExpr->getCallReturnType(compiler.getASTContext()); + } + return expr->getType(); +} + +loplugin::Plugin::Registration refassign("refassign"); + +} // namespace + +#endif // LO_CLANG_SHARED_PLUGINS + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/removeforwardstringdecl.cxx b/compilerplugins/clang/store/removeforwardstringdecl.cxx new file mode 100644 index 000000000..e7a546a8f --- /dev/null +++ b/compilerplugins/clang/store/removeforwardstringdecl.cxx @@ -0,0 +1,78 @@ +/* -*- 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 "removeforwardstringdecl.hxx" + +/* +This is a rewriter. + +Remove all forward declarations of rtl strings. I.e. 'namespace rtl { class OUString; }' etc. +*/ + +namespace loplugin +{ + +RemoveForwardStringDecl::RemoveForwardStringDecl( CompilerInstance& compiler, Rewriter& rewriter ) + : FilteringRewritePlugin( compiler, rewriter ) + { + } + +void RemoveForwardStringDecl::run() + { + TraverseDecl( compiler.getASTContext().getTranslationUnitDecl()); + } + +bool RemoveForwardStringDecl::VisitNamespaceDecl( const NamespaceDecl* declaration ) + { + if( ignoreLocation( declaration )) + return true; + if( declaration->getQualifiedNameAsString() != "rtl" ) + return true; + bool canRemove = true; + for( NamespaceDecl::decl_iterator it = declaration->decls_begin(); + it != declaration->decls_end(); + ++it ) + { + if( *it != NULL ) + { + if( !tryRemoveStringForwardDecl( *it )) + canRemove = false; + } + } + if( canRemove ) // contained only forward decls that we removed + removeText( declaration->getSourceRange(), RemoveLineIfEmpty ); + return true; + } + +bool RemoveForwardStringDecl::tryRemoveStringForwardDecl( const Decl* decl ) + { + const CXXRecordDecl* classdecl = dyn_cast< CXXRecordDecl >( decl ); + if( classdecl == NULL ) + return false; + if( !classdecl->isFreeStanding() || classdecl->isCompleteDefinition()) + return false; // not a simple forward declaration + if( classdecl->getName() == "OString" || classdecl->getName() == "OUString" + || classdecl->getName() == "OStringBuffer" || classdecl->getName() == "OUStringBuffer" + || classdecl->getName() == "OStringHash" || classdecl->getName() == "OUStringHash" + || classdecl->getName() == "OStringLiteral" || classdecl->getName() == "OUStringLiteral" ) + { + removeText( SourceRange( classdecl->getOuterLocStart(), classdecl->getLocEnd()), + RemoveLineIfEmpty | RemoveWholeStatement ); + return true; + } + return false; + } + +static Plugin::Registration< RemoveForwardStringDecl > X( "removeforwardstringdecl" ); + +} // namespace + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/removeforwardstringdecl.hxx b/compilerplugins/clang/store/removeforwardstringdecl.hxx new file mode 100644 index 000000000..ef2c3f967 --- /dev/null +++ b/compilerplugins/clang/store/removeforwardstringdecl.hxx @@ -0,0 +1,35 @@ +/* -*- 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. + * + */ + +#ifndef REMOVEFORWARDSTRINGDECL_H +#define REMOVEFORWARDSTRINGDECL_H + +#include "plugin.hxx" + +namespace loplugin +{ + +class RemoveForwardStringDecl + : public loplugin::FilteringRewritePlugin< RemoveForwardStringDecl > + { + public: + explicit RemoveForwardStringDecl( CompilerInstance& compiler, Rewriter& rewriter ); + virtual void run() override; + bool VisitNamespaceDecl( const NamespaceDecl* declaration ); + private: + bool tryRemoveStringForwardDecl( const Decl* decl ); + }; + +} // namespace + +#endif // REMOVEFORWARDSTRINGDECL_H + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/removevirtuals.cxx b/compilerplugins/clang/store/removevirtuals.cxx new file mode 100644 index 000000000..1dc98304d --- /dev/null +++ b/compilerplugins/clang/store/removevirtuals.cxx @@ -0,0 +1,150 @@ +/* -*- 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 +#include +#include +#include "plugin.hxx" +#include +#include +#include +#include +#include +#include +#include + +/** + This is intended to be run as the second stage of the "unnecessaryvirtuals" clang plugin. +*/ + +namespace { + +class RemoveVirtuals: + public loplugin::FilteringRewritePlugin +{ +public: + explicit RemoveVirtuals(InstantiationData const & data); + ~RemoveVirtuals(); + + virtual void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); } + + bool VisitCXXMethodDecl( const CXXMethodDecl* var ); +private: + // I use a brute-force approach - mmap the results file and do a linear search on it + // It works surprisingly well, because the file is small enough to fit into L2 cache on modern CPU's + size_t mmapFilesize; + int mmapFD; + char* mmappedData; +}; + +size_t getFilesize(const char* filename) +{ + struct stat st; + stat(filename, &st); + return st.st_size; +} + +RemoveVirtuals::RemoveVirtuals(InstantiationData const & data): FilteringRewritePlugin(data) +{ + static const char sInputFile[] = SRCDIR "/result.txt"; + mmapFilesize = getFilesize(sInputFile); + //Open file + mmapFD = open(sInputFile, O_RDONLY, 0); + assert(mmapFD != -1); + //Execute mmap + mmappedData = static_cast(mmap(NULL, mmapFilesize, PROT_READ, MAP_PRIVATE | MAP_POPULATE, mmapFD, 0)); + assert(mmappedData != NULL); +} + +RemoveVirtuals::~RemoveVirtuals() +{ + //Cleanup + int rc = munmap(mmappedData, mmapFilesize); + assert(rc == 0); + close(mmapFD); +} + +std::string niceName(const CXXMethodDecl* functionDecl) +{ + std::string s = + functionDecl->getParent()->getQualifiedNameAsString() + "::" + + functionDecl->getReturnType().getAsString() + "-" + + functionDecl->getNameAsString() + "("; + for (const ParmVarDecl *pParmVarDecl : functionDecl->params()) { + s += pParmVarDecl->getType().getAsString(); + s += ","; + } + s += ")"; + if (functionDecl->isConst()) { + s += "const"; + } + return s; +} + +bool RemoveVirtuals::VisitCXXMethodDecl( const CXXMethodDecl* functionDecl ) +{ + if (rewriter == nullptr) { + return true; + } + if (ignoreLocation(functionDecl)) { + return true; + } + // ignore stuff that forms part of the stable URE interface + if (isInUnoIncludeFile(functionDecl)) { + return true; + } + + // don't mess with templates + if (functionDecl->getParent()->getDescribedClassTemplate() != nullptr) { + return true; + } + if (functionDecl->getTemplatedKind() != FunctionDecl::TK_NonTemplate) { + return true; + } + + if (!functionDecl->isVirtualAsWritten()) { + return true; + } + std::string aNiceName = "\n" + niceName(functionDecl) + "\n"; + const char *aNiceNameStr = aNiceName.c_str(); + char* found = std::search(mmappedData, mmappedData + mmapFilesize, aNiceNameStr, aNiceNameStr + strlen(aNiceNameStr)); + if(!(found < mmappedData + mmapFilesize)) { + return true; + } + if (functionDecl->isPure()) { + if (!removeText(functionDecl->getSourceRange())) { + report( + DiagnosticsEngine::Warning, + "Could not remove unused pure virtual method", + functionDecl->getLocStart()) + << functionDecl->getSourceRange(); + } + } else { + std::string aOrigText = rewriter->getRewrittenText(functionDecl->getSourceRange()); + size_t iVirtualTokenIndex = aOrigText.find_first_of("virtual "); + if (iVirtualTokenIndex == std::string::npos) { + return true; + } + if (!replaceText(functionDecl->getSourceRange(), aOrigText.replace(iVirtualTokenIndex, strlen("virtual "), ""))) { + report( + DiagnosticsEngine::Warning, + "Could not remove virtual qualifier from method", + functionDecl->getLocStart()) + << functionDecl->getSourceRange(); + } + } + return true; +} + + +loplugin::Plugin::Registration< RemoveVirtuals > X("removevirtuals", false); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/returnbyref.cxx b/compilerplugins/clang/store/returnbyref.cxx new file mode 100644 index 000000000..d4049481a --- /dev/null +++ b/compilerplugins/clang/store/returnbyref.cxx @@ -0,0 +1,138 @@ +/* -*- 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 +#include + +#include "plugin.hxx" + +// Find places where we are returning a pointer to something, where we can be returning a reference. +// e.g. +// class A { +// struct X x; +// public: +// X* getX() { return &x; } +// } +// which can be: +// X& getX() { return x; } + +namespace { + +class ReturnByRef: + public loplugin::FilteringPlugin +{ +public: + explicit ReturnByRef(InstantiationData const & data): FilteringPlugin(data) {} + + virtual void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); } + + bool VisitCXXMethodDecl(const CXXMethodDecl * decl); +private: + std::string getFilename(SourceLocation loc); +}; + +bool ReturnByRef::VisitCXXMethodDecl(const CXXMethodDecl * functionDecl) { + if (ignoreLocation(functionDecl)) { + return true; + } + if (functionDecl->isVirtual()) { + return true; + } + if (!functionDecl->isInstance()) { + return true; + } + if (!functionDecl->hasBody()) { + return true; + } + // ignore stuff that forms part of the stable URE interface + if (isInUnoIncludeFile(functionDecl)) { + return true; + } + QualType t1 { functionDecl->getReturnType() }; + if (!t1->isPointerType()) { + return true; + } + // Ignore stuff like: + // operator vcl::Window *(){ return &m_rWindow; } + if (dyn_cast< CXXConversionDecl >( functionDecl ) != nullptr) { + return true; + } + + std::string aFilename = getFilename(functionDecl->getCanonicalDecl()->getLocStart()); + if (aFilename == SRCDIR "/include/o3tl/cow_wrapper.hxx") + { + return true; + } + if ( functionDecl->getNameAsString() == "operator->") { + return true; + } + std::string aFunctionName = functionDecl->getQualifiedNameAsString(); + if (aFunctionName == "SbxValue::data") { + return true; + } + /* + std::string aParentName = functionDecl->getParent()->getQualifiedNameAsString(); + std::string fqn = aParentName + "::" + functionDecl->getNameAsString(); + if (aFilename == "TextCharAttribList::GetAttrib") { + return true; + }*/ + + /* + The AST here looks like: + -CompoundStmt + `-ReturnStmt + `-UnaryOperator + */ + + const CompoundStmt* compoundStmt = dyn_cast< CompoundStmt >( functionDecl->getBody() ); + if (compoundStmt == nullptr || compoundStmt->body_begin() == compoundStmt->body_end()) { + return true; + } + const ReturnStmt* returnStmt = dyn_cast(*compoundStmt->child_begin()); + if (returnStmt == nullptr) { + return true; + } + + const Stmt* nextStmt = dyn_cast(*returnStmt->child_begin())->IgnoreParens(); + const UnaryOperator* unaryOperator = dyn_cast(nextStmt); + if (unaryOperator == nullptr || unaryOperator->getOpcode() != UO_AddrOf) { + return true; + } +nextStmt->dump(); + report( + DiagnosticsEngine::Warning, + "rather return by reference ", + functionDecl->getSourceRange().getBegin()) + << functionDecl->getSourceRange(); + + // display the location of the class member declaration so I don't have to search for it by hand + auto otherLoc = functionDecl->getCanonicalDecl()->getSourceRange().getBegin(); + if (otherLoc != functionDecl->getSourceRange().getBegin()) + { + report( + DiagnosticsEngine::Note, + "rather return by reference", + functionDecl->getCanonicalDecl()->getSourceRange().getBegin()) + << functionDecl->getCanonicalDecl()->getSourceRange(); + } + + return true; +} + +std::string ReturnByRef::getFilename(SourceLocation loc) +{ + SourceLocation spellingLocation = compiler.getSourceManager().getSpellingLoc(loc); + return getFilenameOfLocation(spellingLocation); +} + +loplugin::Plugin::Registration< ReturnByRef > X("returnbyref"); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/returnunique.cxx b/compilerplugins/clang/store/returnunique.cxx new file mode 100644 index 000000000..913c043a4 --- /dev/null +++ b/compilerplugins/clang/store/returnunique.cxx @@ -0,0 +1,83 @@ +/* -*- 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/. + */ + +// Find places where a std::unique_ptr is release()'ed and returned as a raw +// pointer. Some occurrences of that might better be rewritten to return the +// unique_ptr is returned directly. (But other occurrences might be fine the +// way they are, hence place this plugin into store/). + +#include +#include "plugin.hxx" + +namespace { + +class ReturnUnique: + public loplugin::FilteringPlugin +{ +public: + explicit ReturnUnique(InstantiationData const & data): FilteringPlugin(data) {} + + void run() override { + if (compiler.getLangOpts().CPlusPlus) { + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + } + } + + bool VisitReturnStmt(ReturnStmt const * stmt); +}; + +bool ReturnUnique::VisitReturnStmt(ReturnStmt const * stmt) { + if (ignoreLocation(stmt)) { + return true; + } + auto const e1 = stmt->getRetValue(); + if (e1 == nullptr) { + return true; + } + auto const e2 = dyn_cast(e1->IgnoreParenImpCasts()); + if (e2 == nullptr) { + return true; + } + auto const d1 = e2->getMethodDecl(); + if (d1 == nullptr) { // call via ptr to member + return true; + } + auto const d2 = d1->getParent(); + assert(d2 != nullptr); + assert(d2->getParent() != nullptr); + auto const d3 = dyn_cast(d2->getParent()); + if (d3 == nullptr + /* || dyn_cast(d3->getParent()) == nullptr */) + { + return true; + } + auto const id3 = d3->getIdentifier(); + if (id3 == nullptr /* || id3->getName() != "std" */) { + return true; + } + auto const id2 = d2->getIdentifier(); + if (id2 == nullptr || id2->getName() != "unique_ptr") { + return true; + } + auto const id1 = d1->getIdentifier(); + if (id1 == nullptr || id1->getName() != "release") { + return true; + } + report( + DiagnosticsEngine::Warning, "return std::unique_ptr::release", + e2->getLocStart()) + << stmt->getSourceRange(); + return true; +} + +loplugin::Plugin::Registration X("returnunique"); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/revisibility.cxx b/compilerplugins/clang/store/revisibility.cxx new file mode 100644 index 000000000..727d7094a --- /dev/null +++ b/compilerplugins/clang/store/revisibility.cxx @@ -0,0 +1,89 @@ +/* -*- 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 "plugin.hxx" + +// Find function declarations that redundantly re-specify a visibility attribute +// (via SAL_DLLPUBLIC_EXPORT etc.) that was already specified with a previous +// declaration of that function. But MSVC wants consistency of __declspec +// across friend declarations, so just ignore those for now. + +namespace { + +bool hasExplicitVisibilityAttr(Decl const * decl) { + VisibilityAttr const * attr = decl->getAttr(); + return attr != nullptr && !attr->isInherited(); +} + +bool isFriendDecl(Decl const * decl) { + return decl->getFriendObjectKind() != Decl::FOK_None; +} + +class ReVisibility: + public loplugin::FilteringPlugin +{ +public: + explicit ReVisibility(InstantiationData const & data): FilteringPlugin(data) {} + + void run() override + { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); } + + bool VisitFunctionDecl(FunctionDecl const * decl); +}; + +bool ReVisibility::VisitFunctionDecl(FunctionDecl const * decl) { + if (!ignoreLocation(decl) && hasExplicitVisibilityAttr(decl) + && !isFriendDecl(decl)) + { + Decl const * first = nullptr; + for (Decl const * p = decl;;) { + p = p->getPreviousDecl(); + if (p == nullptr) { + break; + } + first = p; + if (hasExplicitVisibilityAttr(p) && !isFriendDecl(p)) { + report( + DiagnosticsEngine::Warning, + "Redundant visibility re-declaration", + decl->getAttr()->getLocation()) + << decl->getAttr()->getRange(); + report( + DiagnosticsEngine::Note, + "Previous visibility declaration is here", + p->getAttr()->getLocation()) + << p->getAttr()->getRange(); + return true; + } + } + if (decl->isThisDeclarationADefinition() && first != nullptr + && !(getFilenameOfLocation( + compiler.getSourceManager().getSpellingLoc( + decl->getLocation())) + .startswith(SRCDIR "/libreofficekit/"))) + { + report( + DiagnosticsEngine::Warning, + "Visibility declaration on definition, not first declaration", + decl->getAttr()->getLocation()) + << decl->getAttr()->getRange(); + report( + DiagnosticsEngine::Note, "First declaration is here", + first->getLocation()) + << first->getSourceRange(); + } + } + return true; +} + +loplugin::Plugin::Registration X("revisibility"); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/rtlconstasciimacro.cxx b/compilerplugins/clang/store/rtlconstasciimacro.cxx new file mode 100644 index 000000000..66e202ce2 --- /dev/null +++ b/compilerplugins/clang/store/rtlconstasciimacro.cxx @@ -0,0 +1,144 @@ +/* -*- 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. + * + */ + +/* +This is a rewriter. + +Remove uses of the macro RTL_CONSTASCII_USTRINGPARAM. One run is for one +specific use (see below), modify source to remove other uses. +*/ + +#include "plugin.hxx" + +#include + +namespace loplugin +{ + +class RtlConstAsciiMacro + : public loplugin::FilteringRewritePlugin< RtlConstAsciiMacro > + , public PPCallbacks + { + public: + explicit RtlConstAsciiMacro( const InstantiationData& data ); + virtual void run() override; + bool VisitCXXConstructExpr( CXXConstructExpr* expr ); + bool VisitCXXTemporaryObjectExpr( CXXTemporaryObjectExpr* expr ); + bool VisitStringLiteral( const StringLiteral* literal ); + virtual void MacroExpands( const Token& macro, const MacroDirective* directive, + SourceRange range, const MacroArgs* args ) override; + enum { isPPCallback = true }; + private: + map< SourceLocation, SourceLocation > expansions; // start location -> end location + bool searchingForString; + bool suitableString; + }; + +RtlConstAsciiMacro::RtlConstAsciiMacro( const InstantiationData& data ) + : FilteringRewritePlugin( data ) + , searchingForString( false ) + { + compiler.getPreprocessor().addPPCallbacks( this ); + } + +void RtlConstAsciiMacro::run() + { + TraverseDecl( compiler.getASTContext().getTranslationUnitDecl()); + } + +void RtlConstAsciiMacro::MacroExpands( const Token& macro, const MacroDirective*, + SourceRange range, const MacroArgs* ) + { + if( macro.getIdentifierInfo()->getName() != "RTL_CONSTASCII_USTRINGPARAM" ) + return; + expansions[ range.getBegin() ] = range.getEnd(); + } + +/* Remove use with the following ctor: + OUString( const sal_Char * value, sal_Int32 length, + rtl_TextEncoding encoding, + sal_uInt32 convertFlags = OSTRING_TO_OUSTRING_CVTFLAGS ) + This means searching for CXXConstructExpr. + For removal when used with functions it should check e.g. for CallExpr. +*/ +bool RtlConstAsciiMacro::VisitCXXConstructExpr( CXXConstructExpr* expr ) + { + if( ignoreLocation( expr )) + return true; + if( expr->getNumArgs() != 4 ) + return true; + // The last argument should be the default one when the macro is used. + if( dyn_cast< CXXDefaultArgExpr >( expr->getArg( 3 )) == NULL ) + return true; + if( expr->getConstructor()->getQualifiedNameAsString() != "rtl::OUString::OUString" ) + return true; + const SourceManager& src = compiler.getSourceManager(); + SourceLocation start = src.getExpansionLoc( expr->getArg( 0 )->getLocStart()); + // Macro fills in the first 3 arguments, so they must all come from the same expansion. + if( start != src.getExpansionLoc( expr->getArg( 2 )->getLocEnd())) + return true; + if( expansions.find( start ) == expansions.end()) + return true; + SourceLocation end = expansions[ start ]; + // Remove the location, since sometimes the same code may be processed more than once + // (e.g. non-trivial default arguments). + expansions.erase( start ); + // Check if the string argument to the macro is suitable. + searchingForString = true; + suitableString = false; + TraverseStmt( expr->getArg( 0 )); + searchingForString = false; + if( !suitableString ) + return true; + // Search for '(' (don't just remove a given length to handle possible whitespace). + const char* text = compiler.getSourceManager().getCharacterData( start ); + const char* pos = text; + while( *pos != '(' ) + ++pos; + ++pos; + if( text[ -1 ] == ' ' && *pos == ' ' ) + ++pos; // do not leave two spaces + removeText( start, pos - text, RemoveLineIfEmpty ); + const char* textend = compiler.getSourceManager().getCharacterData( end ); + if( textend[ -1 ] == ' ' && textend[ 1 ] == ' ' ) + removeText( end, 2, RemoveLineIfEmpty ); // Remove ') '. + else + removeText( end, 1, RemoveLineIfEmpty ); // Remove ')'. + return true; + } + +bool RtlConstAsciiMacro::VisitCXXTemporaryObjectExpr( CXXTemporaryObjectExpr* expr ) + { + return VisitCXXConstructExpr( expr ); + } + +bool RtlConstAsciiMacro::VisitStringLiteral( const StringLiteral* literal ) + { + if( !searchingForString ) + return true; + if( suitableString ) // two string literals? + { + report( DiagnosticsEngine::Warning, "cannot analyze RTL_CONSTASCII_USTRINGPARAM (plugin needs fixing)" ) + << literal->getSourceRange(); + return true; + } + if( !literal->isAscii()) // ignore + return true; + if( !literal->containsNonAsciiOrNull()) + suitableString = true; + return true; + } + +static Plugin::Registration< RtlConstAsciiMacro > X( "rtlconstasciimacro" ); + +} // namespace + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/sfxitemsetrewrite.cxx b/compilerplugins/clang/store/sfxitemsetrewrite.cxx new file mode 100644 index 000000000..e1fa52209 --- /dev/null +++ b/compilerplugins/clang/store/sfxitemsetrewrite.cxx @@ -0,0 +1,419 @@ +/* -*- 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 +#include +#include +#include + +#include "check.hxx" +#include "plugin.hxx" + +namespace { + +bool gap(APSInt n1, APSInt n2) { return n1 < n2 && n2 - n1 > 1; } + +class Visitor final: + public loplugin::FilteringRewritePlugin +{ +public: + explicit Visitor(InstantiationData const & data): FilteringRewritePlugin(data) {} + + bool VisitCXXConstructExpr(CXXConstructExpr const * expr) { + if (ignoreLocation(expr)) { + return true; + } + auto const ctor = expr->getConstructor(); + if (!loplugin::DeclCheck(ctor->getParent()).Class("SfxItemSet") + .GlobalNamespace()) + { + return true; + } + auto const numParams = ctor->getNumParams(); + auto const variadic = ctor->isVariadic(); + if (!(((numParams == 3 && !variadic) || (numParams == 4 && variadic)) + && (loplugin::TypeCheck(ctor->getParamDecl(0)->getType()) + .LvalueReference().Class("SfxItemPool").GlobalNamespace()))) + { + return true; + } + auto const numArgs = expr->getNumArgs(); + if (numArgs < 3) { + report( + DiagnosticsEngine::Warning, + ("unexpected SfxItemPool constructor call with less than three" + " arguments"), + expr->getExprLoc()) + << expr->getSourceRange(); + return true; + } + rewrite(expr, variadic, false, numArgs, expr->getArgs()); + return true; + } + + bool VisitCallExpr(CallExpr const * expr) { + if (ignoreLocation(expr)) { + return true; + } + auto const dre = dyn_cast( + expr->getCallee()->IgnoreParenImpCasts()); + if (dre == nullptr + || !(loplugin::DeclCheck(dre->getDecl()).Function("make_unique") + .Namespace("o3tl").GlobalNamespace())) + { + return true; + } + auto const numTArgs = dre->getNumTemplateArgs(); + if (numTArgs == 0) { + report( + DiagnosticsEngine::Warning, + "unexpected o3tl::make_unique call without template arguments", + expr->getExprLoc()) + << expr->getSourceRange(); + return true; + } + auto const tArg0 = dre->getTemplateArgs()[0].getArgument(); + if (tArg0.getKind() != TemplateArgument::Type) { + report( + DiagnosticsEngine::Warning, + ("unexpected o3tl::make_unique call with non-type first" + " template argument"), + expr->getExprLoc()) + << expr->getSourceRange(); + return true; + } + if (!loplugin::TypeCheck(tArg0.getAsType()).Class("SfxItemSet") + .GlobalNamespace()) + { + return true; + } + auto const numArgs = expr->getNumArgs(); + if (numArgs < 3) { + return true; + } + rewrite(expr, numArgs > 3, true, numArgs, expr->getArgs()); + return true; + } + + bool VisitTemplateSpecializationTypeLoc( + TemplateSpecializationTypeLoc typeLoc) + { + auto const loc = typeLoc.getBeginLoc(); + if (loc.isInvalid() || ignoreLocation(loc)) { + return true; + } + if (!loplugin::TypeCheck(typeLoc.getType()).Struct("Items") + .Namespace("svl").GlobalNamespace()) + { + return true; + } + unsigned const numArgs = typeLoc.getNumArgs(); + if (numArgs == 0) { + report( + DiagnosticsEngine::Warning, + ("unexpected svl::Items specialization with zero template" + " arguments"), + loc) + << typeLoc.getSourceRange(); + return true; + } + if (numArgs % 2 == 1) { + report( + DiagnosticsEngine::Warning, + ("unexpected svl::Items specialization with odd number of" + " template arguments"), + loc) + << typeLoc.getSourceRange(); + return true; + } + std::vector ranges; + auto good = true; + APSInt prev; + for (unsigned i = 0; i != numArgs; ++i) { + auto const argLoc = typeLoc.getArgLoc(i); + auto const & arg = argLoc.getArgument(); + APSInt v; + switch (arg.getKind()) { + case TemplateArgument::Integral: + v = arg.getAsIntegral(); + break; + case TemplateArgument::Expression: + if (arg.getAsExpr()->EvaluateAsInt(v, compiler.getASTContext())) + { + break; + } + // [[fallthrough]]; + default: + report( + DiagnosticsEngine::Warning, + ("unexpected svl::Items specialization with non-integral" + " template argument %0"), + argLoc.getLocation()) + << (i + 1) + << typeLoc.getSourceRange(); + return true; + } + if (i % 2 == 0) { + good = good && (i == 0 || gap(prev, v)); + } else { + if (v < prev) { + report( + DiagnosticsEngine::Warning, + ("unexpected svl::Items specialization with template" + " argument %0 smaller than previous one, %1 < %2"), + argLoc.getLocation()) + << (i + 1) << v.toString(10) << prev.toString(10) + << typeLoc.getSourceRange(); + return true; + } + ranges.emplace_back(prev, v, (i / 2) + 1); + } + prev = v; + } + if (good) { + return true; + } + std::ostringstream buf1; + for (auto const i: ranges) { + buf1 << "\n "; + printBegin(buf1, typeLoc, i); + buf1 << " ... "; + printEnd(buf1, typeLoc, i); + } + std::sort(ranges.begin(), ranges.end()); + std::ostringstream buf2; + for (auto i = ranges.begin(); i != ranges.end();) { + buf2 << "\n "; + printBegin(buf2, typeLoc, *i); + buf2 << " ... "; + auto end = *i; + for (;;) { + auto j = i + 1; + if (j == ranges.end() || gap(get<1>(end), get<0>(*j))) { + printEnd(buf2, typeLoc, end); + i = j; + break; + } + if (get<1>(*j) >= get<1>(end)) { + end = *j; + } + i = j; + } + } + report( + DiagnosticsEngine::Warning, + ("reorder svl::Items specialization template arguments from:%0\nto:" + "%1"), + loc) + << buf1.str() << buf2.str() << typeLoc.getSourceRange(); + return true; + } + +private: + void run() override { + if (compiler.getLangOpts().CPlusPlus) { + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + } + } + + SourceLocation nextToken(SourceLocation loc) { + return loc.getLocWithOffset( + Lexer::MeasureTokenLength( + loc, compiler.getSourceManager(), compiler.getLangOpts())); + } + + SourceLocation atMacroExpansionStart(SourceLocation loc) { + while (loc.isMacroID() + && (compiler.getSourceManager() + .isAtStartOfImmediateMacroExpansion(loc, &loc))) + {} + return loc; + } + + SourceLocation atMacroExpansionEnd(SourceLocation loc) { + while (compiler.getSourceManager().isMacroBodyExpansion(loc)) { + loc = compiler.getSourceManager().getImmediateExpansionRange(loc) + .second; + } + return loc; + } + + using Range = std::tuple; + + void printSource( + std::ostringstream & s, TemplateSpecializationTypeLoc typeLoc, Range r, + bool end) + { + auto const argLoc = typeLoc.getArgLoc( + 2 * (get<2>(r) - 1) + (end ? 1 : 0)); + auto const src1 = argLoc.getSourceRange(); + auto const src2 = SourceRange( + atMacroExpansionStart(src1.getBegin()), + Lexer::getLocForEndOfToken( + compiler.getSourceManager().getExpansionLoc( + atMacroExpansionEnd(src1.getEnd())), + 0, compiler.getSourceManager(), compiler.getLangOpts())); + s << " '" << Lexer::getSourceText( + Lexer::getAsCharRange( + src2, compiler.getSourceManager(), compiler.getLangOpts()), + compiler.getSourceManager(), compiler.getLangOpts()).str() + << "'"; + } + + void printBegin( + std::ostringstream & s, TemplateSpecializationTypeLoc typeLoc, Range r) + { + s << get<2>(r) << "B: " << get<0>(r).toString(10); + printSource(s, typeLoc, r, false); + } + + void printEnd( + std::ostringstream & s, TemplateSpecializationTypeLoc typeLoc, Range r) + { + s << get<2>(r) << "E: " << get<1>(r).toString(10); + printSource(s, typeLoc, r, true); + } + + void rewrite( + Expr const * expr, bool variadic, bool forward, unsigned numArgs, + Expr const * const * args) + { + bool constant = true; + unsigned firstZero = 0; + for (unsigned i = 1; i != numArgs; ++i) { + auto const arg = args[i]; + constant = constant + && arg->isCXX11ConstantExpr(compiler.getASTContext()); + APSInt v; + auto const zero + = ((arg->EvaluateAsInt(v, compiler.getASTContext()) + && v == 0) + || (variadic && i > 4 + && arg->isNullPointerConstant( + compiler.getASTContext(), + Expr::NPC_ValueDependentIsNotNull))); + if (variadic) { + if (zero) { + if (firstZero == 0) { + if (i == 1) { + report( + DiagnosticsEngine::Warning, + ("unexpected missing non-zero arguments before" + " first zero argument in SfxItemPool" + " constructor call"), + arg->getExprLoc()) + << expr->getSourceRange(); + return; + } + if (i % 2 == 0) { + report( + DiagnosticsEngine::Warning, + ("unexpected odd number of potentially non-zero" + " arguments before first definitely zero" + " argument in SfxItemPool constructor call"), + arg->getExprLoc()) + << expr->getSourceRange(); + return; + } + firstZero = i; + } + } else if (firstZero != 0) { + report( + DiagnosticsEngine::Warning, + ("unexpected potentially non-zero argument in" + " SfxItemPool constructor call, following zero" + " argument"), + arg->getExprLoc()) + << expr->getSourceRange(); + return; + } + } else if (zero) { + report( + DiagnosticsEngine::Warning, + "unexpected zero argument in SfxItemPool constructor call", + arg->getExprLoc()) + << expr->getSourceRange(); + return; + } + } + if (variadic && firstZero == 0) { + report( + DiagnosticsEngine::Warning, + ("unexpected SfxItemPool constructor call with no detectable" + " zero arguments"), + expr->getExprLoc()) + << expr->getSourceRange(); + return; + } + if (rewriter != nullptr) { + if (!insertTextBefore( + atMacroExpansionStart(args[1]->getLocStart()), + (constant + ? StringRef("svl::Items<") + : (forward + ? StringRef("std::initializer_list{{") + : StringRef("{{"))))) + { + goto failed; + } + auto const postLoc = atMacroExpansionEnd( + args[numArgs - 1]->getLocEnd()); + auto const postStr = constant ? StringRef(">{}") : StringRef("}}"); + if (variadic) { + //TODO: the replaced range can contain relevant comments: + if (!replaceText( + SourceRange( + nextToken( + atMacroExpansionEnd( + args[firstZero - 1]->getLocEnd())), + postLoc), + postStr)) + { + goto failed; + } + } else { + if (!insertTextAfterToken(postLoc, postStr)) { + goto failed; + } + } + if (!constant && variadic) { + for (unsigned i = 2; i != firstZero - 1; ++i) { + auto const arg = args[i]; + if (!(i % 2 == 0 + ? insertTextAfterToken( + atMacroExpansionEnd(arg->getLocEnd()), "}") + : insertTextBefore( + atMacroExpansionStart(arg->getLocStart()), "{"))) + { + goto failed; + } + } + } + return; + } + failed: //TODO: undo partial changes + report( + DiagnosticsEngine::Warning, + ("rewrite SfxItemPool constructor call with" + " %select{%select{|std::initializer_list}1" + "%{%{..., ...%}, ..., %{..., ...%}%}|svl::Items<...>%{%}}0 around" + " the %select{|leading }2%3 WID arguments%select{| and remove the" + " remaining %4 zero %plural{1:argument|:arguments}4}2"), + expr->getExprLoc()) + << constant << forward << variadic + << (variadic ? firstZero - 1 : numArgs - 1) << (numArgs - firstZero) + << expr->getSourceRange(); + } +}; + +static loplugin::Plugin::Registration reg("sfxitemsetrewrite",true); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/compilerplugins/clang/store/stdexception.cxx b/compilerplugins/clang/store/stdexception.cxx new file mode 100644 index 000000000..47a7d5791 --- /dev/null +++ b/compilerplugins/clang/store/stdexception.cxx @@ -0,0 +1,188 @@ +/* -*- 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 +#include +#include +#include + +#include "plugin.hxx" + +namespace { + +bool isStdException(QualType type) { + //TODO: + std::string name { type.getAsString() }; + return name == "std::exception" || name == "::std::exception"; +} + +class StdException: + public loplugin::FilteringRewritePlugin +{ +public: + explicit StdException(InstantiationData const & data): FilteringRewritePlugin(data) + {} + + virtual void run() override + { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); } + + bool VisitCXXMethodDecl(CXXMethodDecl const * decl); +}; + +bool StdException::VisitCXXMethodDecl(CXXMethodDecl const * decl) { + if (ignoreLocation(decl) + || decl->begin_overridden_methods() == decl->end_overridden_methods()) + { + return true; + } + CXXMethodDecl const * over = nullptr; + for (auto i = decl->begin_overridden_methods(); + i != decl->end_overridden_methods(); ++i) + { + FunctionProtoType const * t + = (*i)->getType()->getAs(); + switch (t->getExceptionSpecType()) { + case EST_None: + continue; + case EST_DynamicNone: + case EST_BasicNoexcept: + return true; + case EST_Dynamic: + { + unsigned n = t->getNumExceptions(); + for (unsigned j = 0; j != n; ++j) { + if (isStdException(t->getExceptionType(j))) { + over = *i; + goto found; + } + } + return true; + } + case EST_ComputedNoexcept: + switch (t->getNoexceptSpec(compiler.getASTContext())) { + case FunctionProtoType::NR_NoNoexcept: + case FunctionProtoType::NR_BadNoexcept: + assert(false); + // fall through + case FunctionProtoType::NR_Dependent: + break; + case FunctionProtoType::NR_Throw: + continue; + case FunctionProtoType::NR_Nothrow: + return true; + } + case EST_MSAny: + case EST_Unevaluated: + case EST_Uninstantiated: + continue; //TODO??? + } + } + return true; +found: + FunctionProtoType const * t = decl->getType()->getAs(); + if (!t->hasDynamicExceptionSpec()) { + report( + DiagnosticsEngine::Warning, + "override does not have dynamic exception specification", + decl->getLocStart()) + << decl->getSourceRange(); + report( + DiagnosticsEngine::Note, + ("overridden declaration with dynamic exception specification" + " including std::exception is here"), + over->getLocStart()); + return true; + } + unsigned n = t->getNumExceptions(); + for (unsigned i = 0; i != n; ++i) { + if (isStdException(t->getExceptionType(i))) { + return true; + } + } + SourceRange r { decl->getSourceRange() }; + SourceLocation l { + compiler.getSourceManager().getExpansionLoc(r.getBegin()) }; + SourceLocation end { + compiler.getSourceManager().getExpansionLoc(r.getEnd()) }; + assert( + l == end + || compiler.getSourceManager().isBeforeInTranslationUnit(l, end)); + bool seenThrow = false; + unsigned parens = 0; + SourceLocation openParen; + SourceLocation loc; + for (;;) { + unsigned n = Lexer::MeasureTokenLength( + l, compiler.getSourceManager(), compiler.getLangOpts()); + std::string s { compiler.getSourceManager().getCharacterData(l), n }; + if (s == "{" || s == ";") { + break; + } + if (!seenThrow) { + if (s == "throw") { + seenThrow = true; + } + } else if (s == "(") { + assert(parens < std::numeric_limits::max()); + ++parens; + if (parens == 1) { + openParen = l; + } + loc = l; + } else if (s == ")") { + assert(parens != 0); + --parens; + if (parens == 0) { + assert(loc.isValid()); + // Only rewrite declarations in include files if a definition is + // also seen, to avoid compilation of a definition (in a main + // file only processed later) to fail with a "mismatch" error + // before the rewriter had a chance to act upon the definition + // (but use the heuristic of assuming pure virtual functions do + // not have definitions): + if (rewriter != nullptr + && (compiler.getSourceManager().isInMainFile( + compiler.getSourceManager().getSpellingLoc(loc)) + || decl->isDefined() || decl->isPure()) + && insertTextAfterToken( + loc, + (loc == openParen + ? "std::exception" : ", std::exception"))) + { + return true; + } + break; + } + loc = l; + } else if (!s.empty() && s.compare(0, 2, "/*") != 0 + && s.compare(0, 2, "//") != 0) + { + loc = l; + } + if (l == end) { + break; + } + l = l.getLocWithOffset(std::max(n, 1)); + } + report( + DiagnosticsEngine::Warning, + "override dropped std::exception from dynamic exception specification", + openParen.isValid() ? openParen : decl->getLocStart()) + << decl->getSourceRange(); + report( + DiagnosticsEngine::Note, "overridden declaration is here", + over->getLocStart()); + return true; +} + +loplugin::Plugin::Registration X("stdexception", true); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/stylepolice.cxx b/compilerplugins/clang/store/stylepolice.cxx new file mode 100644 index 000000000..d3b2e8a44 --- /dev/null +++ b/compilerplugins/clang/store/stylepolice.cxx @@ -0,0 +1,196 @@ +/* -*- 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 +#include +#include +#include + +#include "plugin.hxx" + +// Check for some basic naming mismatches which make the code harder to read +// +// This plugin is deliberately fairly lax, and only targets the most egregeriously faulty code, +// since we have a broad range of styles in our code and we don't want to generate unnecessary +// churn. + +namespace { + +class StylePolice : + public loplugin::FilteringPlugin +{ +public: + explicit StylePolice(InstantiationData const & data): FilteringPlugin(data) {} + + virtual void run() override { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); } + + bool VisitVarDecl(const VarDecl *); +private: + StringRef getFilename(SourceLocation loc); +}; + +StringRef StylePolice::getFilename(SourceLocation loc) +{ + SourceLocation spellingLocation = compiler.getSourceManager().getSpellingLoc(loc); + StringRef name { getFilenameOfLocation(spellingLocation) }; + return name; +} + +bool startswith(const std::string& rStr, const char* pSubStr) { + return rStr.compare(0, strlen(pSubStr), pSubStr) == 0; +} +bool isUpperLetter(char c) { + return c >= 'A' && c <= 'Z'; +} +bool isLowerLetter(char c) { + return c >= 'a' && c <= 'z'; +} +bool isIdentifierLetter(char c) { + return isUpperLetter(c) || isLowerLetter(c); +} +bool matchPointerVar(const std::string& s) { + return s.size() > 2 && s[0] == 'p' && isUpperLetter(s[1]); +} +bool matchRefCountedPointerVar(const std::string& s) { + return s.size() > 2 && s[0] == 'x' && isUpperLetter(s[1]); +} +bool matchMember(const std::string& s) { + return s.size() > 3 && s[0] == 'm' + && ( ( strchr("abnprsx", s[1]) && isUpperLetter(s[2]) ) + || ( s[1] == '_' && isIdentifierLetter(s[2]) ) ); +} + +bool StylePolice::VisitVarDecl(const VarDecl * varDecl) +{ + if (ignoreLocation(varDecl)) { + return true; + } + StringRef aFileName = getFilename(varDecl->getLocStart()); + std::string name = varDecl->getName(); + + if (!varDecl->isLocalVarDecl()) { + return true; + } + + if (matchMember(name)) + { + // these names appear to be taken from some scientific paper + if (aFileName == SRCDIR "/scaddins/source/analysis/bessel.cxx" ) { + } + // lots of places where we are storing a "method id" here + else if (aFileName.startswith(SRCDIR "/connectivity/source/drivers/jdbc") && name.compare(0,3,"mID") == 0) { + } + else { + report( + DiagnosticsEngine::Warning, + "this local variable follows our member field naming convention, which is confusing", + varDecl->getLocation()) + << varDecl->getType() << varDecl->getSourceRange(); + } + } + + QualType qt = varDecl->getType().getDesugaredType(compiler.getASTContext()).getCanonicalType(); + qt = qt.getNonReferenceType(); + std::string typeName = qt.getAsString(); + if (startswith(typeName, "const ")) + typeName = typeName.substr(6); + if (startswith(typeName, "class ")) + typeName = typeName.substr(6); + if (startswith(typeName, "struct ")) + typeName = typeName.substr(7); + std::string aOriginalTypeName = varDecl->getType().getAsString(); + if (startswith(aOriginalTypeName, "const ")) + aOriginalTypeName = aOriginalTypeName.substr(6); + + if (!qt->isPointerType() && !qt->isArrayType() && !qt->isFunctionPointerType() && !qt->isMemberPointerType() + && matchPointerVar(name) + && !startswith(typeName, "boost::intrusive_ptr") + && !startswith(typeName, "std::optional") + && !startswith(typeName, "boost::shared_ptr") + && !startswith(typeName, "com::sun::star::uno::Reference") + && !startswith(typeName, "cppu::OInterfaceIteratorHelper") + && !startswith(typeName, "formula::FormulaCompiler::CurrentFactor") + && aOriginalTypeName != "GLXPixmap" + && !startswith(typeName, "rtl::Reference") + && !startswith(typeName, "ScopedVclPtr") + && typeName.find("::mem_fun") == std::string::npos + && typeName.find("shared_ptr") == std::string::npos + && typeName.find("unique_ptr") == std::string::npos + && typeName.find("::weak_ptr") == std::string::npos + && !startswith(typeName, "_LOKDocViewPrivate") + && !startswith(typeName, "sw::UnoCursorPointer") + && !startswith(typeName, "tools::SvRef") + && !startswith(typeName, "VclPtr") + && !startswith(typeName, "vcl::ScopedBitmapAccess") + // lots of the code seems to regard iterator objects as being "pointer-like" + && typeName.find("iterator<") == std::string::npos + && typeName.find("iter<") == std::string::npos + // libc++ std::__1::__wrap_iter<...> + && aOriginalTypeName != "sal_IntPtr" ) + { + if (aFileName.startswith(SRCDIR "/bridges/") ) { + } else if (aFileName.startswith(SRCDIR "/vcl/source/fontsubset/sft.cxx") ) { + } else { + report( + DiagnosticsEngine::Warning, + "this local variable of type '%0' follows our pointer naming convention, but it is not a pointer, %1", + varDecl->getLocation()) + << typeName << aOriginalTypeName << varDecl->getSourceRange(); + } + } + + + if (matchRefCountedPointerVar(name) + && !startswith(typeName, "boost::intrusive_ptr") + && !startswith(typeName, "com::sun::star::uno::Reference") + && !startswith(typeName, "com::sun::star::uno::Sequence") + && !startswith(typeName, "com::sun::star::uno::WeakReference") + && !startswith(typeName, "drawinglayer::primitive2d::Primitive2DContainer") + && !startswith(typeName, "drawinglayer::primitive3d::Primitive3DContainer") + && !startswith(typeName, "jfw::CXPathObjectPtr") + && !startswith(typeName, "_LOKDocViewPrivate") + && !startswith(typeName, "oox::dump::BinaryInputStreamRef") + && !startswith(typeName, "oox::drawingml::chart::ModelRef") + && !startswith(typeName, "rtl::Reference") + && !startswith(typeName, "Reference") + && !startswith(typeName, "SfxObjectShellLock") + && !startswith(typeName, "store::PageHolderObject") + && !startswith(typeName, "store::ResourceHolder") + && !startswith(typeName, "store::OStoreHandle") + && typeName.find("unique_ptr") == std::string::npos + && typeName.find("shared_ptr") == std::string::npos + && !startswith(typeName, "ScopedVclPtr") + && !startswith(typeName, "svt::EmbeddedObjectRef") + && !startswith(typeName, "tools::SvRef") + && !startswith(typeName, "tools::WeakReference") + && !startswith(typeName, "utl::SharedUNOComponent") + && !startswith(typeName, "VclPtr") + && !startswith(typeName, "vcl::DeleteOnDeinit") + && !startswith(typeName, "vcl::DeleteUnoReferenceOnDeinit") + // there are lots of coordinate/position vars that start with "x" + && !qt->isArithmeticType() + && !startswith(typeName, "float [") + ) + { + report( + DiagnosticsEngine::Warning, + "this local variable of type '%0' follows our ref-counted-pointer naming convention, but it is not a ref-counted-pointer, %1", + varDecl->getLocation()) + << typeName << aOriginalTypeName << varDecl->getSourceRange(); + } + + + return true; +} + +loplugin::Plugin::Registration< StylePolice > X("stylepolice"); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/svstreamoutputoperators.cxx b/compilerplugins/clang/store/svstreamoutputoperators.cxx new file mode 100644 index 000000000..6e0ff6168 --- /dev/null +++ b/compilerplugins/clang/store/svstreamoutputoperators.cxx @@ -0,0 +1,223 @@ +/* -*- 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. + * + */ + +/* +This is a rewriter. + +It changes the SvStream operator<< calls into calls to more explicitly named +methods, which reduces the casting needed, and makes it less likely that we +will accidentally write data to a file using the wrong data-type-size. + +TODO we don't currently cope with macro expansion e.g. if the constant on the RHS is a #define + +TODO we don't currently cope with code like "(*this) << 1;" + +TODO we don't currently cope with code like "aStream << x << endl;" the "endl" parts ends up dangling. + +TODO we don't currently cope with custom overloads of operator<< in some of the use-sites. +*/ + +#include "plugin.hxx" +#include +#include + +namespace loplugin +{ + +class SvStreamOutputOperators + : public loplugin::FilteringRewritePlugin< SvStreamOutputOperators > +{ + public: + explicit SvStreamOutputOperators( InstantiationData const & data ); + virtual void run() override; + bool VisitCallExpr( const CallExpr* call ); + private: + SourceLocation after(const SourceLocation& loc); +}; + +SvStreamOutputOperators::SvStreamOutputOperators( InstantiationData const & data ) + : FilteringRewritePlugin( data ) +{ +} + +void SvStreamOutputOperators::run() +{ + TraverseDecl( compiler.getASTContext().getTranslationUnitDecl()); +} + +bool SvStreamOutputOperators::VisitCallExpr( const CallExpr* callExpr ) +{ + if( ignoreLocation( callExpr )) + return true; + if( callExpr->getNumArgs() < 2 ) + return true; + const FunctionDecl* func = dyn_cast_or_null< FunctionDecl >( callExpr->getCalleeDecl() ); + if ( func == NULL ) + return true; + if( func->getNumParams() != 1 ) + return true; + string qualifiedName = func->getQualifiedNameAsString(); + bool bOutputOperator; + if( qualifiedName == "SvStream::operator<<" ) + bOutputOperator = true; + else if( qualifiedName == "SvStream::operator>>" ) + bOutputOperator = false; + else + return true; + + string arg0 = func->getParamDecl( 0 )->getType().getAsString(); + string newIOMethod; + if (bOutputOperator) + { + if( arg0 == "sal_uInt16" ) + newIOMethod = "WriteUInt16"; + else if( arg0 == "sal_uInt32" ) + newIOMethod = "WriteUInt32"; + else if( arg0 == "sal_uInt64" ) + newIOMethod = "WriteUInt64"; + else if( arg0 == "sal_Int16" ) + newIOMethod = "WriteInt16"; + else if( arg0 == "sal_Int32" ) + newIOMethod = "WriteInt32"; + else if( arg0 == "sal_Int64" ) + newIOMethod = "WriteInt64"; + else if( arg0 == "sal_uInt8" ) + newIOMethod = "WriteUInt8"; + else if( arg0 == "sal_Unicode" ) + newIOMethod = "WriteUnicode"; + else if( arg0 == "rtl::OString" ) + newIOMethod = "WriteOString"; + else if( arg0 == "bool" ) + newIOMethod = "WriteBool"; + else if( arg0 == "signed char" ) + newIOMethod = "WriteSChar"; + else if( arg0 == "char" ) + newIOMethod = "WriteChar"; + else if( arg0 == "unsigned char" ) + newIOMethod = "WriteUChar"; + else if( arg0 == "float" ) + newIOMethod = "WriteFloat"; + else if( arg0 == "double" ) + newIOMethod = "WriteDouble"; + else if( arg0 == "const double &" ) + newIOMethod = "WriteDouble"; + else if( arg0 == "const char *" ) + newIOMethod = "WriteCharPtr"; + else if( arg0 == "char *" ) + newIOMethod = "WriteCharPtr"; + else if( arg0 == "const unsigned char *" ) + newIOMethod = "WriteUCharPtr"; + else if( arg0 == "unsigned char *" ) + newIOMethod = "WriteUCharPtr"; + else if( arg0 == "class SvStream &" ) + newIOMethod = "WriteStream"; + else + { + report( DiagnosticsEngine::Warning, + "found call to operator<< that I cannot convert with type: " + arg0, + callExpr->getLocStart()); + return true; + } + } + else + { + if( arg0 == "sal_uInt16 &" ) + newIOMethod = "ReadUInt16"; + else if( arg0 == "sal_uInt32 &" ) + newIOMethod = "ReadUInt32"; + else if( arg0 == "sal_uInt64 &" ) + newIOMethod = "ReadUInt64"; + else if( arg0 == "sal_Int16 &" ) + newIOMethod = "ReadInt16"; + else if( arg0 == "sal_Int32 &" ) + newIOMethod = "ReadInt32"; + else if( arg0 == "sal_Int64 &" ) + newIOMethod = "ReadInt64"; + else if( arg0 == "sal_uInt8 &" ) + newIOMethod = "ReadUInt8"; + else if( arg0 == "signed char &" ) + newIOMethod = "ReadSChar"; + else if( arg0 == "char &" ) + newIOMethod = "ReadChar"; + else if( arg0 == "unsigned char &" ) + newIOMethod = "ReadUChar"; + else if( arg0 == "float &" ) + newIOMethod = "ReadFloat"; + else if( arg0 == "double &" ) + newIOMethod = "ReadDouble"; + else if( arg0 == "class SvStream &" ) + newIOMethod = "ReadStream"; + else + { + report( DiagnosticsEngine::Warning, + "found call to operator>> that I cannot convert with type: " + arg0, + callExpr->getLocStart()); + return true; + } + } + + // CallExpr overrides the children() method from Stmt, but not the const variant of it, so we need to cast const away. + StmtRange range = const_cast(callExpr)->children(); + const Stmt* child1 = *range; // ImplicitCastExpr + ++range; + const Stmt* child2 = *range; // ImplicitCastExpr + + if( dyn_cast_or_null< UnaryOperator >( child2 ) != NULL ) + { + // remove the "*" before the stream variable + if( !replaceText( callExpr->getLocStart(), 1, "" ) ) + return true; + if( !replaceText( child1->getLocStart().getLocWithOffset(-1), 4, "->" ) ) + return true; + } + else + { + if( !replaceText( child1->getLocStart().getLocWithOffset(-1), 4, "." ) ) + return true; + } + + if( !insertTextBefore( callExpr->getArg( 1 )->getLocStart(), newIOMethod + "( " ) ) + return true; + if( !insertTextAfter( after( callExpr->getLocEnd() ), " )" ) ) + return true; + +//TODO for some reason this is currently removing too much text + // if there was a cast e.g. "r << (sal_Int32) 1", then remove the cast +// const CStyleCastExpr* cast = dyn_cast_or_null< CStyleCastExpr >( callExpr->getArg(1) ); +// if (cast != NULL) +// { +// replaceText( SourceRange( cast->getLParenLoc(), cast->getRParenLoc() ), "" ); +// } + + // if there was already parentheses around the expression, remove them + const ParenExpr* paren = dyn_cast_or_null< ParenExpr >( callExpr->getArg(1) ); + if (paren != NULL) + { + if( !replaceText( paren->getLocStart(), 1, "" ) ) + return true; + if( !replaceText( paren->getLocEnd(), 1, "" ) ) + return true; + } + +// report( DiagnosticsEngine::Note, "found", callExpr->getLocStart()); + return true; +} + +SourceLocation SvStreamOutputOperators::after( const SourceLocation& loc ) +{ + return Lexer::getLocForEndOfToken( loc, 0, compiler.getASTContext().getSourceManager(), compiler.getASTContext().getLangOpts() ); +} + +static Plugin::Registration< SvStreamOutputOperators > X( "svstreamoutputoperators" ); + +} // namespace + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/test/deadclass.cxx b/compilerplugins/clang/store/test/deadclass.cxx new file mode 100644 index 000000000..d8b9cdb60 --- /dev/null +++ b/compilerplugins/clang/store/test/deadclass.cxx @@ -0,0 +1,14 @@ +/* -*- 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/. + */ + +struct Foo { // expected-error {{class has only copy/move constructors, must be dead [loplugin:deadclass]}} + Foo(Foo&); +}; + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/compilerplugins/clang/store/tutorial/tutorial1.cxx b/compilerplugins/clang/store/tutorial/tutorial1.cxx new file mode 100644 index 000000000..9f7c97fb7 --- /dev/null +++ b/compilerplugins/clang/store/tutorial/tutorial1.cxx @@ -0,0 +1,68 @@ +/* -*- 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 "tutorial1.hxx" + +/* +This is a compile check. + +Checks all return statements and warns if they return literal false (i.e. 'return false'). +*/ + +namespace loplugin +{ + +// Ctor, nothing special, pass the argument(s). +Tutorial1::Tutorial1( const InstantiationData& data ) + : FilteringPlugin( data ) + { + } + +// Perform the actual action. +void Tutorial1::run() + { + // Traverse the whole AST of the translation unit (i.e. examine the whole source file). + // The Clang AST helper class will call VisitReturnStmt for every return statement. + TraverseDecl( compiler.getASTContext().getTranslationUnitDecl()); + } + +// This function is called for every return statement. +// Returning true means to continue with examining the AST, false means to stop (just always return true). +bool Tutorial1::VisitReturnStmt( const ReturnStmt* returnstmt ) + { + // Helper function from the LO base plugin class, call at the very beginning to ignore sources + // that should not be processed (e.g. system headers). + if( ignoreLocation( returnstmt )) + return true; + // Get the expression in the return statement (see ReturnStmt API docs). + const Expr* expression = returnstmt->getRetValue(); + if( expression == NULL ) + return true; // plain 'return;' without expression + // Check if the expression is a bool literal (Clang uses dyn_cast<> instead of dynamic_cast<>). + if( const CXXBoolLiteralExpr* boolliteral = dyn_cast< CXXBoolLiteralExpr >( expression )) + { // It is. + if( boolliteral->getValue() == false ) // Is it 'return false;' ? (See CXXBoolLiteralExpr API docs) + { // Ok, warn, use LO plugin helper function. + report( DiagnosticsEngine::Warning, // It's just a warning. + "returning false", // the message + boolliteral->getLocStart()) // and the exact position where the message should point + << returnstmt->getSourceRange(); // and the full return statement to highlight (optional) + } + } + return true; + } + +// Register the plugin action with the LO plugin handling. +static Plugin::Registration< Tutorial1 > tutorial1( "tutorial1" ); + +} // namespace + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/tutorial/tutorial1.hxx b/compilerplugins/clang/store/tutorial/tutorial1.hxx new file mode 100644 index 000000000..a296ab2cf --- /dev/null +++ b/compilerplugins/clang/store/tutorial/tutorial1.hxx @@ -0,0 +1,38 @@ +/* -*- 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. + * + */ + +#ifndef TUTORIAL1_H +#define TUTORIAL1_H + +#include "plugin.hxx" + +namespace loplugin +{ + +// The class implementing the plugin action. +class Tutorial1 + // Inherits from the Clang class that will allow examining the Clang AST tree (i.e. syntax tree). + : public FilteringPlugin< Tutorial1 > + { + public: + // Ctor, nothing special. + Tutorial1( const InstantiationData& data ); + // The function that will be called to perform the actual action. + virtual void run() override; + // Function from Clang, it will be called for every return statement in the source. + bool VisitReturnStmt( const ReturnStmt* returnstmt ); + }; + +} // namespace + +#endif // POSTFIXINCREMENTFIX_H + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/tutorial/tutorial1_example.cxx b/compilerplugins/clang/store/tutorial/tutorial1_example.cxx new file mode 100644 index 000000000..1ec0e1e59 --- /dev/null +++ b/compilerplugins/clang/store/tutorial/tutorial1_example.cxx @@ -0,0 +1,21 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +// This is just an example file to see what AST looks like for return statements. +// To the AST, run : +// clang++ -fsyntax-only -Xclang -ast-dump tutorial1_example.cxx + +void f() + { + return; + } + +bool g() + { + return false; + } + +bool h() + { + return 3 > 2; + } + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/tutorial/tutorial2.cxx b/compilerplugins/clang/store/tutorial/tutorial2.cxx new file mode 100644 index 000000000..49aaaa631 --- /dev/null +++ b/compilerplugins/clang/store/tutorial/tutorial2.cxx @@ -0,0 +1,95 @@ +/* -*- 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 "tutorial2.hxx" + +/* +This is a compile check. + +Warns about if statements with a comparison followed by literal return false: +if( a == 1 ) + return false; +*/ + +namespace loplugin +{ + +Tutorial2::Tutorial2( const InstantiationData& data ) + : FilteringPlugin( data ) + { + } + +void Tutorial2::run() + { + // The Clang AST helper class will call VisitIfStmt for every if statement. + TraverseDecl( compiler.getASTContext().getTranslationUnitDecl()); + } + +// This function is called for every if statement. +bool Tutorial2::VisitIfStmt( const IfStmt* ifstmt ) + { + if( ignoreLocation( ifstmt )) + return true; + // Check if the condition of the if statement is a binary operator. + if( const BinaryOperator* oper = dyn_cast< BinaryOperator >( ifstmt->getCond())) + { + // And if it's operator==. + if( oper->getOpcode() == BO_EQ ) + { + // Now check if the sub-statement is 'return false'. + const Stmt* warn = NULL; // The return statement (for the warning message). + // Check if the sub-statement is directly 'return false;'. + if( isReturnFalse( ifstmt->getThen())) + warn = ifstmt->getThen(); + // Check if the sub-statement is '{ return false; }' + else if( const CompoundStmt* compound = dyn_cast< CompoundStmt >( ifstmt->getThen())) + { + if( compound->size() == 1 ) // one statement + if( isReturnFalse( *compound->body_begin())) // check the one sub-statement + warn = *compound->body_begin(); + } + if( warn != NULL ) // there is a return statement to warn about. + { + report( DiagnosticsEngine::Warning, + "returning false after if with equality comparison", + cast< ReturnStmt >( warn )->getRetValue()->getLocStart()) // the 'false' in the return + << warn->getSourceRange(); + // Also add a note showing the if statement. + report( DiagnosticsEngine::Note, + "the if statement is here", + ifstmt->getLocStart()); + } + } + } + return true; + } + +bool Tutorial2::isReturnFalse( const Stmt* stmt ) + { + // Is it return statement? + if( const ReturnStmt* returnstmt = dyn_cast< ReturnStmt >( stmt )) + { + // dyn_cast_or_null<> can also be passed NULL, unlike dyn_cast<> + if( const CXXBoolLiteralExpr* boolliteral = dyn_cast_or_null< CXXBoolLiteralExpr >( returnstmt->getRetValue())) + { + if( boolliteral->getValue() == false ) + return true; + } + } + return false; + } + +// Register the plugin action with the LO plugin handling. +static Plugin::Registration< Tutorial2 > tutorial2( "tutorial2" ); + +} // namespace + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/tutorial/tutorial2.hxx b/compilerplugins/clang/store/tutorial/tutorial2.hxx new file mode 100644 index 000000000..9c1f486f8 --- /dev/null +++ b/compilerplugins/clang/store/tutorial/tutorial2.hxx @@ -0,0 +1,38 @@ +/* -*- 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. + * + */ + +#ifndef TUTORIAL2_H +#define TUTORIAL2_H + +#include "plugin.hxx" + +namespace loplugin +{ + +// The same like for Tutorial1. +class Tutorial2 + : public FilteringPlugin< Tutorial2 > + { + public: + Tutorial2( const InstantiationData& data ); + virtual void run() override; + // Will be called for every if statement. + bool VisitIfStmt( const IfStmt* ifstmt ); + private: + // Helper function to check if the statement is 'return false;'. + bool isReturnFalse( const Stmt* stmt ); + }; + +} // namespace + +#endif // POSTFIXINCREMENTFIX_H + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/tutorial/tutorial2_example.cxx b/compilerplugins/clang/store/tutorial/tutorial2_example.cxx new file mode 100644 index 000000000..7d72ff68d --- /dev/null +++ b/compilerplugins/clang/store/tutorial/tutorial2_example.cxx @@ -0,0 +1,18 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +// This is just an example file to see what AST looks like for return statements. +// To the AST, run : +// clang++ -fsyntax-only -Xclang -ast-dump tutorial1_example.cxx + +bool g() + { + if( 1 == 2 ) + return false; + if( 1 == 2 ) + { + return false; + } + if( true ) + return false; + } + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/tutorial/tutorial3.cxx b/compilerplugins/clang/store/tutorial/tutorial3.cxx new file mode 100644 index 000000000..33a1249a3 --- /dev/null +++ b/compilerplugins/clang/store/tutorial/tutorial3.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. + * + * Based on LLVM/Clang. + * + * This file is distributed under the University of Illinois Open Source + * License. See LICENSE.TXT for details. + * + */ + +#include "tutorial3.hxx" + +/* +This is a rewriter. + +It looks for if statements with a comparison followed by literal return false +and modifies the return statements to 'return maybereturntrue;' +*/ + +namespace loplugin +{ + +// Ctor, pass arguments. +Tutorial3::Tutorial3( const InstantiationData& data ) + : FilteringRewritePlugin( data ) + { + } + +void Tutorial3::run() + { + TraverseDecl( compiler.getASTContext().getTranslationUnitDecl()); + } + +bool Tutorial3::VisitIfStmt( const IfStmt* ifstmt ) + { + if( ignoreLocation( ifstmt )) + return true; + if( const BinaryOperator* oper = dyn_cast< BinaryOperator >( ifstmt->getCond())) + { + if( oper->getOpcode() == BO_EQ ) + { + // Modify the sub-statement if it is 'return false'. + modifyReturnFalse( ifstmt->getThen()); + // Modify the sub-statement if it is '{ return false; }'. + if( const CompoundStmt* compound = dyn_cast< CompoundStmt >( ifstmt->getThen())) + { + if( compound->size() == 1 ) // one statement + modifyReturnFalse( *compound->body_begin()); + } + } + } + return true; + } + +void Tutorial3::modifyReturnFalse( const Stmt* stmt ) + { + // Is it return statement? + if( const ReturnStmt* returnstmt = dyn_cast< ReturnStmt >( stmt )) + { + // dyn_cast_or_null<> can also be passed NULL, unlike dyn_cast<> + if( const CXXBoolLiteralExpr* boolliteral = dyn_cast_or_null< CXXBoolLiteralExpr >( returnstmt->getRetValue())) + { + if( boolliteral->getValue() == false ) + { // It is, modify the false to true using LO plugin helper function. + replaceText( boolliteral->getSourceRange(), "maybereturntrue" ); + } + } + } + } + +// Register the plugin action with the LO plugin handling. +static Plugin::Registration< Tutorial3 > tutorial3( "tutorial3" ); + +} // namespace + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/tutorial/tutorial3.hxx b/compilerplugins/clang/store/tutorial/tutorial3.hxx new file mode 100644 index 000000000..badb12904 --- /dev/null +++ b/compilerplugins/clang/store/tutorial/tutorial3.hxx @@ -0,0 +1,40 @@ +/* -*- 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. + * + */ + +#ifndef TUTORIAL3_H +#define TUTORIAL3_H + +#include "plugin.hxx" + +namespace loplugin +{ + +// Similar like for Tutorial2, but this time the base class is RewritePlugin. +class Tutorial3 + : public loplugin::FilteringRewritePlugin< Tutorial3 > + { + public: + // One more argument for ctor. + Tutorial3( const InstantiationData& data ); + virtual void run() override; + // Will be called for every if statement. + bool VisitIfStmt( const IfStmt* ifstmt ); + private: + // Helper function to check if the statement is 'return false;' and + // modify it if yes. + void modifyReturnFalse( const Stmt* stmt ); + }; + +} // namespace + +#endif // POSTFIXINCREMENTFIX_H + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/unusedcode.cxx b/compilerplugins/clang/store/unusedcode.cxx new file mode 100644 index 000000000..32fc4d3c2 --- /dev/null +++ b/compilerplugins/clang/store/unusedcode.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. + * + * Based on LLVM/Clang. + * + * This file is distributed under the University of Illinois Open Source + * License. See LICENSE.TXT for details. + * + */ + +/* +This is technically a rewriter, but it actually only generates data about code. + +This is incomplete. + +Checks for all function declarations for whether they are used or not. This information +should be output to files and in a second pass it should be checked (by another tool) +which functions are never used. +*/ + +#include "plugin.hxx" + +namespace loplugin +{ + +class UnusedCode + : public loplugin::FilteringRewritePlugin< UnusedCode > + { + public: + explicit UnusedCode( CompilerInstance& compiler, Rewriter& rewriter ); + virtual void run() override; + bool VisitFunctionDecl( const FunctionDecl* declaration ); + }; + +UnusedCode::UnusedCode( CompilerInstance& compiler, Rewriter& rewriter ) + : FilteringRewritePlugin( compiler, rewriter ) + { + } + +void UnusedCode::run() + { + TraverseDecl( compiler.getASTContext().getTranslationUnitDecl()); + } + +bool UnusedCode::VisitFunctionDecl( const FunctionDecl* declaration ) + { + if( ignoreLocation( declaration )) + return true; + bool isUsed = declaration->isUsed(); + if( const CXXMethodDecl* cxxmethod = dyn_cast< CXXMethodDecl >( declaration )) + { + if( !isUsed && cxxmethod->isVirtual()) + { // Virtual methods are used also if a method they override is used. + for( CXXMethodDecl::method_iterator it = cxxmethod->begin_overridden_methods(); + it != cxxmethod->end_overridden_methods(); + ++it ) + { + if( (*it)->isUsed()) + { + isUsed = true; + break; + } + } + } + } + // Fully qualified name: declaration->getQualifiedNameAsString() + // Is used: isUsed + // The main source file compiled: compiler.getSourceManager().getFileEntryForID( compiler.getSourceManager().getMainFileID())->getName() + return true; + } + +static Plugin::Registration< UnusedCode > X( "unusedcode" ); + +} // namespace + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/valueof.cxx b/compilerplugins/clang/store/valueof.cxx new file mode 100644 index 000000000..2b769a3e8 --- /dev/null +++ b/compilerplugins/clang/store/valueof.cxx @@ -0,0 +1,150 @@ +/* -*- 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. + * + */ + +/* +This is a rewriter. + +Replaces all calls to the deprecated O(U)String::valueOf() . + +*/ + +#include "plugin.hxx" + +namespace loplugin +{ + +class ConvertValueOf + : public loplugin::FilteringRewritePlugin< ConvertValueOf > + { + public: + explicit ConvertValueOf( CompilerInstance& compiler, Rewriter& rewriter ); + virtual void run() override; + bool VisitCallExpr( const CallExpr* call ); + private: + void removeCast( const Expr* arg ); + }; + +ConvertValueOf::ConvertValueOf( CompilerInstance& compiler, Rewriter& rewriter ) + : FilteringRewritePlugin( compiler, rewriter ) + { + } + +void ConvertValueOf::run() + { + TraverseDecl( compiler.getASTContext().getTranslationUnitDecl()); + } + +bool ConvertValueOf::VisitCallExpr( const CallExpr* call ) + { + if( ignoreLocation( call )) + return true; + // Using getDirectCallee() here means that we find only calls + // that call the function directly (i.e. not using a pointer, for example). + // Use getCallee() to include also those : + // if( const FunctionDecl* func = dyn_cast_or_null< FunctionDecl >( call->getCalleeDecl())) + if( const FunctionDecl* func = call->getDirectCallee()) + { + // Optimize, getQualifiedNameAsString() is reportedly expensive, + // so first check fast details like number of arguments or the (unqualified) + // name before checking the fully qualified name. + // See FunctionDecl for all the API about the function. + if( func->getIdentifier() != NULL + && ( func->getName() == "valueOf" )) + { + string qualifiedName = func->getQualifiedNameAsString(); + if( qualifiedName == "rtl::OString::valueOf" ) + { + // Further checks about arguments. Check mainly ParmVarDecl, VarDecl, + // ValueDecl and QualType for Clang API details. + string arg0 = func->getParamDecl( 0 )->getType().getAsString(); + if( arg0 == "sal_Bool" ) + replaceText( call->getCallee()->getSourceRange(), "OString::boolean" ); + else if( arg0 == "sal_Char" ) + replaceText( call->getCallee()->getSourceRange(), "OString" ); + else + { + replaceText( call->getCallee()->getSourceRange(), "OString::number" ); + removeCast( call->getArg( 0 )); + } + } + if( qualifiedName == "rtl::OUString::valueOf" ) + { + // Further checks about arguments. Check mainly ParmVarDecl, VarDecl, + // ValueDecl and QualType for Clang API details. + string arg0 = func->getParamDecl( 0 )->getType().getAsString(); + if( arg0 == "sal_Bool" ) + replaceText( call->getCallee()->getSourceRange(), "OUString::boolean" ); + else if( arg0 == "sal_Unicode" ) + replaceText( call->getCallee()->getSourceRange(), "OUString" ); + else + { + replaceText( call->getCallee()->getSourceRange(), "OUString::number" ); + removeCast( call->getArg( 0 )); + } + } + } + } + return true; + } + +void ConvertValueOf::removeCast( const Expr* arg ) + { + arg = arg->IgnoreImpCasts(); + if( const ExplicitCastExpr* cast = dyn_cast< ExplicitCastExpr >( arg )) + { +// Explicit casts don't seem to actually always change the type (integer promotion +// takes place first?), so remove also preceding implicit casts: +// void f( int ); +// char a; +// f( int( a )); +// |-CallExpr 0x1a84f20 'void' +// | |-ImplicitCastExpr 0x1a84f08 'void (*)(int)' +// | | `-DeclRefExpr 0x1a84eb8 'void (int)' lvalue Function 0x1a58900 'f' 'void (int)' +// | `-CXXFunctionalCastExpr 0x1a84e90 'int' functional cast to int +// | `-ImplicitCastExpr 0x1a84e78 'int' +// | `-ImplicitCastExpr 0x1a84e60 'char' +// | `-DeclRefExpr 0x1a58b88 'char' lvalue Var 0x1a58ab0 'a' 'char' + const Expr* castFrom = cast->getSubExpr()->IgnoreImpCasts(); + if( cast->getType()->isIntegerType() && castFrom->getType()->isIntegerType()) + { + string fromType = castFrom->getType().getAsString(); + if( fromType != "sal_Bool" && fromType != "bool" && fromType != "sal_Char" && fromType != "sal_Unicode" ) + { + if( const CXXFunctionalCastExpr* funcCast = dyn_cast< CXXFunctionalCastExpr >( cast )) + { + removeText( CharSourceRange::getCharRange( funcCast->getLocStart(), + compiler.getSourceManager().getExpansionLoc( funcCast->getSubExpr()->getLocStart()))); + removeText( CharSourceRange::getCharRange( locationAfterToken( + compiler.getSourceManager().getExpansionLoc( funcCast->getSubExpr()->getLocEnd())), + locationAfterToken( funcCast->getLocEnd()))); + } + else if( const CXXNamedCastExpr* namedCast = dyn_cast< CXXNamedCastExpr >( cast )) + { + removeText( CharSourceRange::getCharRange( namedCast->getLocStart(), + compiler.getSourceManager().getExpansionLoc( namedCast->getSubExpr()->getLocStart()))); + removeText( CharSourceRange::getCharRange( locationAfterToken( + compiler.getSourceManager().getExpansionLoc( namedCast->getSubExpr()->getLocEnd())), + locationAfterToken( namedCast->getLocEnd()))); + } + else if( const CStyleCastExpr* cCast = dyn_cast< CStyleCastExpr >( cast )) + removeText( SourceRange( cCast->getLocStart(), cCast->getRParenLoc())); + else + abort(); + } + } + } + } + +static Plugin::Registration< ConvertValueOf > X( "convertvalueof" ); + +} // namespace + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit v1.2.3