From: Aaron Ballman Date: Thu, 9 Feb 2012 01:21:34 +0000 (+0000) Subject: Adding support for warning when a non-C compatible user-defined type is returned... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2c0bf2437089f9a297cf18530361a185e76f2150;p=clang Adding support for warning when a non-C compatible user-defined type is returned from an extern "C" function. Fixes bug 6143 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@150128 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index f255b88a8e..c5155c6e13 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -163,7 +163,9 @@ def warn_parameter_size: Warning< def warn_return_value_size: Warning< "return value of %0 is a large (%1 bytes) pass-by-value object; " "pass it by reference instead ?">, InGroup; - +def warn_return_value_udt: Warning< + "%0 has C-linkage specified, but returns user-defined type %1 which is " + "incompatible with C">, InGroup; def warn_implicit_function_decl : Warning< "implicit declaration of function %0">, InGroup, DefaultIgnore; diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index ed8d8a5d71..3d24c59291 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -5774,6 +5774,17 @@ bool Sema::CheckFunctionDeclaration(Scope *S, FunctionDecl *NewFD, Context.BuiltinInfo.ForgetBuiltin(BuiltinID, Context.Idents); } } + + // If this function is declared as being extern "C", then check to see if + // the function returns a UDT (class, struct, or union type) that is not C + // compatible, and if it does, warn the user. + if (NewFD->isExternC()) { + QualType R = NewFD->getResultType(); + if (!R.isPODType(Context) && + !R->isVoidType()) + Diag( NewFD->getLocation(), diag::warn_return_value_udt ) + << NewFD << R; + } } return Redeclaration; } diff --git a/test/SemaCXX/function-extern-c.cpp b/test/SemaCXX/function-extern-c.cpp new file mode 100644 index 0000000000..f20cd38a3c --- /dev/null +++ b/test/SemaCXX/function-extern-c.cpp @@ -0,0 +1,38 @@ +// RUN: %clang_cc1 -Wreturn-type -fsyntax-only -std=c++11 -verify %s + +class A { +public: + A(const A&); +}; + +struct S { + int i; + double d; + + virtual void B() {} +}; + +union U { + struct { + int i; + virtual void B() {} // Can only do this in C++11 + } t; +}; + +struct S2 { + int i; + double d; +}; + +extern "C" U f3( void ); // expected-warning {{'f3' has C-linkage specified, but returns user-defined type 'U' which is incompatible with C}} +extern "C" S f0(void); // expected-warning {{'f0' has C-linkage specified, but returns user-defined type 'S' which is incompatible with C}} +extern "C" A f4( void ); // expected-warning {{'f4' has C-linkage specified, but returns user-defined type 'A' which is incompatible with C}} + +// These should all be fine +extern "C" S2 f5( void ); +extern "C" void f2( A x ); +extern "C" void f6( S s ); +extern "C" void f7( U u ); +extern "C" double f8(void); +extern "C" long long f11( void ); +extern "C" A *f10( void );