Skip to content

Commit f883365

Browse files
[libc] fix stdbit include test when not all entrypoints are available (#80323)
The intent of the test is to check that: 1. The type generic macros are defined. 2. Those macros dispatch to the correct underlying function. The issue is that when new functionality is added to our stdbit.h without rolling out the new entrypoint to all targets, this test breaks because our generated stdbit.h will not contain declarations for the underlying function. In that case, we should just declare the underlying functions first before including our generated stdbit.h which just contains declarations. A definition is a declaration, but redeclarations must match, hence the additions of noexcept and extern "C".
1 parent 10c2d5f commit f883365

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

libc/test/include/stdbit_test.cpp

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,38 @@
88

99
#include "test/UnitTest/Test.h"
1010

11-
#include <stdbit.h>
12-
1311
/*
1412
* The intent of this test is validate that:
15-
* 1. We provide the definition of the various type generic macros of stdbit.h.
13+
* 1. We provide the definition of the various type generic macros of stdbit.h
14+
* (the macros are transitively included from stdbit-macros.h by stdbit.h).
1615
* 2. It dispatches to the correct underlying function.
1716
* Because unit tests build without public packaging, the object files produced
1817
* do not contain non-namespaced symbols.
1918
*/
2019

21-
unsigned char stdc_leading_zeros_uc(unsigned char) { return 0xAA; }
22-
unsigned short stdc_leading_zeros_us(unsigned short) { return 0xAB; }
23-
unsigned stdc_leading_zeros_ui(unsigned) { return 0xAC; }
24-
unsigned long stdc_leading_zeros_ul(unsigned long) { return 0xAD; }
25-
unsigned long long stdc_leading_zeros_ull(unsigned long long) { return 0xAF; }
26-
unsigned char stdc_leading_ones_uc(unsigned char) { return 0xBA; }
27-
unsigned short stdc_leading_ones_us(unsigned short) { return 0xBB; }
28-
unsigned stdc_leading_ones_ui(unsigned) { return 0xBC; }
29-
unsigned long stdc_leading_ones_ul(unsigned long) { return 0xBD; }
30-
unsigned long long stdc_leading_ones_ull(unsigned long long) { return 0xBF; }
20+
/*
21+
* Declare these BEFORE including stdbit-macros.h so that this test may still be
22+
* run even if a given target doesn't yet have these individual entrypoints
23+
* enabled.
24+
*/
25+
extern "C" {
26+
unsigned char stdc_leading_zeros_uc(unsigned char) noexcept { return 0xAA; }
27+
unsigned short stdc_leading_zeros_us(unsigned short) noexcept { return 0xAB; }
28+
unsigned stdc_leading_zeros_ui(unsigned) noexcept { return 0xAC; }
29+
unsigned long stdc_leading_zeros_ul(unsigned long) noexcept { return 0xAD; }
30+
unsigned long long stdc_leading_zeros_ull(unsigned long long) noexcept {
31+
return 0xAF;
32+
}
33+
unsigned char stdc_leading_ones_uc(unsigned char) noexcept { return 0xBA; }
34+
unsigned short stdc_leading_ones_us(unsigned short) noexcept { return 0xBB; }
35+
unsigned stdc_leading_ones_ui(unsigned) noexcept { return 0xBC; }
36+
unsigned long stdc_leading_ones_ul(unsigned long) noexcept { return 0xBD; }
37+
unsigned long long stdc_leading_ones_ull(unsigned long long) noexcept {
38+
return 0xBF;
39+
}
40+
}
41+
42+
#include "include/llvm-libc-macros/stdbit-macros.h"
3143

3244
TEST(LlvmLibcStdbitTest, TypeGenericMacroLeadingZeros) {
3345
EXPECT_EQ(stdc_leading_zeros(static_cast<unsigned char>(0U)),

0 commit comments

Comments
 (0)