[clang] [analyzer] Model GCC 'cleanup' attribute function calls (PR #221110)

Arseniy Zaostrovnykh via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 11 02:23:45 PDT 2026


================
@@ -149,3 +150,93 @@ int early_return_shape(void) {
     return 1;
   return 0;
 } // no leak on either path: the cleanup frees *p at the return.
+
+//===----------------------------------------------------------------------===//
+// Scope-exit shapes: goto, cleanup ordering and nesting.
+//===----------------------------------------------------------------------===//
+
+// The cleanup runs on every exit from the scope, including jumps.
+
+static void goto_cleanup(int *p) {
+  clang_analyzer_warnIfReached(); // expected-warning {{REACHABLE}}
+  (void)p;
+}
+
+void goto_out_of_block_scope(void) {
+  {
+    int x __attribute__((cleanup(goto_cleanup)));
+    x = 1;
+    goto out;
+  }
+out:;
+}
+
+static void goto_cleanup_at_function_scope(int *p) {
+  clang_analyzer_warnIfReached(); // expected-warning {{REACHABLE}}
+  (void)p;
+}
+
+void goto_at_function_scope(void) {
+  int x __attribute__((cleanup(goto_cleanup_at_function_scope)));
+  x = 1;
+  goto out;
+out:;
+}
----------------
necto wrote:

Can you use a similar trick with `order_probe_global` to show whether the cleanup runs before the `goto` jumps, or after?

https://github.com/llvm/llvm-project/pull/221110


More information about the cfe-commits mailing list