[llvm] d72e711 - [NFC] [CMake] Add -Wno-dangling-else for GCC built unittests (#112817)

via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 26 14:40:14 PDT 2024


Author: Jinsong Ji
Date: 2024-10-26T17:40:11-04:00
New Revision: d72e711e864dad7e3a434d66f3febad2b1596335

URL: https://github.com/llvm/llvm-project/commit/d72e711e864dad7e3a434d66f3febad2b1596335
DIFF: https://github.com/llvm/llvm-project/commit/d72e711e864dad7e3a434d66f3febad2b1596335.diff

LOG: [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.

Added: 
    

Modified: 
    llvm/unittests/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/llvm/unittests/CMakeLists.txt b/llvm/unittests/CMakeLists.txt
index 911ede701982f6..8892f3e75729ab 100644
--- a/llvm/unittests/CMakeLists.txt
+++ b/llvm/unittests/CMakeLists.txt
@@ -14,6 +14,20 @@ function(add_llvm_target_unittest test_dir_name)
   add_llvm_unittest(${test_dir_name} DISABLE_LLVM_LINK_LLVM_DYLIB ${ARGN})
 endfunction()
 
+# gtest macros like EXPECT_TRUE are expanded to a single line
+# multi-statement code with if/else. eg:
+#   if (...)
+#     EXPECT_TURE(...)
+# will be expanded into something like:
+#   if(...)
+#     switch (0) case 0: default: if (...) ; else return;;
+# GCC may emit false positive dangling-else warnings for such code.
+# However, such warnings are actually against LLVM's style guide.
+# disable the warning for GCC so that one can enbable Werror.
+if (CMAKE_COMPILER_IS_GNUCXX)
+  list(APPEND LLVM_COMPILE_FLAGS "-Wno-dangling-else")
+endif ()
+
 add_subdirectory(ADT)
 add_subdirectory(Analysis)
 add_subdirectory(AsmParser)


        


More information about the llvm-commits mailing list