This is an archive of the discontinued LLVM Phabricator instance.

[Sema] Improve side effect checking for unused-lambda-capture warning
ClosedPublic

Authored by malcolm.parsons on Feb 24 2017, 1:56 AM.

Diff Detail

Repository
rL LLVM

Event Timeline

aaron.ballman added inline comments.Feb 27 2017, 11:51 AM
lib/Sema/SemaLambda.cpp
1458 ↗(On Diff #89625)

I think this is missing one more case: capturing a volatile variable by copy does have a side effect in that the variable is read when the capture occurs. e.g., this should not diagnose either:

void f(volatile int v) {
  [v]{}();
}

I found this FIXME comment in Expr::HasSideEffects():

case LambdaExprClass: {
  const LambdaExpr *LE = cast<LambdaExpr>(this);
  for (LambdaExpr::capture_iterator I = LE->capture_begin(),
                                    E = LE->capture_end(); I != E; ++I)
    if (I->getCaptureKind() == LCK_ByCopy)
      // FIXME: Only has a side-effect if the variable is volatile or if
      // the copy would invoke a non-trivial copy constructor.
      return true;
  return false;
}

It seems a shame not to fix this now, but I don't think I can call Sema code from AST code, and my CaptureHasSideEffects() depends on Sema::getCurrentThisType().

Any ideas?

malcolm.parsons marked an inline comment as done.

Handle captures of volatile variables.

aaron.ballman accepted this revision.Feb 28 2017, 1:06 PM

LGTM!

I found this FIXME comment in Expr::HasSideEffects():

case LambdaExprClass: {
  const LambdaExpr *LE = cast<LambdaExpr>(this);
  for (LambdaExpr::capture_iterator I = LE->capture_begin(),
                                    E = LE->capture_end(); I != E; ++I)
    if (I->getCaptureKind() == LCK_ByCopy)
      // FIXME: Only has a side-effect if the variable is volatile or if
      // the copy would invoke a non-trivial copy constructor.
      return true;
  return false;
}

It seems a shame not to fix this now, but I don't think I can call Sema code from AST code, and my CaptureHasSideEffects() depends on Sema::getCurrentThisType().

Any ideas?

Correct, you cannot call Sema code from AST. I don't know of a good way to resolve this off the top of my head given what getCurrentThisType() needs to do. I agree that this would be good to fix as well, but your patch is a good incremental improvement and should't be held up on this.

This revision is now accepted and ready to land.Feb 28 2017, 1:06 PM
This revision was automatically updated to reflect the committed changes.