Skip to content

Commit e741b71

Browse files
authored
[CMake] Disable -Wdangling-reference warnings on GCC (#157541)
This gets rid of 99 warnings which mostly seem like false positives (in a build of LLVM+Clang+LLDB, with GCC 13). The warnings look e.g. like this: ../lib/ObjCopy/COFF/COFFObjcopy.cpp: In function ‘uint64_t llvm::objcopy::coff::getNextRVA(const Object&)’: ../lib/ObjCopy/COFF/COFFObjcopy.cpp:38:18: warning: possibly dangling reference to a temporary [-Wdangling-reference] 38 | const Section &Last = Obj.getSections().back(); | ^~~~ ../lib/ObjCopy/COFF/COFFObjcopy.cpp:38:47: note: the temporary was destroyed at the end of the full expression ‘(& Obj)->llvm::objcopy::coff::Object::getSections().llvm::ArrayRef<llvm::objcopy::coff::Section>::back()’ 38 | const Section &Last = Obj.getSections().back(); | ~~~~~~~~~~~~~~~~~~~~~~^~ In this example, the `Object::getSections()` method returns an `ArrayRef<Section>` from a `std::vector<Section>`. We invoke `back()` on that, and store a reference in a local variable. Even though the temporary `ArrayRef<Section>` has been destroyed, the reference points to something which still is alive in the `std::vector<Section>`. See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109642 and https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110358 for some preexisting discussion on this warning and how it can be silenced selectively since GCC 14.
1 parent 68830c7 commit e741b71

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

llvm/cmake/modules/HandleLLVMOptions.cmake

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,14 @@ if (LLVM_ENABLE_WARNINGS AND (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL))
889889
endif()
890890
endif()
891891

892+
# Disable -Wdangling-reference, a C++-only warning from GCC 13 that seems
893+
# to produce a large number of false positives.
894+
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
895+
if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 13.1)
896+
append("-Wno-dangling-reference" CMAKE_CXX_FLAGS)
897+
endif()
898+
endif()
899+
892900
# Disable -Wredundant-move and -Wpessimizing-move on GCC>=9. GCC wants to
893901
# remove std::move in code like
894902
# "A foo(ConvertibleToA a) { return std::move(a); }",

0 commit comments

Comments
 (0)