Skip to content

Commit 6b5ac96

Browse files
committed
Add compile-time helper methods for testing compiler support
And add missing fly.cpp unit test to Windows build.
1 parent 6ce95a6 commit 6b5ac96

File tree

4 files changed

+115
-5
lines changed

4 files changed

+115
-5
lines changed

build/win/libfly_unit_tests/libfly_unit_tests.vcxproj

+1
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@
210210
</ProjectReference>
211211
</ItemGroup>
212212
<ItemGroup>
213+
<ClCompile Include="..\..\..\test\fly.cpp" />
213214
<ClCompile Include="..\..\..\test\main.cpp" />
214215
<ClCompile Include="..\..\..\test\coders\base64_coder.cpp" />
215216
<ClCompile Include="..\..\..\test\coders\huffman_coder.cpp" />

build/win/libfly_unit_tests/libfly_unit_tests.vcxproj.filters

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
</Filter>
3434
</ItemGroup>
3535
<ItemGroup>
36+
<ClCompile Include="..\..\..\test\fly.cpp" />
3637
<ClCompile Include="..\..\..\test\main.cpp" />
3738
<ClCompile Include="..\..\..\test\coders\base64_coder.cpp">
3839
<Filter>coders</Filter>

fly/fly.hpp

+79
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,29 @@
1111
# error Unsupported operating system. Only Linux, macOS, and Windows are supported.
1212
#endif
1313

14+
// Detect compiler.
15+
#if defined(__clang__)
16+
# define FLY_COMPILER_CLANG
17+
#elif defined(__GNUC__)
18+
# define FLY_COMPILER_GCC
19+
#elif defined(_MSC_VER)
20+
# define FLY_COMPILER_MSVC
21+
#else
22+
# warning Unsupported compiler. Only Clang, GCC, and MSVC are supported.
23+
#endif
24+
25+
// Detect language feature support. See: https://en.cppreference.com/w/cpp/compiler_support
26+
#if defined(FLY_COMPILER_DISABLE_CONSTEVAL)
27+
# undef FLY_COMPILER_SUPPORTS_CONSTEVAL
28+
# define FLY_CONSTEVAL constexpr
29+
#elif defined(FLY_COMPILER_GCC)
30+
# define FLY_COMPILER_SUPPORTS_CONSTEVAL
31+
# define FLY_CONSTEVAL consteval
32+
#else
33+
# undef FLY_COMPILER_SUPPORTS_CONSTEVAL
34+
# define FLY_CONSTEVAL constexpr
35+
#endif
36+
1437
// Define macro to convert a macro parameter to a string.
1538
#define FLY_STRINGIZE(s) #s
1639

@@ -77,4 +100,60 @@ inline constexpr bool is_windows()
77100
#endif
78101
}
79102

103+
/**
104+
* Compile-time helper function to determine if the compiler is Clang.
105+
*
106+
* @return True if the compiler is Clang.
107+
*/
108+
inline constexpr bool is_clang()
109+
{
110+
#if defined(FLY_COMPILER_CLANG)
111+
return true;
112+
#else
113+
return false;
114+
#endif
115+
}
116+
117+
/**
118+
* Compile-time helper function to determine if the compiler is GCC.
119+
*
120+
* @return True if the compiler is GCC.
121+
*/
122+
inline constexpr bool is_gcc()
123+
{
124+
#if defined(FLY_COMPILER_GCC)
125+
return true;
126+
#else
127+
return false;
128+
#endif
129+
}
130+
131+
/**
132+
* Compile-time helper function to determine if the compiler is MSVC.
133+
*
134+
* @return True if the compiler is Clang.
135+
*/
136+
inline constexpr bool is_msvc()
137+
{
138+
#if defined(FLY_COMPILER_MSVC)
139+
return true;
140+
#else
141+
return false;
142+
#endif
143+
}
144+
145+
/**
146+
* Compile-time helper function to determine if immediate functions (consteval) are supported.
147+
*
148+
* @return True if the compiler supports consteval.
149+
*/
150+
inline constexpr bool supports_consteval()
151+
{
152+
#if defined(FLY_COMPILER_SUPPORTS_CONSTEVAL)
153+
return true;
154+
#else
155+
return false;
156+
#endif
157+
}
158+
80159
} // namespace fly

test/fly.cpp

+34-5
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@ CATCH_TEST_CASE("Fly", "[fly]")
88
{
99
CATCH_SECTION("Stringize helper")
1010
{
11-
const std::string s1 = FLY_STRINGIZE();
12-
CATCH_CHECK(s1.empty());
13-
14-
const std::string s2 = FLY_STRINGIZE(libfly);
15-
CATCH_CHECK(s2 == "libfly");
11+
const std::string s = FLY_STRINGIZE(libfly);
12+
CATCH_CHECK(s == "libfly");
1613
}
1714

1815
CATCH_SECTION("Operating system-dependent headers")
@@ -50,6 +47,38 @@ CATCH_TEST_CASE("Fly", "[fly]")
5047
CATCH_CHECK(fly::is_windows());
5148
#else
5249
static_assert(false, "Unknown operating system");
50+
#endif
51+
}
52+
53+
CATCH_SECTION("Compiler helpers")
54+
{
55+
#if defined(__clang__)
56+
CATCH_CHECK(fly::is_clang());
57+
CATCH_CHECK_FALSE(fly::is_gcc());
58+
CATCH_CHECK_FALSE(fly::is_msvc());
59+
#elif defined(__GNUC__)
60+
CATCH_CHECK_FALSE(fly::is_clang());
61+
CATCH_CHECK(fly::is_gcc());
62+
CATCH_CHECK_FALSE(fly::is_msvc());
63+
#elif defined(_MSC_VER)
64+
CATCH_CHECK_FALSE(fly::is_clang());
65+
CATCH_CHECK_FALSE(fly::is_gcc());
66+
CATCH_CHECK(fly::is_msvc());
67+
#else
68+
static_assert(false, "Unknown compiler");
69+
#endif
70+
}
71+
72+
CATCH_SECTION("Language feature helpers")
73+
{
74+
#if defined(__clang__)
75+
CATCH_CHECK_FALSE(fly::supports_consteval());
76+
#elif defined(__GNUC__)
77+
CATCH_CHECK(fly::supports_consteval());
78+
#elif defined(_MSC_VER)
79+
CATCH_CHECK_FALSE(fly::supports_consteval());
80+
#else
81+
static_assert(false, "Unknown compiler");
5382
#endif
5483
}
5584
}

0 commit comments

Comments
 (0)