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

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 1 07:14:33 PST 2024


https://github.com/AaronBallman commented:

Thank you for working on this! I think we want to go in a different direction for this fix. I think the real issue is that we're missing a call to `Sema::CheckFunctionCall()` within `handleCleanupAttr()`. That's currently a private function in `Sema`, so it would have to be hoisted to a public interface so we can call it. But after we call `Sema::CheckAssignmentConstraints()`, I think we should make a fake function call expression so that we can call `Sema::CheckFunctionCall()` on it.

That code should look something like this (note, probably needs more work to ensure source locations point to the correct things for correct diagnostic behavior):
```
  DeclRefExpr *FakeVarRef = DeclRefExpr::Create(
      S.Context, NestedNameSpecifierLoc{}, SourceLocation{}, VD, false,
      DeclarationNameInfo{VD->getDeclName(), VD->getLocation()}, VD->getType(),
      VK_LValue);
  Expr *FakeArg = UnaryOperator::Create(
      S.Context, FakeVarRef, UnaryOperatorKind::UO_AddrOf,
      S.Context.getPointerType(VarDecl->getType()), VK_PRValue, OK_Ordinary,
      SourceLocation{}, false, FPOptionsOverride{});
  CallExpr *FakeCall =
      CallExpr::Create(S.Context, E, ArrayRef{FakeArg}, S.Context.VoidTy,
                       VK_PRValue, SourceLocation{}, FPOptionsOverride{});
  S.CheckFunctionCall(FD, FakeCall, FD->getType()->getAs<FunctionProtoType>());
```
Then we get all of the checking functionality that the user would get had they called the function themselves directly. This includes warning about things like this:
```
struct __attribute__((aligned(8))) S {};

void derp(struct S *ptr);

int main(void) {
  typedef __attribute__((aligned(4))) struct S TypedefAligned4;
  TypedefAligned4 s1;
  derp(&s1); // Warns

  __attribute__((cleanup(derp))) TypedefAligned4 s2; // Should get same warning here as above
}
```

The changes should also come with a release note in `clang/docs/ReleaseNotes.rst` so users know about the fix, and should come with tests in `clang/test/Sema/` to demonstrate the behavioral changes.

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


More information about the cfe-commits mailing list