Skip to content

[clang][Sema] Fix diagnostic for function overloading in extern "C" #106033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ Improvements to Clang's diagnostics
compilation speed with modules. This warning is disabled by default and it needs
to be explicitly enabled or by ``-Weverything``.

- Improved diagnostic when trying to overload a function in an ``extern "C"`` context. (#GH80235)

Improvements to Clang's time-trace
----------------------------------

Expand Down
4 changes: 4 additions & 0 deletions clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3882,6 +3882,10 @@ bool Sema::MergeFunctionDecl(FunctionDecl *New, NamedDecl *&OldD, Scope *S,
if (New->isCXXClassMember() && New->isOutOfLine())
Diag(New->getLocation(), diag::err_member_def_does_not_match_ret_type)
<< New << New->getReturnTypeSourceRange();
else if (Old->isExternC() && New->isExternC() &&
!Old->hasAttr<OverloadableAttr>() &&
!New->hasAttr<OverloadableAttr>())
Diag(New->getLocation(), diag::err_conflicting_types) << New;
else
Diag(New->getLocation(), diag::err_ovl_diff_return_type)
<< New->getReturnTypeSourceRange();
Expand Down
19 changes: 19 additions & 0 deletions clang/test/SemaCXX/extern-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,19 @@ namespace foo {
}
}

namespace extern_ovl {
extern "C" {
__attribute__((overloadable))
void ovl_decl(void); // expected-note {{previous}}

__attribute__((overloadable))
int ovl_decl(int);

__attribute__((overloadable))
int ovl_decl(void); // expected-error {{functions that differ only in their return type}}
}
}

namespace linkage {
namespace redecl {
extern "C" {
Expand All @@ -88,6 +101,12 @@ namespace linkage {
void linkage_redecl(double); // expected-error {{conflicting types}}
}
}
namespace redecl_2 {
extern "C" {
void linkage_redecl_2(); // expected-note {{previous}}
int linkage_redecl_2(int); // expected-error {{conflicting types}}
}
}
namespace from_outer {
void linkage_from_outer_1(); // expected-note {{previous}}
void linkage_from_outer_2(); // expected-note {{previous}}
Expand Down
Loading