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

Stephan Bergmann sbergman at redhat.com
Thu May 18 12:24:54 UTC 2017


 compilerplugins/clang/comparisonwithconstant.cxx |   29 ++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)

New commits:
commit 64e5915b7a2c07c740aae25a4a6d00a306e704e4
Author: Stephan Bergmann <sbergman at redhat.com>
Date:   Thu May 18 14:21:58 2017 +0200

    Avoid nested automatic rewrites
    
    ...which would come out garbled.  One place is
    
    >             bool bRet = SfxItemState::SET == pSet->GetItemState( i, rAttr.Which() != RES_TXTATR_AUTOFMT, &pItem );
    
    in SwAttrHandler::PushAndChg (sw/source/core/text/atrstck.cxx)
    
    Change-Id: I1486313b25850dc59e10edb38b8bd28a30e6aa63

diff --git a/compilerplugins/clang/comparisonwithconstant.cxx b/compilerplugins/clang/comparisonwithconstant.cxx
index d200d8ec212d..b55a73e05c17 100644
--- a/compilerplugins/clang/comparisonwithconstant.cxx
+++ b/compilerplugins/clang/comparisonwithconstant.cxx
@@ -33,11 +33,37 @@ public:
         TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
     }
 
+    // Deliberatley drop RecursiveASTVisitor::TraverseBinEQ's DataRecursionQueue
+    // parameter; TraveseBinEQ must use stack instead of data recursion for any
+    // children's VisitBinaryOperator to see changes to occurrence_ by a parent
+    // VisitBinaryOperator:
+    bool TraverseBinEQ(BinaryOperator * S)
+    {
+        auto const saved = occurrence_;
+        auto const ret = RecursiveASTVisitor::TraverseBinEQ(S);
+        occurrence_ = saved;
+        return ret;
+    }
+
+    // Deliberatley drop RecursiveASTVisitor::TraverseBinNE's DataRecursionQueue
+    // parameter; TraveseBinNE must use stack instead of data recursion for any
+    // children's VisitBinaryOperator to see changes to occurrence_ by a parent
+    // VisitBinaryOperator:
+    bool TraverseBinNE(BinaryOperator * S)
+    {
+        auto const saved = occurrence_;
+        auto const ret = RecursiveASTVisitor::TraverseBinNE(S);
+        occurrence_ = saved;
+        return ret;
+    }
+
     bool VisitBinaryOperator(const BinaryOperator *);
 private:
     bool rewrite(const BinaryOperator *);
     std::string getExprAsString(SourceRange range);
     SourceRange ignoreMacroExpansions(SourceRange range);
+
+    bool occurrence_ = false;
 };
 
 bool ComparisonWithConstant::VisitBinaryOperator(const BinaryOperator* binaryOp)
@@ -63,13 +89,14 @@ bool ComparisonWithConstant::VisitBinaryOperator(const BinaryOperator* binaryOp)
     if (binaryOp->getRHS()->isEvaluatable(compiler.getASTContext())) {
         return true;
     }
-    if (!rewrite(binaryOp))
+    if (occurrence_ || !rewrite(binaryOp))
     {
         report(
             DiagnosticsEngine::Warning, "Rather put constant on right when comparing",
             binaryOp->getSourceRange().getBegin())
             << binaryOp->getSourceRange();
     }
+    occurrence_ = true;
     return true;
 }
 


More information about the Libreoffice-commits mailing list