compilerplugins/clang/stringstatic.cxx      |   19 +++++++++++++++++--
 compilerplugins/clang/test/stringstatic.cxx |    5 ++++-
 2 files changed, 21 insertions(+), 3 deletions(-)

New commits:
commit 6970bded4e6839a9aaf872483222a5fae6cdc31a
Author:     Stephan Bergmann <stephan.bergm...@collabora.com>
AuthorDate: Fri Aug 15 08:08:30 2025 +0200
Commit:     Stephan Bergmann <stephan.bergm...@collabora.com>
CommitDate: Fri Aug 15 16:39:02 2025 +0200

    Reduce loplugin:stringstatic false positives
    
    (This was meant to address the
    
    >     // [-loplugin:stringstatic] false positive: the initializer is not a 
constant expression
    >     static const OUString path = comphelper::LibreOfficeKit::isActive()
    >                                      ? utl::CreateTempURL(nullptr, true)
    >                                      : GetStandardEmbeddedFontsRoot();
    
    from 49509ba5221768327ac2d61f8dc3e8dd038ee753 "LOK: Set up directory for
    temporary embedded font files", but which has meanwhile been removed with
    d5b0462581c92c3b524494130b727822438432e7 "LOK: make sure to have trailing 
slash
    in GetEmbeddedFontsRoot()", anyway.)
    
    Change-Id: I97aa0f20502caa1ab4f6074a2b403e25add489a3
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/189671
    Tested-by: Jenkins
    Reviewed-by: Stephan Bergmann <stephan.bergm...@collabora.com>

diff --git a/compilerplugins/clang/stringstatic.cxx 
b/compilerplugins/clang/stringstatic.cxx
index cec45a364e4b..1cc99e2a175a 100644
--- a/compilerplugins/clang/stringstatic.cxx
+++ b/compilerplugins/clang/stringstatic.cxx
@@ -10,6 +10,7 @@
 #ifndef LO_CLANG_SHARED_PLUGINS
 
 #include "check.hxx"
+#include "compat.hxx"
 #include "plugin.hxx"
 
 /** Look for static O*String and O*String[] which can be constepxr.
@@ -74,9 +75,23 @@ bool StringStatic::VisitVarDecl(VarDecl const* varDecl)
                         return true;
                     }
                     expr = constructExpr->getArg(0);
-                } else if (isa<CallExpr>(expr)) {
-                    return true;
+                }
+                else if (auto const ile = dyn_cast<InitListExpr>(expr)) {
+                    // This covers the
+                    //     static const OUString A1[1] = { u"xxx"_ustr };
+                    // in compilerplugins/clang/test/stringstatic.cxx, but not 
sure how useful it is
+                    // in general:
+                    if (ile->getNumInits() != 1) {
+                        return true;
+                    }
+                    expr = ile->getInit(0);
+                    if 
(isa<UserDefinedLiteral>(compat::IgnoreParenImplicit(expr))) {
+                        break;
+                    }
                 } else {
+                    if (!expr->isConstantInitializer(compiler.getASTContext(), 
false)) {
+                        return true;
+                    }
                     break;
                 }
             }
diff --git a/compilerplugins/clang/test/stringstatic.cxx 
b/compilerplugins/clang/test/stringstatic.cxx
index 7e2089a17da7..a8bb5d267388 100644
--- a/compilerplugins/clang/test/stringstatic.cxx
+++ b/compilerplugins/clang/test/stringstatic.cxx
@@ -13,7 +13,9 @@
 static const OUString TEST1 = "xxx";
 static constexpr OUString TEST2 = u"xxx"_ustr;
 
-void test2()
+OUString f(int);
+
+void test2(bool b)
 {
     (void)TEST2;
     // expected-error@+1 {{rather declare this as constexpr 
[loplugin:stringstatic]}}
@@ -26,4 +28,5 @@ void test2()
     static constexpr OUString A2[1] = { u"xxx"_ustr };
     (void)A1;
     (void)A2;
+    [[maybe_unused]] static OUString const s = b ? f(0) : f(1); // no warning 
expected
 }

Reply via email to