Bug 13699 - Extern "C" routine in different namespaces accepted with different exception signature
Summary: Extern "C" routine in different namespaces accepted with different exception ...
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.4.0
: P2 normal
Target Milestone: 4.4.0
Assignee: Dodji Seketeli
Keywords: accepts-invalid
Depends on:
Blocks: 12944 25940
  Show dependency treegraph
Reported: 2004-01-15 15:16 UTC by Grigory Zagorodnev
Modified: 2008-07-16 23:52 UTC (History)
2 users (show)

See Also:
Host: i686-redhat-linux
Known to work:
Known to fail:
Last reconfirmed: 2008-07-11 18:19:42

first attempt at trying to fix the bug (1.76 KB, patch)
2008-07-07 14:54 UTC, Dodji Seketeli
Details | Diff
display warnings instead of error (1.78 KB, patch)
2008-07-08 17:58 UTC, Dodji Seketeli
Details | Diff
update the patch (1.82 KB, patch)
2008-07-15 15:15 UTC, Dodji Seketeli
Details | Diff
more work in the matter (1.86 KB, patch)
2008-07-16 13:39 UTC, Dodji Seketeli
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Grigory Zagorodnev 2004-01-15 15:16:38 UTC
G++ compiler does not issue any error on the standard-violating code listed 
below, regardless of pedantry level.

	namespace A {
	    extern "C" void foo() throw();
	extern "C" void foo();

According to the C++ Standard, "two declarations for a function with C language 
linkage with the same function name... that appear in different namespace 
scopes refer to the same function" [7.5/6] 

Next, "if any declaration of a function has an exception-specification, all 
declarations, including the definition.., of that function shell have an 
exception-specification with the same set of type-ids" [15.4/2]

In our example, two declarations of the same extern "C" routine 'foo' have 
different exception-specifications. This is the C++ standard violation.

However, G++ compiler silently compiles this code. This makes to think 
extern "C" routine in different namespaces is not the same entity for g++.
Comment 1 Wolfgang Bangerth 2004-01-15 15:29:51 UTC
The compiler treats the two declarations as the same function, though. 
This is evidenced by this program: 
namespace A { 
  extern "C" void foo() throw(); 
  void f() { foo(); } 
extern "C" void foo(); 
void f() { foo(); } 
Both versions of f() should call the same function foo, and they 
indeed do if one looks at the assembler output. 
Comment 2 Andrew Pinski 2004-01-15 16:28:08 UTC
ICC 6.0 rejects this.
Comment 3 Dodji Seketeli 2008-07-07 14:54:50 UTC
Created attachment 15871 [details]
first attempt at trying to fix the bug

This patch checks that re-declaration of extern "C" functions complies with the exception specification constraints.
Comment 4 Dodji Seketeli 2008-07-08 17:58:33 UTC
Created attachment 15876 [details]
display warnings instead of error

Second opus of the patch.

It appears that the patch makes g++ not capable of compiling libstdc++ precisely because libstdc++ contains re-declarations of functions with C linkage specification, without respecting the exception specification constraints.

Therefore, the we now report warnings instead of errors. Errors can be triggered using -pedantic though.

The patch has been regtested on x86.
Comment 5 Dodji Seketeli 2008-07-15 15:15:16 UTC
Created attachment 15911 [details]
update the patch

This patch looks better. It should regtest safely and is not far from behing ready to be submitted to the list.
Comment 6 Dodji Seketeli 2008-07-16 13:39:00 UTC
Created attachment 15916 [details]
more work in the matter

This patch performs a more accurate lookup of the extern C functions to be able to emit pedwarnings on redeclarations that don't comply with exception specification constraints.
Comment 7 Dodji Seketeli 2008-07-16 23:44:48 UTC
Subject: Bug 13699

Author: dodji
Date: Wed Jul 16 23:44:02 2008
New Revision: 137904

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=137904
2008-07-16  Dodji Seketeli  <dseketel@redhat.com>

	PR c++/13699
	* gcc/cp/name-lookup.c (lookup_extern_c_fun_binding_in_all_ns):
	New function.
	(pushdecl_maybe_friend): Check if a redeclaration of extern C function
	complies with exception specification constraints.


Comment 8 Dodji Seketeli 2008-07-16 23:52:19 UTC
fixed in trunk.