Skip to content

Commit d72e711

Browse files
authored
[NFC] [CMake] Add -Wno-dangling-else for GCC built unittests (#112817)
This is one of the many PRs to fix errors with LLVM_ENABLE_WERROR=on. Built by GCC 11. Fix warnings: llvm/unittests/ProfileData/CoverageMappingTest.cpp: In member function ‘virtual void {anonymous}::CoverageMappingTest_TVIdxBuilder_Test::TestBody()’: llvm/unittests/ProfileData/CoverageMappingTest.cpp:1116:10: error: suggest explicit braces to avoid ambiguous ‘else’ [-Werror=dangling-else] 1116 | if (Node.NextIDs[C] < 0) The problem here is because these macros, eg: EXPECT_TRUE are expanded to a single line multi-statement code with if/else, which is indeed ambiguous after pre-processing. a simple example would be like: https://godbolt.org/z/4zjn56qrP if(x) switch (0) case 0: default: if (...) ; else return;; Given that omit braces in such cases is part of LLVM's style guide, and it is hard to add braces in gtest just for GCC's warning, add -Wno-dangling-else for GCC instead.
1 parent d104b8e commit d72e711

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

llvm/unittests/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ function(add_llvm_target_unittest test_dir_name)
1414
add_llvm_unittest(${test_dir_name} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARGN})
1515
endfunction()
1616

17+
# gtest macros like EXPECT_TRUE are expanded to a single line
18+
# multi-statement code with if/else. eg:
19+
# if (...)
20+
# EXPECT_TURE(...)
21+
# will be expanded into something like:
22+
# if(...)
23+
# switch (0) case 0: default: if (...) ; else return;;
24+
# GCC may emit false positive dangling-else warnings for such code.
25+
# However, such warnings are actually against LLVM's style guide.
26+
# disable the warning for GCC so that one can enbable Werror.
27+
if (CMAKE_COMPILER_IS_GNUCXX)
28+
list(APPEND LLVM_COMPILE_FLAGS "-Wno-dangling-else")
29+
endif ()
30+
1731
add_subdirectory(ADT)
1832
add_subdirectory(Analysis)
1933
add_subdirectory(AsmParser)

0 commit comments

Comments
 (0)