[clang] Diagnose misuse of the cleanup attribute (PR #80040)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 9 06:51:21 PST 2024


================
@@ -3780,6 +3780,30 @@ static void handleCleanupAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
       << NI.getName() << ParamTy << Ty;
     return;
   }
+  VarDecl *VD = cast<VarDecl>(D);
+  // Create a reference to the variable declaration. This is a fake/dummy
+  // reference.
+  DeclRefExpr *VariableReference = DeclRefExpr::Create(
+      S.Context, NestedNameSpecifierLoc{}, SourceLocation{Loc}, VD, false,
----------------
AaronBallman wrote:

Making sure we're all on the same page, I think the diagnostic should show up like:
```
extern void free(void *);
extern void *malloc(size_t size);
void t8(void) {
  void *p
  __attribute__((
	  cleanup(
		  free // expected-warning{{attempt to call free on non-heap object 'p'}}
	  )
	))
  = malloc(10);
```
and
```
struct __attribute__((aligned(8))) S {};

void derp(struct S *ptr);

int main(void) {
  typedef __attribute__((aligned(4))) struct S TypedefAligned4;
  TypedefAligned4 s1;
  derp(&s1); // expected-warning {{passing 4-byte aligned argument to 8-byte aligned parameter 1 of 'derp' may result in an unaligned pointer access}}

  __attribute__((cleanup(derp))) // expected-warning {{passing 4-byte aligned argument to 8-byte aligned parameter 1 of 'derp' may result in an unaligned pointer access}}
  TypedefAligned4 s2;
}
```

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


More information about the cfe-commits mailing list