diff options
Diffstat (limited to 'compilerplugins/clang/store')
50 files changed, 7069 insertions, 0 deletions
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 <cassert> +#include <string> +#include <iostream> +#include <fstream> +#include <set> +#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<int>(3, 0) + is doing something different from: + std::vector<int>{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<BadVectorInit> +{ +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<const VarDecl*> 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<DeclRefExpr>(expr->getImplicitObjectArgument()); + if (!declExpr) + return true; + const VarDecl* varDecl = dyn_cast<VarDecl>(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<DeclStmt>(parent)) + { + const DeclStmt* declStmt = dyn_cast<DeclStmt>(parent); + const Decl* decl = declStmt->getSingleDecl(); + if (decl && isa<VarDecl>(decl)) + suspectSet.insert(dyn_cast<VarDecl>(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..d74a4fbc1 --- /dev/null +++ b/compilerplugins/clang/store/bodynotinblock.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. + * + */ + +#pragma once + +#include "plugin.hxx" + +namespace loplugin +{ + +class BodyNotInBlock + : public loplugin::FilteringPlugin<BodyNotInBlock> + { + 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 + +/* 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..147ecaad0 --- /dev/null +++ b/compilerplugins/clang/store/cascadingassignop.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. + * + */ + +#pragma once + +#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 + +/* 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..4671f41b4 --- /dev/null +++ b/compilerplugins/clang/store/cascadingcondop.cxx @@ -0,0 +1,85 @@ +/* -*- 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..599fafd82 --- /dev/null +++ b/compilerplugins/clang/store/cascadingcondop.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. + * + */ + +#pragma once + +#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 + +/* 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..a7b88704c --- /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 <iostream> + +/* + 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<ConstantFunction> +{ + 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(), "gtkinst.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<CXXMethodDecl>(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<OverrideAttr>()) { + 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<CXXConstructorDecl>(pFunctionDecl) || isa<CXXDestructorDecl>(pFunctionDecl) || isa<CXXConversionDecl>(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<CompoundStmt>(pFunctionDecl->getBody()); + bool bEmptyBody = false; + if (pCompoundStmt) { + if (pCompoundStmt->size() > 1) { + return true; + } + if (pCompoundStmt->size() > 0) { + const ReturnStmt *pReturnStmt = dyn_cast<ReturnStmt>(*pCompoundStmt->body_begin()); + if (!pReturnStmt) { + return true; + } + if (const UnaryOperator* unaryOp = dyn_cast<UnaryOperator>(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<ConstantFunction> 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<DeadClass> +{ +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<ClassTemplateSpecializationDecl>(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<DeadClass> 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 <string> +#include <set> + +#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<DefaultParams> +{ +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<UnaryExprOrTypeTraitExpr>(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<MaterializeTemporaryExpr>(arg) + && isa<MaterializeTemporaryExpr>(defaultArgExpr)) + { + const CXXBindTemporaryExpr* strippedArg = dyn_cast_or_null<CXXBindTemporaryExpr>(arg->IgnoreParenCasts()); + if (strippedArg && isa<CXXTemporaryObjectExpr>(strippedArg->getSubExpr()) + && dyn_cast<CXXTemporaryObjectExpr>(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<CXXNullPtrLiteralExpr>(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..52e717d34 --- /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 <cassert> +#include <iterator> +#include <string> + +#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<CXXRecordDecl>(decl->getDeclContext()); + assert(cls != nullptr); + return cls; +} + +class DeletedSpecial: + public loplugin::FilteringPlugin<DeletedSpecial> +{ +public: + explicit DeletedSpecial(InstantiationData const & data): FilteringPlugin(data) {} + + virtual void run() override; + + bool VisitCXXMethodDecl(CXXMethodDecl const * decl); + +private: + bool allowlist( + 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 (allowlist(decl, "ImpGraphic", "vcl/inc/impgraph.hxx") + || allowlist(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<CXXConstructorDecl>(decl); + CXXRecordDecl const * cls = getClass(decl); + if (ctor != nullptr && ctor->isCopyConstructor()) { + if (allowlist(decl, "ImpGraphic", "vcl/inc/impgraph.hxx") + || allowlist(decl, "SbMethod", "include/basic/sbmeth.hxx") + || allowlist(decl, "ScDBCollection::NamedDBs", "sc/inc/dbdata.hxx") + || allowlist(decl, "ScDrawPage", "sc/inc/drawpage.hxx") + || allowlist(decl, "SmEditSource", "starmath/source/accessibility.hxx") + || allowlist(decl, "SwChartDataSequence", "sw/inc/unochart.hxx") + || allowlist(decl, "SwDPage", "sw/inc/dpage.hxx") + || allowlist(decl, "SwRedlineExtraData_Format", "sw/inc/redline.hxx") + || allowlist(decl, "SwRedlineExtraData_FormattingChanges", "sw/inc/redline.hxx") + || allowlist(decl, "SwTextAPIEditSource", "sw/source/core/inc/textapi.hxx") + || allowlist(decl, "XclImpBiff5Decrypter", "sc/source/filter/inc/xistream.hxx") + || allowlist(decl, "XclImpBiff8Decrypter", "sc/source/filter/inc/xistream.hxx") + || allowlist(decl, "configmgr::LocalizedPropertyNode", "configmgr/source/localizedpropertynode.hxx") + || allowlist(decl, "configmgr::LocalizedValueNode", "configmgr/source/localizedvaluenode.hxx") + || allowlist(decl, "configmgr::PropertyNode", "configmgr/source/propertynode.hxx") + || allowlist(decl, "oox::xls::BiffDecoder_RCF", "sc/source/filter/inc/biffcodec.hxx") + || allowlist(decl, "oox::xls::BiffDecoder_XOR", "sc/source/filter/inc/biffcodec.hxx") + || allowlist(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 (allowlist(decl, "AquaA11yFocusListener", "vcl/osx/a11yfocuslistener.hxx") + || allowlist(decl, "DocTemplLocaleHelper", "sfx2/source/doc/doctemplateslocal.hxx") + || allowlist(decl, "ScViewDataTable", "sc/source/filter/excel/../../ui/inc/viewdata.hxx") + || allowlist(decl, "ScViewDataTable", "sc/source/ui/inc/viewdata.hxx") + || allowlist(decl, "SwLineInfo", "sw/source/core/text/inftxt.hxx") + || allowlist(decl, "XRenderPeer", "vcl/unx/generic/gdi/xrender_peer.hxx") + || allowlist(decl, "desktop::DispatchWatcher", "desktop/source/app/dispatchwatcher.hxx") + || allowlist(decl, "desktop::RequestHandler", "desktop/source/app/officeipcthread.hxx") + || allowlist(decl, "desktop::RequestHandler", "desktop/source/lib/../app/officeipcthread.hxx") + || allowlist(decl, "sd::DiscoveryService", "sd/source/ui/remotecontrol/DiscoveryService.hxx") + || allowlist(decl, "sd::IconCache", "sd/source/ui/inc/tools/IconCache.hxx") + || allowlist(decl, "sd::RemoteServer", "sd/source/ui/inc/RemoteServer.hxx") + || allowlist(decl, "sd::slidesorter::cache::PageCacheManager", "sd/source/ui/slidesorter/inc/cache/SlsPageCacheManager.hxx") + || allowlist(decl, "framework::CommandInfoProvider", "include/framework/commandinfoprovider.hxx") + || allowlist(decl, "vcl::SettingsConfigItem", "vcl/inc/configsettings.hxx") + || allowlist(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::allowlist( + 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<DeletedSpecial> 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<DerivedClass> +{ +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<DerivedClass> X("derivedclass"); + +} + +/* vim:set shiftwidth=4 softtabstop=4 tabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/finalprotected.cxx b/compilerplugins/clang/store/finalprotected.cxx new file mode 100644 index 000000000..c7296232a --- /dev/null +++ b/compilerplugins/clang/store/finalprotected.cxx @@ -0,0 +1,84 @@ +/* -*- 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 <string> +#include <iostream> +#include <map> +#include <set> + +#include "plugin.hxx" +#include "clang/AST/CXXInheritance.h" + +// Check for final classes that have protected members + +namespace +{ + +class FinalProtected: + public loplugin::FilteringPlugin<FinalProtected> +{ +public: + explicit FinalProtected(loplugin::InstantiationData const & data): + FilteringPlugin(data) {} + + virtual void run() override { + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + } + + bool VisitCXXMethodDecl(CXXMethodDecl const *); + bool VisitFieldDecl(FieldDecl const *); +}; + + +bool FinalProtected::VisitCXXMethodDecl(CXXMethodDecl const * cxxMethodDecl) +{ + if (ignoreLocation(cxxMethodDecl)) { + return true; + } + if (cxxMethodDecl->getAccess() != AS_protected) { + return true; + } + if (!cxxMethodDecl->getParent()->hasAttr<FinalAttr>()) { + return true; + } + cxxMethodDecl = cxxMethodDecl->getCanonicalDecl(); + report(DiagnosticsEngine::Warning, + "final class should not have protected members - convert them to private", + cxxMethodDecl->getBeginLoc()) + << cxxMethodDecl->getSourceRange(); + return true; +} + +bool FinalProtected::VisitFieldDecl(FieldDecl const * fieldDecl) +{ + if (ignoreLocation(fieldDecl)) { + return true; + } + if (fieldDecl->getAccess() != AS_protected) { + return true; + } + if (!fieldDecl->getParent()->hasAttr<FinalAttr>()) { + return true; + } + fieldDecl = fieldDecl->getCanonicalDecl(); + report(DiagnosticsEngine::Warning, + "final class should not have protected members - convert them to private", + fieldDecl->getBeginLoc()) + << fieldDecl->getSourceRange(); + return true; +} + +loplugin::Plugin::Registration< FinalProtected > finalprotected("finalprotected"); + +} // namespace + +#endif // LO_CLANG_SHARED_PLUGINS + +/* vim:set shiftwidth=4 softtabstop=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 <string> +#include <set> + +#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<FindOnContainer> +{ +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 <cassert> +#include <string> +#include <iostream> +#include <fstream> +#include <set> + +#include "check.hxx" +#include "plugin.hxx" + +/** +comparing floating point numbers using == or != is a bad idea. +*/ + +namespace { + +class FpComparison: + public loplugin::FilteringPlugin<FpComparison> +{ +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..16623edcb --- /dev/null +++ b/compilerplugins/clang/store/lclstaticfix.hxx @@ -0,0 +1,30 @@ +/* -*- 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. + * + */ + +#pragma once + +#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 + +/* 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 <cassert> +#include <string> +#include <iostream> +#include <fstream> +#include <set> + +#include <clang/AST/CXXInheritance.h> +#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<ManualRefCount>, 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/optmove.cxx b/compilerplugins/clang/store/optmove.cxx new file mode 100644 index 000000000..51b5a4b84 --- /dev/null +++ b/compilerplugins/clang/store/optmove.cxx @@ -0,0 +1,161 @@ +/* -*- 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 "check.hxx" + +#include <string> +#include <set> + +/** + * This plugin is unfinished, abandoned because it did not find anything interesting. + * + * Look for variables that are + * (a) copied from + * (b) never used after the copy + * (c) have move operators + * + * The intention being to find places where we can move data (e.g. in containers) instead of copying. +*/ + +namespace +{ +class OptMove : public loplugin::FilteringPlugin<OptMove> +{ +public: + explicit OptMove(loplugin::InstantiationData const& data) + : FilteringPlugin(data) + { + } + + virtual void run() override + { + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + + for (auto const& pair : m_Candidates) + { + //auto varDecl = pair.first; + auto candidate = pair.second; + if (!candidate.canUseExpr) + continue; + report(DiagnosticsEngine::Warning, "can std::move value instead of copy", + candidate.canUseExpr->getSourceRange().getBegin()) + << candidate.canUseExpr->getSourceRange(); + //varDecl->dump(); + } + } + + bool VisitVarDecl(const VarDecl*); + bool VisitCXXOperatorCallExpr(const CXXOperatorCallExpr*); + bool VisitDeclRefExpr(const DeclRefExpr*); + bool VisitFunctionDecl(const FunctionDecl* f) + { + if (f->getIdentifier() && f->getName() == "foo") + f->dump(); + return true; + } + +private: + struct Candidate + { + const DeclRefExpr* operatorArg1 = nullptr; + const Expr* canUseExpr = nullptr; + }; + std::map<const VarDecl*, Candidate> m_Candidates; +}; + +bool OptMove::VisitVarDecl(const VarDecl* varDecl) +{ + if (ignoreLocation(varDecl)) + return true; + if (varDecl->hasGlobalStorage()) + return true; + if (varDecl->getLinkageAndVisibility().getLinkage() == ExternalLinkage) + return true; + if (!varDecl->getType()->isRecordType()) + return true; + + auto cxxRecord = dyn_cast<CXXRecordDecl>(varDecl->getType()->getAsRecordDecl()); + if (!cxxRecord || !cxxRecord->hasDefinition() || !cxxRecord->hasMoveAssignment()) + return true; + // ignore our simpler types for now, I'm after bigger game + auto typeName = cxxRecord->getName(); + if (typeName.contains("Reference") || typeName.contains("Color") || typeName.contains("VclPtr") + || typeName.contains("OString") || typeName.contains("OUString") + || typeName.contains("Rectangle") || typeName.contains("Size") + || typeName.contains("Selection") || typeName.contains("Point") + || typeName.contains("strong_int")) + return true; + m_Candidates.emplace(varDecl, Candidate()); + + if (!varDecl->hasInit()) + return true; + auto cons = dyn_cast<CXXConstructExpr>(varDecl->getInit()); + if (!cons || !cons->getConstructor()->isCopyConstructor()) + return true; + auto arg1 = dyn_cast<DeclRefExpr>(cons->getArg(0)->IgnoreImplicit()); + if (!arg1) + return true; + auto varDecl1 = dyn_cast<VarDecl>(arg1->getDecl()); + if (!varDecl1) + return true; + auto it = m_Candidates.find(varDecl1); + if (it == m_Candidates.end()) + return true; + it->second.operatorArg1 = arg1; + it->second.canUseExpr = cons; + return true; +} + +bool OptMove::VisitCXXOperatorCallExpr(CXXOperatorCallExpr const* cxxOperatorCallExpr) +{ + if (ignoreLocation(cxxOperatorCallExpr)) + return true; + auto op = cxxOperatorCallExpr->getOperator(); + if (op != OO_Equal) + return true; + auto arg0 = dyn_cast<DeclRefExpr>(cxxOperatorCallExpr->getArg(0)->IgnoreImplicit()); + auto arg1 = dyn_cast<DeclRefExpr>(cxxOperatorCallExpr->getArg(1)->IgnoreImplicit()); + if (!arg0 || !arg1) + return true; + auto varDecl0 = dyn_cast<VarDecl>(arg0->getDecl()); + auto varDecl1 = dyn_cast<VarDecl>(arg1->getDecl()); + if (!varDecl0 || !varDecl1) + return true; + auto cxxMethodDecl = dyn_cast_or_null<CXXMethodDecl>(cxxOperatorCallExpr->getDirectCallee()); + if (!cxxMethodDecl || !cxxMethodDecl->isCopyAssignmentOperator()) + return true; + auto it = m_Candidates.find(varDecl1); + if (it == m_Candidates.end()) + return true; + it->second.operatorArg1 = arg1; + it->second.canUseExpr = cxxOperatorCallExpr; + return true; +} + +bool OptMove::VisitDeclRefExpr(const DeclRefExpr* declRefExpr) +{ + if (ignoreLocation(declRefExpr)) + return true; + auto varDecl = dyn_cast<VarDecl>(declRefExpr->getDecl()); + if (!varDecl) + return true; + auto it = m_Candidates.find(varDecl); + if (it == m_Candidates.end()) + return true; + if (it->second.operatorArg1 == declRefExpr) + return true; + m_Candidates.erase(it); + return true; +} + +loplugin::Plugin::Registration<OptMove> noexceptmove("optmove"); +} + +/* 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..7a394ae25 --- /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 <iostream> +#include <fstream> + +/** + * 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 + */ + +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<PaintMethodConversion> +{ +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<PaintMethodConversion> 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..7f496662b --- /dev/null +++ b/compilerplugins/clang/store/postfixincrementfix.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. + * + */ + +#pragma once + +#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 + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/putpoolitem.cxx b/compilerplugins/clang/store/putpoolitem.cxx new file mode 100644 index 000000000..808059997 --- /dev/null +++ b/compilerplugins/clang/store/putpoolitem.cxx @@ -0,0 +1,103 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <cassert> +#include <string> +#include <iostream> +#include <fstream> +#include <set> +#include <unordered_set> + +#include <clang/AST/CXXInheritance.h> + +#include "config_clang.h" + +#include "plugin.hxx" +#include "check.hxx" + +/** + +*/ + +namespace +{ +class PutPoolItem : public loplugin::FilteringPlugin<PutPoolItem> +{ +public: + explicit PutPoolItem(loplugin::InstantiationData const& data) + : FilteringPlugin(data) + { + } + + virtual bool preRun() override + { + // StringRef fn(handler.getMainFileName()); + // if (loplugin::isSamePathname(fn, WORKDIR "/YaccTarget/unoidl/source/sourceprovider-parser.cxx")) + // return false; + return true; + } + virtual void run() override + { + if (preRun()) + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + } + + bool VisitCXXMemberCallExpr(const CXXMemberCallExpr*); + bool VisitFunctionDecl(const FunctionDecl*) + { + // if (f->getIdentifier() && f->getName() == "foo") + // f->dump(); + return true; + } +}; + +bool PutPoolItem::VisitCXXMemberCallExpr(const CXXMemberCallExpr* cxxCallExpr) +{ + if (ignoreLocation(cxxCallExpr)) + return true; + auto tc = loplugin::TypeCheck(cxxCallExpr->getObjectType()); + if (!tc.Class("SfxItemSet")) + return true; + if (!cxxCallExpr->getMethodDecl()->getIdentifier() + || cxxCallExpr->getMethodDecl()->getName() != "Put") + return true; + auto argExpr = dyn_cast<CXXOperatorCallExpr>(cxxCallExpr->getArg(0)->IgnoreImplicit()); + if (!argExpr) + return true; + if (argExpr->getOperator() != OO_Star) + return true; + auto ptrExpr = argExpr->getArg(0)->IgnoreImplicit(); + auto tc2 = loplugin::TypeCheck(ptrExpr->getType()); + if (!tc2.Class("unique_ptr")) + return true; + // ignore calls when we are passing a copy of a member field + if (isa<MemberExpr>(ptrExpr)) + return true; + + StringRef fn = getFilenameOfLocation( + compiler.getSourceManager().getSpellingLoc(cxxCallExpr->getBeginLoc())); + if (loplugin::isSamePathname(fn, SRCDIR "/sc/source/ui/app/inputwin.cxx") + || loplugin::isSamePathname(fn, SRCDIR "/sc/source/ui/dbgui/csvgrid.cxx") + || loplugin::isSamePathname(fn, SRCDIR "/sw/source/uibase/shells/basesh.cxx") + || loplugin::isSamePathname(fn, SRCDIR "/sw/source/uibase/shells/textsh.cxx") + || loplugin::isSamePathname(fn, SRCDIR "/sw/source/filter/xml/xmlimpit.cxx") + || loplugin::isSamePathname(fn, SRCDIR "/sw/source/uibase/shells/tabsh.cxx")) + return true; + + // argExpr->dump(); + + report(DiagnosticsEngine::Warning, "could use std::move?", cxxCallExpr->getBeginLoc()) + << cxxCallExpr->getSourceRange(); + return true; +} + +loplugin::Plugin::Registration<PutPoolItem> putpoolitem("putpoolitem", true); +} + +/* 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..6d8e28605 --- /dev/null +++ b/compilerplugins/clang/store/refassign.cxx @@ -0,0 +1,151 @@ +/* -*- 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 <cassert> +#include <string> +#include <iostream> +#include <fstream> +#include <set> + +#include <clang/AST/CXXInheritance.h> + +#include "check.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<RefAssign> +{ +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<CXXOperatorCallExpr>(binaryOp->getLHS()->IgnoreParenImpCasts())) + return true; + if (isa<CXXOperatorCallExpr>(binaryOp->getRHS()->IgnoreParenImpCasts())) + return true; + + // if we are assigning to a parameter we probably mean it + if (auto declRefExpr = dyn_cast<DeclRefExpr>(binaryOp->getLHS()->IgnoreParenImpCasts())) + if (declRefExpr->getDecl() && isa<ParmVarDecl>(declRefExpr->getDecl())) + return true; + + if (auto callExpr = dyn_cast<CallExpr>(binaryOp->getRHS()->IgnoreParenImpCasts())) + if (auto functionDecl = dyn_cast_or_null<FunctionDecl>(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", + binaryOp->getBeginLoc()) + << rhsType << lhsType << binaryOp->getSourceRange(); + return true; +} + +clang::QualType RefAssign::ExtractType(Expr const* expr) +{ + expr = expr->IgnoreParenImpCasts(); + if (auto declReflExpr = dyn_cast<DeclRefExpr>(expr)) + { + if (auto varDecl = dyn_cast<VarDecl>(declReflExpr->getDecl())) + return varDecl->getType(); + } + else if (auto callExpr = dyn_cast<CallExpr>(expr)) + { + if (callExpr->isTypeDependent()) + return {}; + // callExpr->dump(); + return callExpr->getCallReturnType(compiler.getASTContext()); + } + return expr->getType(); +} + +loplugin::Plugin::Registration<RefAssign> 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..bedd2c534 --- /dev/null +++ b/compilerplugins/clang/store/removeforwardstringdecl.hxx @@ -0,0 +1,32 @@ +/* -*- 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. + * + */ + +#pragma once + +#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 + +/* 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 <cassert> +#include <string> +#include <iostream> +#include "plugin.hxx" +#include <sys/mman.h> +#include <sys/types.h> +#include <fcntl.h> +#include <unistd.h> +#include <sys/stat.h> +#include <assert.h> +#include <cstring> + +/** + This is intended to be run as the second stage of the "unnecessaryvirtuals" clang plugin. +*/ + +namespace { + +class RemoveVirtuals: + public loplugin::FilteringRewritePlugin<RemoveVirtuals> +{ +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<char*>(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 <string> +#include <set> + +#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<ReturnByRef> +{ +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<ReturnStmt>(*compoundStmt->child_begin()); + if (returnStmt == nullptr) { + return true; + } + + const Stmt* nextStmt = dyn_cast<Expr>(*returnStmt->child_begin())->IgnoreParens(); + const UnaryOperator* unaryOperator = dyn_cast<UnaryOperator>(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 <memory> +#include "plugin.hxx" + +namespace { + +class ReturnUnique: + public loplugin::FilteringPlugin<ReturnUnique> +{ +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<CXXMemberCallExpr>(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<NamespaceDecl>(d2->getParent()); + if (d3 == nullptr + /* || dyn_cast<TranslationUnitDecl>(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<ReturnUnique> 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<VisibilityAttr>(); + return attr != nullptr && !attr->isInherited(); +} + +bool isFriendDecl(Decl const * decl) { + return decl->getFriendObjectKind() != Decl::FOK_None; +} + +class ReVisibility: + public loplugin::FilteringPlugin<ReVisibility> +{ +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<VisibilityAttr>()->getLocation()) + << decl->getAttr<VisibilityAttr>()->getRange(); + report( + DiagnosticsEngine::Note, + "Previous visibility declaration is here", + p->getAttr<VisibilityAttr>()->getLocation()) + << p->getAttr<VisibilityAttr>()->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<VisibilityAttr>()->getLocation()) + << decl->getAttr<VisibilityAttr>()->getRange(); + report( + DiagnosticsEngine::Note, "First declaration is here", + first->getLocation()) + << first->getSourceRange(); + } + } + return true; +} + +loplugin::Plugin::Registration<ReVisibility> 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..c930fbfd1 --- /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 <clang/Lex/Preprocessor.h> + +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 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 <algorithm> +#include <sstream> +#include <tuple> +#include <vector> + +#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<Visitor> +{ +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<DeclRefExpr>( + 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<Range> 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<APSInt, APSInt, unsigned>; + + 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<SfxItemSet::Pair>{{") + : 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<SfxItemSet::Pair>}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<Visitor> reg("sfxitemsetrewrite",true); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/compilerplugins/clang/store/simplifybool.cxx b/compilerplugins/clang/store/simplifybool.cxx new file mode 100644 index 000000000..973ab6a7a --- /dev/null +++ b/compilerplugins/clang/store/simplifybool.cxx @@ -0,0 +1,1333 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <cassert> + +#include "plugin.hxx" +#include "clang/AST/CXXInheritance.h" + +namespace { + +// Like clang::Stmt::IgnoreImplicit (lib/AST/Stmt.cpp), but also looking through implicit +// UserDefinedConversion's member function call: +Expr const * ignoreAllImplicit(Expr const * expr) { + if (auto const e = dyn_cast<ExprWithCleanups>(expr)) { + expr = e->getSubExpr(); + } + if (auto const e = dyn_cast<MaterializeTemporaryExpr>(expr)) { + expr = e->getSubExpr(); + } + if (auto const e = dyn_cast<CXXBindTemporaryExpr>(expr)) { + expr = e->getSubExpr(); + } + while (auto const e = dyn_cast<ImplicitCastExpr>(expr)) { + expr = e->getSubExpr(); + if (e->getCastKind() == CK_UserDefinedConversion) { + auto const ce = cast<CXXMemberCallExpr>(expr); + assert(ce->getNumArgs() == 0); + expr = ce->getImplicitObjectArgument(); + } + } + return expr; +} + +Expr const * ignoreParenImpCastAndComma(Expr const * expr) { + for (;;) { + expr = expr->IgnoreParenImpCasts(); + auto e = dyn_cast<BinaryOperator>(expr); + if (e == nullptr || e->getOpcode() != BO_Comma) { + return expr; + } + expr = e->getRHS(); + } +} + +Expr const * getSubExprOfLogicalNegation(Expr const * expr) { + auto e = dyn_cast<UnaryOperator>(ignoreParenImpCastAndComma(expr)); + return e == nullptr || e->getOpcode() != UO_LNot + ? nullptr : e->getSubExpr(); +} + +clang::Type const * stripConstRef(clang::Type const * type) { + auto lvalueType = dyn_cast<LValueReferenceType>(type); + if (!lvalueType) + return type; + return lvalueType->getPointeeType()->getUnqualifiedDesugaredType(); +} + +bool isCompatibleTypeForOperator(clang::Type const * paramType, CXXRecordDecl const * argRecordDecl) { + paramType = stripConstRef(paramType); + auto paramRecordType = dyn_cast<RecordType>(paramType); + if (!paramRecordType) + return false; + CXXRecordDecl const * paramRecordDecl = dyn_cast<CXXRecordDecl>(paramRecordType->getDecl()); + if (!paramRecordDecl) + return false; + return argRecordDecl == paramRecordDecl || argRecordDecl->isDerivedFrom(paramRecordDecl); +} + +FunctionDecl const * findMemberOperator(CXXRecordDecl const * recordDecl, OverloadedOperatorKind ooOpcode, CXXRecordDecl const * rhs) { + for (auto it = recordDecl->method_begin(); it != recordDecl->method_end(); ++it) { + if (it->getOverloadedOperator() == ooOpcode) { + if (it->getNumParams() == 1 && isCompatibleTypeForOperator(it->getParamDecl(0)->getType().getTypePtr(), rhs)) + return *it; + } + } + return nullptr; +} + +// Magic value to indicate we assume this operator exists +static FunctionDecl const * const ASSUME_OPERATOR_EXISTS = reinterpret_cast<FunctionDecl const *>(-1); + +// Search for an operator with matching parameter types; while this may miss some operators with +// odd parameter types that would actually be used by the compiler, it is overall better to have too +// many false negatives (i.e., miss valid loplugin:simplifybool warnings) than false positives here: +FunctionDecl const * findOperator(CompilerInstance& compiler, BinaryOperator::Opcode opcode, clang::Type const * lhsType, clang::Type const * rhsType) { + auto lhsRecordType = dyn_cast<RecordType>(lhsType); + if (!lhsRecordType) + return nullptr; + auto rhsRecordType = dyn_cast<RecordType>(rhsType); + if (!rhsRecordType) + return nullptr; + CXXRecordDecl const * lhsRecordDecl = dyn_cast<CXXRecordDecl>(lhsRecordType->getDecl()); + if (!lhsRecordDecl) + return nullptr; + CXXRecordDecl const * rhsRecordDecl = dyn_cast<CXXRecordDecl>(rhsRecordType->getDecl()); + if (!rhsRecordDecl) + return nullptr; + + auto ctx = lhsRecordDecl->getCanonicalDecl()->getDeclContext(); + + /* + It looks the clang Sema::LookupOverloadedOperatorName is the chunk of functionality I need, + but I have no idea how to call it from here. + Actually finding the right standard library operators requires doing conversions and other funky stuff. + For now, just assume that standard library operators are well-behaved, and have negated operators. + */ + if (ctx->isStdNamespace()) + return ASSUME_OPERATOR_EXISTS; + if (auto namespaceDecl = dyn_cast<NamespaceDecl>(ctx)) { + // because, of course, half the standard library is not "in the standard namespace" + if (namespaceDecl->getName() == "__gnu_debug") + return ASSUME_OPERATOR_EXISTS; + } + + // search for member overloads + // (using the hard way here because DeclContext::lookup does not work for member operators) + auto ooOpcode = BinaryOperator::getOverloadedOperator(opcode); + FunctionDecl const * foundFunction = findMemberOperator(lhsRecordDecl, ooOpcode, rhsRecordDecl); + if (foundFunction) + return foundFunction; + auto ForallBasesCallback = [&](const CXXRecordDecl *baseCXXRecordDecl) + { + if (baseCXXRecordDecl->isInvalidDecl()) + return false; + foundFunction = findMemberOperator(baseCXXRecordDecl, ooOpcode, rhsRecordDecl); + return false; + }; + + lhsRecordDecl->forallBases(ForallBasesCallback); + if (foundFunction) + return foundFunction; + + // search for free function overloads + if (ctx->getDeclKind() == Decl::LinkageSpec) { + ctx = ctx->getParent(); + } + auto operatorDeclName = compiler.getASTContext().DeclarationNames.getCXXOperatorName(ooOpcode); + auto res = ctx->lookup(operatorDeclName); + for (auto d = res.begin(); d != res.end(); ++d) { + FunctionDecl const * f = dyn_cast<FunctionDecl>(*d); + if (!f || f->getNumParams() != 2) + continue; + if (!isCompatibleTypeForOperator(f->getParamDecl(0)->getType().getTypePtr(), lhsRecordDecl)) + continue; + if (!isCompatibleTypeForOperator(f->getParamDecl(1)->getType().getTypePtr(), rhsRecordDecl)) + continue; + return f; + } + return nullptr; +} + +enum class Value { Unknown, False, True }; + +Value getValue(Expr const * expr) { + expr = ignoreParenImpCastAndComma(expr); + if (expr->getType()->isBooleanType()) { + // Instead going via Expr::isCXX11ConstantExpr would turn up exactly one + // additional place in svx/source/dialog/framelinkarray.cxx + // + // const bool DIAG_DBL_CLIP_DEFAULT = false; + // ... + // ... = mxImpl.get() ? mxImpl->mbDiagDblClip : DIAG_DBL_CLIP_DEFAULT; + // + // where it is unclear whether it is not actually better to consider + // DIAG_DBL_CLIP_DEFAULT a tunable parameter (and thus not to simplify): + auto lit = dyn_cast<CXXBoolLiteralExpr>(expr); + if (lit != nullptr) { + return lit->getValue() ? Value::True : Value::False; + } + } + return Value::Unknown; +} + +class SimplifyBool: + public loplugin::FilteringPlugin<SimplifyBool> +{ +public: + explicit SimplifyBool(loplugin::InstantiationData const & data): + FilteringPlugin(data) {} + + void run() override; + + bool VisitUnaryOperator(UnaryOperator const * expr); + + bool VisitBinaryOperator(BinaryOperator const * expr); + + bool VisitConditionalOperator(ConditionalOperator const * expr); + + bool TraverseFunctionDecl(FunctionDecl *); + + bool TraverseCXXMethodDecl(CXXMethodDecl *); + +private: + bool visitBinLT(BinaryOperator const * expr); + + bool visitBinGT(BinaryOperator const * expr); + + bool visitBinLE(BinaryOperator const * expr); + + bool visitBinGE(BinaryOperator const * expr); + + bool visitBinEQ(BinaryOperator const * expr); + + bool visitBinNE(BinaryOperator const * expr); + + FunctionDecl* m_insideFunctionDecl = nullptr; +}; + +void SimplifyBool::run() { + if (compiler.getLangOpts().CPlusPlus) { + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + } +} + +bool SimplifyBool::VisitUnaryOperator(UnaryOperator const * expr) { + if (expr->getOpcode() != UO_LNot) { + return true; + } + if (ignoreLocation(expr)) { + return true; + } + auto e = getSubExprOfLogicalNegation(expr->getSubExpr()); + if (e) { + // Ignore macros, otherwise + // OSL_ENSURE(!b, ...); + // triggers. + if (e->getBeginLoc().isMacroID()) + return true; + // double logical not of an int is an idiom to convert to bool + auto const sub = ignoreAllImplicit(e); + if (!sub->getType()->isBooleanType()) + return true; + report( + DiagnosticsEngine::Warning, + ("double logical negation expression of the form '!!A' (with A of type" + " %0) can %select{logically|literally}1 be simplified as 'A'"), + expr->getBeginLoc()) + << sub->getType() + << sub->getType()->isBooleanType() + << expr->getSourceRange(); + return true; + } + auto sub = expr->getSubExpr()->IgnoreParenImpCasts(); + auto reversed = false; + if (auto const rewritten = dyn_cast<CXXRewrittenBinaryOperator>(sub)) { + if (rewritten->isReversed()) { + if (rewritten->getOperator() == BO_EQ) { + auto const sem = rewritten->getSemanticForm(); + bool match; + if (auto const op1 = dyn_cast<BinaryOperator>(sem)) { + match = op1->getOpcode() == BO_EQ; + } else if (auto const op2 = dyn_cast<CXXOperatorCallExpr>(sem)) { + match = op2->getOperator() == OO_EqualEqual; + } else { + match = false; + } + if (match) { + sub = sem; + reversed = true; + } + } + } + } + if (auto binaryOp = dyn_cast<BinaryOperator>(sub)) { + // Ignore macros, otherwise + // OSL_ENSURE(!b, ...); + // triggers. + if (binaryOp->getBeginLoc().isMacroID()) + return true; + if (binaryOp->isComparisonOp()) + { + auto t = binaryOp->getLHS()->IgnoreImpCasts()->getType()->getUnqualifiedDesugaredType(); + if (t->isTemplateTypeParmType() || t->isDependentType() || t->isRecordType()) + return true; + // for floating point (with NaN) !(x<y) need not be equivalent to x>=y + if (t->isFloatingType() || + binaryOp->getRHS()->IgnoreImpCasts()->getType()->getUnqualifiedDesugaredType()->isFloatingType()) + return true; + report( + DiagnosticsEngine::Warning, + ("logical negation of comparison operator, can be simplified by inverting operator"), + expr->getBeginLoc()) + << expr->getSourceRange(); + } + else if (binaryOp->isLogicalOp()) + { + // if we find a negation condition inside, it is definitely better + // to expand it out + bool foundLNot = false; + auto containsNegationOrComparison = [&](Expr const * expr) { + expr = ignoreParenImpCastAndComma(expr); + if (auto unaryOp = dyn_cast<UnaryOperator>(expr)) + if (unaryOp->getOpcode() == UO_LNot) + { + foundLNot = true; + return expr; + } + if (auto binaryOp = dyn_cast<BinaryOperator>(expr)) + if (binaryOp->isComparisonOp()) + return expr; + if (auto cxxOpCall = dyn_cast<CXXOperatorCallExpr>(expr)) + if (cxxOpCall->isComparisonOp()) + return expr; + return (Expr const*)nullptr; + }; + auto lhs = containsNegationOrComparison(binaryOp->getLHS()); + auto rhs = containsNegationOrComparison(binaryOp->getRHS()); + if (foundLNot || (lhs && rhs)) + report( + DiagnosticsEngine::Warning, + ("logical negation of logical op containing negation, can be simplified"), + binaryOp->getBeginLoc()) + << binaryOp->getSourceRange(); + } + } + if (auto binaryOp = dyn_cast<CXXOperatorCallExpr>(sub)) { + // Ignore macros, otherwise + // OSL_ENSURE(!b, ...); + // triggers. + if (binaryOp->getBeginLoc().isMacroID()) + return true; + auto op = binaryOp->getOperator(); + // Negating things like > and >= would probably not be wise, there is no guarantee the negation holds for operator overloaded types. + // However, == and != are normally considered ok. + if (!(op == OO_EqualEqual || op == OO_ExclaimEqual)) + return true; + BinaryOperator::Opcode negatedOpcode = BinaryOperator::negateComparisonOp(BinaryOperator::getOverloadedOpcode(op)); + auto lhs = binaryOp->getArg(reversed ? 1 : 0)->IgnoreImpCasts()->getType()->getUnqualifiedDesugaredType(); + auto rhs = binaryOp->getArg(reversed ? 0 : 1)->IgnoreImpCasts()->getType()->getUnqualifiedDesugaredType(); + auto const negOp = findOperator(compiler, negatedOpcode, lhs, rhs); + if (!negOp) + return true; + // if we are inside a similar operator, ignore, eg. operator!= is often defined by calling !operator== + if (m_insideFunctionDecl && m_insideFunctionDecl->getNumParams() >= 1) { + auto t = stripConstRef(m_insideFunctionDecl->getParamDecl(0)->getType().getTypePtr()); + if (t == lhs) + return true; + } + // QA code + StringRef fn(handler.getMainFileName()); + if (loplugin::isSamePathname(fn, SRCDIR "/testtools/source/bridgetest/bridgetest.cxx")) + return true; + report( + DiagnosticsEngine::Warning, + ("logical negation of comparison operator, can be simplified by inverting operator"), + expr->getBeginLoc()) + << expr->getSourceRange(); + if (negOp != ASSUME_OPERATOR_EXISTS) + report( + DiagnosticsEngine::Note, "the presumed corresponding negated operator for %0 and %1 is declared here", + negOp->getLocation()) + << binaryOp->getArg(reversed ? 1 : 0)->IgnoreImpCasts()->getType() + << binaryOp->getArg(reversed ? 0 : 1)->IgnoreImpCasts()->getType() + << negOp->getSourceRange(); + } + return true; +} + +bool SimplifyBool::VisitBinaryOperator(BinaryOperator const * expr) { + switch (expr->getOpcode()) { + case BO_LT: + return visitBinLT(expr); + case BO_GT: + return visitBinGT(expr); + case BO_LE: + return visitBinLE(expr); + case BO_GE: + return visitBinGE(expr); + case BO_EQ: + return visitBinEQ(expr); + case BO_NE: + return visitBinNE(expr); + default: + return true; + } +} + +bool SimplifyBool::visitBinLT(BinaryOperator const * expr) { + if (ignoreLocation(expr)) { + return true; + } + if (!(expr->getLHS()->IgnoreImpCasts()->getType()->isBooleanType() + && expr->getRHS()->IgnoreImpCasts()->getType()->isBooleanType())) + { + return true; + } + auto v1 = getValue(expr->getLHS()); + auto v2 = getValue(expr->getRHS()); + switch (v1) { + case Value::Unknown: + switch (v2) { + case Value::Unknown: + break; + case Value::False: + report( + DiagnosticsEngine::Warning, + ("less-than expression of the form 'A < false' (with A of type" + " %0) can logically be simplified as 'false'"), + expr->getBeginLoc()) + << expr->getLHS()->IgnoreImpCasts()->getType() + << expr->getSourceRange(); + break; + case Value::True: + { + auto e = getSubExprOfLogicalNegation(expr->getLHS()); + if (e == nullptr) { + report( + DiagnosticsEngine::Warning, + ("less-than expression of the form 'A < true' (with A" + " of type %0) can %select{logically|literally}1 be" + " simplified as '!A'"), + expr->getBeginLoc()) + << expr->getLHS()->IgnoreImpCasts()->getType() + << (expr->getLHS()->IgnoreImpCasts()->getType() + ->isBooleanType()) + << expr->getSourceRange(); + } else { + report( + DiagnosticsEngine::Warning, + ("less-than expression of the form '!A < true' (with A" + " of type %0) can %select{logically|literally}1 be" + " simplified as 'A'"), + expr->getBeginLoc()) + << e->IgnoreImpCasts()->getType() + << e->IgnoreImpCasts()->getType()->isBooleanType() + << expr->getSourceRange(); + } + break; + } + } + break; + case Value::False: + switch (v2) { + case Value::Unknown: + report( + DiagnosticsEngine::Warning, + ("less-than expression of the form 'false < A' (with A of type" + " %0) can %select{logically|literally}1 be simplified as 'A'"), + expr->getBeginLoc()) + << expr->getRHS()->IgnoreImpCasts()->getType() + << expr->getRHS()->IgnoreImpCasts()->getType()->isBooleanType() + << expr->getSourceRange(); + break; + case Value::False: + report( + DiagnosticsEngine::Warning, + ("less-than expression of the form 'false < false' can" + " literally be simplified as 'false'"), + expr->getBeginLoc()) + << expr->getSourceRange(); + break; + case Value::True: + report( + DiagnosticsEngine::Warning, + ("less-than expression of the form 'false < true' can" + " literally be simplified as 'true'"), + expr->getBeginLoc()) + << expr->getSourceRange(); + break; + } + break; + case Value::True: + switch (v2) { + case Value::Unknown: + report( + DiagnosticsEngine::Warning, + ("less-than expression of the form 'true < A' (with A of type" + " %0) can logically be simplified as 'false'"), + expr->getBeginLoc()) + << expr->getRHS()->IgnoreImpCasts()->getType() + << expr->getSourceRange(); + break; + case Value::False: + report( + DiagnosticsEngine::Warning, + ("less-than expression of the form 'true < false' can" + " literally be simplified as 'false'"), + expr->getBeginLoc()) + << expr->getSourceRange(); + break; + case Value::True: + report( + DiagnosticsEngine::Warning, + ("less-than expression of the form 'true < true' can" + " literally be simplified as 'false'"), + expr->getBeginLoc()) + << expr->getSourceRange(); + break; + } + break; + } + return true; +} + +bool SimplifyBool::visitBinGT(BinaryOperator const * expr) { + if (ignoreLocation(expr)) { + return true; + } + if (!(expr->getLHS()->IgnoreImpCasts()->getType()->isBooleanType() + && expr->getRHS()->IgnoreImpCasts()->getType()->isBooleanType())) + { + return true; + } + auto v1 = getValue(expr->getLHS()); + auto v2 = getValue(expr->getRHS()); + switch (v1) { + case Value::Unknown: + switch (v2) { + case Value::Unknown: + break; + case Value::False: + report( + DiagnosticsEngine::Warning, + ("greater-than expression of the form 'A > false' (with A of" + " type %0) can %select{logically|literally}1 be simplified as" + " 'A'"), + expr->getBeginLoc()) + << expr->getLHS()->IgnoreImpCasts()->getType() + << expr->getLHS()->IgnoreImpCasts()->getType()->isBooleanType() + << expr->getSourceRange(); + break; + case Value::True: + report( + DiagnosticsEngine::Warning, + ("greater-than expression of the form 'A > true' (with A of" + " type %0) can logically be simplified as 'false'"), + expr->getBeginLoc()) + << expr->getLHS()->IgnoreImpCasts()->getType() + << expr->getSourceRange(); + break; + } + break; + case Value::False: + switch (v2) { + case Value::Unknown: + report( + DiagnosticsEngine::Warning, + ("greater-than expression of the form 'false > A' (with A of" + " type %0) can logically be simplified as 'false'"), + expr->getBeginLoc()) + << expr->getRHS()->IgnoreImpCasts()->getType() + << expr->getSourceRange(); + break; + case Value::False: + report( + DiagnosticsEngine::Warning, + ("greater-than expression of the form 'false > false' can" + " literally be simplified as 'false'"), + expr->getBeginLoc()) + << expr->getSourceRange(); + break; + case Value::True: + report( + DiagnosticsEngine::Warning, + ("greater-than expression of the form 'false > true' can" + " literally be simplified as 'false'"), + expr->getBeginLoc()) + << expr->getSourceRange(); + break; + } + break; + case Value::True: + switch (v2) { + case Value::Unknown: + { + auto e = getSubExprOfLogicalNegation(expr->getRHS()); + if (e == nullptr) { + report( + DiagnosticsEngine::Warning, + ("greater-than expression of the form 'true > A' (with" + " A of type %0) can %select{logically|literally}1 be" + " simplified as '!A'"), + expr->getBeginLoc()) + << expr->getRHS()->IgnoreImpCasts()->getType() + << (expr->getRHS()->IgnoreImpCasts()->getType() + ->isBooleanType()) + << expr->getSourceRange(); + } else { + report( + DiagnosticsEngine::Warning, + ("greater-than expression of the form 'true > !A' (with" + " A of type %0) can %select{logically|literally}1 be" + " simplified as 'A'"), + expr->getBeginLoc()) + << e->IgnoreImpCasts()->getType() + << e->IgnoreImpCasts()->getType()->isBooleanType() + << expr->getSourceRange(); + } + break; + } + case Value::False: + report( + DiagnosticsEngine::Warning, + ("greater-than expression of the form 'true > false' can" + " literally be simplified as 'true'"), + expr->getBeginLoc()) + << expr->getSourceRange(); + break; + case Value::True: + report( + DiagnosticsEngine::Warning, + ("greater-than expression of the form 'true > true' can" + " literally be simplified as 'false'"), + expr->getBeginLoc()) + << expr->getSourceRange(); + break; + } + break; + } + return true; +} + +bool SimplifyBool::visitBinLE(BinaryOperator const * expr) { + if (ignoreLocation(expr)) { + return true; + } + if (!(expr->getLHS()->IgnoreImpCasts()->getType()->isBooleanType() + && expr->getRHS()->IgnoreImpCasts()->getType()->isBooleanType())) + { + return true; + } + auto v1 = getValue(expr->getLHS()); + auto v2 = getValue(expr->getRHS()); + switch (v1) { + case Value::Unknown: + switch (v2) { + case Value::Unknown: + break; + case Value::False: + { + auto e = getSubExprOfLogicalNegation(expr->getLHS()); + if (e == nullptr) { + report( + DiagnosticsEngine::Warning, + ("less-than-or-equal-to expression of the form 'A <=" + " false' (with A of type %0) can" + " %select{logically|literally}1 be simplified as" + " '!A'"), + expr->getBeginLoc()) + << expr->getLHS()->IgnoreImpCasts()->getType() + << (expr->getLHS()->IgnoreImpCasts()->getType() + ->isBooleanType()) + << expr->getSourceRange(); + } else { + report( + DiagnosticsEngine::Warning, + ("less-than-or-equal-to expression of the form '!A <=" + " false' (with A of type %0) can" + " %select{logically|literally}1 be simplified as 'A'"), + expr->getBeginLoc()) + << e->IgnoreImpCasts()->getType() + << e->IgnoreImpCasts()->getType()->isBooleanType() + << expr->getSourceRange(); + } + break; + } + case Value::True: + report( + DiagnosticsEngine::Warning, + ("less-than-or-equal-to expression of the form 'A <= true'" + " (with A of type %0) can logically be simplified as 'true'"), + expr->getBeginLoc()) + << expr->getLHS()->IgnoreImpCasts()->getType() + << expr->getSourceRange(); + break; + } + break; + case Value::False: + switch (v2) { + case Value::Unknown: + report( + DiagnosticsEngine::Warning, + ("less-than-or-equal-to expression of the form 'false <= A'" + " (with A of type %0) can logically be simplified as 'true'"), + expr->getBeginLoc()) + << expr->getRHS()->IgnoreImpCasts()->getType() + << expr->getSourceRange(); + break; + case Value::False: + report( + DiagnosticsEngine::Warning, + ("less-than-or-equal-to expression of the form 'false <= false'" + " can literally be simplified as 'true'"), + expr->getBeginLoc()) + << expr->getSourceRange(); + break; + case Value::True: + report( + DiagnosticsEngine::Warning, + ("less-than-or-equal-to expression of the form 'false <= true'" + " can literally be simplified as 'true'"), + expr->getBeginLoc()) + << expr->getSourceRange(); + break; + } + break; + case Value::True: + switch (v2) { + case Value::Unknown: + report( + DiagnosticsEngine::Warning, + ("less-than-or-equal-to expression of the form 'true <= A'" + " (with A of type %0) can %select{logically|literally}1 be" + " simplified as 'A'"), + expr->getBeginLoc()) + << expr->getRHS()->IgnoreImpCasts()->getType() + << expr->getRHS()->IgnoreImpCasts()->getType()->isBooleanType() + << expr->getSourceRange(); + break; + case Value::False: + report( + DiagnosticsEngine::Warning, + ("less-than-or-equal-to expression of the form 'true <= false'" + " can literally be simplified as 'false'"), + expr->getBeginLoc()) + << expr->getSourceRange(); + break; + case Value::True: + report( + DiagnosticsEngine::Warning, + ("less-than-or-equal-to expression of the form 'true <= true'" + " can literally be simplified as 'true'"), + expr->getBeginLoc()) + << expr->getSourceRange(); + break; + } + break; + } + return true; +} + +bool SimplifyBool::visitBinGE(BinaryOperator const * expr) { + if (ignoreLocation(expr)) { + return true; + } + if (!(expr->getLHS()->IgnoreImpCasts()->getType()->isBooleanType() + && expr->getRHS()->IgnoreImpCasts()->getType()->isBooleanType())) + { + return true; + } + auto v1 = getValue(expr->getLHS()); + auto v2 = getValue(expr->getRHS()); + switch (v1) { + case Value::Unknown: + switch (v2) { + case Value::Unknown: + break; + case Value::False: + report( + DiagnosticsEngine::Warning, + ("greater-than-or-equal-to expression of the form 'A >= false'" + " (with A of type %0) can logically be simplified as 'true'"), + expr->getBeginLoc()) + << expr->getLHS()->IgnoreImpCasts()->getType() + << expr->getSourceRange(); + break; + case Value::True: + report( + DiagnosticsEngine::Warning, + ("greater-than-or-equal-to expression of the form 'A >= true'" + " (with A of type %0) can %select{logically|literally}1 be" + " simplified as 'A'"), + expr->getBeginLoc()) + << expr->getLHS()->IgnoreImpCasts()->getType() + << expr->getLHS()->IgnoreImpCasts()->getType()->isBooleanType() + << expr->getSourceRange(); + break; + } + break; + case Value::False: + switch (v2) { + case Value::Unknown: + { + auto e = getSubExprOfLogicalNegation(expr->getRHS()); + if (e == nullptr) { + report( + DiagnosticsEngine::Warning, + ("greater-than-or-equal-to expression of the form" + " 'false >= A' (with A of type %0) can" + " %select{logically|literally}1 be simplified as" + " '!A'"), + expr->getBeginLoc()) + << expr->getRHS()->IgnoreImpCasts()->getType() + << (expr->getRHS()->IgnoreImpCasts()->getType() + ->isBooleanType()) + << expr->getSourceRange(); + } else { + report( + DiagnosticsEngine::Warning, + ("greater-than-or-equal-to expression of the form" + " 'false >= !A' (with A of type %0) can" + " %select{logically|literally}1 be simplified as 'A'"), + expr->getBeginLoc()) + << e->IgnoreImpCasts()->getType() + << e->IgnoreImpCasts()->getType()->isBooleanType() + << expr->getSourceRange(); + } + break; + } + case Value::False: + report( + DiagnosticsEngine::Warning, + ("greater-than-or-equal-to expression of the form 'false >=" + " false' can literally be simplified as 'true'"), + expr->getBeginLoc()) + << expr->getSourceRange(); + break; + case Value::True: + report( + DiagnosticsEngine::Warning, + ("greater-than-or-equal-to expression of the form 'false >=" + " true' can literally be simplified as 'false'"), + expr->getBeginLoc()) + << expr->getSourceRange(); + break; + } + break; + case Value::True: + switch (v2) { + case Value::Unknown: + report( + DiagnosticsEngine::Warning, + ("greater-than-or-equal-to expression of the form 'true >= A'" + " (with A of type %0) can logically be simplified as 'true'"), + expr->getBeginLoc()) + << expr->getRHS()->IgnoreImpCasts()->getType() + << expr->getSourceRange(); + break; + case Value::False: + report( + DiagnosticsEngine::Warning, + ("greater-than-or-equal-to expression of the form 'true >=" + " false' can literally be simplified as 'true'"), + expr->getBeginLoc()) + << expr->getSourceRange(); + break; + case Value::True: + report( + DiagnosticsEngine::Warning, + ("greater-than-or-equal-to expression of the form 'true >=" + " true' can literally be simplified as 'true'"), + expr->getBeginLoc()) + << expr->getSourceRange(); + break; + } + break; + } + return true; +} + +bool SimplifyBool::visitBinEQ(BinaryOperator const * expr) { + if (ignoreLocation(expr)) { + return true; + } + if (!(expr->getLHS()->IgnoreImpCasts()->getType()->isBooleanType() + && expr->getRHS()->IgnoreImpCasts()->getType()->isBooleanType())) + { + return true; + } + auto v1 = getValue(expr->getLHS()); + auto v2 = getValue(expr->getRHS()); + switch (v1) { + case Value::Unknown: + switch (v2) { + case Value::Unknown: + break; + case Value::False: + { + auto e = getSubExprOfLogicalNegation(expr->getLHS()); + if (e == nullptr) { + report( + DiagnosticsEngine::Warning, + ("equal-to expression of the form 'A == false' (with A" + " of type %0) can %select{logically|literally}1 be" + " simplified as '!A'"), + expr->getBeginLoc()) + << expr->getLHS()->IgnoreImpCasts()->getType() + << (expr->getLHS()->IgnoreImpCasts()->getType() + ->isBooleanType()) + << expr->getSourceRange(); + } else { + report( + DiagnosticsEngine::Warning, + ("equal-to expression of the form '!A == false' (with A" + " of type %0) can %select{logically|literally}1 be" + " simplified as 'A'"), + expr->getBeginLoc()) + << e->IgnoreImpCasts()->getType() + << e->IgnoreImpCasts()->getType()->isBooleanType() + << expr->getSourceRange(); + } + break; + } + case Value::True: + report( + DiagnosticsEngine::Warning, + ("equal-to expression of the form 'A == true' (with A of type" + " %0) can %select{logically|literally}1 be simplified as 'A'"), + expr->getBeginLoc()) + << expr->getLHS()->IgnoreImpCasts()->getType() + << expr->getLHS()->IgnoreImpCasts()->getType()->isBooleanType() + << expr->getSourceRange(); + break; + } + break; + case Value::False: + switch (v2) { + case Value::Unknown: + { + auto e = getSubExprOfLogicalNegation(expr->getRHS()); + if (e == nullptr) { + report( + DiagnosticsEngine::Warning, + ("equal-to expression of the form 'false == A' (with A" + " of type %0) can %select{logically|literally}1 be" + " simplified as '!A'"), + expr->getBeginLoc()) + << expr->getRHS()->IgnoreImpCasts()->getType() + << (expr->getRHS()->IgnoreImpCasts()->getType() + ->isBooleanType()) + << expr->getSourceRange(); + } else { + report( + DiagnosticsEngine::Warning, + ("equal-to expression of the form 'false == !A' (with A" + " of type %0) can %select{logically|literally}1 be" + " simplified as 'A'"), + expr->getBeginLoc()) + << e->IgnoreImpCasts()->getType() + << e->IgnoreImpCasts()->getType()->isBooleanType() + << expr->getSourceRange(); + } + break; + } + case Value::False: + report( + DiagnosticsEngine::Warning, + ("equal-to expression of the form 'false == false' can" + " literally be simplified as 'true'"), + expr->getBeginLoc()) + << expr->getSourceRange(); + break; + case Value::True: + report( + DiagnosticsEngine::Warning, + ("equal-to expression of the form 'false == true' can" + " literally be simplified as 'false'"), + expr->getBeginLoc()) + << expr->getSourceRange(); + break; + } + break; + case Value::True: + switch (v2) { + case Value::Unknown: + report( + DiagnosticsEngine::Warning, + ("equal-to expression of the form 'true == A' (with A of type" + " %0) can %select{logically|literally}1 be simplified as 'A'"), + expr->getBeginLoc()) + << expr->getRHS()->IgnoreImpCasts()->getType() + << expr->getRHS()->IgnoreImpCasts()->getType()->isBooleanType() + << expr->getSourceRange(); + break; + case Value::False: + report( + DiagnosticsEngine::Warning, + ("equal-to expression of the form 'true == false' can" + " literally be simplified as 'false'"), + expr->getBeginLoc()) + << expr->getSourceRange(); + break; + case Value::True: + report( + DiagnosticsEngine::Warning, + ("equal-to expression of the form 'true == true' can" + " literally be simplified as 'true'"), + expr->getBeginLoc()) + << expr->getSourceRange(); + break; + } + break; + } + return true; +} + +bool SimplifyBool::visitBinNE(BinaryOperator const * expr) { + if (ignoreLocation(expr)) { + return true; + } + if (!(expr->getLHS()->IgnoreImpCasts()->getType()->isBooleanType() + && expr->getRHS()->IgnoreImpCasts()->getType()->isBooleanType())) + { + return true; + } + auto v1 = getValue(expr->getLHS()); + auto v2 = getValue(expr->getRHS()); + switch (v1) { + case Value::Unknown: + switch (v2) { + case Value::Unknown: + break; + case Value::False: + report( + DiagnosticsEngine::Warning, + ("not-equal-to expression of the form 'A != false' (with A of" + " type %0) can %select{logically|literally}1 be simplified as" + " 'A'"), + expr->getBeginLoc()) + << expr->getLHS()->IgnoreImpCasts()->getType() + << expr->getLHS()->IgnoreImpCasts()->getType()->isBooleanType() + << expr->getSourceRange(); + break; + case Value::True: + { + auto e = getSubExprOfLogicalNegation(expr->getLHS()); + if (e == nullptr) { + report( + DiagnosticsEngine::Warning, + ("not-equal-to expression of the form 'A != true' (with" + " A of type %0) can %select{logically|literally}1 be" + " simplified as '!A'"), + expr->getBeginLoc()) + << expr->getLHS()->IgnoreImpCasts()->getType() + << (expr->getLHS()->IgnoreImpCasts()->getType() + ->isBooleanType()) + << expr->getSourceRange(); + } else { + report( + DiagnosticsEngine::Warning, + ("not-equal-to expression of the form '!A != true'" + " (with A of type %0) can" + " %select{logically|literally}1 be simplified as 'A'"), + expr->getBeginLoc()) + << e->IgnoreImpCasts()->getType() + << e->IgnoreImpCasts()->getType()->isBooleanType() + << expr->getSourceRange(); + } + break; + } + } + break; + case Value::False: + switch (v2) { + case Value::Unknown: + report( + DiagnosticsEngine::Warning, + ("not-equal-to expression of the form 'false != A' (with A of" + " type %0) can %select{logically|literally}1 be simplified as" + " 'A'"), + expr->getBeginLoc()) + << expr->getRHS()->IgnoreImpCasts()->getType() + << expr->getRHS()->IgnoreImpCasts()->getType()->isBooleanType() + << expr->getSourceRange(); + break; + case Value::False: + report( + DiagnosticsEngine::Warning, + ("not-equal-to expression of the form 'false != false' can" + " literally be simplified as 'false'"), + expr->getBeginLoc()) + << expr->getSourceRange(); + break; + case Value::True: + report( + DiagnosticsEngine::Warning, + ("not-equal-to expression of the form 'false != true' can" + " literally be simplified as 'true'"), + expr->getBeginLoc()) + << expr->getSourceRange(); + break; + } + break; + case Value::True: + switch (v2) { + case Value::Unknown: + { + auto e = getSubExprOfLogicalNegation(expr->getRHS()); + if (e == nullptr) { + report( + DiagnosticsEngine::Warning, + ("not-equal-to expression of the form 'true != A' (with" + " A of type %0) can %select{logically|literally}1 be" + " simplified as '!A'"), + expr->getBeginLoc()) + << expr->getRHS()->IgnoreImpCasts()->getType() + << (expr->getRHS()->IgnoreImpCasts()->getType() + ->isBooleanType()) + << expr->getSourceRange(); + } else { + report( + DiagnosticsEngine::Warning, + ("not-equal-to expression of the form 'true != !A'" + " (with A of type %0) can" + " %select{logically|literally}1 be simplified as 'A'"), + expr->getBeginLoc()) + << e->IgnoreImpCasts()->getType() + << e->IgnoreImpCasts()->getType()->isBooleanType() + << expr->getSourceRange(); + } + break; + } + case Value::False: + report( + DiagnosticsEngine::Warning, + ("not-equal-to expression of the form 'true != false' can" + " literally be simplified as 'true'"), + expr->getBeginLoc()) + << expr->getSourceRange(); + break; + case Value::True: + report( + DiagnosticsEngine::Warning, + ("not-equal-to expression of the form 'true != true' can" + " literally be simplified as 'false'"), + expr->getBeginLoc()) + << expr->getSourceRange(); + break; + } + break; + } + return true; +} + +bool SimplifyBool::VisitConditionalOperator(ConditionalOperator const * expr) { + if (ignoreLocation(expr)) { + return true; + } + auto v1 = getValue(expr->getTrueExpr()); + auto v2 = getValue(expr->getFalseExpr()); + switch (v1) { + case Value::Unknown: + switch (v2) { + case Value::Unknown: + break; + case Value::False: + report( + DiagnosticsEngine::Warning, + ("conditional expression of the form 'A ? B : false' (with A of" + " type %0 and B of type %1) can %select{logically|literally}2" + " be simplified as 'A && B'"), + expr->getBeginLoc()) + << expr->getCond()->IgnoreImpCasts()->getType() + << expr->getTrueExpr()->IgnoreImpCasts()->getType() + << ((expr->getCond()->IgnoreImpCasts()->getType() + ->isBooleanType()) + && (expr->getTrueExpr()->IgnoreImpCasts()->getType() + ->isBooleanType())) + << expr->getSourceRange(); + break; + case Value::True: + { + auto e = getSubExprOfLogicalNegation(expr->getCond()); + if (e == nullptr) { + report( + DiagnosticsEngine::Warning, + ("conditional expression of the form 'A ? B : true'" + " (with A of type %0 and B of type %1) can" + " %select{logically|literally}2 be simplified as '!A" + " || B'"), + expr->getBeginLoc()) + << expr->getCond()->IgnoreImpCasts()->getType() + << expr->getTrueExpr()->IgnoreImpCasts()->getType() + << ((expr->getCond()->IgnoreImpCasts()->getType() + ->isBooleanType()) + && (expr->getTrueExpr()->IgnoreImpCasts()->getType() + ->isBooleanType())) + << expr->getSourceRange(); + } else { + report( + DiagnosticsEngine::Warning, + ("conditional expression of the form '!A ? B : true'" + " (with A of type %0 and B of type %1) can" + " %select{logically|literally}2 be simplified as 'A ||" + " B'"), + expr->getBeginLoc()) + << e->IgnoreImpCasts()->getType() + << expr->getTrueExpr()->IgnoreImpCasts()->getType() + << (e->IgnoreImpCasts()->getType()->isBooleanType() + && (expr->getTrueExpr()->IgnoreImpCasts() + ->getType()->isBooleanType())) + << expr->getSourceRange(); + } + break; + } + } + break; + case Value::False: + switch (v2) { + case Value::Unknown: + { + auto e = getSubExprOfLogicalNegation(expr->getCond()); + if (e == nullptr) { + report( + DiagnosticsEngine::Warning, + ("conditional expression of the form 'A ? false : B'" + " (with A of type %0 and B of type %1) can" + " %select{logically|literally}2 be simplified as '!A" + " && B'"), + expr->getBeginLoc()) + << expr->getCond()->IgnoreImpCasts()->getType() + << expr->getFalseExpr()->IgnoreImpCasts()->getType() + << ((expr->getCond()->IgnoreImpCasts()->getType() + ->isBooleanType()) + && (expr->getFalseExpr()->IgnoreImpCasts() + ->getType()->isBooleanType())) + << expr->getSourceRange(); + } else { + report( + DiagnosticsEngine::Warning, + ("conditional expression of the form '!A ? false : B'" + " (with A of type %0 and B of type %1) can" + " %select{logically|literally}2 be simplified as 'A &&" + " B'"), + expr->getBeginLoc()) + << e->IgnoreImpCasts()->getType() + << expr->getFalseExpr()->IgnoreImpCasts()->getType() + << (e->IgnoreImpCasts()->getType()->isBooleanType() + && (expr->getFalseExpr()->IgnoreImpCasts() + ->getType()->isBooleanType())) + << expr->getSourceRange(); + } + break; + } + case Value::False: + report( + DiagnosticsEngine::Warning, + ("conditional expression of the form 'A ? false : false' (with" + " A of type %0) can logically be simplified as 'false'"), + expr->getBeginLoc()) + << expr->getCond()->IgnoreImpCasts()->getType() + << expr->getSourceRange(); + break; + case Value::True: + { + auto e = getSubExprOfLogicalNegation(expr->getCond()); + if (e == nullptr) { + report( + DiagnosticsEngine::Warning, + ("conditional expression of the form 'A ? false : true'" + " (with A of type %0) can" + " %select{logically|literally}1 be simplified as" + " '!A'"), + expr->getBeginLoc()) + << expr->getCond()->IgnoreImpCasts()->getType() + << (expr->getCond()->IgnoreImpCasts()->getType() + ->isBooleanType()) + << expr->getSourceRange(); + } else { + report( + DiagnosticsEngine::Warning, + ("conditional expression of the form '!A ? false :" + " true' (with A of type %0) can" + " %select{logically|literally}1 be simplified as 'A'"), + expr->getBeginLoc()) + << e->IgnoreImpCasts()->getType() + << e->IgnoreImpCasts()->getType()->isBooleanType() + << expr->getSourceRange(); + } + break; + } + } + break; + case Value::True: + switch (v2) { + case Value::Unknown: + report( + DiagnosticsEngine::Warning, + ("conditional expression of the form 'A ? true : B' (with A of" + " type %0 and B of type %1) can %select{logically|literally}2" + " be simplified as 'A || B'"), + expr->getBeginLoc()) + << expr->getCond()->IgnoreImpCasts()->getType() + << expr->getFalseExpr()->IgnoreImpCasts()->getType() + << ((expr->getCond()->IgnoreImpCasts()->getType() + ->isBooleanType()) + && (expr->getFalseExpr()->IgnoreImpCasts()->getType() + ->isBooleanType())) + << expr->getSourceRange(); + break; + case Value::False: + report( + DiagnosticsEngine::Warning, + ("conditional expression of the form 'A ? true : false' (with A" + " of type %0) can %select{logically|literally}1 be simplified" + " as 'A'"), + expr->getBeginLoc()) + << expr->getCond()->IgnoreImpCasts()->getType() + << expr->getCond()->IgnoreImpCasts()->getType()->isBooleanType() + << expr->getSourceRange(); + break; + case Value::True: + report( + DiagnosticsEngine::Warning, + ("conditional expression of the form 'A ? true : true' (with A" + " of type %0) can logically be simplified as 'true'"), + expr->getBeginLoc()) + << expr->getCond()->IgnoreImpCasts()->getType() + << expr->getSourceRange(); + break; + } + break; + } + return true; +} + +bool SimplifyBool::TraverseFunctionDecl(FunctionDecl * functionDecl) { + auto copy = m_insideFunctionDecl; + m_insideFunctionDecl = functionDecl; + bool ret = RecursiveASTVisitor::TraverseFunctionDecl(functionDecl); + m_insideFunctionDecl = copy; + return ret; +} + +bool SimplifyBool::TraverseCXXMethodDecl(CXXMethodDecl * functionDecl) { + auto copy = m_insideFunctionDecl; + m_insideFunctionDecl = functionDecl; + bool ret = RecursiveASTVisitor::TraverseCXXMethodDecl(functionDecl); + m_insideFunctionDecl = copy; + return ret; +} + +loplugin::Plugin::Registration<SimplifyBool> X("simplifybool"); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ 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 <algorithm> +#include <cassert> +#include <limits> +#include <string> + +#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<StdException> +{ +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<FunctionProtoType>(); + 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<FunctionProtoType>(); + 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<unsigned>::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<unsigned>(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<StdException> X("stdexception", true); + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/compilerplugins/clang/store/stringbuffer.cxx b/compilerplugins/clang/store/stringbuffer.cxx new file mode 100644 index 000000000..899c9b6ac --- /dev/null +++ b/compilerplugins/clang/store/stringbuffer.cxx @@ -0,0 +1,75 @@ +/* -*- 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 "check.hxx" +#include "plugin.hxx" +#include <vector> + +/** Look for appending result of adding OUString/OString to OUStringBuffer + */ +namespace +{ +class StringBuffer : public loplugin::FilteringPlugin<StringBuffer> +{ +public: + explicit StringBuffer(loplugin::InstantiationData const& rData) + : FilteringPlugin(rData) + { + } + + bool preRun() override + { + StringRef fn(handler.getMainFileName()); + return !loplugin::hasPathnamePrefix(fn, SRCDIR "/sal/qa/"); + } + + void run() override + { + if (preRun()) + { + TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); + } + } + + bool VisitCXXMemberCallExpr(CXXMemberCallExpr const*); +}; + +bool StringBuffer::VisitCXXMemberCallExpr(CXXMemberCallExpr const* memberCallExpr) +{ + if (ignoreLocation(memberCallExpr)) + return true; + if (!loplugin::DeclCheck(memberCallExpr->getRecordDecl()) + .Class("OUStringBuffer") + .Namespace("rtl") + .GlobalNamespace()) + return true; + if (!memberCallExpr->getMethodDecl()->getIdentifier()) + return true; + if (memberCallExpr->getMethodDecl()->getName() != "append") + return true; + auto matTemp = dyn_cast<MaterializeTemporaryExpr>(memberCallExpr->getArg(0)); + if (!matTemp) + return true; + if (!isa<CXXOperatorCallExpr>(matTemp->getSubExpr())) + return true; + report(DiagnosticsEngine::Warning, + "appending added result of OUString to OUStringBuffer, rather do .append(x).append(y)", + memberCallExpr->getBeginLoc()) + << memberCallExpr->getSourceRange(); + return true; +} + +loplugin::Plugin::Registration<StringBuffer> stringbuffer("stringbuffer", false); + +} // namespace + +#endif // LO_CLANG_SHARED_PLUGINS + +/* 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 <memory> +#include <regex> +#include <string> +#include <set> + +#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<StylePolice> +{ +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 <clang/Lex/Lexer.h> +#include <iostream> + +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*>(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..ffae241d4 --- /dev/null +++ b/compilerplugins/clang/store/test/deadclass.cxx @@ -0,0 +1,15 @@ +/* -*- 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..10f73f04b --- /dev/null +++ b/compilerplugins/clang/store/tutorial/tutorial1.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. + * + */ + +#pragma once + +#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 + +/* 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..9ae2de354 --- /dev/null +++ b/compilerplugins/clang/store/tutorial/tutorial2.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. + * + */ + +#pragma once + +#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 + +/* 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..11378ef76 --- /dev/null +++ b/compilerplugins/clang/store/tutorial/tutorial3.hxx @@ -0,0 +1,37 @@ +/* -*- 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. + * + */ + +#pragma once + +#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 + +/* 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..808e0c158 --- /dev/null +++ b/compilerplugins/clang/store/valueof.cxx @@ -0,0 +1,148 @@ +/* -*- 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 + { + 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 <line:6:5, col:16> 'void' +// | |-ImplicitCastExpr 0x1a84f08 <col:5> 'void (*)(int)' <FunctionToPointerDecay> +// | | `-DeclRefExpr 0x1a84eb8 <col:5> 'void (int)' lvalue Function 0x1a58900 'f' 'void (int)' +// | `-CXXFunctionalCastExpr 0x1a84e90 <col:8, col:15> 'int' functional cast to int <NoOp> +// | `-ImplicitCastExpr 0x1a84e78 <col:13> 'int' <IntegralCast> +// | `-ImplicitCastExpr 0x1a84e60 <col:13> 'char' <LValueToRValue> +// | `-DeclRefExpr 0x1a58b88 <col:13> '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_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: */ |