[cfe-dev] Incorrect result from DeclRefExpr::refersToEnclosingVariableCapture?

Tim Prince via cfe-dev cfe-dev at lists.llvm.org
Wed Oct 21 14:14:13 PDT 2015


Hi everyone,

We've been working on upgrading our frontend from clang 3.4 to 3.7 and have run
into an issue where 'DeclRefExpr::refersToEnclosingVariableOrCapture' seems to be
returning the incorrect result. Here's a test case:

  int fn1() {
    const int i = 0;
    return [] { return i; }();
  }

Calling 'refersToEnclosingVariableOrCapture' on the 'DeclRefExpr' for 'i' within
the lambda body yields false. In clang 3.4, the equivalent 'refersToCapturedVariable'
method yields true. It looks this behavior was probably introduced by r224323,
which changed 'Sema::BuildDeclRefExpr' to determine this via 'tryCaptureVariable'.

So with that background, I have a few questions:

- Is this code even valid? It seems like it shouldn't be since there's no
  capture-default or capture-list, but both clang and GCC accept it.
- Assuming the code is valid, is this a bug in clang?
- If not, is there some other way to detect when a DeclRefExpr in a lambda body
  refers to a declaration from the enclosing scope?

Incidentally, GCC accepts this similar test case, while clang does not:

  void fn1() {
    const int i = 0;
    [] { i; }();
  }

Thanks!
- Tim



More information about the cfe-dev mailing list