-
Notifications
You must be signed in to change notification settings - Fork 10.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a -Wclass-varargs to warn on objects of any class type being pass…
…ed through an ellipsis. Since C++11 relaxed the rules on this, we allow a lot more bad code through silently, such as: const char *format = "%s"; std::experimental::string_view view = "foo"; printf(format, view); In this case, not only warn about a class type being used here, but also suggest that calling c_str() might be a good idea. llvm-svn: 202461
- Loading branch information
Showing
6 changed files
with
88 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// RUN: %clang_cc1 -verify -Wclass-varargs -std=c++98 %s | ||
// RUN: %clang_cc1 -verify -Wclass-varargs -std=c++11 %s | ||
|
||
struct A {}; | ||
struct B { ~B(); }; | ||
class C { char *c_str(); }; | ||
struct D { char *c_str(); }; | ||
struct E { E(); }; | ||
struct F { F(); char *c_str(); }; | ||
|
||
void v(...); | ||
void w(const char*, ...) __attribute__((format(printf, 1, 2))); | ||
|
||
void test(A a, B b, C c, D d, E e, F f) { | ||
v(a); // expected-warning-re {{passing object of class type 'A' through variadic function{{$}}}} | ||
v(b); // expected-error-re {{cannot pass object of non-{{POD|trivial}} type 'B' through variadic function; call will abort at runtime}} | ||
v(c); // expected-warning {{passing object of class type 'C' through variadic function; did you mean to call '.c_str()'?}} | ||
v(d); // expected-warning {{passing object of class type 'D' through variadic function; did you mean to call '.c_str()'?}} | ||
v(e); | ||
v(f); | ||
#if __cplusplus < 201103L | ||
// expected-error@-3 {{cannot pass object of non-POD type 'E' through variadic function; call will abort at runtime}} | ||
// expected-error@-3 {{cannot pass object of non-POD type 'F' through variadic function; call will abort at runtime}} | ||
#else | ||
// expected-warning-re@-6 {{passing object of class type 'E' through variadic function{{$}}}} | ||
// expected-warning@-6 {{passing object of class type 'F' through variadic function; did you mean to call '.c_str()'?}} | ||
#endif | ||
|
||
v(d.c_str()); | ||
v(f.c_str()); | ||
v(0); | ||
v('x'); | ||
|
||
w("%s", a); // expected-warning {{format specifies type 'char *' but the argument has type 'A'}} | ||
w("%s", b); // expected-error-re {{cannot pass non-{{POD|trivial}} object of type 'B' to variadic function; expected type from format string was 'char *'}} | ||
w("%s", c); // expected-warning {{format specifies type 'char *' but the argument has type 'C'}} | ||
w("%s", d); // expected-warning {{format specifies type 'char *' but the argument has type 'D'}} | ||
w("%s", e); | ||
w("%s", f); | ||
#if __cplusplus < 201103L | ||
// expected-error@-3 {{cannot pass non-POD object of type 'E' to variadic function; expected type from format string was 'char *'}} | ||
// expected-error@-3 {{cannot pass non-POD object of type 'F' to variadic function; expected type from format string was 'char *'}} | ||
// expected-note@-4 {{did you mean to call the c_str() method?}} | ||
#else | ||
// expected-warning@-7 {{format specifies type 'char *' but the argument has type 'E'}} | ||
// expected-warning@-7 {{format specifies type 'char *' but the argument has type 'F'}} | ||
#endif | ||
} |