-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
[clang][Sema] Fix diagnostic for function overloading in extern "C" #106033
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-clang Author: None (s-watanabe314) ChangesFixes #80235 When trying to overload a function within This patch uses Regression tests verify that the expected diagnostics are given when trying to overload functions within Full diff: https://github.com/llvm/llvm-project/pull/106033.diff 2 Files Affected:
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 503e93f9257137..63bd15dad7c48a 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -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();
diff --git a/clang/test/SemaCXX/extern-c.cpp b/clang/test/SemaCXX/extern-c.cpp
index 68d1494b94918f..86012670ddcd57 100644
--- a/clang/test/SemaCXX/extern-c.cpp
+++ b/clang/test/SemaCXX/extern-c.cpp
@@ -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" {
@@ -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}}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Thanks for the patch! I’ll merge once CI is done with this.
Ah, actually, this is still missing a release note (in |
Fixes llvm#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.
cff1426
to
69ebe6a
Compare
Thank you for comments. I added release note. |
Co-authored-by: Sirraide <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This make sense to me, @dwblaikie wdyt?
Seems OK to me |
Thank you for the approval. I have been granted commit access, so I will merge it myself. |
@s-watanabe314 Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/72/builds/2892 Here is the relevant piece of the build log for the reference
|
Fixes #80235
When trying to overload a function within
extern "C"
, the diagnosticfunctions 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 asfunctions 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 declaringstatic void foo()
andvoid foo()
within anextern "C"
block. Therefore, I decided to callisExternC()
after the compilation error is confirmed and select the diagnostic message. The diagnostic message isconflicting types for 'func'
similar to the diagnostic in C, andfunctions 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.