Skip to content

Commit 9baf5ad

Browse files
committed
poor diagnostic due to overloading in extern "C" block #80235
Fixes #80235 When trying to overload a function within `extern "C"`, the diagnostic `functions that differ only in their return type cannot be overloaded` is given. This diagnostic is inappropriate because overloading is basically not allowed in the C language. However, if the redeclared function has the `((overloadable))` attribute, it should be diagnosed as `functions that differ only in their return type cannot be overloaded`. This patch uses `isExternC()` to provide an appropriate diagnostic during the diagnostic process. `isExternC()` updates the linkage information cache internally, so calling it before merging functions can cause clang to crash. An example is declaring `static void foo()` and `void foo()` within an `extern "C"` block. Therefore, I decided to call `isExternC()` after the compilation error is confirmed and select the diagnostic message. The diagnostic message is `conflicting types for 'func'` similar to the diagnostic in C, and `functions that differ only in their return type cannot be overloaded` if the `((overloadable))` attribute is given. Regression tests verify that the expected diagnostics are given when trying to overload functions within `extern "C"` and when the `((overloadable))` attribute is present.
1 parent 82579f9 commit 9baf5ad

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

clang/lib/Sema/SemaDecl.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3882,6 +3882,10 @@ bool Sema::MergeFunctionDecl(FunctionDecl *New, NamedDecl *&OldD, Scope *S,
38823882
if (New->isCXXClassMember() && New->isOutOfLine())
38833883
Diag(New->getLocation(), diag::err_member_def_does_not_match_ret_type)
38843884
<< New << New->getReturnTypeSourceRange();
3885+
else if (Old->isExternC() && New->isExternC() &&
3886+
!Old->hasAttr<OverloadableAttr>() &&
3887+
!New->hasAttr<OverloadableAttr>())
3888+
Diag(New->getLocation(), diag::err_conflicting_types) << New;
38853889
else
38863890
Diag(New->getLocation(), diag::err_ovl_diff_return_type)
38873891
<< New->getReturnTypeSourceRange();

clang/test/SemaCXX/extern-c.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,19 @@ namespace foo {
7777
}
7878
}
7979

80+
namespace extern_ovl {
81+
extern "C" {
82+
__attribute__((overloadable))
83+
void ovl_decl(void); // expected-note {{previous}}
84+
85+
__attribute__((overloadable))
86+
int ovl_decl(int);
87+
88+
__attribute__((overloadable))
89+
int ovl_decl(void); // expected-error {{functions that differ only in their return type}}
90+
}
91+
}
92+
8093
namespace linkage {
8194
namespace redecl {
8295
extern "C" {
@@ -88,6 +101,12 @@ namespace linkage {
88101
void linkage_redecl(double); // expected-error {{conflicting types}}
89102
}
90103
}
104+
namespace redecl_2 {
105+
extern "C" {
106+
void linkage_redecl_2(); // expected-note {{previous}}
107+
int linkage_redecl_2(int); // expected-error {{conflicting types}}
108+
}
109+
}
91110
namespace from_outer {
92111
void linkage_from_outer_1(); // expected-note {{previous}}
93112
void linkage_from_outer_2(); // expected-note {{previous}}

0 commit comments

Comments
 (0)