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

Conversation

s-watanabe314
Copy link
Contributor

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.

Copy link

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 @ followed by their GitHub username.

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.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Aug 26, 2024
@llvmbot
Copy link
Member

llvmbot commented Aug 26, 2024

@llvm/pr-subscribers-clang

Author: None (s-watanabe314)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/106033.diff

2 Files Affected:

  • (modified) clang/lib/Sema/SemaDecl.cpp (+4)
  • (modified) clang/test/SemaCXX/extern-c.cpp (+19)
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}}

@s-watanabe314 s-watanabe314 changed the title poor diagnostic due to overloading in extern "C" block [clang][Sema] Fix diagnostic for function overloading in extern "C" Aug 26, 2024
Copy link
Member

@Sirraide Sirraide left a 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.

@Sirraide
Copy link
Member

Ah, actually, this is still missing a release note (in clang/docs/ReleaseNotes.rst, in the ‘Improvements to Clang's diagnostics’ section)

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.
@s-watanabe314 s-watanabe314 force-pushed the feature/clang-diagnostic-externC branch from cff1426 to 69ebe6a Compare August 27, 2024 02:27
@s-watanabe314
Copy link
Contributor Author

Thank you for comments. I added release note.

Copy link
Collaborator

@shafik shafik left a 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?

@dwblaikie
Copy link
Collaborator

This make sense to me, @dwblaikie wdyt?

Seems OK to me

@s-watanabe314
Copy link
Contributor Author

Thank you for the approval. I have been granted commit access, so I will merge it myself.

@s-watanabe314 s-watanabe314 merged commit 78abeca into llvm:main Sep 3, 2024
9 checks passed
Copy link

github-actions bot commented Sep 3, 2024

@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-ci
Copy link
Collaborator

llvm-ci commented Sep 3, 2024

LLVM Buildbot has detected a new failure on builder sanitizer-ppc64le-linux running on ppc64le-sanitizer while building clang at step 2 "annotate".

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
Step 2 (annotate) failure: 'python ../sanitizer_buildbot/sanitizers/zorg/buildbot/builders/sanitizers/buildbot_selector.py' (failure)
...
PASS: ThreadSanitizer-powerpc64le :: race_on_read.cpp (2344 of 2489)
PASS: ThreadSanitizer-powerpc64le :: mutexset8.cpp (2345 of 2489)
PASS: SanitizerCommon-msan-powerpc64le-Linux :: Posix/posix_memalign-alignment.cpp (2346 of 2489)
PASS: ScudoStandalone-Unit :: ./ScudoUnitTest-powerpc64le-Test/77/277 (2347 of 2489)
PASS: ThreadSanitizer-powerpc64le :: race_on_fputs.cpp (2348 of 2489)
PASS: ThreadSanitizer-powerpc64le :: custom_mutex4.cpp (2349 of 2489)
PASS: ThreadSanitizer-powerpc64le :: signal_errno.cpp (2350 of 2489)
PASS: ThreadSanitizer-powerpc64le :: race_on_heap.cpp (2351 of 2489)
PASS: SanitizerCommon-tsan-powerpc64le-Linux :: Posix/setvbuf.cpp (2352 of 2489)
PASS: SanitizerCommon-msan-powerpc64le-Linux :: get_module_and_offset_for_pc.cpp (2353 of 2489)
FAIL: ThreadSanitizer-powerpc64le :: signal_block.cpp (2354 of 2489)
******************** TEST 'ThreadSanitizer-powerpc64le :: signal_block.cpp' FAILED ********************
Exit Code: 1

Command Output (stderr):
--
RUN: at line 1: /home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/build_default/./bin/clang  -fsanitize=thread -Wall  -m64 -fno-function-sections   -gline-tables-only -I/home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/llvm-project/compiler-rt/test/tsan/../ -O1 /home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/llvm-project/compiler-rt/test/tsan/signal_block.cpp -o /home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/build_default/runtimes/runtimes-bins/compiler-rt/test/tsan/POWERPC64LEConfig/Output/signal_block.cpp.tmp &&  /home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/build_default/runtimes/runtimes-bins/compiler-rt/test/tsan/POWERPC64LEConfig/Output/signal_block.cpp.tmp 2>&1 | FileCheck /home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/llvm-project/compiler-rt/test/tsan/signal_block.cpp
+ /home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/build_default/./bin/clang -fsanitize=thread -Wall -m64 -fno-function-sections -gline-tables-only -I/home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/llvm-project/compiler-rt/test/tsan/../ -O1 /home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/llvm-project/compiler-rt/test/tsan/signal_block.cpp -o /home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/build_default/runtimes/runtimes-bins/compiler-rt/test/tsan/POWERPC64LEConfig/Output/signal_block.cpp.tmp
+ /home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/build_default/runtimes/runtimes-bins/compiler-rt/test/tsan/POWERPC64LEConfig/Output/signal_block.cpp.tmp
+ FileCheck /home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/llvm-project/compiler-rt/test/tsan/signal_block.cpp
/home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/llvm-project/compiler-rt/test/tsan/signal_block.cpp:59:15: error: CHECK-NOT: excluded string found in input
// CHECK-NOT: WARNING: ThreadSanitizer:
              ^
<stdin>:2:1: note: found here
WARNING: ThreadSanitizer: signal handler spoils errno (pid=1174113)
^~~~~~~~~~~~~~~~~~~~~~~~~

Input file: <stdin>
Check file: /home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/llvm-project/compiler-rt/test/tsan/signal_block.cpp

-dump-input=help explains the following input dump.

Input was:
<<<<<<
        1: ================== 
        2: WARNING: ThreadSanitizer: signal handler spoils errno (pid=1174113) 
not:59     !~~~~~~~~~~~~~~~~~~~~~~~~                                            error: no match expected
        3:  Signal 10 handler invoked at: 
        4:  #0 handler(int) /home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/llvm-project/compiler-rt/test/tsan/signal_block.cpp:13 (signal_block.cpp.tmp+0xfc650) 
        5:  #1 thread(void*) /home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/llvm-project/compiler-rt/test/tsan/signal_block.cpp:25:5 (signal_block.cpp.tmp+0xfc7a0) 
        6:  
        7: SUMMARY: ThreadSanitizer: signal handler spoils errno /home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/llvm-project/compiler-rt/test/tsan/signal_block.cpp:13 in handler(int) 
        8: ================== 
        9: DONE 
       10: ThreadSanitizer: reported 1 warnings 
>>>>>>

--

Step 9 (test compiler-rt debug) failure: test compiler-rt debug (failure)
...
PASS: ThreadSanitizer-powerpc64le :: race_on_read.cpp (2344 of 2489)
PASS: ThreadSanitizer-powerpc64le :: mutexset8.cpp (2345 of 2489)
PASS: SanitizerCommon-msan-powerpc64le-Linux :: Posix/posix_memalign-alignment.cpp (2346 of 2489)
PASS: ScudoStandalone-Unit :: ./ScudoUnitTest-powerpc64le-Test/77/277 (2347 of 2489)
PASS: ThreadSanitizer-powerpc64le :: race_on_fputs.cpp (2348 of 2489)
PASS: ThreadSanitizer-powerpc64le :: custom_mutex4.cpp (2349 of 2489)
PASS: ThreadSanitizer-powerpc64le :: signal_errno.cpp (2350 of 2489)
PASS: ThreadSanitizer-powerpc64le :: race_on_heap.cpp (2351 of 2489)
PASS: SanitizerCommon-tsan-powerpc64le-Linux :: Posix/setvbuf.cpp (2352 of 2489)
PASS: SanitizerCommon-msan-powerpc64le-Linux :: get_module_and_offset_for_pc.cpp (2353 of 2489)
FAIL: ThreadSanitizer-powerpc64le :: signal_block.cpp (2354 of 2489)
******************** TEST 'ThreadSanitizer-powerpc64le :: signal_block.cpp' FAILED ********************
Exit Code: 1

Command Output (stderr):
--
RUN: at line 1: /home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/build_default/./bin/clang  -fsanitize=thread -Wall  -m64 -fno-function-sections   -gline-tables-only -I/home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/llvm-project/compiler-rt/test/tsan/../ -O1 /home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/llvm-project/compiler-rt/test/tsan/signal_block.cpp -o /home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/build_default/runtimes/runtimes-bins/compiler-rt/test/tsan/POWERPC64LEConfig/Output/signal_block.cpp.tmp &&  /home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/build_default/runtimes/runtimes-bins/compiler-rt/test/tsan/POWERPC64LEConfig/Output/signal_block.cpp.tmp 2>&1 | FileCheck /home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/llvm-project/compiler-rt/test/tsan/signal_block.cpp
+ /home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/build_default/./bin/clang -fsanitize=thread -Wall -m64 -fno-function-sections -gline-tables-only -I/home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/llvm-project/compiler-rt/test/tsan/../ -O1 /home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/llvm-project/compiler-rt/test/tsan/signal_block.cpp -o /home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/build_default/runtimes/runtimes-bins/compiler-rt/test/tsan/POWERPC64LEConfig/Output/signal_block.cpp.tmp
+ /home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/build_default/runtimes/runtimes-bins/compiler-rt/test/tsan/POWERPC64LEConfig/Output/signal_block.cpp.tmp
+ FileCheck /home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/llvm-project/compiler-rt/test/tsan/signal_block.cpp
/home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/llvm-project/compiler-rt/test/tsan/signal_block.cpp:59:15: error: CHECK-NOT: excluded string found in input
// CHECK-NOT: WARNING: ThreadSanitizer:
              ^
<stdin>:2:1: note: found here
WARNING: ThreadSanitizer: signal handler spoils errno (pid=1174113)
^~~~~~~~~~~~~~~~~~~~~~~~~

Input file: <stdin>
Check file: /home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/llvm-project/compiler-rt/test/tsan/signal_block.cpp

-dump-input=help explains the following input dump.

Input was:
<<<<<<
        1: ================== 
        2: WARNING: ThreadSanitizer: signal handler spoils errno (pid=1174113) 
not:59     !~~~~~~~~~~~~~~~~~~~~~~~~                                            error: no match expected
        3:  Signal 10 handler invoked at: 
        4:  #0 handler(int) /home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/llvm-project/compiler-rt/test/tsan/signal_block.cpp:13 (signal_block.cpp.tmp+0xfc650) 
        5:  #1 thread(void*) /home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/llvm-project/compiler-rt/test/tsan/signal_block.cpp:25:5 (signal_block.cpp.tmp+0xfc7a0) 
        6:  
        7: SUMMARY: ThreadSanitizer: signal handler spoils errno /home/buildbots/llvm-external-buildbots/workers/ppc64le-sanitizer/sanitizer-ppc64le/build/llvm-project/compiler-rt/test/tsan/signal_block.cpp:13 in handler(int) 
        8: ================== 
        9: DONE 
       10: ThreadSanitizer: reported 1 warnings 
>>>>>>

--


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

poor diagnostic due to overloading in extern "C" block
6 participants