Skip to content

Commit 2868a73

Browse files
committed
Add a -Wclass-varargs to warn on objects of any class type being passed 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
1 parent 854d6d0 commit 2868a73

File tree

6 files changed

+88
-10
lines changed

6 files changed

+88
-10
lines changed

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ def NullDereference : DiagGroup<"null-dereference">;
219219
def InitializerOverrides : DiagGroup<"initializer-overrides">;
220220
def NonNull : DiagGroup<"nonnull">;
221221
def NonPODVarargs : DiagGroup<"non-pod-varargs">;
222+
def ClassVarargs : DiagGroup<"class-varargs", [NonPODVarargs]>;
222223
def : DiagGroup<"nonportable-cfstrings">;
223224
def NonVirtualDtor : DiagGroup<"non-virtual-dtor">;
224225
def OveralignedType : DiagGroup<"over-aligned">;

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5725,6 +5725,11 @@ def warn_cxx98_compat_pass_non_pod_arg_to_vararg : Warning<
57255725
"passing object of trivial but non-POD type %0 through variadic"
57265726
" %select{function|block|method|constructor}1 is incompatible with C++98">,
57275727
InGroup<CXX98Compat>, DefaultIgnore;
5728+
def warn_pass_class_arg_to_vararg : Warning<
5729+
"passing object of class type %0 through variadic "
5730+
"%select{function|block|method|constructor}1"
5731+
"%select{|; did you mean to call '%3'?}2">,
5732+
InGroup<ClassVarargs>, DefaultIgnore;
57285733
def err_cannot_pass_to_vararg : Error<
57295734
"cannot pass %select{expression of type %1|initializer list}0 to variadic "
57305735
"%select{function|block|method|constructor}2">;

clang/include/clang/Sema/Sema.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7249,6 +7249,9 @@ class Sema {
72497249
/// function, issuing a diagnostic if not.
72507250
void checkVariadicArgument(const Expr *E, VariadicCallType CT);
72517251

7252+
/// Check to see if a given expression could have '.c_str()' called on it.
7253+
bool hasCStrMethod(const Expr *E);
7254+
72527255
/// GatherArgumentsForCall - Collector argument expressions for various
72537256
/// form of call prototypes.
72547257
bool GatherArgumentsForCall(SourceLocation CallLoc, FunctionDecl *FDecl,

clang/lib/Sema/SemaChecking.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2765,7 +2765,7 @@ class CheckPrintfHandler : public CheckFormatHandler {
27652765
const analyze_printf::OptionalFlag &flag,
27662766
const char *startSpecifier, unsigned specifierLen);
27672767
bool checkForCStrMembers(const analyze_printf::ArgType &AT,
2768-
const Expr *E, const CharSourceRange &CSR);
2768+
const Expr *E);
27692769

27702770
};
27712771
}
@@ -2899,11 +2899,12 @@ CXXRecordMembersNamed(StringRef Name, Sema &S, QualType Ty) {
28992899
if (!RT)
29002900
return Results;
29012901
const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl());
2902-
if (!RD)
2902+
if (!RD || !RD->getDefinition())
29032903
return Results;
29042904

29052905
LookupResult R(S, &S.PP.getIdentifierTable().get(Name), SourceLocation(),
29062906
Sema::LookupMemberName);
2907+
R.suppressDiagnostics();
29072908

29082909
// We just need to include all members of the right kind turned up by the
29092910
// filter, at this point.
@@ -2916,12 +2917,26 @@ CXXRecordMembersNamed(StringRef Name, Sema &S, QualType Ty) {
29162917
return Results;
29172918
}
29182919

2920+
/// Check if we could call '.c_str()' on an object.
2921+
///
2922+
/// FIXME: This returns the wrong results in some cases (if cv-qualifiers don't
2923+
/// allow the call, or if it would be ambiguous).
2924+
bool Sema::hasCStrMethod(const Expr *E) {
2925+
typedef llvm::SmallPtrSet<CXXMethodDecl*, 1> MethodSet;
2926+
MethodSet Results =
2927+
CXXRecordMembersNamed<CXXMethodDecl>("c_str", *this, E->getType());
2928+
for (MethodSet::iterator MI = Results.begin(), ME = Results.end();
2929+
MI != ME; ++MI)
2930+
if ((*MI)->getMinRequiredArguments() == 0)
2931+
return true;
2932+
return false;
2933+
}
2934+
29192935
// Check if a (w)string was passed when a (w)char* was needed, and offer a
29202936
// better diagnostic if so. AT is assumed to be valid.
29212937
// Returns true when a c_str() conversion method is found.
29222938
bool CheckPrintfHandler::checkForCStrMembers(
2923-
const analyze_printf::ArgType &AT, const Expr *E,
2924-
const CharSourceRange &CSR) {
2939+
const analyze_printf::ArgType &AT, const Expr *E) {
29252940
typedef llvm::SmallPtrSet<CXXMethodDecl*, 1> MethodSet;
29262941

29272942
MethodSet Results =
@@ -2930,7 +2945,7 @@ bool CheckPrintfHandler::checkForCStrMembers(
29302945
for (MethodSet::iterator MI = Results.begin(), ME = Results.end();
29312946
MI != ME; ++MI) {
29322947
const CXXMethodDecl *Method = *MI;
2933-
if (Method->getNumParams() == 0 &&
2948+
if (Method->getMinRequiredArguments() == 0 &&
29342949
AT.matchesType(S.Context, Method->getReturnType())) {
29352950
// FIXME: Suggest parens if the expression needs them.
29362951
SourceLocation EndLoc =
@@ -3317,7 +3332,7 @@ CheckPrintfHandler::checkFormatExpr(const analyze_printf::PrintfSpecifier &FS,
33173332
<< CSR
33183333
<< E->getSourceRange(),
33193334
E->getLocStart(), /*IsStringLocation*/false, CSR);
3320-
checkForCStrMembers(AT, E, CSR);
3335+
checkForCStrMembers(AT, E);
33213336
break;
33223337

33233338
case Sema::VAK_Invalid:

clang/lib/Sema/SemaExpr.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -806,14 +806,20 @@ void Sema::checkVariadicArgument(const Expr *E, VariadicCallType CT) {
806806

807807
// Complain about passing non-POD types through varargs.
808808
switch (VAK) {
809-
case VAK_Valid:
810-
break;
811-
812809
case VAK_ValidInCXX11:
813810
DiagRuntimeBehavior(
814811
E->getLocStart(), 0,
815812
PDiag(diag::warn_cxx98_compat_pass_non_pod_arg_to_vararg)
816-
<< E->getType() << CT);
813+
<< Ty << CT);
814+
// Fall through.
815+
case VAK_Valid:
816+
if (Ty->isRecordType()) {
817+
// This is unlikely to be what the user intended. If the class has a
818+
// 'c_str' member function, the user probably meant to call that.
819+
DiagRuntimeBehavior(E->getLocStart(), 0,
820+
PDiag(diag::warn_pass_class_arg_to_vararg)
821+
<< Ty << CT << hasCStrMethod(E) << ".c_str()");
822+
}
817823
break;
818824

819825
case VAK_Undefined:

clang/test/SemaCXX/vararg-class.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// RUN: %clang_cc1 -verify -Wclass-varargs -std=c++98 %s
2+
// RUN: %clang_cc1 -verify -Wclass-varargs -std=c++11 %s
3+
4+
struct A {};
5+
struct B { ~B(); };
6+
class C { char *c_str(); };
7+
struct D { char *c_str(); };
8+
struct E { E(); };
9+
struct F { F(); char *c_str(); };
10+
11+
void v(...);
12+
void w(const char*, ...) __attribute__((format(printf, 1, 2)));
13+
14+
void test(A a, B b, C c, D d, E e, F f) {
15+
v(a); // expected-warning-re {{passing object of class type 'A' through variadic function{{$}}}}
16+
v(b); // expected-error-re {{cannot pass object of non-{{POD|trivial}} type 'B' through variadic function; call will abort at runtime}}
17+
v(c); // expected-warning {{passing object of class type 'C' through variadic function; did you mean to call '.c_str()'?}}
18+
v(d); // expected-warning {{passing object of class type 'D' through variadic function; did you mean to call '.c_str()'?}}
19+
v(e);
20+
v(f);
21+
#if __cplusplus < 201103L
22+
// expected-error@-3 {{cannot pass object of non-POD type 'E' through variadic function; call will abort at runtime}}
23+
// expected-error@-3 {{cannot pass object of non-POD type 'F' through variadic function; call will abort at runtime}}
24+
#else
25+
// expected-warning-re@-6 {{passing object of class type 'E' through variadic function{{$}}}}
26+
// expected-warning@-6 {{passing object of class type 'F' through variadic function; did you mean to call '.c_str()'?}}
27+
#endif
28+
29+
v(d.c_str());
30+
v(f.c_str());
31+
v(0);
32+
v('x');
33+
34+
w("%s", a); // expected-warning {{format specifies type 'char *' but the argument has type 'A'}}
35+
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 *'}}
36+
w("%s", c); // expected-warning {{format specifies type 'char *' but the argument has type 'C'}}
37+
w("%s", d); // expected-warning {{format specifies type 'char *' but the argument has type 'D'}}
38+
w("%s", e);
39+
w("%s", f);
40+
#if __cplusplus < 201103L
41+
// expected-error@-3 {{cannot pass non-POD object of type 'E' to variadic function; expected type from format string was 'char *'}}
42+
// expected-error@-3 {{cannot pass non-POD object of type 'F' to variadic function; expected type from format string was 'char *'}}
43+
// expected-note@-4 {{did you mean to call the c_str() method?}}
44+
#else
45+
// expected-warning@-7 {{format specifies type 'char *' but the argument has type 'E'}}
46+
// expected-warning@-7 {{format specifies type 'char *' but the argument has type 'F'}}
47+
#endif
48+
}

0 commit comments

Comments
 (0)