[PATCH] D157385: [clang][CFG] Cleanup functions

Balázs Benics via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 1 06:16:45 PDT 2023


steakhal added a comment.

When I added `-analyzer-config cfg-lifetime=true` to `clang/test/Analysis/scopes-cfg-output.cpp`, suddenly duplicated lifetime ends entries appeared where we have `CleanupFunctions`.
My output is:

  void test_cleanup_functions()
   [B2 (ENTRY)]
     Succs (1): B1
  
   [B1]
     1: CFGScopeBegin(i)
     2: int i __attribute__((cleanup(cleanup_int)));
     3: CleanupFunction (cleanup_int)
     4: [B1.2] (Lifetime ends)
     5: [B1.2] (Lifetime ends)
     6: CFGScopeEnd(i)
     Preds (1): B2
     Succs (1): B0
  
   [B0 (EXIT)]
     Preds (1): B1
  
  void test_cleanup_functions2(int m)
   [B4 (ENTRY)]
     Succs (1): B3
  
   [B1]
     1: 10
     2: i
     3: [B1.2] = [B1.1]
     4: return;
     5: CleanupFunction (cleanup_int)
     6: [B3.2] (Lifetime ends)
     7: [B3.2] (Lifetime ends)
     8: CFGScopeEnd(i)
     Preds (1): B3
     Succs (1): B0
  
   [B2]
     1: return;
     2: CleanupFunction (cleanup_int)
     3: [B3.2] (Lifetime ends)
     4: [B3.2] (Lifetime ends)
     5: CFGScopeEnd(i)
     Preds (1): B3
     Succs (1): B0
  
   [B3]
     1: CFGScopeBegin(i)
     2: int i __attribute__((cleanup(cleanup_int)));
     3: m
     4: [B3.3] (ImplicitCastExpr, LValueToRValue, int)
     5: 1
     6: [B3.4] == [B3.5]
     T: if [B3.6]
     Preds (1): B4
     Succs (2): B2 B1
  
   [B0 (EXIT)]
     Preds (2): B1 B2
  
  void test()
   [B2 (ENTRY)]
     Succs (1): B1
  
   [B1]
     1: CFGScopeBegin(f)
     2:  (CXXConstructExpr, [B1.3], F)
     3: F f __attribute__((cleanup(cleanup_F)));
     4: CleanupFunction (cleanup_F)
     5: [B1.3].~F() (Implicit destructor)
     6: [B1.3] (Lifetime ends)
     7: CFGScopeEnd(f)
     Preds (1): B2
     Succs (1): B0
  
   [B0 (EXIT)]
     Preds (1): B1

Notice the `[B3.2] (Lifetime ends)` lines for example.

The order in which the Lifetime, Scope and Cleanup elements appear looks correct; my only concern is the duplicate Lifetime ends marker.

About the `noreturn` cleanup function, well, GCC says: `It is undefined what happens if cleanup_function does not return normally.` here <https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#index-cleanup-variable-attribute>, thus I'm not sure what to do in that case. GCC seems to optimize accordingly, but clang does not. See https://godbolt.org/z/z8s6bPPjv.

FYI Unfortunately, I don't have much experience with CFG either.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D157385/new/

https://reviews.llvm.org/D157385



More information about the cfe-commits mailing list