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

Noel Grandin (via logerrit) logerrit at kemper.freedesktop.org
Fri Feb 21 13:48:49 UTC 2020


 compilerplugins/clang/sequenceloop.cxx      |    6 ++++++
 compilerplugins/clang/test/sequenceloop.cxx |    9 +++++++++
 2 files changed, 15 insertions(+)

New commits:
commit 3aca35f1505fa552eaa316a2d47a60ef52646525
Author:     Noel Grandin <noel.grandin at collabora.co.uk>
AuthorDate: Fri Feb 21 12:24:15 2020 +0200
Commit:     Noel Grandin <noel.grandin at collabora.co.uk>
CommitDate: Fri Feb 21 14:48:10 2020 +0100

    loplugin:sequenceloop improve rvalue detection
    
    to avoid false positives
    
    Change-Id: Id20eb0837fa6764139af3fc4481c768700ec2dad
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/89184
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <noel.grandin at collabora.co.uk>

diff --git a/compilerplugins/clang/sequenceloop.cxx b/compilerplugins/clang/sequenceloop.cxx
index 7f14d6c4d951..9a620f26be52 100644
--- a/compilerplugins/clang/sequenceloop.cxx
+++ b/compilerplugins/clang/sequenceloop.cxx
@@ -63,6 +63,12 @@ bool SequenceLoop::VisitCXXForRangeStmt(CXXForRangeStmt const* forStmt)
     if (!tc2.LvalueReference().Const())
         return true;
 
+    // bit awkward, but the only way to sniff out if we're being passed a value or a reference
+    auto rangeDeclStmt = dyn_cast<DeclStmt>(forStmt->getRangeStmt());
+    auto rangeVarDecl = dyn_cast<VarDecl>(rangeDeclStmt->getSingleDecl());
+    if (rangeVarDecl->getType()->isRValueReferenceType())
+        return true;
+
     report(DiagnosticsEngine::Warning,
            ("use std::as_const, or otherwise make the for-range-initializer expression const, to"
             " avoid creating a copy of the Sequence"),
diff --git a/compilerplugins/clang/test/sequenceloop.cxx b/compilerplugins/clang/test/sequenceloop.cxx
index e124fda27093..41cb32f0495a 100644
--- a/compilerplugins/clang/test/sequenceloop.cxx
+++ b/compilerplugins/clang/test/sequenceloop.cxx
@@ -30,6 +30,15 @@ void foo2(const css::uno::Sequence<css::uno::Reference<css::uno::XInterface>>& a
     for (const auto& x : aSeq)
         x.get();
 }
+
+css::uno::Sequence<css::uno::Reference<css::uno::XInterface>> getSequenceByValue();
+
+// no warning expected
+void foo3()
+{
+    for (const auto& x : getSequenceByValue())
+        x.get();
+}
 };
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */


More information about the Libreoffice-commits mailing list