[cfe-dev] -Wcatch-incomplete-type-extensions

Howard Hinnant hhinnant at apple.com
Wed Jan 18 18:38:15 PST 2012


On Jan 18, 2012, at 9:17 PM, Eli Friedman wrote:

> On Wed, Jan 18, 2012 at 6:01 PM, Howard Hinnant <hhinnant at apple.com> wrote:
>> I'm wondering why this is a warning and not an error:
>> 
>> struct A;
>> 
>> void foo();
>> 
>> #include <iostream>
>> 
>> int main()
>> {
>>    try
>>    {
>>        foo();
>>    }
>>    catch (A&)
>>    {
>>        std::cout << "Caught an A\n";
>>    }
>>    catch (...)
>>    {
>>        std::cout << "Caught ...\n";
>>    }
>> }
>> 
>> struct A {};
>> 
>> void foo()
>> {
>>    throw A();
>> }
>> 
>> test.cpp:13:14: warning: ISO C++ forbids catching a reference to incomplete type 'A' [-Wcatch-incomplete-type-extensions]
>>    catch (A&)
>>             ^
>> test.cpp:1:8: note: forward declaration of 'A'
>> struct A;
>>       ^
>> 1 warning generated.
>> 
>> g++-4.2 just says:
>> 
>> test.cpp: In function ‘int main()’:
>> test.cpp:13: error: invalid use of incomplete type ‘struct A’
>> test.cpp:1: error: forward declaration of ‘struct A’
> 
> See http://llvm.org/bugs/show_bug.cgi?id=6527 .  Granted, I'm not
> really sure that fix was appropriate.
> 
> -Eli

Thanks Eli.  That is the context I was looking for.  This looks more like a template-2-phase-lookup issue to me (where clang is doing a better job than gcc during template parsing).

In order to match the thrown object to a catch handler the personality function needs to read the std::type_info* out of the lsda table at the end of a procedure fragment.  So the question becomes:  what does the compiler encode for a std::type_info* for an incomplete class type?  I haven't found a way to entice the compiler to reveal that except in the case of clang and in the case of the incomplete type in the catch clause.  For example typeid(A) enforces a complete type A.

If clang created a special type_info-derived class to relegate incomplete types to, I could detect them and call terminate.  But as it is, I don't know how to detect type_info's to incomplete types.  The Itanium ABI (http://sourcery.mentor.com/public/cxx-abi/abi.html#rtti) specifies how to detect the type_info's for fundamental types, arrays, functions, enums, classes (which presumably includes unions), pointers, and member pointers.  But not incomplete types.

Quote (from http://sourcery.mentor.com/public/cxx-abi/abi.html#rtti):

	• The possible derived types are:

		• abi::__fundamental_type_info
		• abi::__array_type_info
		• abi::__function_type_info
		• abi::__enum_type_info
		• abi::__class_type_info
		• abi::__si_class_type_info
		• abi::__vmi_class_type_info
		• abi::__pbase_type_info
		• abi::__pointer_type_info
		• abi::__pointer_to_member_type_info

My recommendation at this point is to turn this back into a hard error.  But I'm still early in the learning curve on this issue.

Howard





More information about the cfe-dev mailing list