Skip to content

[libc] Fix typo and amend restrict qualifier #152410

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 2 commits into from
Aug 7, 2025

Conversation

Caslyn
Copy link
Contributor

@Caslyn Caslyn commented Aug 6, 2025

This removes an extraneous ',' in the generated dlfcn header.

This also adds __restrict to dladdr's declaration per POSIX. Another fix is made: the C standard restrict keyword is removed from dlinfo.cpp/dlinfo.h (but note that dlfcn.yaml still annotates __restrict for dlinfo's decl).

This removes an extraneous ',' in the generated dlfcn header.

This also adds `__restrict` to `dladdr`'s declaration per POSIX. The
`restrict` C standard keyword is removed from dlinfo.cpp/dlinfo.h
(but note that the dlinfo's declaration in dlfcn.h still annoates
__restrict in the decl).
@Caslyn Caslyn marked this pull request as ready for review August 7, 2025 00:28
@Caslyn Caslyn self-assigned this Aug 7, 2025
@Caslyn Caslyn requested a review from frobtech August 7, 2025 00:28
@llvmbot llvmbot added the libc label Aug 7, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 7, 2025

@llvm/pr-subscribers-libc

Author: Caslyn Tonelli (Caslyn)

Changes

This removes an extraneous ',' in the generated dlfcn header.

This also adds __restrict to dladdr's declaration per POSIX. Another fix is made: the C standard restrict keyword is removed from dlinfo.cpp/dlinfo.h (but note that dlfcn.yaml still annotates __restrict for dlinfo's decl).


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

3 Files Affected:

  • (modified) libc/include/dlfcn.yaml (+3-3)
  • (modified) libc/src/dlfcn/dlinfo.cpp (+1-2)
  • (modified) libc/src/dlfcn/dlinfo.h (+1-1)
diff --git a/libc/include/dlfcn.yaml b/libc/include/dlfcn.yaml
index db2893aaff5d9..bf17a11111050 100644
--- a/libc/include/dlfcn.yaml
+++ b/libc/include/dlfcn.yaml
@@ -46,7 +46,7 @@ enums:
     standards:
       - gnu
     value: 2
-  - name: RTLD_DI_CONFIGADDR,
+  - name: RTLD_DI_CONFIGADDR
     standards:
       - gnu
     value: 3
@@ -127,5 +127,5 @@ functions:
       - POSIX
     return_type: int
     arguments:
-      - type: const void *
-      - type: Dl_info *
+      - type: const void *__restrict
+      - type: Dl_info *__restrict
diff --git a/libc/src/dlfcn/dlinfo.cpp b/libc/src/dlfcn/dlinfo.cpp
index d78cade5ea593..ec2b0fe1464bc 100644
--- a/libc/src/dlfcn/dlinfo.cpp
+++ b/libc/src/dlfcn/dlinfo.cpp
@@ -15,8 +15,7 @@
 namespace LIBC_NAMESPACE_DECL {
 
 // TODO: https://github.com/llvm/llvm-project/issues/149911
-LLVM_LIBC_FUNCTION(int, dlinfo,
-                   (void *restrict handle, int request, void *restrict info)) {
+LLVM_LIBC_FUNCTION(int, dlinfo, (void *handle, int request, void *info)) {
   return -1;
 }
 
diff --git a/libc/src/dlfcn/dlinfo.h b/libc/src/dlfcn/dlinfo.h
index c2c34f02bd6f1..98f627636a34c 100644
--- a/libc/src/dlfcn/dlinfo.h
+++ b/libc/src/dlfcn/dlinfo.h
@@ -13,7 +13,7 @@
 
 namespace LIBC_NAMESPACE_DECL {
 
-int dlinfo(void *restrict, int, void *restrict);
+int dlinfo(void *, int, void *);
 
 } // namespace LIBC_NAMESPACE_DECL
 

@lntue lntue changed the title [lib] Fix typo and amend restrict qualifier [libc] Fix typo and amend restrict qualifier Aug 7, 2025
@@ -15,8 +15,7 @@
namespace LIBC_NAMESPACE_DECL {

// TODO: https://github.com/llvm/llvm-project/issues/149911
LLVM_LIBC_FUNCTION(int, dlinfo,
(void *restrict handle, int request, void *restrict info)) {
LLVM_LIBC_FUNCTION(int, dlinfo, (void *handle, int request, void *info)) {
Copy link
Contributor

@lntue lntue Aug 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dlinfo in dlfcn.yaml is declared with:

      - type: void *__restrict
      - type: int
      - type: void *__restrict

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW I wasn't sure about annotating dlinfo.cpp/dlinfo.h. For example, I looked at how the existing dlsym added __restrict in dlfcn.yaml[0] but omits the qualifiers in its source files[1].

[0] https://github.com/llvm/llvm-project/blob/main/libc/include/dlfcn.yaml#L110-L116
[1] https://github.com/llvm/llvm-project/blob/main/libc/src/dlfcn/dlsym.cpp

Double checking to proceed with adding __restrict on the stub definition? (I can add it on the other dlfcn stubs as well).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double checking to proceed with adding __restrict on the stub definition? (I can add it on the other dlfcn stubs as well).

Yes please. We should keep the signatures consistent. Thanks,

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For standard qualifiers like this, the declaration is authoritative and if the definition doesn't match and the compiler doesn't make that an error then it means it doesn't change the semantics to have it only on the declaration. (The situation is different for certain attributes.)

However, note that there won't be any cross-checking of the public (extern "C") signature and the namespaced internal signature unless you're doing a production build where the definitions are aliases and the compiler will complain about mismatches. So it's important to take care that the src/fooheader/barfunc.h internal declaration precisely matches the public C signature in the generated public function.

And, notwithstanding the first paragraph, for comprehensibility and maintenance reasons, it should always be our practice to use the full precise signature in the definition just as it is in the definition (modulo function attributes).

Ergo, this is missing dladdr.{cpp,h} changes to match the generated declaration, and dlinfo.{cpp,h}| were better as they were.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in d01192e

Copy link
Contributor

@frobtech frobtech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with changes to keep restrict everywhere for both dladdr and dlinfo

@@ -15,8 +15,7 @@
namespace LIBC_NAMESPACE_DECL {

// TODO: https://github.com/llvm/llvm-project/issues/149911
LLVM_LIBC_FUNCTION(int, dlinfo,
(void *restrict handle, int request, void *restrict info)) {
LLVM_LIBC_FUNCTION(int, dlinfo, (void *handle, int request, void *info)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For standard qualifiers like this, the declaration is authoritative and if the definition doesn't match and the compiler doesn't make that an error then it means it doesn't change the semantics to have it only on the declaration. (The situation is different for certain attributes.)

However, note that there won't be any cross-checking of the public (extern "C") signature and the namespaced internal signature unless you're doing a production build where the definitions are aliases and the compiler will complain about mismatches. So it's important to take care that the src/fooheader/barfunc.h internal declaration precisely matches the public C signature in the generated public function.

And, notwithstanding the first paragraph, for comprehensibility and maintenance reasons, it should always be our practice to use the full precise signature in the definition just as it is in the definition (modulo function attributes).

Ergo, this is missing dladdr.{cpp,h} changes to match the generated declaration, and dlinfo.{cpp,h}| were better as they were.

@Caslyn Caslyn merged commit b8195e3 into llvm:main Aug 7, 2025
19 checks passed
@Caslyn Caslyn deleted the correct-dladdr-params branch August 7, 2025 23:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants