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

Libreoffice Gerrit user logerrit at kemper.freedesktop.org
Mon Feb 18 09:23:09 UTC 2019


 compilerplugins/clang/simplifybool.cxx      |   17 +++++++++++------
 compilerplugins/clang/test/simplifybool.cxx |    3 +++
 2 files changed, 14 insertions(+), 6 deletions(-)

New commits:
commit 893ee431803629111eb9271f72bde6a82cafebec
Author:     Stephan Bergmann <sbergman at redhat.com>
AuthorDate: Mon Feb 18 09:05:34 2019 +0100
Commit:     Stephan Bergmann <sbergman at redhat.com>
CommitDate: Mon Feb 18 10:22:48 2019 +0100

    Tell about presumed corresponding negated operator
    
    Change-Id: Ic7ed64ecc4902853dc7431294484abb74e8da65b
    Reviewed-on: https://gerrit.libreoffice.org/67953
    Tested-by: Jenkins
    Reviewed-by: Stephan Bergmann <sbergman at redhat.com>

diff --git a/compilerplugins/clang/simplifybool.cxx b/compilerplugins/clang/simplifybool.cxx
index 40c5b6fc48ba..4f7e418f0655 100644
--- a/compilerplugins/clang/simplifybool.cxx
+++ b/compilerplugins/clang/simplifybool.cxx
@@ -53,15 +53,15 @@ Expr const * getSubExprOfLogicalNegation(Expr const * expr) {
         ? nullptr : e->getSubExpr();
 }
 
-bool existsOperator(CompilerInstance& compiler, clang::RecordType const * recordType, BinaryOperator::Opcode opcode) {
+FunctionDecl const * findOperator(CompilerInstance& compiler, clang::RecordType const * recordType, BinaryOperator::Opcode opcode) {
     OverloadedOperatorKind over = BinaryOperator::getOverloadedOperator(opcode);
     CXXRecordDecl const * recordDecl = dyn_cast<CXXRecordDecl>(recordType->getDecl());
     if (!recordDecl)
-        return false;
+        return nullptr;
     // search for member overloads
     for (auto it = recordDecl->method_begin(); it != recordDecl->method_end(); ++it) {
         if (it->getOverloadedOperator() == over) {
-            return true;
+            return *it;
         }
     }
     // search for free function overloads
@@ -80,9 +80,9 @@ bool existsOperator(CompilerInstance& compiler, clang::RecordType const * record
         if (!lvalue)
             continue;
         if (lvalue->getPointeeType().getTypePtr() == recordType)
-            return true;
+            return f;
     }
-    return false;
+    return nullptr;
 }
 
 enum class Value { Unknown, False, True };
@@ -236,7 +236,8 @@ bool SimplifyBool::VisitUnaryLNot(UnaryOperator const * expr) {
         if (!t->isRecordType())
             return true;
         auto recordType = dyn_cast<RecordType>(t);
-        if (!existsOperator(compiler, recordType, negatedOpcode))
+        auto const negOp = findOperator(compiler, recordType, negatedOpcode);
+        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) {
@@ -254,6 +255,10 @@ bool SimplifyBool::VisitUnaryLNot(UnaryOperator const * expr) {
             ("logical negation of comparison operator, can be simplified by inverting operator"),
             compat::getBeginLoc(expr))
             << expr->getSourceRange();
+        report(
+            DiagnosticsEngine::Note, "the presumed corresponding negated operator is declared here",
+            negOp->getLocation())
+            << negOp->getSourceRange();
     }
     return true;
 }
diff --git a/compilerplugins/clang/test/simplifybool.cxx b/compilerplugins/clang/test/simplifybool.cxx
index 64288253c8e2..8428502ff01d 100644
--- a/compilerplugins/clang/test/simplifybool.cxx
+++ b/compilerplugins/clang/test/simplifybool.cxx
@@ -8,6 +8,7 @@
  */
 
 #include <rtl/ustring.hxx>
+// expected-note at rtl/ustring.hxx:* 2 {{the presumed corresponding negated operator is declared here [loplugin:simplifybool]}}
 
 namespace group1
 {
@@ -75,6 +76,7 @@ struct Record2
 {
     bool operator==(const Record2&) const;
     bool operator!=(const Record2&) const;
+    // expected-note at -1 {{the presumed corresponding negated operator is declared here [loplugin:simplifybool]}}
 };
 
 struct Record3
@@ -83,6 +85,7 @@ struct Record3
 
 bool operator==(const Record3&, const Record3&);
 bool operator!=(const Record3&, const Record3&);
+// expected-note at -1 {{the presumed corresponding negated operator is declared here [loplugin:simplifybool]}}
 
 void testRecord()
 {


More information about the Libreoffice-commits mailing list