From 5c54b6523f74db4f79540361fc1630aaee06bcd5 Mon Sep 17 00:00:00 2001 From: Stephan Bergmann Date: Fri, 22 Sep 2017 08:16:59 +0200 Subject: loplugin:stringconstant: warn about explicit length mismatch (just in case) Change-Id: I9eaf6e920fc7fc3ebc8c705df1cdf34b325db74a Reviewed-on: https://gerrit.libreoffice.org/42614 Tested-by: Jenkins Reviewed-by: Stephan Bergmann --- compilerplugins/clang/stringconstant.cxx | 12 ++++++++++-- compilerplugins/clang/test/stringconstant.cxx | 2 ++ 2 files changed, 12 insertions(+), 2 deletions(-) (limited to 'compilerplugins') diff --git a/compilerplugins/clang/stringconstant.cxx b/compilerplugins/clang/stringconstant.cxx index 2eab378e443f..a4e0b79438b2 100644 --- a/compilerplugins/clang/stringconstant.cxx +++ b/compilerplugins/clang/stringconstant.cxx @@ -842,11 +842,19 @@ bool StringConstant::VisitCXXConstructExpr(CXXConstructExpr const * expr) { } APSInt res; if (!expr->getArg(1)->EvaluateAsInt( - res, compiler.getASTContext()) - || res != n) + res, compiler.getASTContext())) { return true; } + if (res != n) { + report( + DiagnosticsEngine::Warning, + ("suspicious 'rtl::OUString' constructor with literal" + " of length %0 and non-matching length argument %1"), + expr->getExprLoc()) + << n << res.toString(10) << expr->getSourceRange(); + return true; + } if (!expr->getArg(2)->EvaluateAsInt( res, compiler.getASTContext())) { diff --git a/compilerplugins/clang/test/stringconstant.cxx b/compilerplugins/clang/test/stringconstant.cxx index 1a1f0c37580c..41112d836520 100644 --- a/compilerplugins/clang/test/stringconstant.cxx +++ b/compilerplugins/clang/test/stringconstant.cxx @@ -64,6 +64,8 @@ int main() { (void) OUString("xxx", 3, RTL_TEXTENCODING_ASCII_US); // expected-error {{simplify construction of 'OUString' with string constant argument [loplugin:stringconstant]}} (void) OUString("xxx", 3, RTL_TEXTENCODING_ISO_8859_1); // expected-error {{simplify construction of 'OUString' with string constant argument (but beware, the given textencoding 12 is not RTL_TEXTENCODING_ASCII_US) [loplugin:stringconstant]}} (void) OUString("x\xA0x", 3, RTL_TEXTENCODING_ISO_8859_1); + + (void) OUString("xxx", 2, RTL_TEXTENCODING_ASCII_US); // expected-error {{suspicious 'rtl::OUString' constructor with literal of length 3 and non-matching length argument 2 [loplugin:stringconstant]}} } -- cgit