[clang] Crash when referencing capture in static lambda (PR #74661)

2023-12-06 Thread Ben Jackson via cfe-commits

https://github.com/puremourning created 
https://github.com/llvm/llvm-project/pull/74661

The constant evaluator could try to reference a lambda capture in a static 
lambda call operator. Static lambdas can't have captures, so we simply abort. 
Either the lambda needs to be made non-static, or the capture (and reference to 
it) need to be removed.

Fixes: https://github.com/llvm/llvm-project/issues/74608

>From a523d23d05862229a37a5322252ba4163a4e3556 Mon Sep 17 00:00:00 2001
From: Ben Jackson 
Date: Wed, 6 Dec 2023 21:59:21 +
Subject: [PATCH] Crash when referencing capture in static lambda

The constant evaluator could try to reference a lambda capture in a
static lambda call operator. Static lambdas can't have captures, so we
simply abort. Either the lambda needs to be made non-static, or the
capture (and reference to it) need to be removed.
---
 clang/lib/AST/ExprConstant.cpp  | 14 --
 clang/test/Parser/cxx2b-lambdas.cpp | 12 
 2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 986302e1fd225f..e806318efd8a5e 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -8492,14 +8492,24 @@ bool LValueExprEvaluator::VisitVarDecl(const Expr *E, 
const VarDecl *VD) {
   return false;
 
 if (auto *FD = Info.CurrentCall->LambdaCaptureFields.lookup(VD)) {
+  auto *MD = cast(Info.CurrentCall->Callee);
+
+  // Static lambda function call operators can't have captures. We already
+  // diagnosed this, so bail out here.
+  if (MD->isStatic()) {
+assert(Info.CurrentCall->This == nullptr &&
+   "This should not be set for a static call operator");
+return false;
+  }
+
   // Start with 'Result' referring to the complete closure object...
-  if (auto *MD = cast(Info.CurrentCall->Callee);
-  MD->isExplicitObjectMemberFunction()) {
+  if (MD->isExplicitObjectMemberFunction()) {
 APValue *RefValue =
 Info.getParamSlot(Info.CurrentCall->Arguments, 
MD->getParamDecl(0));
 Result.setFrom(Info.Ctx, *RefValue);
   } else
 Result = *Info.CurrentCall->This;
+
   // ... then update it to refer to the field of the closure object
   // that represents the capture.
   if (!HandleLValueMember(Info, E, Result, FD))
diff --git a/clang/test/Parser/cxx2b-lambdas.cpp 
b/clang/test/Parser/cxx2b-lambdas.cpp
index bb9ed226afffaf..ad975a17b6e476 100644
--- a/clang/test/Parser/cxx2b-lambdas.cpp
+++ b/clang/test/Parser/cxx2b-lambdas.cpp
@@ -66,3 +66,15 @@ void static_captures() {
 }
   };
 }
+
+constexpr auto static_capture_constexpr() {
+  char n = 'n';
+  return [n] static { return n; }(); // expected-error {{a static lambda 
cannot have any captures}}
+}
+static_assert(static_capture_constexpr()); // expected-error {{static 
assertion expression is not an integral constant expression}}
+
+constexpr auto capture_constexpr() {
+  char n = 'n';
+  return [n] { return n; }();
+}
+static_assert(capture_constexpr());

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] Crash when referencing capture in static lambda (PR #74661)

2023-12-06 Thread Ben Jackson via cfe-commits

puremourning wrote:

I guess should ping @royjacobson on this one?

https://github.com/llvm/llvm-project/pull/74661
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Crash when referencing capture in static lambda (PR #74661)

2023-12-06 Thread Ben Jackson via cfe-commits

https://github.com/puremourning edited 
https://github.com/llvm/llvm-project/pull/74661
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Crash when referencing capture in static lambda (PR #74661)

2023-12-07 Thread Ben Jackson via cfe-commits

https://github.com/puremourning updated 
https://github.com/llvm/llvm-project/pull/74661

>From 8a72385996824f9cdd0feb5a1ca7aa771c03fe99 Mon Sep 17 00:00:00 2001
From: Ben Jackson 
Date: Wed, 6 Dec 2023 21:59:21 +
Subject: [PATCH] Crash when referencing capture in static lambda

The constant evaluator could try to reference a lambda capture in a
static lambda call operator. Static lambdas can't have captures, so we
simply abort. Either the lambda needs to be made non-static, or the
capture (and reference to it) need to be removed.
---
 clang/docs/ReleaseNotes.rst |  4 
 clang/lib/AST/ExprConstant.cpp  | 14 --
 clang/test/Parser/cxx2b-lambdas.cpp | 12 
 3 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 1fbd332f74057..43292d1eef227 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -848,6 +848,10 @@ Miscellaneous Clang Crashes Fixed
   `Issue 41302 `_
 - Fixed a crash when ``-ast-dump=json`` was used for code using class
   template deduction guides.
+- Fixed a crash when a lambda marked as `static` referenced a captured variable
+  in an expression.
+  `Issue 74608 `_
+
 
 OpenACC Specific Changes
 
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 986302e1fd225..e806318efd8a5 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -8492,14 +8492,24 @@ bool LValueExprEvaluator::VisitVarDecl(const Expr *E, 
const VarDecl *VD) {
   return false;
 
 if (auto *FD = Info.CurrentCall->LambdaCaptureFields.lookup(VD)) {
+  auto *MD = cast(Info.CurrentCall->Callee);
+
+  // Static lambda function call operators can't have captures. We already
+  // diagnosed this, so bail out here.
+  if (MD->isStatic()) {
+assert(Info.CurrentCall->This == nullptr &&
+   "This should not be set for a static call operator");
+return false;
+  }
+
   // Start with 'Result' referring to the complete closure object...
-  if (auto *MD = cast(Info.CurrentCall->Callee);
-  MD->isExplicitObjectMemberFunction()) {
+  if (MD->isExplicitObjectMemberFunction()) {
 APValue *RefValue =
 Info.getParamSlot(Info.CurrentCall->Arguments, 
MD->getParamDecl(0));
 Result.setFrom(Info.Ctx, *RefValue);
   } else
 Result = *Info.CurrentCall->This;
+
   // ... then update it to refer to the field of the closure object
   // that represents the capture.
   if (!HandleLValueMember(Info, E, Result, FD))
diff --git a/clang/test/Parser/cxx2b-lambdas.cpp 
b/clang/test/Parser/cxx2b-lambdas.cpp
index bb9ed226afffa..ad975a17b6e47 100644
--- a/clang/test/Parser/cxx2b-lambdas.cpp
+++ b/clang/test/Parser/cxx2b-lambdas.cpp
@@ -66,3 +66,15 @@ void static_captures() {
 }
   };
 }
+
+constexpr auto static_capture_constexpr() {
+  char n = 'n';
+  return [n] static { return n; }(); // expected-error {{a static lambda 
cannot have any captures}}
+}
+static_assert(static_capture_constexpr()); // expected-error {{static 
assertion expression is not an integral constant expression}}
+
+constexpr auto capture_constexpr() {
+  char n = 'n';
+  return [n] { return n; }();
+}
+static_assert(capture_constexpr());

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Crash when referencing capture in static lambda (PR #74661)

2023-12-07 Thread Ben Jackson via cfe-commits

puremourning wrote:

> Can you add a release note? Otherwise it looks good to me

Yep, sure thing. Done.

https://github.com/llvm/llvm-project/pull/74661
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Crash when referencing capture in static lambda (PR #74661)

2023-12-07 Thread Ben Jackson via cfe-commits

https://github.com/puremourning updated 
https://github.com/llvm/llvm-project/pull/74661

>From 13afd0e5087340c62b31ff5d35c06b28f93086a5 Mon Sep 17 00:00:00 2001
From: Ben Jackson 
Date: Wed, 6 Dec 2023 21:59:21 +
Subject: [PATCH] Crash when referencing capture in static lambda

The constant evaluator could try to reference a lambda capture in a
static lambda call operator. Static lambdas can't have captures, so we
simply abort. Either the lambda needs to be made non-static, or the
capture (and reference to it) need to be removed.
---
 clang/docs/ReleaseNotes.rst |  4 
 clang/lib/AST/ExprConstant.cpp  | 14 --
 clang/test/Parser/cxx2b-lambdas.cpp | 12 
 3 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 1fbd332f74057..43292d1eef227 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -848,6 +848,10 @@ Miscellaneous Clang Crashes Fixed
   `Issue 41302 `_
 - Fixed a crash when ``-ast-dump=json`` was used for code using class
   template deduction guides.
+- Fixed a crash when a lambda marked as `static` referenced a captured variable
+  in an expression.
+  `Issue 74608 `_
+
 
 OpenACC Specific Changes
 
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 986302e1fd225..41cf9443a6481 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -8492,14 +8492,24 @@ bool LValueExprEvaluator::VisitVarDecl(const Expr *E, 
const VarDecl *VD) {
   return false;
 
 if (auto *FD = Info.CurrentCall->LambdaCaptureFields.lookup(VD)) {
+  const auto *MD = cast(Info.CurrentCall->Callee);
+
+  // Static lambda function call operators can't have captures. We already
+  // diagnosed this, so bail out here.
+  if (MD->isStatic()) {
+assert(Info.CurrentCall->This == nullptr &&
+   "This should not be set for a static call operator");
+return false;
+  }
+
   // Start with 'Result' referring to the complete closure object...
-  if (auto *MD = cast(Info.CurrentCall->Callee);
-  MD->isExplicitObjectMemberFunction()) {
+  if (MD->isExplicitObjectMemberFunction()) {
 APValue *RefValue =
 Info.getParamSlot(Info.CurrentCall->Arguments, 
MD->getParamDecl(0));
 Result.setFrom(Info.Ctx, *RefValue);
   } else
 Result = *Info.CurrentCall->This;
+
   // ... then update it to refer to the field of the closure object
   // that represents the capture.
   if (!HandleLValueMember(Info, E, Result, FD))
diff --git a/clang/test/Parser/cxx2b-lambdas.cpp 
b/clang/test/Parser/cxx2b-lambdas.cpp
index bb9ed226afffa..ad975a17b6e47 100644
--- a/clang/test/Parser/cxx2b-lambdas.cpp
+++ b/clang/test/Parser/cxx2b-lambdas.cpp
@@ -66,3 +66,15 @@ void static_captures() {
 }
   };
 }
+
+constexpr auto static_capture_constexpr() {
+  char n = 'n';
+  return [n] static { return n; }(); // expected-error {{a static lambda 
cannot have any captures}}
+}
+static_assert(static_capture_constexpr()); // expected-error {{static 
assertion expression is not an integral constant expression}}
+
+constexpr auto capture_constexpr() {
+  char n = 'n';
+  return [n] { return n; }();
+}
+static_assert(capture_constexpr());

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Crash when referencing capture in static lambda (PR #74661)

2023-12-07 Thread Ben Jackson via cfe-commits

https://github.com/puremourning updated 
https://github.com/llvm/llvm-project/pull/74661

>From cdd9b653de7aed3d54b5ac6c98fc943183509278 Mon Sep 17 00:00:00 2001
From: Ben Jackson 
Date: Wed, 6 Dec 2023 21:59:21 +
Subject: [PATCH] Crash when referencing capture in static lambda

The constant evaluator could try to reference a lambda capture in a
static lambda call operator. Static lambdas can't have captures, so we
simply abort. Either the lambda needs to be made non-static, or the
capture (and reference to it) need to be removed.
---
 clang/docs/ReleaseNotes.rst |  4 
 clang/lib/AST/ExprConstant.cpp  | 14 --
 clang/test/Parser/cxx2b-lambdas.cpp | 12 
 3 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 1fbd332f74057f..0ff8cd23b9426a 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -848,6 +848,10 @@ Miscellaneous Clang Crashes Fixed
   `Issue 41302 `_
 - Fixed a crash when ``-ast-dump=json`` was used for code using class
   template deduction guides.
+- Fixed a crash when a lambda marked as ``static`` referenced a captured
+  variable in an expression.
+  `Issue 74608 `_
+
 
 OpenACC Specific Changes
 
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 986302e1fd225f..41cf9443a64818 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -8492,14 +8492,24 @@ bool LValueExprEvaluator::VisitVarDecl(const Expr *E, 
const VarDecl *VD) {
   return false;
 
 if (auto *FD = Info.CurrentCall->LambdaCaptureFields.lookup(VD)) {
+  const auto *MD = cast(Info.CurrentCall->Callee);
+
+  // Static lambda function call operators can't have captures. We already
+  // diagnosed this, so bail out here.
+  if (MD->isStatic()) {
+assert(Info.CurrentCall->This == nullptr &&
+   "This should not be set for a static call operator");
+return false;
+  }
+
   // Start with 'Result' referring to the complete closure object...
-  if (auto *MD = cast(Info.CurrentCall->Callee);
-  MD->isExplicitObjectMemberFunction()) {
+  if (MD->isExplicitObjectMemberFunction()) {
 APValue *RefValue =
 Info.getParamSlot(Info.CurrentCall->Arguments, 
MD->getParamDecl(0));
 Result.setFrom(Info.Ctx, *RefValue);
   } else
 Result = *Info.CurrentCall->This;
+
   // ... then update it to refer to the field of the closure object
   // that represents the capture.
   if (!HandleLValueMember(Info, E, Result, FD))
diff --git a/clang/test/Parser/cxx2b-lambdas.cpp 
b/clang/test/Parser/cxx2b-lambdas.cpp
index bb9ed226afffaf..ad975a17b6e476 100644
--- a/clang/test/Parser/cxx2b-lambdas.cpp
+++ b/clang/test/Parser/cxx2b-lambdas.cpp
@@ -66,3 +66,15 @@ void static_captures() {
 }
   };
 }
+
+constexpr auto static_capture_constexpr() {
+  char n = 'n';
+  return [n] static { return n; }(); // expected-error {{a static lambda 
cannot have any captures}}
+}
+static_assert(static_capture_constexpr()); // expected-error {{static 
assertion expression is not an integral constant expression}}
+
+constexpr auto capture_constexpr() {
+  char n = 'n';
+  return [n] { return n; }();
+}
+static_assert(capture_constexpr());

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Interp] Reject static lambdas with captures (PR #74718)

2023-12-11 Thread Ben Jackson via cfe-commits

https://github.com/puremourning edited 
https://github.com/llvm/llvm-project/pull/74718
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Interp] Reject static lambdas with captures (PR #74718)

2023-12-11 Thread Ben Jackson via cfe-commits

https://github.com/puremourning commented:

Lgtm 

https://github.com/llvm/llvm-project/pull/74718
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][Interp] Reject static lambdas with captures (PR #74718)

2023-12-11 Thread Ben Jackson via cfe-commits


@@ -61,6 +61,11 @@ ByteCodeEmitter::compileFunc(const FunctionDecl *FuncDecl) {
   MD->getParent()->getCaptureFields(LC, LTC);
 
   for (auto Cap : LC) {
+// Static lambdas cannot have any captures. If this one does,
+// it has already been diagnosed and we can only ignore it.
+if (MD->isStatic())

puremourning wrote:

Not sure we need to even enter this loop. 

https://github.com/llvm/llvm-project/pull/74718
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D11976: [libclang] Return deduced type for auto type, not the one written in the source.

2015-08-22 Thread Ben Jackson via cfe-commits
puremourning added a comment.

I can confirm that this patch fixes a number test cases found in ycmd 
 (a code-completion server). Specifically 
these ones marked as 'sic' 


So I'm in favour of this change :)


http://reviews.llvm.org/D11976



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


Re: [PATCH] D22129: [clang-rename] add documentation

2016-07-11 Thread Ben Jackson via cfe-commits
puremourning added a subscriber: puremourning.
puremourning added a comment.

Couple of drive-by comments. Thanks for doing this.



Comment at: docs/clang-rename.rst:45
@@ +44,3 @@
+
+The tool is currently supporting renaming actions inside single Translation 
Unit
+only. It is planned to extend tool functionality to support multi-TU renaming

... currently supports ... inside a single ...

Is more natural English. 


Comment at: docs/clang-rename.rst:46
@@ +45,3 @@
+The tool is currently supporting renaming actions inside single Translation 
Unit
+only. It is planned to extend tool functionality to support multi-TU renaming
+actions in the future.

... the tool's functionality ...


http://reviews.llvm.org/D22129



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits