[llvm] e741b71 - [CMake] Disable -Wdangling-reference warnings on GCC (#157541)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 9 02:26:58 PDT 2025
Author: Martin Storsjö
Date: 2025-09-09T12:26:54+03:00
New Revision: e741b719281e355d5098937d7a78f9078d361d61
URL: https://github.com/llvm/llvm-project/commit/e741b719281e355d5098937d7a78f9078d361d61
DIFF: https://github.com/llvm/llvm-project/commit/e741b719281e355d5098937d7a78f9078d361d61.diff
LOG: [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.
Added:
Modified:
llvm/cmake/modules/HandleLLVMOptions.cmake
Removed:
################################################################################
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); }",
More information about the llvm-commits
mailing list