[libc] Request for Annex K via `__STDC_WANT_LIB_EXT1__` doesn't work as expected
Source code:
#include <stdio.h>
#define __STDC_WANT_LIB_EXT1__ 1
#include <string.h>
int foo() {
return strnlen_s("abc", 2);
}
Compile with:
clang -c a.c
We get the error:
a.c:6:10: error: call to undeclared function 'strnlen_s'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
6 | return strnlen_s("abc", 2);
| ^
If we move the line #define __STDC_WANT_LIB_EXT1__ 1 before #include <stdio.h>, it works as expected.
Analysis
I believe this issue is caused by the header inclusion guard in llvm-libc-macros/annex-k-macros.h.
The inclusion of stdio.h implies a recursive inclusion of annex-k-macros.h. But at that point, the __STDC_WANT_LIB_EXT1__ is not yet defined, therefore the macro who controls the availability of Annex K functionality, LIBC_HAS_ANNEX_K, is not defined.
By the time __STDC_WANT_LIB_EXT1__ is defined and string.h is included, the recursive inclusion of annex-k-macros.h (by the second time now) is stopped at the header inclusion guard, hence LIBC_HAS_ANNEX_K is again not defined. As a result, the declaration of strnlen_s is never brought in.
Found by @simpal01
@llvm/issue-subscribers-libc
Author: Victor Campos (vhscampos)
int foo() { return strnlen_s("abc", 2); }
Compile with:
`clang a.c`
We get the error:
a.c:6:10: error: call to undeclared function 'strnlen_s'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration] 6 | return strnlen_s("abc", 2); | ^
If we move the line `#define __STDC_WANT_LIB_EXT1__ 1` before `#include <stdio.h>`, it works as expected.
**Analysis**
I believe this issue is caused by the header inclusion guard in `llvm-libc-macros/annex-k-macros.h`.
The inclusion of `stdio.h` implies a recursive inclusion of `annex-k-macros.h`. But at that point, the `__STDC_WANT_LIB_EXT1__` is not yet defined, therefore the macro who controls the availability of Annex K functionality, `LIBC_HAS_ANNEX_K`, is not defined.
By the time `__STDC_WANT_LIB_EXT1__` is defined and `string.h` is included, the recursive inclusion of `annex-k-macros.h` (by the second time now) is stopped at the header inclusion guard, hence `LIBC_HAS_ANNEX_K` is again not defined. As a result, the declaration of `strnlen_s` is never brought in.
</details>
One solution is to remove the header inclusion guard from annex-k-macros.h and surround the #define LIBC_HAS_ANNEX_K by an #ifndef condition.
@vhscampos Hi, is anyone currently working on this issue? If not, I’d like to take it on.
Hi @ChenMiaoi. No, it is untaken. Feel free to! Please add me as a reviewer once you have a PR.
Hi!
This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:
- Check that no other contributor is working on this issue. If someone is assigned to the issue or claimed to be working on it, ping the person. After one week without a response, the assignee may be changed.
- Leave a comment indicating that you are working on the issue, or just create a pull request after following the steps below. Mention this issue in the description of the pull request.
- Fix the issue locally. Be aware that LLVM's AI policy forbids the use of AI tools to fix good first issues since it squanders a learning opportunity. Using LLMs to search and learn is permitted, since the goal is learning.
- Run the test suite locally. Remember that the subdirectories under
test/create fine-grained testing targets, so you can e.g. usemake check-clang-astto only run Clang's AST tests. - Create a Git commit.
- Run
git clang-format HEAD~1to format your changes. - Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation. Mention this issue in the description of the pull request.
If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below.
@llvm/issue-subscribers-good-first-issue
Author: Victor Campos (vhscampos)
int foo() { return strnlen_s("abc", 2); }
Compile with:
`clang -c a.c`
We get the error:
a.c:6:10: error: call to undeclared function 'strnlen_s'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration] 6 | return strnlen_s("abc", 2); | ^
If we move the line `#define __STDC_WANT_LIB_EXT1__ 1` before `#include <stdio.h>`, it works as expected.
**Analysis**
I believe this issue is caused by the header inclusion guard in `llvm-libc-macros/annex-k-macros.h`.
The inclusion of `stdio.h` implies a recursive inclusion of `annex-k-macros.h`. But at that point, the `__STDC_WANT_LIB_EXT1__` is not yet defined, therefore the macro who controls the availability of Annex K functionality, `LIBC_HAS_ANNEX_K`, is not defined.
By the time `__STDC_WANT_LIB_EXT1__` is defined and `string.h` is included, the recursive inclusion of `annex-k-macros.h` (by the second time now) is stopped at the header inclusion guard, hence `LIBC_HAS_ANNEX_K` is again not defined. As a result, the declaration of `strnlen_s` is never brought in.
Found by @<!-- -->simpal01
</details>
Hi @ChenMiaoi if you're not currently working on this issue ,could I work on this issue?
13. Hi @ChenMiaoi if you're not currently working on this issue ,could I work on this issue?
Of course! My focus is currently on Clang frontend. When you open your PR, please remind me to close mine.
@vhscampos sir can I work on this issue