From 7d03534a6d5f1d21781bdd49dded99d796722329 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Storsj=C3=B6?= Date: Mon, 8 Sep 2025 23:03:05 +0300 Subject: [PATCH] [CMake] Disable -Wdangling-reference warnings on GCC MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This gets rid of 99 warnings which mostly seem like false positives (in a build of LLVM+Clang+LLDB). 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::back()’ 38 | const Section &Last = Obj.getSections().back(); | ~~~~~~~~~~~~~~~~~~~~~~^~ In this example, the `Object::getSections()` method returns an `ArrayRef
` from a `std::vector
`. We invoke `back()` on that, and store a reference in a local variable. Even though the temporary `ArrayRef
` has been destroyed, the reference points to something which still is alive in the `std::vector
`. --- llvm/cmake/modules/HandleLLVMOptions.cmake | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/llvm/cmake/modules/HandleLLVMOptions.cmake b/llvm/cmake/modules/HandleLLVMOptions.cmake index a67543cdb668f..5ab34bc3b9c74 100644 --- a/llvm/cmake/modules/HandleLLVMOptions.cmake +++ b/llvm/cmake/modules/HandleLLVMOptions.cmake @@ -889,6 +889,14 @@ if (LLVM_ENABLE_WARNINGS AND (LLVM_COMPILER_IS_GCC_COMPATIBLE OR CLANG_CL)) endif() endif() + # Disable -Wdangling-reference, a C++-only warning from GCC 13 that seems + # to produce a large number of false positives. + if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 13.1) + append("-Wno-dangling-reference" CMAKE_CXX_FLAGS) + endif() + endif() + # Disable -Wredundant-move and -Wpessimizing-move on GCC>=9. GCC wants to # remove std::move in code like # "A foo(ConvertibleToA a) { return std::move(a); }",