Skip to content

Commit 93b1256

Browse files
committed
[compiler-rt][test] Heed COMPILER_RT_DEBUG when compiling unittests
When trying to debug some `compiler-rt` unittests, I initially had a hard time because - even in a `Debug` build one needs to set `COMPILER_RT_DEBUG` to get debugging info for some of the code and - even so the unittests used a hardcoded `-O2` which often makes debugging impossible. This patch addresses this by instead using `-O0` if `COMPILER_RT_DEBUG`. Two tests in `sanitizer_type_traits_test.cpp` need to be disabled since they have undefined references to `__sanitizer::integral_constant<bool, true>::value`. Tested on `sparcv9-sun-solaris2.11`, `amd64-pc-solaris2.11`, and `x86_64-pc-linux-gnu`. Differential Revision: https://reviews.llvm.org/D91620
1 parent 27c634a commit 93b1256

File tree

8 files changed

+17
-36
lines changed

8 files changed

+17
-36
lines changed

compiler-rt/CMakeLists.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,11 @@ if (NOT MSVC)
406406

407407
# Build with optimization, unless we're in debug mode.
408408
if(COMPILER_RT_DEBUG)
409-
list(APPEND SANITIZER_COMMON_CFLAGS -O1)
409+
list(APPEND SANITIZER_COMMON_CFLAGS -O0)
410+
list(APPEND COMPILER_RT_TEST_COMPILER_CFLAGS -O0)
410411
else()
411412
list(APPEND SANITIZER_COMMON_CFLAGS -O3)
413+
list(APPEND COMPILER_RT_TEST_COMPILER_CFLAGS -O3)
412414
endif()
413415
endif()
414416

@@ -443,6 +445,7 @@ if(MSVC)
443445
string(REGEX REPLACE "(^| )/Z[i7I]($| )" " /Z7 "
444446
"${var_to_update}" "${${var_to_update}}")
445447
endforeach()
448+
list(APPEND COMPILER_RT_UNITTEST_CFLAGS -gcodeview)
446449
elseif(APPLE)
447450
# On Apple platforms use full debug info (i.e. not `-gline-tables-only`)
448451
# for all build types so that the runtime can be debugged.
@@ -452,8 +455,13 @@ elseif(APPLE)
452455
list(APPEND SANITIZER_COMMON_CFLAGS -g)
453456
elseif(COMPILER_RT_HAS_GLINE_TABLES_ONLY_FLAG AND NOT COMPILER_RT_DEBUG)
454457
list(APPEND SANITIZER_COMMON_CFLAGS -gline-tables-only)
458+
# -gline-tables-only must be enough for these tests, so use it if possible.
459+
list(APPEND COMPILER_RT_TEST_COMPILER_CFLAGS -gline-tables-only)
460+
list(APPEND COMPILER_RT_UNITTEST_CFLAGS -gline-tables-only)
455461
elseif(COMPILER_RT_HAS_G_FLAG)
456462
list(APPEND SANITIZER_COMMON_CFLAGS -g)
463+
list(APPEND COMPILER_RT_TEST_COMPILER_CFLAGS -g)
464+
list(APPEND COMPILER_RT_UNITTEST_CFLAGS -g)
457465
endif()
458466

459467
if(LLVM_ENABLE_MODULES)

compiler-rt/lib/asan/tests/CMakeLists.txt

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ set(ASAN_UNITTEST_COMMON_CFLAGS
2929
-I${COMPILER_RT_SOURCE_DIR}/lib/asan
3030
-I${COMPILER_RT_SOURCE_DIR}/lib/sanitizer_common/tests
3131
-fno-rtti
32-
-O2
3332
-Wno-format
3433
-Werror=sign-compare)
3534
append_list_if(COMPILER_RT_HAS_WVARIADIC_MACROS_FLAG -Wno-variadic-macros ASAN_UNITTEST_COMMON_CFLAGS)
@@ -40,16 +39,6 @@ set(ASAN_UNITTEST_COMMON_LINK_FLAGS
4039
${COMPILER_RT_UNITTEST_LINK_FLAGS}
4140
${COMPILER_RT_UNWINDER_LINK_LIBS}
4241
${SANITIZER_TEST_CXX_LIBRARIES})
43-
44-
# -gline-tables-only must be enough for ASan, so use it if possible.
45-
if(COMPILER_RT_TEST_COMPILER_ID MATCHES "Clang")
46-
list(APPEND ASAN_UNITTEST_COMMON_CFLAGS -gline-tables-only)
47-
else()
48-
list(APPEND ASAN_UNITTEST_COMMON_CFLAGS -g)
49-
endif()
50-
if(MSVC)
51-
list(APPEND ASAN_UNITTEST_COMMON_CFLAGS -gcodeview)
52-
endif()
5342
list(APPEND ASAN_UNITTEST_COMMON_LINK_FLAGS -g)
5443

5544
# Use -D instead of definitions to please custom compile command.

compiler-rt/lib/fuzzer/tests/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ set(LIBFUZZER_UNITTEST_CFLAGS
44
${COMPILER_RT_UNITTEST_CFLAGS}
55
${COMPILER_RT_GTEST_CFLAGS}
66
-I${COMPILER_RT_SOURCE_DIR}/lib/fuzzer
7-
-fno-rtti
8-
-O2)
7+
-fno-rtti)
98

109
if (APPLE)
1110
set(FUZZER_SUPPORTED_OS osx)

compiler-rt/lib/gwp_asan/tests/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ set(GWP_ASAN_UNITTEST_CFLAGS
66
${SANITIZER_TEST_CXX_CFLAGS}
77
-std=c++17
88
-I${COMPILER_RT_SOURCE_DIR}/lib/
9-
-O2
109
-g
1110
-fno-omit-frame-pointer)
1211

compiler-rt/lib/interception/tests/CMakeLists.txt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,14 @@ set(INTERCEPTION_TEST_CFLAGS_COMMON
1818
-I${COMPILER_RT_SOURCE_DIR}/lib
1919
-I${COMPILER_RT_SOURCE_DIR}/lib/interception
2020
-fno-rtti
21-
-O2
2221
-Werror=sign-compare)
2322

2423
set(INTERCEPTION_TEST_LINK_FLAGS_COMMON
2524
${COMPILER_RT_UNITTEST_LINK_FLAGS}
2625
${COMPILER_RT_UNWINDER_LINK_LIBS}
2726
${SANITIZER_TEST_CXX_LIBRARIES})
2827

29-
# -gline-tables-only must be enough for these tests, so use it if possible.
30-
if(COMPILER_RT_TEST_COMPILER_ID MATCHES "Clang")
31-
list(APPEND INTERCEPTION_TEST_CFLAGS_COMMON -gline-tables-only)
32-
else()
33-
list(APPEND INTERCEPTION_TEST_CFLAGS_COMMON -g)
34-
endif()
3528
if(MSVC)
36-
list(APPEND INTERCEPTION_TEST_CFLAGS_COMMON -gcodeview)
3729
list(APPEND INTERCEPTION_TEST_LINK_FLAGS_COMMON
3830
-Wl,-largeaddressaware
3931
-Wl,-nodefaultlib:libcmt,-defaultlib:msvcrt,-defaultlib:oldnames

compiler-rt/lib/msan/tests/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ set(MSAN_UNITTEST_COMMON_CFLAGS
3333
-I${COMPILER_RT_SOURCE_DIR}/lib
3434
-I${COMPILER_RT_SOURCE_DIR}/lib/msan
3535
-g
36-
-O2
3736
-fno-omit-frame-pointer
3837
-mno-omit-leaf-frame-pointer
3938
-Wno-deprecated-declarations

compiler-rt/lib/sanitizer_common/tests/CMakeLists.txt

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ set(SANITIZER_TEST_CFLAGS_COMMON
6767
-I${COMPILER_RT_SOURCE_DIR}/lib
6868
-I${COMPILER_RT_SOURCE_DIR}/lib/sanitizer_common
6969
-fno-rtti
70-
-O2
7170
-Werror=sign-compare
7271
-Wno-gnu-zero-variadic-macro-arguments
7372
)
@@ -76,16 +75,6 @@ set(SANITIZER_TEST_LINK_FLAGS_COMMON
7675
${COMPILER_RT_UNITTEST_LINK_FLAGS}
7776
${COMPILER_RT_UNWINDER_LINK_LIBS}
7877
${SANITIZER_TEST_CXX_LIBRARIES})
79-
80-
# -gline-tables-only must be enough for these tests, so use it if possible.
81-
if(COMPILER_RT_TEST_COMPILER_ID MATCHES "Clang")
82-
list(APPEND SANITIZER_TEST_CFLAGS_COMMON -gline-tables-only)
83-
else()
84-
list(APPEND SANITIZER_TEST_CFLAGS_COMMON -g)
85-
endif()
86-
if(MSVC)
87-
list(APPEND SANITIZER_TEST_CFLAGS_COMMON -gcodeview)
88-
endif()
8978
list(APPEND SANITIZER_TEST_LINK_FLAGS_COMMON -g)
9079

9180
if(NOT MSVC)

compiler-rt/lib/sanitizer_common/tests/sanitizer_type_traits_test.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,21 @@ struct TestStruct {
6969
};
7070

7171
TEST(SanitizerCommon, IsTriviallyDestructible) {
72+
// Causes undefined references to __sanitizer::integral_constant<bool,
73+
// true>::value.
74+
#if !SANITIZER_DEBUG
7275
ASSERT_TRUE((is_trivially_destructible<int>::value));
7376
ASSERT_TRUE((is_trivially_destructible<TestStruct>::value));
77+
#endif
7478
ASSERT_FALSE((is_trivially_destructible<std::vector<int>>::value));
7579
}
7680

7781
TEST(SanitizerCommon, IsTriviallyCopyable) {
82+
#if !SANITIZER_DEBUG
7883
ASSERT_TRUE((is_trivially_copyable<int>::value));
7984
ASSERT_TRUE((is_trivially_copyable<TestStruct>::value));
85+
#endif
8086
ASSERT_FALSE((is_trivially_copyable<std::vector<int>>::value));
8187
}
8288

83-
} // namespace __sanitizer
89+
} // namespace __sanitizer

0 commit comments

Comments
 (0)