[PATCH] D36915: [Sema] Diagnose local variables and parameters captured by lambda and block expressions in a default argument

Akira Hatanaka via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Sep 11 19:31:33 PDT 2017


ahatanak updated this revision to Diff 114753.
ahatanak added a comment.

Address review comments.

- Detect invalid references to parameters or local variables by default arguments in tryCaptureVariable. Before parsing or instantiating the default argument expression, the enclosing DeclContext is saved to ParentOfDefaultArg, which tryCaptureVariable uses to detect invalid references (if the referenced variable belongs to ParentOfDefaultArg or an enclosing DeclContext, it is not valid).

- In CheckCXXDefaultArgExpr, save the parameters and their instantiations that appear before the parameter with default argument to the current LocalInstantiationScope so that findInstantiationOf doesn't assert when it tries to find the instantiation of a parameter that is referenced in the default arugment.

There are still cases where clang rejects references to local variables or parameters that shouldn't be rejected. For example:

- Local variables or parameters referenced in _Generic's controlling-expression or the expressions of the selections that are not chosen.
- The following code is rejected even though 'x' is not odr-used:

  void func() {
    const int x = 1;
    void foo1(int a0 = x);
  }



- dcl.fct.default/p7.cpp

I plan to work on a fix after this patch is committed.


https://reviews.llvm.org/D36915

Files:
  include/clang/Sema/Sema.h
  lib/Parse/ParseDecl.cpp
  lib/Sema/Sema.cpp
  lib/Sema/SemaExpr.cpp
  test/CXX/dcl.decl/dcl.meaning/dcl.fct.default/p7.cpp
  test/CXX/expr/expr.prim/expr.prim.lambda/p13.cpp
  test/SemaCXX/default1.cpp
  test/SemaObjCXX/blocks.mm

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D36915.114753.patch
Type: text/x-patch
Size: 14582 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170912/c9ef081e/attachment-0001.bin>


More information about the cfe-commits mailing list