diff options
author | Anders Carlsson <andersca@mac.com> | 2009-01-13 05:48:52 +0000 |
---|---|---|
committer | Anders Carlsson <andersca@mac.com> | 2009-01-13 05:48:52 +0000 |
commit | 906fed0fb54a338961aba3aa54802b7d68de94c7 (patch) | |
tree | 0ce6bb608a4a5693b22ecd9d3147f0ee7721ade9 | |
parent | 518fda1d121dcba3ad7276f5e9a94f733f6e5ecd (diff) |
Warn when someone tries to pass a variable with a non-POD type to a varargs function/method/block.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@62148 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | include/clang/Basic/DiagnosticKinds.def | 3 | ||||
-rw-r--r-- | lib/Sema/SemaExpr.cpp | 11 | ||||
-rw-r--r-- | lib/Sema/SemaExprObjC.cpp | 9 | ||||
-rw-r--r-- | lib/Sema/SemaOverload.cpp | 7 | ||||
-rw-r--r-- | test/SemaCXX/vararg-non-pod.cpp | 49 | ||||
-rw-r--r-- | test/SemaObjCXX/vararg-non-pod.mm | 18 |
6 files changed, 96 insertions, 1 deletions
diff --git a/include/clang/Basic/DiagnosticKinds.def b/include/clang/Basic/DiagnosticKinds.def index ee0cad4d13..a9f60d2d63 100644 --- a/include/clang/Basic/DiagnosticKinds.def +++ b/include/clang/Basic/DiagnosticKinds.def @@ -1410,6 +1410,9 @@ DIAG(err_typecheck_call_too_few_args, ERROR, "too few arguments to %select{function|block|method}0 call") DIAG(err_typecheck_call_too_many_args, ERROR, "too many arguments to %select{function|block|method}0 call") +DIAG(warn_cannot_pass_non_pod_arg_to_vararg, WARNING, + "cannot pass object of non-POD type %0 through variadic " + "%select{function|block|method}1; call will abort at runtime") DIAG(err_typecheck_closure_too_many_args, ERROR, "too many arguments to closure call") DIAG(err_typecheck_call_invalid_ordered_compare, ERROR, diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index d3d3d3556c..2721bb8dc6 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -1688,6 +1688,17 @@ Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, // Promote the arguments (C99 6.5.2.2p7). for (unsigned i = NumArgsInProto; i != NumArgs; i++) { Expr *Arg = Args[i]; + if (!Arg->getType()->isPODType()) { + int CallType = 0; + if (Fn->getType()->isBlockPointerType()) + CallType = 1; // Block + else if (isa<MemberExpr>(Fn)) + CallType = 2; + + Diag(Arg->getLocStart(), + diag::warn_cannot_pass_non_pod_arg_to_vararg) << + Arg->getType() << CallType; + } DefaultArgumentPromotion(Arg); Call->setArg(i, Arg); } diff --git a/lib/Sema/SemaExprObjC.cpp b/lib/Sema/SemaExprObjC.cpp index 415bc94759..a73f386dab 100644 --- a/lib/Sema/SemaExprObjC.cpp +++ b/lib/Sema/SemaExprObjC.cpp @@ -156,8 +156,15 @@ bool Sema::CheckMessageArgumentTypes(Expr **Args, unsigned NumArgs, // Promote additional arguments to variadic methods. if (Method->isVariadic()) { - for (unsigned i = NumNamedArgs; i < NumArgs; ++i) + for (unsigned i = NumNamedArgs; i < NumArgs; ++i) { + if (!Args[i]->getType()->isPODType()) { + Diag(Args[i]->getLocStart(), + diag::warn_cannot_pass_non_pod_arg_to_vararg) << + Args[i]->getType() << 2; // Method + } + DefaultArgumentPromotion(Args[i]); + } } else { // Check for extra arguments to non-variadic methods. if (NumArgs != NumNamedArgs) { diff --git a/lib/Sema/SemaOverload.cpp b/lib/Sema/SemaOverload.cpp index c66db173eb..d74a5d54b9 100644 --- a/lib/Sema/SemaOverload.cpp +++ b/lib/Sema/SemaOverload.cpp @@ -3576,6 +3576,13 @@ Sema::BuildCallToObjectOfClassType(Scope *S, Expr *Object, // Promote the arguments (C99 6.5.2.2p7). for (unsigned i = NumArgsInProto; i != NumArgs; i++) { Expr *Arg = Args[i]; + + if (!Arg->getType()->isPODType()) { + Diag(Arg->getLocStart(), + diag::warn_cannot_pass_non_pod_arg_to_vararg) << + Arg->getType() << 2; // Method + } + DefaultArgumentPromotion(Arg); TheCall->setArg(i + 1, Arg); } diff --git a/test/SemaCXX/vararg-non-pod.cpp b/test/SemaCXX/vararg-non-pod.cpp new file mode 100644 index 0000000000..d413335243 --- /dev/null +++ b/test/SemaCXX/vararg-non-pod.cpp @@ -0,0 +1,49 @@ +// RUN: clang -fsyntax-only -verify -fblocks %s + +class C { +public: + C(int); + void g(int a, ...); + static void h(int a, ...); +}; + +void g(int a, ...); + +void t1() +{ + C c(10); + + g(10, c); // expected-warning{{cannot pass object of non-POD type 'class C' through variadic function; call will abort at runtime}} +} + +void t2() +{ + C c(10); + + c.g(10, c); // expected-warning{{cannot pass object of non-POD type 'class C' through variadic method; call will abort at runtime}} + + C::h(10, c); // expected-warning{{cannot pass object of non-POD type 'class C' through variadic function; call will abort at runtime}} +} + +int (^block)(int, ...); + +void t3() +{ + C c(10); + + block(10, c); // expected-warning{{cannot pass object of non-POD type 'class C' through variadic block; call will abort at runtime}} +} + +class D { +public: + void operator() (int a, ...); +}; + +void t4() +{ + C c(10); + + D d; + + d(10, c); // expected-warning{{Line 48: cannot pass object of non-POD type 'class C' through variadic method; call will abort at runtime}} +} diff --git a/test/SemaObjCXX/vararg-non-pod.mm b/test/SemaObjCXX/vararg-non-pod.mm new file mode 100644 index 0000000000..3431dea9e5 --- /dev/null +++ b/test/SemaObjCXX/vararg-non-pod.mm @@ -0,0 +1,18 @@ +// RUN: clang -fsyntax-only -verify %s + +class C { +public: + C(int); +}; + +@interface D +- (void)g:(int)a, ...; +@end + +void t1(D *d) +{ + C c(10); + + [d g:10, c]; // expected-warning{{cannot pass object of non-POD type 'class C' through variadic method; call will abort at runtime}} +} + |