Skip to content

Conversation

mstorsjo
Copy link
Member

@mstorsjo mstorsjo commented Sep 8, 2025

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>.

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<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>`.
@llvmbot llvmbot added the cmake Build system in general and CMake in particular label Sep 8, 2025
@firewave
Copy link

firewave commented Sep 8, 2025

I assume this takes care of #125544, #125542, #125538.

Should probably get some upstream bug report as well otherwise it will never improve.

@mstorsjo
Copy link
Member Author

mstorsjo commented Sep 8, 2025

I assume this takes care of #125544, #125542, #125538.

Yes, it seems so.

Should probably get some upstream bug report as well otherwise it will never improve.

Yes, ideally... Do you feel like writing a report? I see that you had some analysis in #125538 already. The example I outline in the commit message here also is quite clear, both in why it isn't an issue, but also why it's understandably hard for the compiler to know whether it is an issue or not.

@firewave
Copy link

firewave commented Sep 8, 2025

Yes, ideally... Do you feel like writing a report?

TBH, no. I would need to reproduce them with the latest compiler and currently I have no LLVM build set up to begin with.

Copy link
Member

@MaskRay MaskRay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having upstream GCC bug filed would be nice if someone is willing to do the work, and then add a comment to this PR or the linked issues.

@mstorsjo
Copy link
Member Author

mstorsjo commented Sep 9, 2025

Re upstream bug reports, I found https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109642 and https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110358 which cover the majority of our cases. The gist of it:

std::span, std::reference_wrapper and std::ref_view are all special cased for the warning.

Would you consider perhaps reducing the scope of the warning in order to reduce the false positive rate, alternatively move it out of Wall?

...I think I'll have to move it to -Wextra (for GCC 13.2).

c++: avoid -Wdangling-reference for std::span-like classes

Since GCC 14, this detects any class which looks like std::span, which should match ArrayRef, and avoids the warnings for them.

If compiling with GCC 15, I do see some of these warnings relating to ArrayRef to be silenced - but there's even more of them for other cases; with GCC 15, I see up to 274 of these warnings, while I "only" had 99 with GCC 13.

c++: implement [[gnu::no_dangling]]

Since GCC 14, it's also possible to tag individual classes/functions with this attribute (classes like ArrayRef and similar), to excempt them from this warning.

I did find one odd case which did seem like a separate true false positive though, which I filed at https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121871.

@mstorsjo mstorsjo merged commit e741b71 into llvm:main Sep 9, 2025
10 of 11 checks passed
@mstorsjo mstorsjo deleted the silence-gcc-warning branch September 9, 2025 09:26
@llvm-ci
Copy link
Collaborator

llvm-ci commented Sep 9, 2025

LLVM Buildbot has detected a new failure on builder cross-project-tests-sie-ubuntu-dwarf5 running on doug-worker-1b while building llvm at step 6 "test-build-unified-tree-check-cross-project".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/163/builds/26124

Here is the relevant piece of the build log for the reference
Step 6 (test-build-unified-tree-check-cross-project) failure: test (failure)
******************** TEST 'cross-project-tests :: debuginfo-tests/dexter-tests/optnone-vectors-and-functions.cpp' FAILED ********************
Exit Code: 2

Command Output (stdout):
--
optnone-vectors-and-functions.cpp: (1.0000)

## BEGIN ##
.   [0, "int vector[4] test1<int vector[4]>(int vector[4], int vector[4])", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-vectors-and-functions.cpp", 30, 11, "StopReason.BREAKPOINT", "StepKind.FUNC", []]
.   [1, "int vector[4] test1<int vector[4]>(int vector[4], int vector[4])", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-vectors-and-functions.cpp", 31, 12, "StopReason.BREAKPOINT", "StepKind.VERTICAL_FORWARD", []]
.   [2, "int vector[4] test1<int vector[4]>(int vector[4], int vector[4])", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-vectors-and-functions.cpp", 32, 10, "StopReason.BREAKPOINT", "StepKind.VERTICAL_FORWARD", []]
.   [3, "int vector[4] test2<int vector[4]>(int vector[4], int vector[4])", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-vectors-and-functions.cpp", 59, 7, "StopReason.BREAKPOINT", "StepKind.VERTICAL_FORWARD", []]
.   [4, "int vector[4] test2<int vector[4]>(int vector[4], int vector[4])", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-vectors-and-functions.cpp", 61, 9, "StopReason.BREAKPOINT", "StepKind.VERTICAL_FORWARD", []]
.   [5, "int vector[4] test2<int vector[4]>(int vector[4], int vector[4])", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-vectors-and-functions.cpp", 62, 12, "StopReason.BREAKPOINT", "StepKind.VERTICAL_FORWARD", []]
.   [6, "int vector[4] test2<int vector[4]>(int vector[4], int vector[4])", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-vectors-and-functions.cpp", 65, 14, "StopReason.BREAKPOINT", "StepKind.VERTICAL_FORWARD", []]
.   [7, "int vector[4] test2<int vector[4]>(int vector[4], int vector[4])", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-vectors-and-functions.cpp", 66, 10, "StopReason.BREAKPOINT", "StepKind.VERTICAL_FORWARD", []]
.   [8, "int vector[4] test3<int vector[4]>(int vector[4])", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-vectors-and-functions.cpp", 93, 23, "StopReason.BREAKPOINT", "StepKind.VERTICAL_FORWARD", []]
.   [9, "int vector[4] test3<int vector[4]>(int vector[4])", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-vectors-and-functions.cpp", 94, 10, "StopReason.BREAKPOINT", "StepKind.VERTICAL_FORWARD", []]
.   [10, "int vector[4] test4<int vector[4]>(int vector[4], int vector[4])", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-vectors-and-functions.cpp", 110, 13, "StopReason.BREAKPOINT", "StepKind.VERTICAL_FORWARD", []]
.   [11, "int vector[4] test4<int vector[4]>(int vector[4], int vector[4])", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-vectors-and-functions.cpp", 110, 13, "StopReason.BREAKPOINT", "StepKind.SAME", []]
.   [12, "int vector[4] test4<int vector[4]>(int vector[4], int vector[4])", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-vectors-and-functions.cpp", 110, 13, "StopReason.BREAKPOINT", "StepKind.SAME", []]
.   [13, "int vector[4] test4<int vector[4]>(int vector[4], int vector[4])", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-vectors-and-functions.cpp", 110, 13, "StopReason.BREAKPOINT", "StepKind.SAME", []]
.   [14, "int vector[4] test4<int vector[4]>(int vector[4], int vector[4])", "/home/buildbot/buildbot-root/cross-project-tests-sie-ubuntu-dwarf5/llvm-project/cross-project-tests/debuginfo-tests/dexter-tests/optnone-vectors-and-functions.cpp", 111, 10, "StopReason.BREAKPOINT", "StepKind.VERTICAL_FORWARD", []]
## END (15 steps) ##


  optnone-vectors-and-functions.cpp:110 [i] ExpectValue [0/21]
    expected encountered watches:
      0
      1
      2
      3

  optnone-vectors-and-functions.cpp:110-111 [x[0]] ExpectValue [0/14]
    expected encountered watches:
      1
      8

  optnone-vectors-and-functions.cpp:110-111 [x[1]] ExpectValue [0/14]
    expected encountered watches:
      2
      9

  optnone-vectors-and-functions.cpp:110-111 [x[2]] ExpectValue [0/14]
    expected encountered watches:
      10
      3

  optnone-vectors-and-functions.cpp:110-111 [x[3]] ExpectValue [0/14]
    expected encountered watches:
...

@llvm-ci
Copy link
Collaborator

llvm-ci commented Sep 9, 2025

LLVM Buildbot has detected a new failure on builder flang-x86_64-windows running on minipc-ryzen-win while building llvm at step 9 "install-build-unified-treeall".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/166/builds/2237

Here is the relevant piece of the build log for the reference
Step 9 (install-build-unified-treeall) failure: build (failure)
...
70.711 [0/5/368] Building CXX object compiler-rt/lib/sanitizer_common/CMakeFiles/RTSanitizerCommonSymbolizerNoHooks.x86_64.dir/sanitizer_unwind_win.cpp.obj
70.724 [0/4/369] Linking CXX static library C:\buildbot\flang-x86_64-windows\build\lib\clang\22\lib\windows\orc_rt-x86_64.lib
70.724 [0/3/370] Linking CXX static library compiler-rt\lib\fuzzer\tests\RTFuzzerTest.x86_64.lib
70.727 [0/2/371] Linking CXX static library compiler-rt\lib\orc\tests\RTOrc.test.x86_64.lib
70.779 [0/1/372] Linking CXX shared library C:\buildbot\flang-x86_64-windows\build\lib\clang\22\lib\windows\clang_rt.asan_dynamic-x86_64.dll
78.497 [3/4/90] Linking CXX executable bin\llvm-reduce.exe
78.704 [2/3/91] No install step for 'runtimes'
78.814 [1/3/93] Completed 'runtimes'
80.582 [1/2/94] Linking CXX shared library bin\LLVM-C.dll
82.885 [1/1/95] Linking CXX executable bin\llvm-split.exe
FAILED: [code=4294967295] bin/llvm-split.exe 
C:\Windows\system32\cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --msvc-ver=1944 --intdir=tools\llvm-split\CMakeFiles\llvm-split.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100261~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100261~1.0\x64\mt.exe --manifests  -- C:\PROGRA~1\MICROS~2\2022\COMMUN~1\VC\Tools\MSVC\1444~1.352\bin\Hostx64\x64\link.exe /nologo tools\llvm-split\CMakeFiles\llvm-split.dir\llvm-split.cpp.obj tools\llvm-split\CMakeFiles\llvm-split.dir\__\__\resources\windows_version_resource.rc.res  /out:bin\llvm-split.exe /implib:lib\llvm-split.lib /pdb:bin\llvm-split.pdb /version:0.0 /machine:x64 /STACK:10000000 /INCREMENTAL:NO /subsystem:console  lib\LLVMX86AsmParser.lib  lib\LLVMX86CodeGen.lib  lib\LLVMX86Desc.lib  lib\LLVMX86Info.lib  lib\LLVMTransformUtils.lib  lib\LLVMBitWriter.lib  lib\LLVMCodeGen.lib  lib\LLVMCore.lib  lib\LLVMIRReader.lib  lib\LLVMMC.lib  lib\LLVMSupport.lib  lib\LLVMTarget.lib  lib\LLVMTargetParser.lib  lib\LLVMipo.lib  lib\LLVMMCDisassembler.lib  lib\LLVMAsmPrinter.lib  lib\LLVMCFGuard.lib  lib\LLVMGlobalISel.lib  lib\LLVMIRPrinter.lib  lib\LLVMSelectionDAG.lib  lib\LLVMObjCARCOpts.lib  lib\LLVMCGData.lib  lib\LLVMCodeGenTypes.lib  lib\LLVMInstrumentation.lib  lib\LLVMFrontendOpenMP.lib  lib\LLVMScalarOpts.lib  lib\LLVMAggressiveInstCombine.lib  lib\LLVMFrontendOffloading.lib  lib\LLVMObjectYAML.lib  lib\LLVMFrontendAtomic.lib  lib\LLVMFrontendDirective.lib  lib\LLVMInstCombine.lib  lib\LLVMLinker.lib  lib\LLVMVectorize.lib  lib\LLVMSandboxIR.lib  lib\LLVMAnalysis.lib  lib\LLVMProfileData.lib  lib\LLVMSymbolize.lib  lib\LLVMDebugInfoGSYM.lib  lib\LLVMDebugInfoDWARF.lib  lib\LLVMDebugInfoDWARFLowLevel.lib  lib\LLVMDebugInfoPDB.lib  lib\LLVMDebugInfoCodeView.lib  "C:\Program Files\Microsoft Visual Studio\2022\Community\DIA SDK\lib\amd64\diaguids.lib"  lib\LLVMDebugInfoMSF.lib  lib\LLVMDebugInfoBTF.lib  lib\LLVMObject.lib  lib\LLVMAsmParser.lib  lib\LLVMMCParser.lib  lib\LLVMBitReader.lib  lib\LLVMTextAPI.lib  lib\LLVMFrontendHLSL.lib  lib\LLVMRemarks.lib  lib\LLVMBitstreamReader.lib  lib\LLVMBinaryFormat.lib  lib\LLVMDemangle.lib  psapi.lib  shell32.lib  ole32.lib  uuid.lib  advapi32.lib  ws2_32.lib  ntdll.lib  delayimp.lib  -delayload:shell32.dll  -delayload:ole32.dll  kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib && cd ."
LINK: command "C:\PROGRA~1\MICROS~2\2022\COMMUN~1\VC\Tools\MSVC\1444~1.352\bin\Hostx64\x64\link.exe /nologo tools\llvm-split\CMakeFiles\llvm-split.dir\llvm-split.cpp.obj tools\llvm-split\CMakeFiles\llvm-split.dir\__\__\resources\windows_version_resource.rc.res /out:bin\llvm-split.exe /implib:lib\llvm-split.lib /pdb:bin\llvm-split.pdb /version:0.0 /machine:x64 /STACK:10000000 /INCREMENTAL:NO /subsystem:console lib\LLVMX86AsmParser.lib lib\LLVMX86CodeGen.lib lib\LLVMX86Desc.lib lib\LLVMX86Info.lib lib\LLVMTransformUtils.lib lib\LLVMBitWriter.lib lib\LLVMCodeGen.lib lib\LLVMCore.lib lib\LLVMIRReader.lib lib\LLVMMC.lib lib\LLVMSupport.lib lib\LLVMTarget.lib lib\LLVMTargetParser.lib lib\LLVMipo.lib lib\LLVMMCDisassembler.lib lib\LLVMAsmPrinter.lib lib\LLVMCFGuard.lib lib\LLVMGlobalISel.lib lib\LLVMIRPrinter.lib lib\LLVMSelectionDAG.lib lib\LLVMObjCARCOpts.lib lib\LLVMCGData.lib lib\LLVMCodeGenTypes.lib lib\LLVMInstrumentation.lib lib\LLVMFrontendOpenMP.lib lib\LLVMScalarOpts.lib lib\LLVMAggressiveInstCombine.lib lib\LLVMFrontendOffloading.lib lib\LLVMObjectYAML.lib lib\LLVMFrontendAtomic.lib lib\LLVMFrontendDirective.lib lib\LLVMInstCombine.lib lib\LLVMLinker.lib lib\LLVMVectorize.lib lib\LLVMSandboxIR.lib lib\LLVMAnalysis.lib lib\LLVMProfileData.lib lib\LLVMSymbolize.lib lib\LLVMDebugInfoGSYM.lib lib\LLVMDebugInfoDWARF.lib lib\LLVMDebugInfoDWARFLowLevel.lib lib\LLVMDebugInfoPDB.lib lib\LLVMDebugInfoCodeView.lib C:\Program Files\Microsoft Visual Studio\2022\Community\DIA SDK\lib\amd64\diaguids.lib lib\LLVMDebugInfoMSF.lib lib\LLVMDebugInfoBTF.lib lib\LLVMObject.lib lib\LLVMAsmParser.lib lib\LLVMMCParser.lib lib\LLVMBitReader.lib lib\LLVMTextAPI.lib lib\LLVMFrontendHLSL.lib lib\LLVMRemarks.lib lib\LLVMBitstreamReader.lib lib\LLVMBinaryFormat.lib lib\LLVMDemangle.lib psapi.lib shell32.lib ole32.lib uuid.lib advapi32.lib ws2_32.lib ntdll.lib delayimp.lib -delayload:shell32.dll -delayload:ole32.dll kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST:EMBED,ID=1" failed (exit code 0) with the following output:

  Version 14.44.35213.0

  ExceptionCode            = C0000005
  ExceptionFlags           = 00000000
  ExceptionAddress         = 00007FF7480407CB (00007FF748010000) "C:\PROGRA~1\MICROS~2\2022\COMMUN~1\VC\Tools\MSVC\1444~1.352\bin\Hostx64\x64\link.exe"
  NumberParameters         = 00000002
  ExceptionInformation[ 0] = 0
  ExceptionInformation[ 1] = 7FF749E15524

CONTEXT:
  Rax    = 7FF749E154DC  R8     = 1C4
  Rbx    = 24B82C3C108  R9     = 3A64
  Rcx    = 0  R10    = AA64
  Rdx    = A64E  R11    = 100
  Rsp    = 71E47FD6B0  R12    = 1
  Rbp    = 71E47FD7B0  E13    = 24BE1F758F8
  Rsi    = 0  R14    = 7FF748265898
  Rdi    = 24BE0F04100  R15    = 24B8D786A01
  Rip    = 7FF7480407CB  EFlags = 10202
  SegCs  = 33  SegDs  = 2B
  SegSs  = 2B  SegEs  = 2B
  SegFs  = 53  SegGs  = 2B
  Dr0    = 0  Dr3    = 0
  Dr1    = 0  Dr6    = 0
  Dr2    = 0  Dr7    = 0

msvcprt.lib(MSVCP140.dll) : fatal error LNK1000: Internal error during IMAGE::Pass2
Access violation
ninja: build stopped: subcommand failed.
Cache directory:    C:\Users\buildbot-worker\AppData\Local\ccache
Config file:        C:\Users\buildbot-worker\AppData\Local\ccache\ccache.conf
System config file: C:\ProgramData\ccache\ccache.conf
Stats updated:      09/09/25 03:09:00
Local storage:
  Cache size (GB):    5.0 / 5.0 (99.97%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cmake Build system in general and CMake in particular
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants