[Libreoffice-commits] core.git: 3 commits - compilerplugins/clang

Noel Grandin noel.grandin at collabora.co.uk
Fri Oct 27 08:32:19 UTC 2017


 compilerplugins/clang/checkunusedparams.cxx   |    2 -
 compilerplugins/clang/constantparam.cxx       |    9 +++----
 compilerplugins/clang/constparams.cxx         |    2 -
 compilerplugins/clang/dodgyswitch.cxx         |    4 +--
 compilerplugins/clang/expandablemethods.cxx   |    2 -
 compilerplugins/clang/inlinefields.cxx        |    4 +--
 compilerplugins/clang/memoryvar.cxx           |    2 -
 compilerplugins/clang/plugin.cxx              |   33 ++++++++------------------
 compilerplugins/clang/plugin.hxx              |    8 ++----
 compilerplugins/clang/singlevalfields.cxx     |   10 +++----
 compilerplugins/clang/unusedenumconstants.cxx |    2 -
 compilerplugins/clang/unusedfields.cxx        |    2 -
 compilerplugins/clang/unusedmethods.cxx       |    6 ++--
 13 files changed, 36 insertions(+), 50 deletions(-)

New commits:
commit 8136a87d9af2cb6b7aecc6cf450d13904e155de8
Author: Noel Grandin <noel.grandin at collabora.co.uk>
Date:   Fri Oct 27 10:15:59 2017 +0200

    loplugin:constantparam was not always using canonical location for function
    
    Change-Id: I8a15da534ba2cf9968cba0ee1f1bb74d7e3a0d54

diff --git a/compilerplugins/clang/constantparam.cxx b/compilerplugins/clang/constantparam.cxx
index 376d9df9731d..426e86995040 100644
--- a/compilerplugins/clang/constantparam.cxx
+++ b/compilerplugins/clang/constantparam.cxx
@@ -236,9 +236,8 @@ bool ConstantParam::VisitCallExpr(const CallExpr * callExpr) {
     else {
         functionDecl = callExpr->getDirectCallee();
     }
-    if (functionDecl == nullptr) {
+    if (!functionDecl)
         return true;
-    }
     functionDecl = functionDecl->getCanonicalDecl();
     // method overrides don't always specify the same default params (although they probably should)
     // so we need to work our way up to the root method
@@ -282,10 +281,10 @@ bool ConstantParam::VisitCallExpr(const CallExpr * callExpr) {
 bool ConstantParam::VisitDeclRefExpr( const DeclRefExpr* declRefExpr )
 {
     const Decl* decl = declRefExpr->getDecl();
-    if (!isa<FunctionDecl>(decl)) {
-        return true;
-    }
     const FunctionDecl* functionDecl = dyn_cast<FunctionDecl>(decl);
+    if (!functionDecl)
+        return true;
+    functionDecl = functionDecl->getCanonicalDecl();
     for (unsigned i = 0; i < functionDecl->getNumParams(); ++i)
     {
         addToCallSet(functionDecl, i, functionDecl->getParamDecl(i)->getName(), "unknown3");
commit 8a39134d5c177ea9735424a8e9f40bfd8986a1c6
Author: Noel Grandin <noel.grandin at collabora.co.uk>
Date:   Fri Oct 27 10:15:33 2017 +0200

    no need to build the parent tree twice
    
    lets just use the built in parents stuff
    
    Change-Id: I7bb705acfcd6c8c18168676b0cdb13c26ba5b95a

diff --git a/compilerplugins/clang/plugin.cxx b/compilerplugins/clang/plugin.cxx
index d7ab4ad3e63e..e512cc399d16 100644
--- a/compilerplugins/clang/plugin.cxx
+++ b/compilerplugins/clang/plugin.cxx
@@ -116,23 +116,20 @@ void Plugin::registerPlugin( Plugin* (*create)( const InstantiationData& ), cons
     PluginHandler::registerPlugin( create, optionName, isPPCallback, byDefault );
 }
 
-std::unordered_map< const Stmt*, const Stmt* > Plugin::parents;
-
 const Stmt* Plugin::getParentStmt( const Stmt* stmt )
 {
-    if( parents.empty())
-        buildParents( compiler );
-    //if(parents.count(stmt)!=1)stmt->dump();
-    //assert( parents.count( stmt ) == 1 );
-    return parents[ stmt ];
+    auto parentsRange = compiler.getASTContext().getParents(*stmt);
+    if ( parentsRange.begin() == parentsRange.end())
+        return nullptr;
+    return parentsRange.begin()->get<Stmt>();
 }
 
 Stmt* Plugin::getParentStmt( Stmt* stmt )
 {
-    if( parents.empty())
-        buildParents( compiler );
-    //assert( parents.count( stmt ) == 1 );
-    return const_cast< Stmt* >( parents[ stmt ] );
+    auto parentsRange = compiler.getASTContext().getParents(*stmt);
+    if ( parentsRange.begin() == parentsRange.end())
+        return nullptr;
+    return const_cast<Stmt*>(parentsRange.begin()->get<Stmt>());
 }
 
 static const Decl* getDeclContext(ASTContext& context, const Stmt* stmt)
@@ -256,14 +253,6 @@ void ParentBuilder::walk( const Stmt* stmt )
 
 } // namespace
 
-void Plugin::buildParents( CompilerInstance& compiler )
-{
-    assert( parents.empty());
-    ParentBuilder builder;
-    builder.parents = &parents;
-    builder.TraverseDecl( compiler.getASTContext().getTranslationUnitDecl());
-}
-
 SourceLocation Plugin::locationAfterToken( SourceLocation location )
 {
     return Lexer::getLocForEndOfToken( location, 0, compiler.getSourceManager(), compiler.getLangOpts());
diff --git a/compilerplugins/clang/plugin.hxx b/compilerplugins/clang/plugin.hxx
index 39c1c18b14d1..1f57dfeb0ffa 100644
--- a/compilerplugins/clang/plugin.hxx
+++ b/compilerplugins/clang/plugin.hxx
@@ -88,8 +88,6 @@ private:
     template< typename T > static Plugin* createHelper( const InstantiationData& data );
     enum { isRewriter = false };
     const char* name;
-    static std::unordered_map< const Stmt*, const Stmt* > parents;
-    static void buildParents( CompilerInstance& compiler );
 };
 
 /**
commit 5585bc2fbbe8b6072cfdd5cdaaf326f5124632ce
Author: Noel Grandin <noel.grandin at collabora.co.uk>
Date:   Fri Oct 27 09:51:21 2017 +0200

    rename loplugin::Plugin::parentStmt
    
    to getParentStmt and rename parentFunctionDecl to getParentFunctionDecl,
    so I don't keep using accidentally naming my variables the same as the
    functions.
    
    Change-Id: I66f9452458c8b439e5132191ac5219fb6d420708

diff --git a/compilerplugins/clang/checkunusedparams.cxx b/compilerplugins/clang/checkunusedparams.cxx
index 40f47191a5c6..1de5e5669738 100644
--- a/compilerplugins/clang/checkunusedparams.cxx
+++ b/compilerplugins/clang/checkunusedparams.cxx
@@ -132,7 +132,7 @@ void CheckUnusedParams::checkForFunctionDecl(Expr const * expr, bool bCheckOnly)
     if (!functionDecl)
         return;
     if (bCheckOnly)
-        parentStmt(expr)->dump();
+        getParentStmt(expr)->dump();
     else
         m_addressOfSet.insert(functionDecl->getCanonicalDecl());
 }
diff --git a/compilerplugins/clang/constparams.cxx b/compilerplugins/clang/constparams.cxx
index ca30048f9b1c..bf715d16e91a 100644
--- a/compilerplugins/clang/constparams.cxx
+++ b/compilerplugins/clang/constparams.cxx
@@ -289,7 +289,7 @@ bool ConstParams::VisitDeclRefExpr( const DeclRefExpr* declRefExpr )
 // related ParamVarDecl can be const.
 bool ConstParams::checkIfCanBeConst(const Stmt* stmt, const ParmVarDecl* parmVarDecl)
 {
-    const Stmt* parent = parentStmt( stmt );
+    const Stmt* parent = getParentStmt( stmt );
     if (!parent)
     {
         // check if we're inside a CXXCtorInitializer
diff --git a/compilerplugins/clang/dodgyswitch.cxx b/compilerplugins/clang/dodgyswitch.cxx
index b731e30419f9..171707c8106a 100644
--- a/compilerplugins/clang/dodgyswitch.cxx
+++ b/compilerplugins/clang/dodgyswitch.cxx
@@ -62,13 +62,13 @@ bool DodgySwitch::VisitCaseStmt(CaseStmt const * caseStmt)
 
 bool DodgySwitch::IsParentSwitch(Stmt const * stmt)
 {
-    auto parent = parentStmt(stmt);
+    auto parent = getParentStmt(stmt);
     if (isa<CaseStmt>(parent) || isa<DefaultStmt>(parent)) // daisy chain
         return true;
     auto compoundStmt = dyn_cast<CompoundStmt>(parent);
     if (!compoundStmt)
         return false;
-    return isa<SwitchStmt>(parentStmt(compoundStmt));
+    return isa<SwitchStmt>(getParentStmt(compoundStmt));
 }
 
 loplugin::Plugin::Registration< DodgySwitch > X("dodgyswitch", false);
diff --git a/compilerplugins/clang/expandablemethods.cxx b/compilerplugins/clang/expandablemethods.cxx
index 958d563d0adb..c286711037ed 100644
--- a/compilerplugins/clang/expandablemethods.cxx
+++ b/compilerplugins/clang/expandablemethods.cxx
@@ -272,7 +272,7 @@ void ExpandableMethods::functionTouchedFromExpr( const FunctionDecl* calleeFunct
 
     calledFromSet.emplace(toString(expr->getLocStart()), niceName(canonicalFunctionDecl));
 
-    if (const UnaryOperator* unaryOp = dyn_cast_or_null<UnaryOperator>(parentStmt(expr))) {
+    if (const UnaryOperator* unaryOp = dyn_cast_or_null<UnaryOperator>(getParentStmt(expr))) {
         if (unaryOp->getOpcode() == UO_AddrOf) {
             addressOfSet.insert(niceName(canonicalFunctionDecl));
         }
diff --git a/compilerplugins/clang/inlinefields.cxx b/compilerplugins/clang/inlinefields.cxx
index 5ffb93080e3e..42551dc61660 100644
--- a/compilerplugins/clang/inlinefields.cxx
+++ b/compilerplugins/clang/inlinefields.cxx
@@ -184,7 +184,7 @@ bool InlineFields::VisitBinAssign(const BinaryOperator * binaryOp)
     if (!fieldDecl || !fieldDecl->getType()->isPointerType()) {
         return true;
     }
-    const FunctionDecl* parentFunction = parentFunctionDecl(binaryOp);
+    const FunctionDecl* parentFunction = getParentFunctionDecl(binaryOp);
     if (!parentFunction) {
         return true;
     }
@@ -227,7 +227,7 @@ bool InlineFields::VisitCXXDeleteExpr(const CXXDeleteExpr * deleteExpr)
     }
     // TODO for some reason, this part is not working properly, it doesn't find the parent
     // function for delete statements properly
-    const FunctionDecl* parentFunction = parentFunctionDecl(deleteExpr);
+    const FunctionDecl* parentFunction = getParentFunctionDecl(deleteExpr);
     if (!parentFunction) {
         return true;
     }
diff --git a/compilerplugins/clang/memoryvar.cxx b/compilerplugins/clang/memoryvar.cxx
index 3b480660ed3f..2fecf86eba40 100644
--- a/compilerplugins/clang/memoryvar.cxx
+++ b/compilerplugins/clang/memoryvar.cxx
@@ -145,7 +145,7 @@ bool MemoryVar::VisitCXXNewExpr(const CXXNewExpr *newExpr)
     if (ignoreLocation(newExpr)) {
         return true;
     }
-    const Stmt* stmt = parentStmt(newExpr);
+    const Stmt* stmt = getParentStmt(newExpr);
 
     const DeclStmt* declStmt = dyn_cast<DeclStmt>(stmt);
     if (declStmt) {
diff --git a/compilerplugins/clang/plugin.cxx b/compilerplugins/clang/plugin.cxx
index c9415ab13f03..d7ab4ad3e63e 100644
--- a/compilerplugins/clang/plugin.cxx
+++ b/compilerplugins/clang/plugin.cxx
@@ -118,7 +118,7 @@ void Plugin::registerPlugin( Plugin* (*create)( const InstantiationData& ), cons
 
 std::unordered_map< const Stmt*, const Stmt* > Plugin::parents;
 
-const Stmt* Plugin::parentStmt( const Stmt* stmt )
+const Stmt* Plugin::getParentStmt( const Stmt* stmt )
 {
     if( parents.empty())
         buildParents( compiler );
@@ -127,7 +127,7 @@ const Stmt* Plugin::parentStmt( const Stmt* stmt )
     return parents[ stmt ];
 }
 
-Stmt* Plugin::parentStmt( Stmt* stmt )
+Stmt* Plugin::getParentStmt( Stmt* stmt )
 {
     if( parents.empty())
         buildParents( compiler );
@@ -153,7 +153,7 @@ static const Decl* getDeclContext(ASTContext& context, const Stmt* stmt)
     return nullptr;
 }
 
-const FunctionDecl* Plugin::parentFunctionDecl( const Stmt* stmt )
+const FunctionDecl* Plugin::getParentFunctionDecl( const Stmt* stmt )
 {
     const Decl *decl = getDeclContext(compiler.getASTContext(), stmt);
     if (decl)
diff --git a/compilerplugins/clang/plugin.hxx b/compilerplugins/clang/plugin.hxx
index f480e8374bc1..39c1c18b14d1 100644
--- a/compilerplugins/clang/plugin.hxx
+++ b/compilerplugins/clang/plugin.hxx
@@ -67,9 +67,9 @@ protected:
      Returns the parent of the given AST node. Clang's internal AST representation doesn't provide this information,
      it can only provide children, but getting the parent is often useful for inspecting a part of the AST.
     */
-    const Stmt* parentStmt( const Stmt* stmt );
-    Stmt* parentStmt( Stmt* stmt );
-    const FunctionDecl* parentFunctionDecl( const Stmt* stmt );
+    const Stmt* getParentStmt( const Stmt* stmt );
+    Stmt* getParentStmt( Stmt* stmt );
+    const FunctionDecl* getParentFunctionDecl( const Stmt* stmt );
     /**
      Checks if the location is inside an UNO file, more specifically, if it forms part of the URE stable interface,
      which is not allowed to be changed.
diff --git a/compilerplugins/clang/singlevalfields.cxx b/compilerplugins/clang/singlevalfields.cxx
index 5ccadf55a6b1..6e158ea872f6 100644
--- a/compilerplugins/clang/singlevalfields.cxx
+++ b/compilerplugins/clang/singlevalfields.cxx
@@ -245,21 +245,21 @@ bool SingleValFields::VisitMemberExpr( const MemberExpr* memberExpr )
     if (ignoreLocation(memberExpr) || !isInterestingType(fieldDecl->getType()))
         return true;
 
-    const FunctionDecl* parentFunction = parentFunctionDecl(memberExpr);
+    const FunctionDecl* parentFunction = getParentFunctionDecl(memberExpr);
     const CXXMethodDecl* methodDecl = dyn_cast_or_null<CXXMethodDecl>(parentFunction);
     if (methodDecl && (methodDecl->isCopyAssignmentOperator() || methodDecl->isMoveAssignmentOperator()))
        return true;
 
     // walk up the tree until we find something interesting
     const Stmt* child = memberExpr;
-    const Stmt* parent = parentStmt(memberExpr);
+    const Stmt* parent = getParentStmt(memberExpr);
     bool bPotentiallyAssignedTo = false;
     bool bDump = false;
     std::string assignValue;
 
     // check for field being returned by non-const ref eg. Foo& getFoo() { return f; }
     if (parentFunction && parent && isa<ReturnStmt>(parent)) {
-        const Stmt* parent2 = parentStmt(parent);
+        const Stmt* parent2 = getParentStmt(parent);
         if (parent2 && isa<CompoundStmt>(parent2)) {
             QualType qt = compat::getReturnType(*parentFunction).getDesugaredType(compiler.getASTContext());
             if (!qt.isConstQualified() && qt->isReferenceType()) {
@@ -292,7 +292,7 @@ bool SingleValFields::VisitMemberExpr( const MemberExpr* memberExpr )
              || isa<ExprWithCleanups>(parent))
         {
             child = parent;
-            parent = parentStmt(parent);
+            parent = getParentStmt(parent);
         }
         else if (isa<UnaryOperator>(parent))
         {
@@ -304,7 +304,7 @@ bool SingleValFields::VisitMemberExpr( const MemberExpr* memberExpr )
                 break;
             }
             child = parent;
-            parent = parentStmt(parent);
+            parent = getParentStmt(parent);
         }
         else if (isa<CXXOperatorCallExpr>(parent))
         {
diff --git a/compilerplugins/clang/unusedenumconstants.cxx b/compilerplugins/clang/unusedenumconstants.cxx
index a2f052339e84..88461c1075fd 100644
--- a/compilerplugins/clang/unusedenumconstants.cxx
+++ b/compilerplugins/clang/unusedenumconstants.cxx
@@ -144,7 +144,7 @@ bool UnusedEnumConstants::VisitDeclRefExpr( const DeclRefExpr* declRefExpr )
 
     const Stmt * parent = declRefExpr;
 try_again:
-    parent = parentStmt(parent);
+    parent = getParentStmt(parent);
     bool bWrite = false;
     bool bRead = false;
     bool bDump = false;
diff --git a/compilerplugins/clang/unusedfields.cxx b/compilerplugins/clang/unusedfields.cxx
index 48cbb864d1d0..1f15404cb697 100644
--- a/compilerplugins/clang/unusedfields.cxx
+++ b/compilerplugins/clang/unusedfields.cxx
@@ -930,7 +930,7 @@ bool UnusedFields::VisitDeclRefExpr( const DeclRefExpr* declRefExpr )
 }
 
 void UnusedFields::checkTouchedFromOutside(const FieldDecl* fieldDecl, const Expr* memberExpr) {
-    const FunctionDecl* memberExprParentFunction = parentFunctionDecl(memberExpr);
+    const FunctionDecl* memberExprParentFunction = getParentFunctionDecl(memberExpr);
     const CXXMethodDecl* methodDecl = dyn_cast_or_null<CXXMethodDecl>(memberExprParentFunction);
 
     MyFieldInfo fieldInfo = niceName(fieldDecl);
diff --git a/compilerplugins/clang/unusedmethods.cxx b/compilerplugins/clang/unusedmethods.cxx
index 881427fcfd9b..73bc1ac5d66a 100644
--- a/compilerplugins/clang/unusedmethods.cxx
+++ b/compilerplugins/clang/unusedmethods.cxx
@@ -230,13 +230,13 @@ bool UnusedMethods::VisitCallExpr(CallExpr* expr)
 gotfunc:
     logCallToRootMethods(calleeFunctionDecl, callSet);
 
-    const Stmt* parent = parentStmt(expr);
+    const Stmt* parent = getParentStmt(expr);
 
     // Now do the checks necessary for the "can be private" analysis
     CXXMethodDecl* calleeMethodDecl = dyn_cast<CXXMethodDecl>(calleeFunctionDecl);
     if (calleeMethodDecl && calleeMethodDecl->getAccess() != AS_private)
     {
-        const FunctionDecl* parentFunctionOfCallSite = parentFunctionDecl(expr);
+        const FunctionDecl* parentFunctionOfCallSite = getParentFunctionDecl(expr);
         if (parentFunctionOfCallSite != calleeFunctionDecl) {
             if (!parentFunctionOfCallSite || !ignoreLocation(parentFunctionOfCallSite)) {
                 calledFromOutsideSet.insert(niceName(calleeFunctionDecl));
@@ -333,7 +333,7 @@ bool UnusedMethods::VisitDeclRefExpr( const DeclRefExpr* declRefExpr )
     const CXXMethodDecl* methodDecl = dyn_cast<CXXMethodDecl>(functionDecl);
     if (methodDecl && methodDecl->getAccess() != AS_private)
     {
-        const FunctionDecl* parentFunctionOfCallSite = parentFunctionDecl(declRefExpr);
+        const FunctionDecl* parentFunctionOfCallSite = getParentFunctionDecl(declRefExpr);
         if (parentFunctionOfCallSite != functionDecl) {
             if (!parentFunctionOfCallSite || !ignoreLocation(parentFunctionOfCallSite)) {
                 calledFromOutsideSet.insert(niceName(functionDecl));


More information about the Libreoffice-commits mailing list