<div dir="ltr">can you just match on the name of the template instead?</div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Mar 23, 2016 at 12:46 PM, Etienne Bergeron via cfe-commits <span dir="ltr"><<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">etienneb created this revision.<br>
etienneb added a reviewer: alexfh.<br>
etienneb added a subscriber: cfe-commits.<br>
<br>
The current checker is able to recognize std::string but do not recognize other string variants.<br>
This path is adding the support for any string define with basic_string without consiring the<br>
the char type.<br>
<br>
The most common variant is: std::wstring based on wchar_t.<br>
<br>
There are also other string variants added to the standard: u16string, u32string, etc...<br>
<br>
<a href="http://reviews.llvm.org/D18412" rel="noreferrer" target="_blank">http://reviews.llvm.org/D18412</a><br>
<br>
Files:<br>
clang-tidy/readability/RedundantStringCStrCheck.cpp<br>
test/clang-tidy/readability-redundant-string-cstr.cpp<br>
<br>
Index: test/clang-tidy/readability-redundant-string-cstr.cpp<br>
===================================================================<br>
--- test/clang-tidy/readability-redundant-string-cstr.cpp<br>
+++ test/clang-tidy/readability-redundant-string-cstr.cpp<br>
@@ -12,14 +12,20 @@<br>
const C *c_str() const;<br>
};<br>
typedef basic_string<char, std::char_traits<char>, std::allocator<char>> string;<br>
+typedef basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t>> wstring;<br>
+typedef basic_string<char16_t, std::char_traits<char16_t>, std::allocator<char16_t>> u16string;<br>
+typedef basic_string<char32_t, std::char_traits<char32_t>, std::allocator<char32_t>> u32string;<br>
}<br>
+<br>
namespace llvm {<br>
struct StringRef {<br>
StringRef(const char *p);<br>
StringRef(const std::string &);<br>
};<br>
}<br>
<br>
+// Tests for std::string.<br>
+<br>
void f1(const std::string &s) {<br>
f1(s.c_str());<br>
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to `c_str()` [readability-redundant-string-cstr]<br>
@@ -39,3 +45,27 @@<br>
// CHECK-FIXES: {{^ }}std::string s;{{$}}<br>
// CHECK-FIXES-NEXT: {{^ }}f3(s);{{$}}<br>
}<br>
+<br>
+// Tests for std::wstring.<br>
+<br>
+void g1(const std::wstring &s) {<br>
+ g1(s.c_str());<br>
+ // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to `c_str()` [readability-redundant-string-cstr]<br>
+ // CHECK-FIXES: {{^ }}f1(s);{{$}}<br>
+}<br>
+<br>
+// Tests for std::u16string.<br>
+<br>
+void h1(const std::u16string &s) {<br>
+ h1(s.c_str());<br>
+ // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to `c_str()` [readability-redundant-string-cstr]<br>
+ // CHECK-FIXES: {{^ }}f1(s);{{$}}<br>
+}<br>
+<br>
+// Tests for std::u32string.<br>
+<br>
+void k1(const std::u32string &s) {<br>
+ k1(s.c_str());<br>
+ // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: redundant call to `c_str()` [readability-redundant-string-cstr]<br>
+ // CHECK-FIXES: {{^ }}f1(s);{{$}}<br>
+}<br>
Index: clang-tidy/readability/RedundantStringCStrCheck.cpp<br>
===================================================================<br>
--- clang-tidy/readability/RedundantStringCStrCheck.cpp<br>
+++ clang-tidy/readability/RedundantStringCStrCheck.cpp<br>
@@ -66,11 +66,11 @@<br>
}<br>
<br>
const char StringConstructor[] =<br>
- "::std::basic_string<char, std::char_traits<char>, std::allocator<char> >"<br>
+ "::std::basic_string<.*, std::char_traits<.*>, std::allocator<.*> >"<br>
"::basic_string";<br>
<br>
const char StringCStrMethod[] =<br>
- "::std::basic_string<char, std::char_traits<char>, std::allocator<char> >"<br>
+ "::std::basic_string<.*, std::char_traits<.*>, std::allocator<.*> >"<br>
"::c_str";<br>
<br>
} // end namespace<br>
@@ -89,18 +89,18 @@<br>
const auto StringConstructorExpr = expr(anyOf(<br>
cxxConstructExpr(<br>
argumentCountIs(1),<br>
- hasDeclaration(cxxMethodDecl(hasName(StringConstructor)))),<br>
+ hasDeclaration(cxxMethodDecl(matchesName(StringConstructor)))),<br>
cxxConstructExpr(<br>
argumentCountIs(2),<br>
- hasDeclaration(cxxMethodDecl(hasName(StringConstructor))),<br>
+ hasDeclaration(cxxMethodDecl(matchesName(StringConstructor))),<br>
// If present, the second argument is the alloc object which must not<br>
// be present explicitly.<br>
hasArgument(1, cxxDefaultArgExpr()))));<br>
<br>
// Match a call to the string 'c_str()' method.<br>
- const auto StringCStrCallExpr = cxxMemberCallExpr(<br>
- callee(memberExpr().bind("member")),<br>
- callee(cxxMethodDecl(hasName(StringCStrMethod))),<br>
+ const auto StringCStrCallExpr =<br>
+ cxxMemberCallExpr(callee(memberExpr().bind("member")),<br>
+ callee(cxxMethodDecl(matchesName(StringCStrMethod))),<br>
on(expr().bind("arg"))).bind("call");<br>
<br>
Finder->addMatcher(<br>
<br>
<br>
<br>_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@lists.llvm.org">cfe-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits</a><br>
<br></blockquote></div><br></div>