[llvm] Suppress GCC dangling-pointer false positive for RAII listener pattern (PR #180875)

Eric Christopher via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 10 18:57:26 PST 2026


https://github.com/echristo created https://github.com/llvm/llvm-project/pull/180875

### Summary

Suppress GCC `-Wdangling-pointer` false positive for RAII listener pattern.

#### Details

GCC's `-Wdangling-pointer` incorrectly warns about storing 'this' in a constructor-registered listener. This is a valid RAII pattern where the destructor removes the pointer before scope exit.

### Tested

- Built with ToT clang and GCC 13.3.0 on Linux x86_64.
- All existing tests pass

>From abbacde52d18290ad2e314054961e1330572bad6 Mon Sep 17 00:00:00 2001
From: Eric Christopher <echristo at gmail.com>
Date: Fri, 6 Feb 2026 01:17:46 -0800
Subject: [PATCH] ### Summary

Suppress GCC `-Wdangling-pointer` false positive for RAII listener pattern.

#### Details

GCC's `-Wdangling-pointer` incorrectly warns about storing 'this' in a constructor-registered listener. This is a valid RAII pattern where the destructor removes the pointer before scope exit.

### Tested

- Built with ToT clang and GCC 13.3.0 on Linux x86_64.
- All existing tests pass
---
 llvm/include/llvm/CodeGen/SelectionDAG.h | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/llvm/include/llvm/CodeGen/SelectionDAG.h b/llvm/include/llvm/CodeGen/SelectionDAG.h
index b5458bf7180ca..28661946d2159 100644
--- a/llvm/include/llvm/CodeGen/SelectionDAG.h
+++ b/llvm/include/llvm/CodeGen/SelectionDAG.h
@@ -324,7 +324,16 @@ class SelectionDAG {
 
     explicit DAGUpdateListener(SelectionDAG &D)
       : Next(D.UpdateListeners), DAG(D) {
+      // GCC's -Wdangling-pointer warns about storing 'this' from a local.
+      // This is a false positive for the RAII listener pattern.
+#if defined(__GNUC__) && !defined(__clang__)
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wdangling-pointer"
+#endif
       DAG.UpdateListeners = this;
+#if defined(__GNUC__) && !defined(__clang__)
+#pragma GCC diagnostic pop
+#endif
     }
 
     virtual ~DAGUpdateListener() {



More information about the llvm-commits mailing list