[PATCH] D44844: [PR36880] Avoid -Wunused-lambda-capture false positive for explicit this capture used in an unresolved member in a generic lambda

Alex Lorenz via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 23 13:26:17 PDT 2018


arphaman created this revision.
arphaman added reviewers: malcolm.parsons, ahatanak, vsapsai.
Herald added a subscriber: jkorous-apple.

Clang emits an incorrect -Wunused-lambda-capture for 'this' capture in a generic lambda in a template where 'this' is used implicitly in an unresolved member expression. This patch ensures that the use of 'this' is checked as a potential lambda capture use when the lambda is instantiated.

rdar://38803903


Repository:
  rC Clang

https://reviews.llvm.org/D44844

Files:
  include/clang/AST/ExprCXX.h
  lib/AST/ExprCXX.cpp
  lib/Sema/TreeTransform.h
  test/SemaCXX/warn-unused-lambda-capture.cpp


Index: test/SemaCXX/warn-unused-lambda-capture.cpp
===================================================================
--- test/SemaCXX/warn-unused-lambda-capture.cpp
+++ test/SemaCXX/warn-unused-lambda-capture.cpp
@@ -200,3 +200,21 @@
   auto vla_unused = [&c] {}; // expected-warning{{lambda capture 'c' is not used}}
 }
 } // namespace pr35555
+
+namespace pr36880 {
+
+template <class T> struct DummyTemplate {
+  template <class T2> void methodTemplate(const T2 &) {}
+
+  void ToTemplate(const int &param) {
+    [this](const auto &p) { methodTemplate(p); }(param); // no warning
+  }
+};
+
+void main() {
+  int i = 0;
+  DummyTemplate<bool> dt;
+  dt.ToTemplate(i);
+}
+
+} // namespace pr36880
Index: lib/Sema/TreeTransform.h
===================================================================
--- lib/Sema/TreeTransform.h
+++ lib/Sema/TreeTransform.h
@@ -11264,6 +11264,12 @@
       return ExprError();
     BaseType = Base.get()->getType();
   } else {
+    // Check an implicit 'this' base expression to ensure that an explicit
+    // 'this' capture is marked as 'used'.
+    if (Old->isImplicitCXXThisAccess())
+      getSema().CheckCXXThisCapture(Old->getMemberNameInfo().getLoc(),
+                                    /*Explicit=*/false,
+                                    /*BuildAndDiagnose=*/false);
     BaseType = getDerived().TransformType(Old->getBaseType());
   }
 
Index: lib/AST/ExprCXX.cpp
===================================================================
--- lib/AST/ExprCXX.cpp
+++ lib/AST/ExprCXX.cpp
@@ -1246,6 +1246,12 @@
   return cast<Expr>(Base)->isImplicitCXXThis();
 }
 
+bool UnresolvedMemberExpr::isImplicitCXXThisAccess() const {
+  if (!Base)
+    return false;
+  return cast<Expr>(Base)->isImplicitCXXThis();
+}
+
 UnresolvedMemberExpr *UnresolvedMemberExpr::Create(
     const ASTContext &C, bool HasUnresolvedUsing, Expr *Base, QualType BaseType,
     bool IsArrow, SourceLocation OperatorLoc,
Index: include/clang/AST/ExprCXX.h
===================================================================
--- include/clang/AST/ExprCXX.h
+++ include/clang/AST/ExprCXX.h
@@ -3505,6 +3505,9 @@
   /// The source location of the operator is invalid in this case.
   bool isImplicitAccess() const;
 
+  /// True if this an implicit access with a 'this' base object.
+  bool isImplicitCXXThisAccess() const;
+
   /// \brief Retrieve the base object of this member expressions,
   /// e.g., the \c x in \c x.m.
   Expr *getBase() {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D44844.139645.patch
Type: text/x-patch
Size: 2465 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180323/e9f4f1ed/attachment.bin>


More information about the cfe-commits mailing list