diff options
Diffstat (limited to 'compilerplugins')
-rw-r--r-- | compilerplugins/clang/test/unnecessarygetstr.cxx | 7 | ||||
-rw-r--r-- | compilerplugins/clang/unnecessarygetstr.cxx | 18 |
2 files changed, 22 insertions, 3 deletions
diff --git a/compilerplugins/clang/test/unnecessarygetstr.cxx b/compilerplugins/clang/test/unnecessarygetstr.cxx index c0960557a89b..bdb58cde2ef7 100644 --- a/compilerplugins/clang/test/unnecessarygetstr.cxx +++ b/compilerplugins/clang/test/unnecessarygetstr.cxx @@ -10,6 +10,7 @@ #include <sal/config.h> #include <ostream> +#include <sstream> #include <string_view> #include <string> @@ -128,4 +129,10 @@ void foo(const OString&); void test(std::string v) { foo(v.c_str()); } } +// no warning expected +namespace test7 +{ +void test(const OString& v) { std::stringstream aStream(v.getStr()); } +} + /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/compilerplugins/clang/unnecessarygetstr.cxx b/compilerplugins/clang/unnecessarygetstr.cxx index c80877a78554..589ab405f786 100644 --- a/compilerplugins/clang/unnecessarygetstr.cxx +++ b/compilerplugins/clang/unnecessarygetstr.cxx @@ -11,6 +11,7 @@ #include <cassert> #include <stack> +#include <unordered_set> #include "check.hxx" #include "plugin.hxx" @@ -60,11 +61,20 @@ public: if (ignoreLocation(constructExpr)) return true; auto tc = loplugin::TypeCheck(constructExpr->getType()); - if (tc.ClassOrStruct("basic_string").StdNamespace()) + if (tc.ClassOrStruct("basic_stringstream").StdNamespace()) + { + // ignore the implicit-conversion nodes that are added here + if (constructExpr->getNumArgs() > 0) + nodesToIgnore.insert(constructExpr->getArg(0)->IgnoreImplicit()); + } + else if (tc.ClassOrStruct("basic_string").StdNamespace()) { if (constructExpr->getNumArgs() == 1 || constructExpr->getNumArgs() == 2) - checkForGetStr(constructExpr->getArg(0), "string constructor", - /*isOStringConstructor*/ false); + { + if (nodesToIgnore.find(constructExpr) == nodesToIgnore.end()) + checkForGetStr(constructExpr->getArg(0), "string constructor", + /*isOStringConstructor*/ false); + } } else if (tc.ClassOrStruct("basic_string_view").StdNamespace()) { @@ -138,6 +148,8 @@ private: TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); } } + + std::unordered_set<const Expr*> nodesToIgnore; }; loplugin::Plugin::Registration<UnnecessaryGetStr> unnecessarygetstr("unnecessarygetstr"); |