[clang] [Clang][Sema] Use correct TemplateName when transforming TemplateSpecializationType (PR #93411)

2024-05-26 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky updated 
https://github.com/llvm/llvm-project/pull/93411

>From f5f0b14945a70e3e4fd92d5e5cbdb428334fe2b8 Mon Sep 17 00:00:00 2001
From: Qizhi Hu <836744...@qq.com>
Date: Sat, 25 May 2024 16:30:27 +0800
Subject: [PATCH 1/2] [Clang][Sema] Use correct TemplateName when transforming
 TemplateSpecializationType

---
 clang/lib/Sema/TreeTransform.h | 43 +-
 1 file changed, 42 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index dee335b526991..7e8b080a347e8 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -29,8 +29,10 @@
 #include "clang/AST/StmtObjC.h"
 #include "clang/AST/StmtOpenACC.h"
 #include "clang/AST/StmtOpenMP.h"
+#include "clang/AST/TypeLoc.h"
 #include "clang/Basic/DiagnosticParse.h"
 #include "clang/Basic/OpenMPKinds.h"
+#include "clang/Sema/DeclSpec.h"
 #include "clang/Sema/Designator.h"
 #include "clang/Sema/EnterExpressionEvaluationContext.h"
 #include "clang/Sema/Lookup.h"
@@ -7216,7 +7218,46 @@ 
TreeTransform::TransformElaboratedType(TypeLocBuilder ,
   return QualType();
   }
 
-  QualType NamedT = getDerived().TransformType(TLB, TL.getNamedTypeLoc());
+  QualType NamedT;
+  if (0 && SemaRef.getLangOpts().CPlusPlus20 && QualifierLoc && 
isa(TL.getNamedTypeLoc().getType())) {
+const TemplateSpecializationType *TST = 
TL.getNamedTypeLoc().getType()->getAs();
+TemplateSpecializationTypeLoc SpecTL =
+TL.getNamedTypeLoc().castAs();
+// TemplateArgumentListInfo NewTemplateArgs;
+// NewTemplateArgs.setLAngleLoc(SpecTL.getLAngleLoc());
+// NewTemplateArgs.setRAngleLoc(SpecTL.getRAngleLoc());
+
+// typedef TemplateArgumentLocContainerIterator<
+// TemplateSpecializationTypeLoc> ArgIterator;
+// if (getDerived().TransformTemplateArguments(ArgIterator(SpecTL, 0),
+// ArgIterator(SpecTL, 
SpecTL.getNumArgs()),
+// NewTemplateArgs))
+//   return QualType();
+
+CXXScopeSpec SS;
+SS.Adopt(QualifierLoc);
+TemplateName InstName = getDerived().RebuildTemplateName(
+SS, TL.getTemplateKeywordLoc(), 
*TST->getTemplateName().getAsTemplateDecl()->getIdentifier(), 
TL.getNamedTypeLoc().getBeginLoc(), QualType(), nullptr,
+false);
+
+if (InstName.isNull())
+  return QualType();
+
+// If it's still dependent, make a dependent specialization.
+// if (InstName.getAsDependentTemplateName())
+//   return SemaRef.Context.getDependentTemplateSpecializationType(
+//   Keyword, QualifierLoc.getNestedNameSpecifier(), Name,
+//   Args.arguments());
+
+// Otherwise, make an elaborated type wrapping a non-dependent
+// specialization.
+// NamedT = getDerived().RebuildTemplateSpecializationType(InstName, 
TL.getNamedTypeLoc().getBeginLoc(), NewTemplateArgs);
+NamedT = TransformTemplateSpecializationType(TLB, SpecTL, InstName);
+  } else {
+NamedT = getDerived().TransformType(TLB, TL.getNamedTypeLoc());
+
+  }
+
   if (NamedT.isNull())
 return QualType();
 

>From 3ebed0fecd90101b57082909348a60070c689d12 Mon Sep 17 00:00:00 2001
From: Qizhi Hu <836744...@qq.com>
Date: Sun, 26 May 2024 19:35:17 +0800
Subject: [PATCH 2/2] [Clang][Sema] Use correct TemplateName when transforming
 TemplateSpecializationType

---
 clang/lib/Sema/TreeTransform.h| 54 ++-
 clang/test/SemaCXX/PR91677.cpp| 14 +
 .../SemaTemplate/typename-specifier-3.cpp |  7 +--
 3 files changed, 35 insertions(+), 40 deletions(-)
 create mode 100644 clang/test/SemaCXX/PR91677.cpp

diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index 7e8b080a347e8..6ef2eec09ec02 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -29,10 +29,8 @@
 #include "clang/AST/StmtObjC.h"
 #include "clang/AST/StmtOpenACC.h"
 #include "clang/AST/StmtOpenMP.h"
-#include "clang/AST/TypeLoc.h"
 #include "clang/Basic/DiagnosticParse.h"
 #include "clang/Basic/OpenMPKinds.h"
-#include "clang/Sema/DeclSpec.h"
 #include "clang/Sema/Designator.h"
 #include "clang/Sema/EnterExpressionEvaluationContext.h"
 #include "clang/Sema/Lookup.h"
@@ -7219,48 +7217,30 @@ 
TreeTransform::TransformElaboratedType(TypeLocBuilder ,
   }
 
   QualType NamedT;
-  if (0 && SemaRef.getLangOpts().CPlusPlus20 && QualifierLoc && 
isa(TL.getNamedTypeLoc().getType())) {
-const TemplateSpecializationType *TST = 
TL.getNamedTypeLoc().getType()->getAs();
+  if (SemaRef.getLangOpts().CPlusPlus20 && QualifierLoc &&
+  isa(TL.getNamedTypeLoc().getType())) {
 TemplateSpecializationTypeLoc SpecTL =
 TL.getNamedTypeLoc().castAs();
-// TemplateArgumentListInfo NewTemplateArgs;
-// NewTemplateArgs.setLAngleLoc(SpecTL.getLAngleLoc());
-// NewTemplateArgs.setRAngleLoc(SpecTL.getRAngleLoc());
-
-// typedef 

[clang] [Clang][Sema] Use correct TemplateName when transforming TemplateSpecializationType (PR #93411)

2024-05-26 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky updated 
https://github.com/llvm/llvm-project/pull/93411

>From f5f0b14945a70e3e4fd92d5e5cbdb428334fe2b8 Mon Sep 17 00:00:00 2001
From: Qizhi Hu <836744...@qq.com>
Date: Sat, 25 May 2024 16:30:27 +0800
Subject: [PATCH 1/2] [Clang][Sema] Use correct TemplateName when transforming
 TemplateSpecializationType

---
 clang/lib/Sema/TreeTransform.h | 43 +-
 1 file changed, 42 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index dee335b526991..7e8b080a347e8 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -29,8 +29,10 @@
 #include "clang/AST/StmtObjC.h"
 #include "clang/AST/StmtOpenACC.h"
 #include "clang/AST/StmtOpenMP.h"
+#include "clang/AST/TypeLoc.h"
 #include "clang/Basic/DiagnosticParse.h"
 #include "clang/Basic/OpenMPKinds.h"
+#include "clang/Sema/DeclSpec.h"
 #include "clang/Sema/Designator.h"
 #include "clang/Sema/EnterExpressionEvaluationContext.h"
 #include "clang/Sema/Lookup.h"
@@ -7216,7 +7218,46 @@ 
TreeTransform::TransformElaboratedType(TypeLocBuilder ,
   return QualType();
   }
 
-  QualType NamedT = getDerived().TransformType(TLB, TL.getNamedTypeLoc());
+  QualType NamedT;
+  if (0 && SemaRef.getLangOpts().CPlusPlus20 && QualifierLoc && 
isa(TL.getNamedTypeLoc().getType())) {
+const TemplateSpecializationType *TST = 
TL.getNamedTypeLoc().getType()->getAs();
+TemplateSpecializationTypeLoc SpecTL =
+TL.getNamedTypeLoc().castAs();
+// TemplateArgumentListInfo NewTemplateArgs;
+// NewTemplateArgs.setLAngleLoc(SpecTL.getLAngleLoc());
+// NewTemplateArgs.setRAngleLoc(SpecTL.getRAngleLoc());
+
+// typedef TemplateArgumentLocContainerIterator<
+// TemplateSpecializationTypeLoc> ArgIterator;
+// if (getDerived().TransformTemplateArguments(ArgIterator(SpecTL, 0),
+// ArgIterator(SpecTL, 
SpecTL.getNumArgs()),
+// NewTemplateArgs))
+//   return QualType();
+
+CXXScopeSpec SS;
+SS.Adopt(QualifierLoc);
+TemplateName InstName = getDerived().RebuildTemplateName(
+SS, TL.getTemplateKeywordLoc(), 
*TST->getTemplateName().getAsTemplateDecl()->getIdentifier(), 
TL.getNamedTypeLoc().getBeginLoc(), QualType(), nullptr,
+false);
+
+if (InstName.isNull())
+  return QualType();
+
+// If it's still dependent, make a dependent specialization.
+// if (InstName.getAsDependentTemplateName())
+//   return SemaRef.Context.getDependentTemplateSpecializationType(
+//   Keyword, QualifierLoc.getNestedNameSpecifier(), Name,
+//   Args.arguments());
+
+// Otherwise, make an elaborated type wrapping a non-dependent
+// specialization.
+// NamedT = getDerived().RebuildTemplateSpecializationType(InstName, 
TL.getNamedTypeLoc().getBeginLoc(), NewTemplateArgs);
+NamedT = TransformTemplateSpecializationType(TLB, SpecTL, InstName);
+  } else {
+NamedT = getDerived().TransformType(TLB, TL.getNamedTypeLoc());
+
+  }
+
   if (NamedT.isNull())
 return QualType();
 

>From e60cdac25c4ea2e85894e51dfd5268544cadd27d Mon Sep 17 00:00:00 2001
From: Qizhi Hu <836744...@qq.com>
Date: Sun, 26 May 2024 19:35:17 +0800
Subject: [PATCH 2/2] [Clang][Sema] Use correct TemplateName when transforming
 TemplateSpecializationType

---
 clang/lib/Sema/TreeTransform.h| 54 ++-
 clang/test/SemaCXX/PR91677.cpp| 31 +++
 .../SemaTemplate/typename-specifier-3.cpp |  7 +--
 3 files changed, 52 insertions(+), 40 deletions(-)
 create mode 100644 clang/test/SemaCXX/PR91677.cpp

diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index 7e8b080a347e8..6ef2eec09ec02 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -29,10 +29,8 @@
 #include "clang/AST/StmtObjC.h"
 #include "clang/AST/StmtOpenACC.h"
 #include "clang/AST/StmtOpenMP.h"
-#include "clang/AST/TypeLoc.h"
 #include "clang/Basic/DiagnosticParse.h"
 #include "clang/Basic/OpenMPKinds.h"
-#include "clang/Sema/DeclSpec.h"
 #include "clang/Sema/Designator.h"
 #include "clang/Sema/EnterExpressionEvaluationContext.h"
 #include "clang/Sema/Lookup.h"
@@ -7219,48 +7217,30 @@ 
TreeTransform::TransformElaboratedType(TypeLocBuilder ,
   }
 
   QualType NamedT;
-  if (0 && SemaRef.getLangOpts().CPlusPlus20 && QualifierLoc && 
isa(TL.getNamedTypeLoc().getType())) {
-const TemplateSpecializationType *TST = 
TL.getNamedTypeLoc().getType()->getAs();
+  if (SemaRef.getLangOpts().CPlusPlus20 && QualifierLoc &&
+  isa(TL.getNamedTypeLoc().getType())) {
 TemplateSpecializationTypeLoc SpecTL =
 TL.getNamedTypeLoc().castAs();
-// TemplateArgumentListInfo NewTemplateArgs;
-// NewTemplateArgs.setLAngleLoc(SpecTL.getLAngleLoc());
-// NewTemplateArgs.setRAngleLoc(SpecTL.getRAngleLoc());
-
-// typedef 

[clang] [Clang][Sema] Use correct TemplateName when transforming TemplateSpecializationType (PR #93411)

2024-05-26 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky created 
https://github.com/llvm/llvm-project/pull/93411

Consider the following testcase:
```cpp
namespace PR12884_original {
  template  struct A {
struct B { ##1
  template  struct X {};
  typedef int arg;
};
struct C {
  typedef B::X x; 
};
  };

  template <> struct A::B { ##2
template  struct X {};
static const int arg = 0;
  };

  A::C::x a;
}
```
It will crash when compiling with `clang(assertions trunk)`. The reason is that 
we lookup `X`(`B::X`) in `##1` when instantiating `typedef B::X x; ` during instantiating `A::C::x`. This is incorrect because we 
should lookup `X` in `##2` when we see the declaration `A::C::x a;`. Since 
clang parse `A::B` to an `ElaboratedType`(`typename` is not required 
while compiling with `-std=c++20`)  while `typename A::B` turns to be a 
`DependentTemplateSpecializationType`, we should rebuild the `TemplateName` 
with transformed `Qualifier`(whose type is `NestedNameSpecifier`) to make sure 
the lookup context is correct.
This patch also attempts to fix #91677 which crashes with the same reason.

>From ee56373184dff3fd721709ca07a1770957eace29 Mon Sep 17 00:00:00 2001
From: Qizhi Hu <836744...@qq.com>
Date: Sat, 25 May 2024 16:30:27 +0800
Subject: [PATCH 1/2] [Clang][Sema] Use correct TemplateName when transforming
 TemplateSpecializationType

---
 clang/lib/Sema/TreeTransform.h | 43 +-
 1 file changed, 42 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index dee335b526991..7e8b080a347e8 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -29,8 +29,10 @@
 #include "clang/AST/StmtObjC.h"
 #include "clang/AST/StmtOpenACC.h"
 #include "clang/AST/StmtOpenMP.h"
+#include "clang/AST/TypeLoc.h"
 #include "clang/Basic/DiagnosticParse.h"
 #include "clang/Basic/OpenMPKinds.h"
+#include "clang/Sema/DeclSpec.h"
 #include "clang/Sema/Designator.h"
 #include "clang/Sema/EnterExpressionEvaluationContext.h"
 #include "clang/Sema/Lookup.h"
@@ -7216,7 +7218,46 @@ 
TreeTransform::TransformElaboratedType(TypeLocBuilder ,
   return QualType();
   }
 
-  QualType NamedT = getDerived().TransformType(TLB, TL.getNamedTypeLoc());
+  QualType NamedT;
+  if (0 && SemaRef.getLangOpts().CPlusPlus20 && QualifierLoc && 
isa(TL.getNamedTypeLoc().getType())) {
+const TemplateSpecializationType *TST = 
TL.getNamedTypeLoc().getType()->getAs();
+TemplateSpecializationTypeLoc SpecTL =
+TL.getNamedTypeLoc().castAs();
+// TemplateArgumentListInfo NewTemplateArgs;
+// NewTemplateArgs.setLAngleLoc(SpecTL.getLAngleLoc());
+// NewTemplateArgs.setRAngleLoc(SpecTL.getRAngleLoc());
+
+// typedef TemplateArgumentLocContainerIterator<
+// TemplateSpecializationTypeLoc> ArgIterator;
+// if (getDerived().TransformTemplateArguments(ArgIterator(SpecTL, 0),
+// ArgIterator(SpecTL, 
SpecTL.getNumArgs()),
+// NewTemplateArgs))
+//   return QualType();
+
+CXXScopeSpec SS;
+SS.Adopt(QualifierLoc);
+TemplateName InstName = getDerived().RebuildTemplateName(
+SS, TL.getTemplateKeywordLoc(), 
*TST->getTemplateName().getAsTemplateDecl()->getIdentifier(), 
TL.getNamedTypeLoc().getBeginLoc(), QualType(), nullptr,
+false);
+
+if (InstName.isNull())
+  return QualType();
+
+// If it's still dependent, make a dependent specialization.
+// if (InstName.getAsDependentTemplateName())
+//   return SemaRef.Context.getDependentTemplateSpecializationType(
+//   Keyword, QualifierLoc.getNestedNameSpecifier(), Name,
+//   Args.arguments());
+
+// Otherwise, make an elaborated type wrapping a non-dependent
+// specialization.
+// NamedT = getDerived().RebuildTemplateSpecializationType(InstName, 
TL.getNamedTypeLoc().getBeginLoc(), NewTemplateArgs);
+NamedT = TransformTemplateSpecializationType(TLB, SpecTL, InstName);
+  } else {
+NamedT = getDerived().TransformType(TLB, TL.getNamedTypeLoc());
+
+  }
+
   if (NamedT.isNull())
 return QualType();
 

>From cb29d45c174f4fd267c273c4386a2010941a63ac Mon Sep 17 00:00:00 2001
From: Qizhi Hu <836744...@qq.com>
Date: Sun, 26 May 2024 19:35:17 +0800
Subject: [PATCH 2/2] [Clang][Sema] Use correct TemplateName when transforming
 TemplateSpecializationType

---
 clang/lib/Sema/TreeTransform.h| 54 ++-
 clang/test/SemaCXX/PR91677.cpp| 31 +++
 .../SemaTemplate/typename-specifier-3.cpp |  7 +--
 3 files changed, 52 insertions(+), 40 deletions(-)
 create mode 100644 clang/test/SemaCXX/PR91677.cpp

diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index 7e8b080a347e8..6ef2eec09ec02 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -29,10 +29,8 @@
 #include "clang/AST/StmtObjC.h"
 

[clang] [clang][ASTImporter] Fix possible crash "given incorrect InsertPos for specialization". (PR #89887)

2024-05-25 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky approved this pull request.


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


[clang] [clang][ASTImporter] Fix possible crash "given incorrect InsertPos for specialization". (PR #89887)

2024-05-25 Thread Qizhi Hu via cfe-commits

jcsxky wrote:

> > Could you please show your commands which reproduced this crash? I tested 
> > locally with the following commands and it runs OK.
> > ```c++
> > clang++ -cc1 -std=c++17 -emit-pch -o test.cpp.ast test.cpp
> > clang++ -cc1 -x c++ -ast-merge test.cpp.ast  /dev/null -ast-dump
> > ```
> 
> That code is only an example, it differs not much of the real code that 
> caused the crash. But it is not enough to use this code for the problem 
> reproduction. This code can be used to get the case when the specialization 
> list is changed before the insertion, but even then no crash happens:
> 
> ```c++
> namespace N {
> template 
> int B = B + B;
> template <>
> int B<0> = 0;
> template <>
> int B<1> = 1;
> }
> int A = N::B<5>;
> ```
> 
> With clang version 18.1.6 the original crash does not occur any more. The 
> "original crash" was reproduced on specific source files of project 
> "contour". Probably I can attach the files and command, but some changes can 
> be required to make it work.

Could you please show me how to reproduce the crash in detail if possible? Then 
I can debug this issue in my spare time.

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


[clang] [clang][ASTImporter] Fix possible crash "given incorrect InsertPos for specialization". (PR #89887)

2024-05-24 Thread Qizhi Hu via cfe-commits

jcsxky wrote:

> A test is needed to make the change acceptable but I could not find an easy 
> case to provoke the situation. The problem looks to be related to his code:
> 
> ```c++
> using size_t = int;
> template class tuple;
> 
> template
> struct integral_constant
> {
> static constexpr T value = v;
> using value_type = T;
> using type = integral_constant; // using injected-class-name
> constexpr operator value_type() const noexcept { return value; }
> constexpr value_type operator()() const noexcept { return value; } // 
> since c++14
> };
> 
> using true_type = integral_constant;
> using false_type = integral_constant;
> 
> template
> struct is_same : false_type {};
>  
> template
> struct is_same : true_type {};
> 
> template< class T, class U >
> inline constexpr bool is_same_v = is_same::value;
> 
> namespace A {
>   template
>   struct __tuple_count;
> 
>   template
> inline constexpr size_t __tuple_count_v =
>   __tuple_count<_Tp, _Tuple>::value;
> 
>   template
> struct __tuple_count<_Tp, tuple<_Types...>>
> : integral_constant { };
> 
>   template
> struct __tuple_count<_Tp, tuple<_First, _Rest...>>
> : integral_constant<
>   size_t,
>   __tuple_count_v<_Tp, tuple<_Rest...>> + is_same_v<_Tp, _First>> { };
> };
> 
> 
> size_t x = A::__tuple_count_v>;
> ```

Could you please show your commands which reproduced this crash? I tested 
locally with the following commands and it runs OK.
```cpp
clang++ -cc1 -std=c++17 -emit-pch -o test.cpp.ast test.cpp
clang++ -cc1 -x c++ -ast-merge test.cpp.ast  /dev/null -ast-dump
```

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


[clang] [Clang][AST] fix crash in mangle lambda expression (PR #78896)

2024-05-19 Thread Qizhi Hu via cfe-commits

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


[clang] [clang] visit constraint of NTTP (PR #91842)

2024-05-16 Thread Qizhi Hu via cfe-commits

jcsxky wrote:

> I don't think this is the right approach. I stepped though the example and 
> the reason we reject is because:
> 
> * We substitute a dependent `AutoType` in for the types of the template 
> parameters when they are initially built.
> * We call `getMoreSpecialized` determine whether the partial specialization 
> is more specialized than the primary.
> * We determine that neither template is at least as specialized as the other 
> via `isAtLeastAsSpecializedAs`.
> * We call `TemplateParameterListsAreEqual` per [[temp.func.order] 
> p6.2.2](http://eel.is/c++draft/temp.func.order#6.2.2) to check for template 
> parameter equivalence, and compare the two template parameters by calling 
> `MatchTemplateParameterKind`.
> * `MatchTemplateParameterKind` calls `ASTContext::getUnconstrainedType` to 
> get the unconstrained type of the template parameters per [[temp.over.link] 
> p6 sentence 2](http://eel.is/c++draft/temp.over.link#6.sentence-2). For the 
> class templates template parameter, it returns the type unchanged (a 
> _**dependent**_ `AutoType`). For the class template partial specializations 
> template parameter, it returns an unconstrained `AutoType` _**that isn't 
> dependent**_.
> * We compare the adjusted types and determine they aren't equal, so we 
> consider neither template to be more specialized than the other.
> 
> So, I think the correct fix is to propagate dependence in 
> `ASTContext::getUnconstrainedType`. I have a branch that implements this 
> [here](https://github.com/sdkrystian/llvm-project/tree/partial-spec-dependent-auto).
>  WDYT @erichkeane @cor3ntin @zyn0217?

This is really a perfect approach and it has addressed the underlying issue. 
And thanks for your explaination! 

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


[clang] [clang] visit constraint of NTTP (PR #91842)

2024-05-12 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky updated 
https://github.com/llvm/llvm-project/pull/91842

>From 0ebdcec675c39b26b8bee1a8b07c12fae2c58523 Mon Sep 17 00:00:00 2001
From: huqizhi 
Date: Sat, 11 May 2024 14:04:23 +0800
Subject: [PATCH] [clang] visit constraint of NTTP

---
 clang/docs/ReleaseNotes.rst   |  1 +
 clang/lib/AST/StmtProfile.cpp |  4 
 .../temp.fct/temp.func.order/p6.cpp   |  6 +++---
 clang/test/SemaCXX/PR77377.cpp| 19 +++
 4 files changed, 27 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/SemaCXX/PR77377.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 7c5dcc59c7016..30d359c582d3f 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -707,6 +707,7 @@ Bug Fixes to C++ Support
   initialized, rather than evaluating them as a part of the larger manifestly 
constant evaluated
   expression.
 - Fix a bug in access control checking due to dealyed checking of friend 
declaration. Fixes (#GH12361).
+- Fix a bug on template class partial specialization due to traverse of 
constraint of NTTP. Fixes (#GH77377).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/lib/AST/StmtProfile.cpp b/clang/lib/AST/StmtProfile.cpp
index 8fb8940142eb0..a23a2efa2389e 100644
--- a/clang/lib/AST/StmtProfile.cpp
+++ b/clang/lib/AST/StmtProfile.cpp
@@ -2257,6 +2257,10 @@ void StmtProfiler::VisitSubstNonTypeTemplateParmExpr(
 const SubstNonTypeTemplateParmExpr *E) {
   // Profile exactly as the replacement expression.
   Visit(E->getReplacement());
+  if (auto *NTTP = dyn_cast(E->getParameter());
+  NTTP && NTTP->getPlaceholderTypeConstraint()) {
+Visit(NTTP->getPlaceholderTypeConstraint());
+  }
 }
 
 void StmtProfiler::VisitFunctionParmPackExpr(const FunctionParmPackExpr *S) {
diff --git a/clang/test/CXX/temp/temp.decls/temp.fct/temp.func.order/p6.cpp 
b/clang/test/CXX/temp/temp.decls/temp.fct/temp.func.order/p6.cpp
index 9f44878da6254..5f9719a2dc561 100644
--- a/clang/test/CXX/temp/temp.decls/temp.fct/temp.func.order/p6.cpp
+++ b/clang/test/CXX/temp/temp.decls/temp.fct/temp.func.order/p6.cpp
@@ -79,14 +79,14 @@ template struct Y2 {}; // 
expected-note {{partial
 template class U, typename... Z>
 struct Y3 { Y3()=delete; };
 template class U, typename... Z>
-struct Y3 { Y3()=delete; };
+struct Y3 { Y3()=delete; }; // expected-note {{partial 
specialization matches [with T = int, I = 1, W = 1, S = A{}, U = S, Z = ]}}
 template class U, typename... Z>
-struct Y3 {};
+struct Y3 {}; // expected-note {{partial specialization 
matches [with T = int, I = 1, W = 1, S = A{}, U = S, Z = ]}}
 
 void f() {
   Y1 a;
   Y2 b; // expected-error {{ambiguous partial specializations}}
-  Y3 c;
+  Y3 c; // expected-error {{ambiguous partial 
specializations of 'Y3'}}
 }
 
 // Per [temp.func.order]p6.2.2, specifically "if the function parameters that
diff --git a/clang/test/SemaCXX/PR77377.cpp b/clang/test/SemaCXX/PR77377.cpp
new file mode 100644
index 0..ae34809c3903d
--- /dev/null
+++ b/clang/test/SemaCXX/PR77377.cpp
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 -verify -std=c++20 -fsyntax-only %s
+// expected-no-diagnostics
+
+template
+constexpr bool is_same_v = false;
+
+template
+constexpr bool is_same_v = true;
+
+template
+concept same_as = is_same_v;
+
+template 
+struct A {};
+
+template  auto p>
+struct A {};
+
+A<0> a;

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


[clang] [clang] visit constraint of NTTP (PR #91842)

2024-05-12 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky updated 
https://github.com/llvm/llvm-project/pull/91842

>From 4133001711b82c93e057e1bd05476c5d052d597f Mon Sep 17 00:00:00 2001
From: huqizhi 
Date: Sat, 11 May 2024 14:04:23 +0800
Subject: [PATCH] [clang] visit constraint of NTTP

---
 clang/docs/ReleaseNotes.rst   |  1 +
 clang/lib/AST/StmtProfile.cpp |  4 
 .../temp.fct/temp.func.order/p6.cpp   |  6 +++---
 clang/test/SemaCXX/PR77377.cpp| 19 +++
 4 files changed, 27 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/SemaCXX/PR77377.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 7c5dcc59c7016..30d359c582d3f 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -707,6 +707,7 @@ Bug Fixes to C++ Support
   initialized, rather than evaluating them as a part of the larger manifestly 
constant evaluated
   expression.
 - Fix a bug in access control checking due to dealyed checking of friend 
declaration. Fixes (#GH12361).
+- Fix a bug on template class partial specialization due to traverse of 
constraint of NTTP. Fixes (#GH77377).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/lib/AST/StmtProfile.cpp b/clang/lib/AST/StmtProfile.cpp
index 8fb8940142eb0..a23a2efa2389e 100644
--- a/clang/lib/AST/StmtProfile.cpp
+++ b/clang/lib/AST/StmtProfile.cpp
@@ -2257,6 +2257,10 @@ void StmtProfiler::VisitSubstNonTypeTemplateParmExpr(
 const SubstNonTypeTemplateParmExpr *E) {
   // Profile exactly as the replacement expression.
   Visit(E->getReplacement());
+  if (auto *NTTP = dyn_cast(E->getParameter());
+  NTTP && NTTP->getPlaceholderTypeConstraint()) {
+Visit(NTTP->getPlaceholderTypeConstraint());
+  }
 }
 
 void StmtProfiler::VisitFunctionParmPackExpr(const FunctionParmPackExpr *S) {
diff --git a/clang/test/CXX/temp/temp.decls/temp.fct/temp.func.order/p6.cpp 
b/clang/test/CXX/temp/temp.decls/temp.fct/temp.func.order/p6.cpp
index 9f44878da6254..5f9719a2dc561 100644
--- a/clang/test/CXX/temp/temp.decls/temp.fct/temp.func.order/p6.cpp
+++ b/clang/test/CXX/temp/temp.decls/temp.fct/temp.func.order/p6.cpp
@@ -79,14 +79,14 @@ template struct Y2 {}; // 
expected-note {{partial
 template class U, typename... Z>
 struct Y3 { Y3()=delete; };
 template class U, typename... Z>
-struct Y3 { Y3()=delete; };
+struct Y3 { Y3()=delete; }; // expected-note {{partial 
specialization matches [with T = int, I = 1, W = 1, S = A{}, U = S, Z = ]}}
 template class U, typename... Z>
-struct Y3 {};
+struct Y3 {}; // expected-note {{partial specialization 
matches [with T = int, I = 1, W = 1, S = A{}, U = S, Z = ]}}
 
 void f() {
   Y1 a;
   Y2 b; // expected-error {{ambiguous partial specializations}}
-  Y3 c;
+  Y3 c; // expected-error {{ambiguous partial 
specializations of 'Y3'}}
 }
 
 // Per [temp.func.order]p6.2.2, specifically "if the function parameters that
diff --git a/clang/test/SemaCXX/PR77377.cpp b/clang/test/SemaCXX/PR77377.cpp
new file mode 100644
index 0..ae34809c3903d
--- /dev/null
+++ b/clang/test/SemaCXX/PR77377.cpp
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 -verify -std=c++20 -fsyntax-only %s
+// expected-no-diagnostics
+
+template
+constexpr bool is_same_v = false;
+
+template
+constexpr bool is_same_v = true;
+
+template
+concept same_as = is_same_v;
+
+template 
+struct A {};
+
+template  auto p>
+struct A {};
+
+A<0> a;

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


[clang] [clang] visit constraint of NTTP (PR #91842)

2024-05-11 Thread Qizhi Hu via cfe-commits


@@ -79,14 +79,14 @@ template struct Y2 {}; // 
expected-note {{partial
 template class U, typename... Z>
 struct Y3 { Y3()=delete; };
 template class U, typename... Z>
-struct Y3 { Y3()=delete; };
+struct Y3 { Y3()=delete; }; // expected-note {{partial 
specialization matches [with T = int, I = 1, W = 1, S = A{}, U = S, Z = ]}}
 template class U, typename... Z>
-struct Y3 {};
+struct Y3 {}; // expected-note {{partial specialization 
matches [with T = int, I = 1, W = 1, S = A{}, U = S, Z = ]}}
 
 void f() {
   Y1 a;
   Y2 b; // expected-error {{ambiguous partial specializations}}
-  Y3 c;
+  Y3 c; // expected-error {{ambiguous partial 
specializations of 'Y3'}}

jcsxky wrote:

When instantiation, we are checking which one of the two partial specialization 
is more specialized. Obviously, the first one(`auto D`) is not more specialized 
than the second(`auto E`). When applied this patch, the second one is not more 
specialized than the first as well. This is because `isSameTemplateArg` return 
`false` and the result is not `TemplateDeductionResult::Success`.
Although we get correct result, it is not because of ignoring the 
type-constraint. Back to the quote, if we ignore the use of type-constraints 
for placeholder types, is the following example ill-formed due to their 
equivalent template arguments?
```cpp
template  constexpr bool True = true;
template  concept C = True;
template  concept D = C && sizeof(T) > 2;
template  concept E = D && alignof(T) > 1;

template
struct Y3 { Y3()=delete; };

template
struct Y3 { Y3()=delete; }; 

template
struct Y3 {};
```
But EDG, gcc and MSVC all accept this code. So I think the existing test is 
rejected may not be related to the quote. WDYT? @cor3ntin @zyn0217 @erichkeane 

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


[clang] [clang] visit constraint of NTTP (PR #91842)

2024-05-11 Thread Qizhi Hu via cfe-commits

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


[clang] [clang] visit constraint of NTTP (PR #91842)

2024-05-11 Thread Qizhi Hu via cfe-commits


@@ -79,14 +79,14 @@ template struct Y2 {}; // 
expected-note {{partial
 template class U, typename... Z>
 struct Y3 { Y3()=delete; };
 template class U, typename... Z>
-struct Y3 { Y3()=delete; };
+struct Y3 { Y3()=delete; }; // expected-note {{partial 
specialization matches [with T = int, I = 1, W = 1, S = A{}, U = S, Z = ]}}
 template class U, typename... Z>
-struct Y3 {};
+struct Y3 {}; // expected-note {{partial specialization 
matches [with T = int, I = 1, W = 1, S = A{}, U = S, Z = ]}}
 
 void f() {
   Y1 a;
   Y2 b; // expected-error {{ambiguous partial specializations}}
-  Y3 c;
+  Y3 c; // expected-error {{ambiguous partial 
specializations of 'Y3'}}

jcsxky wrote:

I think the quote can explain why we should report error here. Maybe more work 
are required to find out where we do the ignore and how this patch impacts this 
case.

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


[clang] [clang] visit constraint of NTTP (PR #91842)

2024-05-11 Thread Qizhi Hu via cfe-commits

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


[clang] [clang] visit constraint of NTTP (PR #91842)

2024-05-11 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky created 
https://github.com/llvm/llvm-project/pull/91842

attempt to fix 
`isSameTemplateArg` returns incorrect result since clang didn't visit 
constraint of NTTP. Furthermore,  It makes class template partial 
specialization not more specialized than the class template itself.

>From ddc0fbb7a249fbc6f2d2795fda262b2e7ca0a4e9 Mon Sep 17 00:00:00 2001
From: huqizhi 
Date: Sat, 11 May 2024 14:04:23 +0800
Subject: [PATCH] [clang] visit constraint of NTTP

---
 clang/docs/ReleaseNotes.rst   |  1 +
 clang/lib/AST/StmtProfile.cpp |  5 +
 .../temp.fct/temp.func.order/p6.cpp   |  6 +++---
 clang/test/SemaCXX/PR77377.cpp| 19 +++
 4 files changed, 28 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/SemaCXX/PR77377.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 7c5dcc59c7016..30d359c582d3f 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -707,6 +707,7 @@ Bug Fixes to C++ Support
   initialized, rather than evaluating them as a part of the larger manifestly 
constant evaluated
   expression.
 - Fix a bug in access control checking due to dealyed checking of friend 
declaration. Fixes (#GH12361).
+- Fix a bug on template class partial specialization due to traverse of 
constraint of NTTP. Fixes (#GH77377).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/lib/AST/StmtProfile.cpp b/clang/lib/AST/StmtProfile.cpp
index 8fb8940142eb0..0bb5a40d7350e 100644
--- a/clang/lib/AST/StmtProfile.cpp
+++ b/clang/lib/AST/StmtProfile.cpp
@@ -2257,6 +2257,11 @@ void StmtProfiler::VisitSubstNonTypeTemplateParmExpr(
 const SubstNonTypeTemplateParmExpr *E) {
   // Profile exactly as the replacement expression.
   Visit(E->getReplacement());
+  if (auto *NTTP =
+  dyn_cast_if_present(E->getParameter());
+  NTTP && NTTP->getPlaceholderTypeConstraint()) {
+Visit(NTTP->getPlaceholderTypeConstraint());
+  }
 }
 
 void StmtProfiler::VisitFunctionParmPackExpr(const FunctionParmPackExpr *S) {
diff --git a/clang/test/CXX/temp/temp.decls/temp.fct/temp.func.order/p6.cpp 
b/clang/test/CXX/temp/temp.decls/temp.fct/temp.func.order/p6.cpp
index 9f44878da6254..5f9719a2dc561 100644
--- a/clang/test/CXX/temp/temp.decls/temp.fct/temp.func.order/p6.cpp
+++ b/clang/test/CXX/temp/temp.decls/temp.fct/temp.func.order/p6.cpp
@@ -79,14 +79,14 @@ template struct Y2 {}; // 
expected-note {{partial
 template class U, typename... Z>
 struct Y3 { Y3()=delete; };
 template class U, typename... Z>
-struct Y3 { Y3()=delete; };
+struct Y3 { Y3()=delete; }; // expected-note {{partial 
specialization matches [with T = int, I = 1, W = 1, S = A{}, U = S, Z = ]}}
 template class U, typename... Z>
-struct Y3 {};
+struct Y3 {}; // expected-note {{partial specialization 
matches [with T = int, I = 1, W = 1, S = A{}, U = S, Z = ]}}
 
 void f() {
   Y1 a;
   Y2 b; // expected-error {{ambiguous partial specializations}}
-  Y3 c;
+  Y3 c; // expected-error {{ambiguous partial 
specializations of 'Y3'}}
 }
 
 // Per [temp.func.order]p6.2.2, specifically "if the function parameters that
diff --git a/clang/test/SemaCXX/PR77377.cpp b/clang/test/SemaCXX/PR77377.cpp
new file mode 100644
index 0..ae34809c3903d
--- /dev/null
+++ b/clang/test/SemaCXX/PR77377.cpp
@@ -0,0 +1,19 @@
+// RUN: %clang_cc1 -verify -std=c++20 -fsyntax-only %s
+// expected-no-diagnostics
+
+template
+constexpr bool is_same_v = false;
+
+template
+constexpr bool is_same_v = true;
+
+template
+concept same_as = is_same_v;
+
+template 
+struct A {};
+
+template  auto p>
+struct A {};
+
+A<0> a;

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


[clang] [Clang][Sema] access checking of friend declaration should not be delayed (PR #91430)

2024-05-10 Thread Qizhi Hu via cfe-commits

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


[clang] [Clang][Sema] access checking of friend declaration should not be delayed (PR #91430)

2024-05-09 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky updated 
https://github.com/llvm/llvm-project/pull/91430

>From 002751420c7f71fa9903d94e135565793bd8c6f8 Mon Sep 17 00:00:00 2001
From: Qizhi Hu <836744...@qq.com>
Date: Tue, 7 May 2024 22:32:39 +0800
Subject: [PATCH 1/2] [Clang][Sema] access checking of friend declaration
 should not be delayed

---
 clang/include/clang/Sema/Scope.h |  6 ++
 clang/lib/Parse/ParseDecl.cpp|  7 +--
 clang/lib/Sema/Scope.cpp |  2 ++
 clang/lib/Sema/SemaAccess.cpp| 12 ++--
 clang/test/SemaCXX/PR12361.cpp   | 30 ++
 5 files changed, 53 insertions(+), 4 deletions(-)
 create mode 100644 clang/test/SemaCXX/PR12361.cpp

diff --git a/clang/include/clang/Sema/Scope.h b/clang/include/clang/Sema/Scope.h
index 1752a25111a77..084db73034219 100644
--- a/clang/include/clang/Sema/Scope.h
+++ b/clang/include/clang/Sema/Scope.h
@@ -159,6 +159,9 @@ class Scope {
 
 /// This is a scope of type alias declaration.
 TypeAliasScope = 0x2000,
+
+/// This is a scope of friend declaration.
+FriendScope = 0x4000,
   };
 
 private:
@@ -586,6 +589,9 @@ class Scope {
   /// Determine whether this scope is a type alias scope.
   bool isTypeAliasScope() const { return getFlags() & Scope::TypeAliasScope; }
 
+  /// Determine whether this scope is a friend scope.
+  bool isFriendScope() const { return getFlags() & Scope::FriendScope; }
+
   /// Returns if rhs has a higher scope depth than this.
   ///
   /// The caller is responsible for calling this only if one of the two scopes
diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index 2c11ae693c354..5b5fc02ad4023 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -4331,9 +4331,12 @@ void Parser::ParseDeclarationSpecifiers(
 
 // friend
 case tok::kw_friend:
-  if (DSContext == DeclSpecContext::DSC_class)
+  if (DSContext == DeclSpecContext::DSC_class) {
 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
-  else {
+Scope *CurS = getCurScope();
+if (!isInvalid && CurS)
+  CurS->setFlags(CurS->getFlags() | Scope::FriendScope);
+  } else {
 PrevSpec = ""; // not actually used by the diagnostic
 DiagID = diag::err_friend_invalid_in_context;
 isInvalid = true;
diff --git a/clang/lib/Sema/Scope.cpp b/clang/lib/Sema/Scope.cpp
index 11a41753a1bda..780aa898b1085 100644
--- a/clang/lib/Sema/Scope.cpp
+++ b/clang/lib/Sema/Scope.cpp
@@ -229,6 +229,8 @@ void Scope::dumpImpl(raw_ostream ) const {
   {ClassInheritanceScope, "ClassInheritanceScope"},
   {CatchScope, "CatchScope"},
   {OpenACCComputeConstructScope, "OpenACCComputeConstructScope"},
+  {TypeAliasScope, "TypeAliasScope"},
+  {FriendScope, "FriendScope"},
   };
 
   for (auto Info : FlagInfo) {
diff --git a/clang/lib/Sema/SemaAccess.cpp b/clang/lib/Sema/SemaAccess.cpp
index 6a707eeb66d01..72c6736bb6648 100644
--- a/clang/lib/Sema/SemaAccess.cpp
+++ b/clang/lib/Sema/SemaAccess.cpp
@@ -1477,8 +1477,16 @@ static Sema::AccessResult CheckAccess(Sema , 
SourceLocation Loc,
   //   void foo(A::private_type);
   //   void B::foo(A::private_type);
   if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
-S.DelayedDiagnostics.add(DelayedDiagnostic::makeAccess(Loc, Entity));
-return Sema::AR_delayed;
+Scope *TS = S.getCurScope();
+bool IsFriendDeclaration = false;
+while (TS && !IsFriendDeclaration) {
+  IsFriendDeclaration = TS->isFriendScope();
+  TS = TS->getParent();
+}
+if (!IsFriendDeclaration) {
+  S.DelayedDiagnostics.add(DelayedDiagnostic::makeAccess(Loc, Entity));
+  return Sema::AR_delayed;
+}
   }
 
   EffectiveContext EC(S.CurContext);
diff --git a/clang/test/SemaCXX/PR12361.cpp b/clang/test/SemaCXX/PR12361.cpp
new file mode 100644
index 0..95ceb45b7ba04
--- /dev/null
+++ b/clang/test/SemaCXX/PR12361.cpp
@@ -0,0 +1,30 @@
+ // RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
+ // RUN: %clang_cc1 -fsyntax-only -verify -std=c++17 %s
+ 
+class D {
+class E{
+class F{}; // expected-note{{implicitly declared private here}}
+friend  void foo(D::E::F& q);
+};
+friend  void foo(D::E::F& q); // expected-error{{'F' is a private member 
of 'D::E'}}
+};
+
+void foo(D::E::F& q) {}
+
+class D1 {
+class E1{
+class F1{}; // expected-note{{implicitly declared private here}}
+friend  D1::E1::F1 foo1();
+};
+friend  D1::E1::F1 foo1(); // expected-error{{'F1' is a private member of 
'D1::E1'}}
+};
+
+D1::E1::F1 foo1() { return D1::E1::F1(); }
+
+class D2 {
+class E2{
+class F2{};
+friend  void foo2();
+};
+friend  void foo2(){ D2::E2::F2 c;}
+};

>From 113c8508881babe25ab9d1daa9ab9a72018da124 Mon Sep 17 00:00:00 2001
From: Qizhi Hu <836744...@qq.com>
Date: Wed, 8 May 2024 21:22:01 +0800
Subject: [PATCH 2/2] apply reviews

---
 

[clang] [Clang][Sema] access checking of friend declaration should not be delayed (PR #91430)

2024-05-08 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky updated 
https://github.com/llvm/llvm-project/pull/91430

>From 324e4361b4cb1b17065b4dc7d4bdfa41fe781e7d Mon Sep 17 00:00:00 2001
From: Qizhi Hu <836744...@qq.com>
Date: Tue, 7 May 2024 22:32:39 +0800
Subject: [PATCH 1/2] [Clang][Sema] access checking of friend declaration
 should not be delayed

---
 clang/include/clang/Sema/Scope.h |  6 ++
 clang/lib/Parse/ParseDecl.cpp|  7 +--
 clang/lib/Sema/Scope.cpp |  2 ++
 clang/lib/Sema/SemaAccess.cpp| 12 ++--
 clang/test/SemaCXX/PR12361.cpp   | 30 ++
 5 files changed, 53 insertions(+), 4 deletions(-)
 create mode 100644 clang/test/SemaCXX/PR12361.cpp

diff --git a/clang/include/clang/Sema/Scope.h b/clang/include/clang/Sema/Scope.h
index 1752a25111a77..084db73034219 100644
--- a/clang/include/clang/Sema/Scope.h
+++ b/clang/include/clang/Sema/Scope.h
@@ -159,6 +159,9 @@ class Scope {
 
 /// This is a scope of type alias declaration.
 TypeAliasScope = 0x2000,
+
+/// This is a scope of friend declaration.
+FriendScope = 0x4000,
   };
 
 private:
@@ -586,6 +589,9 @@ class Scope {
   /// Determine whether this scope is a type alias scope.
   bool isTypeAliasScope() const { return getFlags() & Scope::TypeAliasScope; }
 
+  /// Determine whether this scope is a friend scope.
+  bool isFriendScope() const { return getFlags() & Scope::FriendScope; }
+
   /// Returns if rhs has a higher scope depth than this.
   ///
   /// The caller is responsible for calling this only if one of the two scopes
diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index 4e4b05b21383e..78a81c77f48c6 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -4332,9 +4332,12 @@ void Parser::ParseDeclarationSpecifiers(
 
 // friend
 case tok::kw_friend:
-  if (DSContext == DeclSpecContext::DSC_class)
+  if (DSContext == DeclSpecContext::DSC_class) {
 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
-  else {
+Scope *CurS = getCurScope();
+if (!isInvalid && CurS)
+  CurS->setFlags(CurS->getFlags() | Scope::FriendScope);
+  } else {
 PrevSpec = ""; // not actually used by the diagnostic
 DiagID = diag::err_friend_invalid_in_context;
 isInvalid = true;
diff --git a/clang/lib/Sema/Scope.cpp b/clang/lib/Sema/Scope.cpp
index 11a41753a1bda..780aa898b1085 100644
--- a/clang/lib/Sema/Scope.cpp
+++ b/clang/lib/Sema/Scope.cpp
@@ -229,6 +229,8 @@ void Scope::dumpImpl(raw_ostream ) const {
   {ClassInheritanceScope, "ClassInheritanceScope"},
   {CatchScope, "CatchScope"},
   {OpenACCComputeConstructScope, "OpenACCComputeConstructScope"},
+  {TypeAliasScope, "TypeAliasScope"},
+  {FriendScope, "FriendScope"},
   };
 
   for (auto Info : FlagInfo) {
diff --git a/clang/lib/Sema/SemaAccess.cpp b/clang/lib/Sema/SemaAccess.cpp
index 6a707eeb66d01..72c6736bb6648 100644
--- a/clang/lib/Sema/SemaAccess.cpp
+++ b/clang/lib/Sema/SemaAccess.cpp
@@ -1477,8 +1477,16 @@ static Sema::AccessResult CheckAccess(Sema , 
SourceLocation Loc,
   //   void foo(A::private_type);
   //   void B::foo(A::private_type);
   if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
-S.DelayedDiagnostics.add(DelayedDiagnostic::makeAccess(Loc, Entity));
-return Sema::AR_delayed;
+Scope *TS = S.getCurScope();
+bool IsFriendDeclaration = false;
+while (TS && !IsFriendDeclaration) {
+  IsFriendDeclaration = TS->isFriendScope();
+  TS = TS->getParent();
+}
+if (!IsFriendDeclaration) {
+  S.DelayedDiagnostics.add(DelayedDiagnostic::makeAccess(Loc, Entity));
+  return Sema::AR_delayed;
+}
   }
 
   EffectiveContext EC(S.CurContext);
diff --git a/clang/test/SemaCXX/PR12361.cpp b/clang/test/SemaCXX/PR12361.cpp
new file mode 100644
index 0..95ceb45b7ba04
--- /dev/null
+++ b/clang/test/SemaCXX/PR12361.cpp
@@ -0,0 +1,30 @@
+ // RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
+ // RUN: %clang_cc1 -fsyntax-only -verify -std=c++17 %s
+ 
+class D {
+class E{
+class F{}; // expected-note{{implicitly declared private here}}
+friend  void foo(D::E::F& q);
+};
+friend  void foo(D::E::F& q); // expected-error{{'F' is a private member 
of 'D::E'}}
+};
+
+void foo(D::E::F& q) {}
+
+class D1 {
+class E1{
+class F1{}; // expected-note{{implicitly declared private here}}
+friend  D1::E1::F1 foo1();
+};
+friend  D1::E1::F1 foo1(); // expected-error{{'F1' is a private member of 
'D1::E1'}}
+};
+
+D1::E1::F1 foo1() { return D1::E1::F1(); }
+
+class D2 {
+class E2{
+class F2{};
+friend  void foo2();
+};
+friend  void foo2(){ D2::E2::F2 c;}
+};

>From 97723ff0ec73ac107bc74f222852e4c12b727eb0 Mon Sep 17 00:00:00 2001
From: Qizhi Hu <836744...@qq.com>
Date: Wed, 8 May 2024 21:22:01 +0800
Subject: [PATCH 2/2] apply reviews

---
 

[clang] [Clang][Sema] access checking of friend declaration should not be delayed (PR #91430)

2024-05-08 Thread Qizhi Hu via cfe-commits


@@ -1477,8 +1477,16 @@ static Sema::AccessResult CheckAccess(Sema , 
SourceLocation Loc,
   //   void foo(A::private_type);
   //   void B::foo(A::private_type);
   if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
-S.DelayedDiagnostics.add(DelayedDiagnostic::makeAccess(Loc, Entity));
-return Sema::AR_delayed;
+Scope *TS = S.getCurScope();
+bool IsFriendDeclaration = false;
+while (TS && !IsFriendDeclaration) {
+  IsFriendDeclaration = TS->isFriendScope();
+  TS = TS->getParent();
+}
+if (!IsFriendDeclaration) {
+  S.DelayedDiagnostics.add(DelayedDiagnostic::makeAccess(Loc, Entity));
+  return Sema::AR_delayed;
+}

jcsxky wrote:

Added.

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


[clang] [Clang][Sema] access checking of friend declaration should not be delayed (PR #91430)

2024-05-08 Thread Qizhi Hu via cfe-commits


@@ -229,6 +229,8 @@ void Scope::dumpImpl(raw_ostream ) const {
   {ClassInheritanceScope, "ClassInheritanceScope"},
   {CatchScope, "CatchScope"},
   {OpenACCComputeConstructScope, "OpenACCComputeConstructScope"},
+  {TypeAliasScope, "TypeAliasScope"},

jcsxky wrote:

Removed. This will be done in a follow-up.

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


[clang] [Clang][Sema] access checking of friend declaration should not be delayed (PR #91430)

2024-05-08 Thread Qizhi Hu via cfe-commits


@@ -1477,8 +1477,16 @@ static Sema::AccessResult CheckAccess(Sema , 
SourceLocation Loc,
   //   void foo(A::private_type);

jcsxky wrote:

Done.

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


[clang] [Clang][Sema] access checking of friend declaration should not be delayed (PR #91430)

2024-05-08 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky updated 
https://github.com/llvm/llvm-project/pull/91430

>From ec4f21b7823a89a793b0799b22c6bbdb0bfb4c52 Mon Sep 17 00:00:00 2001
From: Qizhi Hu <836744...@qq.com>
Date: Tue, 7 May 2024 22:32:39 +0800
Subject: [PATCH 1/2] [Clang][Sema] access checking of friend declaration
 should not be delayed

---
 clang/include/clang/Sema/Scope.h |  6 ++
 clang/lib/Parse/ParseDecl.cpp|  7 +--
 clang/lib/Sema/Scope.cpp |  2 ++
 clang/lib/Sema/SemaAccess.cpp| 12 ++--
 clang/test/SemaCXX/PR12361.cpp   | 30 ++
 5 files changed, 53 insertions(+), 4 deletions(-)
 create mode 100644 clang/test/SemaCXX/PR12361.cpp

diff --git a/clang/include/clang/Sema/Scope.h b/clang/include/clang/Sema/Scope.h
index 1752a25111a77..084db73034219 100644
--- a/clang/include/clang/Sema/Scope.h
+++ b/clang/include/clang/Sema/Scope.h
@@ -159,6 +159,9 @@ class Scope {
 
 /// This is a scope of type alias declaration.
 TypeAliasScope = 0x2000,
+
+/// This is a scope of friend declaration.
+FriendScope = 0x4000,
   };
 
 private:
@@ -586,6 +589,9 @@ class Scope {
   /// Determine whether this scope is a type alias scope.
   bool isTypeAliasScope() const { return getFlags() & Scope::TypeAliasScope; }
 
+  /// Determine whether this scope is a friend scope.
+  bool isFriendScope() const { return getFlags() & Scope::FriendScope; }
+
   /// Returns if rhs has a higher scope depth than this.
   ///
   /// The caller is responsible for calling this only if one of the two scopes
diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index 4e4b05b21383e..78a81c77f48c6 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -4332,9 +4332,12 @@ void Parser::ParseDeclarationSpecifiers(
 
 // friend
 case tok::kw_friend:
-  if (DSContext == DeclSpecContext::DSC_class)
+  if (DSContext == DeclSpecContext::DSC_class) {
 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
-  else {
+Scope *CurS = getCurScope();
+if (!isInvalid && CurS)
+  CurS->setFlags(CurS->getFlags() | Scope::FriendScope);
+  } else {
 PrevSpec = ""; // not actually used by the diagnostic
 DiagID = diag::err_friend_invalid_in_context;
 isInvalid = true;
diff --git a/clang/lib/Sema/Scope.cpp b/clang/lib/Sema/Scope.cpp
index 11a41753a1bda..780aa898b1085 100644
--- a/clang/lib/Sema/Scope.cpp
+++ b/clang/lib/Sema/Scope.cpp
@@ -229,6 +229,8 @@ void Scope::dumpImpl(raw_ostream ) const {
   {ClassInheritanceScope, "ClassInheritanceScope"},
   {CatchScope, "CatchScope"},
   {OpenACCComputeConstructScope, "OpenACCComputeConstructScope"},
+  {TypeAliasScope, "TypeAliasScope"},
+  {FriendScope, "FriendScope"},
   };
 
   for (auto Info : FlagInfo) {
diff --git a/clang/lib/Sema/SemaAccess.cpp b/clang/lib/Sema/SemaAccess.cpp
index 6a707eeb66d01..72c6736bb6648 100644
--- a/clang/lib/Sema/SemaAccess.cpp
+++ b/clang/lib/Sema/SemaAccess.cpp
@@ -1477,8 +1477,16 @@ static Sema::AccessResult CheckAccess(Sema , 
SourceLocation Loc,
   //   void foo(A::private_type);
   //   void B::foo(A::private_type);
   if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
-S.DelayedDiagnostics.add(DelayedDiagnostic::makeAccess(Loc, Entity));
-return Sema::AR_delayed;
+Scope *TS = S.getCurScope();
+bool IsFriendDeclaration = false;
+while (TS && !IsFriendDeclaration) {
+  IsFriendDeclaration = TS->isFriendScope();
+  TS = TS->getParent();
+}
+if (!IsFriendDeclaration) {
+  S.DelayedDiagnostics.add(DelayedDiagnostic::makeAccess(Loc, Entity));
+  return Sema::AR_delayed;
+}
   }
 
   EffectiveContext EC(S.CurContext);
diff --git a/clang/test/SemaCXX/PR12361.cpp b/clang/test/SemaCXX/PR12361.cpp
new file mode 100644
index 0..95ceb45b7ba04
--- /dev/null
+++ b/clang/test/SemaCXX/PR12361.cpp
@@ -0,0 +1,30 @@
+ // RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
+ // RUN: %clang_cc1 -fsyntax-only -verify -std=c++17 %s
+ 
+class D {
+class E{
+class F{}; // expected-note{{implicitly declared private here}}
+friend  void foo(D::E::F& q);
+};
+friend  void foo(D::E::F& q); // expected-error{{'F' is a private member 
of 'D::E'}}
+};
+
+void foo(D::E::F& q) {}
+
+class D1 {
+class E1{
+class F1{}; // expected-note{{implicitly declared private here}}
+friend  D1::E1::F1 foo1();
+};
+friend  D1::E1::F1 foo1(); // expected-error{{'F1' is a private member of 
'D1::E1'}}
+};
+
+D1::E1::F1 foo1() { return D1::E1::F1(); }
+
+class D2 {
+class E2{
+class F2{};
+friend  void foo2();
+};
+friend  void foo2(){ D2::E2::F2 c;}
+};

>From 77cfb6f93c8c91cfbe5d3d1ed4e90450afa026fa Mon Sep 17 00:00:00 2001
From: Qizhi Hu <836744...@qq.com>
Date: Wed, 8 May 2024 21:22:01 +0800
Subject: [PATCH 2/2] apply reviews

---
 

[clang] [Clang][Sema] access checking of friend declaration should not be delayed (PR #91430)

2024-05-08 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky updated 
https://github.com/llvm/llvm-project/pull/91430

>From a4eee6db746e8de0743369f701d1b6a3fcc84754 Mon Sep 17 00:00:00 2001
From: Qizhi Hu <836744...@qq.com>
Date: Tue, 7 May 2024 22:32:39 +0800
Subject: [PATCH 1/2] [Clang][Sema] access checking of friend declaration
 should not be delayed

---
 clang/include/clang/Sema/Scope.h |  6 ++
 clang/lib/Parse/ParseDecl.cpp|  7 +--
 clang/lib/Sema/Scope.cpp |  2 ++
 clang/lib/Sema/SemaAccess.cpp| 12 ++--
 clang/test/SemaCXX/PR12361.cpp   | 30 ++
 5 files changed, 53 insertions(+), 4 deletions(-)
 create mode 100644 clang/test/SemaCXX/PR12361.cpp

diff --git a/clang/include/clang/Sema/Scope.h b/clang/include/clang/Sema/Scope.h
index 1752a25111a77..084db73034219 100644
--- a/clang/include/clang/Sema/Scope.h
+++ b/clang/include/clang/Sema/Scope.h
@@ -159,6 +159,9 @@ class Scope {
 
 /// This is a scope of type alias declaration.
 TypeAliasScope = 0x2000,
+
+/// This is a scope of friend declaration.
+FriendScope = 0x4000,
   };
 
 private:
@@ -586,6 +589,9 @@ class Scope {
   /// Determine whether this scope is a type alias scope.
   bool isTypeAliasScope() const { return getFlags() & Scope::TypeAliasScope; }
 
+  /// Determine whether this scope is a friend scope.
+  bool isFriendScope() const { return getFlags() & Scope::FriendScope; }
+
   /// Returns if rhs has a higher scope depth than this.
   ///
   /// The caller is responsible for calling this only if one of the two scopes
diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index 4e4b05b21383e..78a81c77f48c6 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -4332,9 +4332,12 @@ void Parser::ParseDeclarationSpecifiers(
 
 // friend
 case tok::kw_friend:
-  if (DSContext == DeclSpecContext::DSC_class)
+  if (DSContext == DeclSpecContext::DSC_class) {
 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
-  else {
+Scope *CurS = getCurScope();
+if (!isInvalid && CurS)
+  CurS->setFlags(CurS->getFlags() | Scope::FriendScope);
+  } else {
 PrevSpec = ""; // not actually used by the diagnostic
 DiagID = diag::err_friend_invalid_in_context;
 isInvalid = true;
diff --git a/clang/lib/Sema/Scope.cpp b/clang/lib/Sema/Scope.cpp
index 11a41753a1bda..780aa898b1085 100644
--- a/clang/lib/Sema/Scope.cpp
+++ b/clang/lib/Sema/Scope.cpp
@@ -229,6 +229,8 @@ void Scope::dumpImpl(raw_ostream ) const {
   {ClassInheritanceScope, "ClassInheritanceScope"},
   {CatchScope, "CatchScope"},
   {OpenACCComputeConstructScope, "OpenACCComputeConstructScope"},
+  {TypeAliasScope, "TypeAliasScope"},
+  {FriendScope, "FriendScope"},
   };
 
   for (auto Info : FlagInfo) {
diff --git a/clang/lib/Sema/SemaAccess.cpp b/clang/lib/Sema/SemaAccess.cpp
index 6a707eeb66d01..72c6736bb6648 100644
--- a/clang/lib/Sema/SemaAccess.cpp
+++ b/clang/lib/Sema/SemaAccess.cpp
@@ -1477,8 +1477,16 @@ static Sema::AccessResult CheckAccess(Sema , 
SourceLocation Loc,
   //   void foo(A::private_type);
   //   void B::foo(A::private_type);
   if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
-S.DelayedDiagnostics.add(DelayedDiagnostic::makeAccess(Loc, Entity));
-return Sema::AR_delayed;
+Scope *TS = S.getCurScope();
+bool IsFriendDeclaration = false;
+while (TS && !IsFriendDeclaration) {
+  IsFriendDeclaration = TS->isFriendScope();
+  TS = TS->getParent();
+}
+if (!IsFriendDeclaration) {
+  S.DelayedDiagnostics.add(DelayedDiagnostic::makeAccess(Loc, Entity));
+  return Sema::AR_delayed;
+}
   }
 
   EffectiveContext EC(S.CurContext);
diff --git a/clang/test/SemaCXX/PR12361.cpp b/clang/test/SemaCXX/PR12361.cpp
new file mode 100644
index 0..95ceb45b7ba04
--- /dev/null
+++ b/clang/test/SemaCXX/PR12361.cpp
@@ -0,0 +1,30 @@
+ // RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
+ // RUN: %clang_cc1 -fsyntax-only -verify -std=c++17 %s
+ 
+class D {
+class E{
+class F{}; // expected-note{{implicitly declared private here}}
+friend  void foo(D::E::F& q);
+};
+friend  void foo(D::E::F& q); // expected-error{{'F' is a private member 
of 'D::E'}}
+};
+
+void foo(D::E::F& q) {}
+
+class D1 {
+class E1{
+class F1{}; // expected-note{{implicitly declared private here}}
+friend  D1::E1::F1 foo1();
+};
+friend  D1::E1::F1 foo1(); // expected-error{{'F1' is a private member of 
'D1::E1'}}
+};
+
+D1::E1::F1 foo1() { return D1::E1::F1(); }
+
+class D2 {
+class E2{
+class F2{};
+friend  void foo2();
+};
+friend  void foo2(){ D2::E2::F2 c;}
+};

>From bce02c31f4b6601e7d031f74fa7e46d868a7d423 Mon Sep 17 00:00:00 2001
From: Qizhi Hu <836744...@qq.com>
Date: Wed, 8 May 2024 21:22:01 +0800
Subject: [PATCH 2/2] apply reviews

---
 

[clang] [Clang][Sema] access checking of friend declaration should not be delayed (PR #91430)

2024-05-08 Thread Qizhi Hu via cfe-commits

jcsxky wrote:

> Can you add a changelog entry?

Ah, I forgot that! will be added when applying other reviews.

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


[clang] [Clang][Sema] access checking of friend declaration should not be delayed (PR #91430)

2024-05-07 Thread Qizhi Hu via cfe-commits

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


[clang] [Clang][Sema] access checking of friend declaration should not be delayed (PR #91430)

2024-05-07 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky updated 
https://github.com/llvm/llvm-project/pull/91430

>From a4eee6db746e8de0743369f701d1b6a3fcc84754 Mon Sep 17 00:00:00 2001
From: Qizhi Hu <836744...@qq.com>
Date: Tue, 7 May 2024 22:32:39 +0800
Subject: [PATCH] [Clang][Sema] access checking of friend declaration should
 not be delayed

---
 clang/include/clang/Sema/Scope.h |  6 ++
 clang/lib/Parse/ParseDecl.cpp|  7 +--
 clang/lib/Sema/Scope.cpp |  2 ++
 clang/lib/Sema/SemaAccess.cpp| 12 ++--
 clang/test/SemaCXX/PR12361.cpp   | 30 ++
 5 files changed, 53 insertions(+), 4 deletions(-)
 create mode 100644 clang/test/SemaCXX/PR12361.cpp

diff --git a/clang/include/clang/Sema/Scope.h b/clang/include/clang/Sema/Scope.h
index 1752a25111a77..084db73034219 100644
--- a/clang/include/clang/Sema/Scope.h
+++ b/clang/include/clang/Sema/Scope.h
@@ -159,6 +159,9 @@ class Scope {
 
 /// This is a scope of type alias declaration.
 TypeAliasScope = 0x2000,
+
+/// This is a scope of friend declaration.
+FriendScope = 0x4000,
   };
 
 private:
@@ -586,6 +589,9 @@ class Scope {
   /// Determine whether this scope is a type alias scope.
   bool isTypeAliasScope() const { return getFlags() & Scope::TypeAliasScope; }
 
+  /// Determine whether this scope is a friend scope.
+  bool isFriendScope() const { return getFlags() & Scope::FriendScope; }
+
   /// Returns if rhs has a higher scope depth than this.
   ///
   /// The caller is responsible for calling this only if one of the two scopes
diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index 4e4b05b21383e..78a81c77f48c6 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -4332,9 +4332,12 @@ void Parser::ParseDeclarationSpecifiers(
 
 // friend
 case tok::kw_friend:
-  if (DSContext == DeclSpecContext::DSC_class)
+  if (DSContext == DeclSpecContext::DSC_class) {
 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
-  else {
+Scope *CurS = getCurScope();
+if (!isInvalid && CurS)
+  CurS->setFlags(CurS->getFlags() | Scope::FriendScope);
+  } else {
 PrevSpec = ""; // not actually used by the diagnostic
 DiagID = diag::err_friend_invalid_in_context;
 isInvalid = true;
diff --git a/clang/lib/Sema/Scope.cpp b/clang/lib/Sema/Scope.cpp
index 11a41753a1bda..780aa898b1085 100644
--- a/clang/lib/Sema/Scope.cpp
+++ b/clang/lib/Sema/Scope.cpp
@@ -229,6 +229,8 @@ void Scope::dumpImpl(raw_ostream ) const {
   {ClassInheritanceScope, "ClassInheritanceScope"},
   {CatchScope, "CatchScope"},
   {OpenACCComputeConstructScope, "OpenACCComputeConstructScope"},
+  {TypeAliasScope, "TypeAliasScope"},
+  {FriendScope, "FriendScope"},
   };
 
   for (auto Info : FlagInfo) {
diff --git a/clang/lib/Sema/SemaAccess.cpp b/clang/lib/Sema/SemaAccess.cpp
index 6a707eeb66d01..72c6736bb6648 100644
--- a/clang/lib/Sema/SemaAccess.cpp
+++ b/clang/lib/Sema/SemaAccess.cpp
@@ -1477,8 +1477,16 @@ static Sema::AccessResult CheckAccess(Sema , 
SourceLocation Loc,
   //   void foo(A::private_type);
   //   void B::foo(A::private_type);
   if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
-S.DelayedDiagnostics.add(DelayedDiagnostic::makeAccess(Loc, Entity));
-return Sema::AR_delayed;
+Scope *TS = S.getCurScope();
+bool IsFriendDeclaration = false;
+while (TS && !IsFriendDeclaration) {
+  IsFriendDeclaration = TS->isFriendScope();
+  TS = TS->getParent();
+}
+if (!IsFriendDeclaration) {
+  S.DelayedDiagnostics.add(DelayedDiagnostic::makeAccess(Loc, Entity));
+  return Sema::AR_delayed;
+}
   }
 
   EffectiveContext EC(S.CurContext);
diff --git a/clang/test/SemaCXX/PR12361.cpp b/clang/test/SemaCXX/PR12361.cpp
new file mode 100644
index 0..95ceb45b7ba04
--- /dev/null
+++ b/clang/test/SemaCXX/PR12361.cpp
@@ -0,0 +1,30 @@
+ // RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
+ // RUN: %clang_cc1 -fsyntax-only -verify -std=c++17 %s
+ 
+class D {
+class E{
+class F{}; // expected-note{{implicitly declared private here}}
+friend  void foo(D::E::F& q);
+};
+friend  void foo(D::E::F& q); // expected-error{{'F' is a private member 
of 'D::E'}}
+};
+
+void foo(D::E::F& q) {}
+
+class D1 {
+class E1{
+class F1{}; // expected-note{{implicitly declared private here}}
+friend  D1::E1::F1 foo1();
+};
+friend  D1::E1::F1 foo1(); // expected-error{{'F1' is a private member of 
'D1::E1'}}
+};
+
+D1::E1::F1 foo1() { return D1::E1::F1(); }
+
+class D2 {
+class E2{
+class F2{};
+friend  void foo2();
+};
+friend  void foo2(){ D2::E2::F2 c;}
+};

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


[clang] [Clang][Sema] access checking of friend declaration should not be delayed (PR #91430)

2024-05-07 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky created 
https://github.com/llvm/llvm-project/pull/91430

attempt to fix https://github.com/llvm/llvm-project/issues/12361
Consider this example:
```cpp
class D {
class E{
class F{}; // expected-note{{implicitly declared private here}}
friend  void foo(D::E::F& q);
};
friend  void foo(D::E::F& q);
};

void foo(D::E::F& q) {}
```
The first friend declaration of foo is correct. After that, the second  friend 
declaration delayed access checking and set its previous declaration to be the 
first one. When doing access checking of `F`(which is private filed of `E`), we 
put its canonical declaration(the first friend declaration) into 
`EffectiveContext.Functions`. Actually, we are still checking the first one. 
This is incorrect due to the delayed checking.
Creating a new scope to indicate we are parsing a friend declaration and doing 
access checking in time.

>From 836a66f221777e5c3bdd78087f82c2567854c10c Mon Sep 17 00:00:00 2001
From: Qizhi Hu <836744...@qq.com>
Date: Tue, 7 May 2024 22:32:39 +0800
Subject: [PATCH] [Clang][Sema] access checking of friend declaration should
 not be delayed

---
 clang/include/clang/Sema/Scope.h |  6 ++
 clang/lib/Parse/ParseDecl.cpp|  7 +--
 clang/lib/Sema/Scope.cpp |  2 ++
 clang/lib/Sema/SemaAccess.cpp|  8 +++-
 clang/test/SemaCXX/PR12361.cpp   | 30 ++
 5 files changed, 50 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/SemaCXX/PR12361.cpp

diff --git a/clang/include/clang/Sema/Scope.h b/clang/include/clang/Sema/Scope.h
index 1752a25111a77..084db73034219 100644
--- a/clang/include/clang/Sema/Scope.h
+++ b/clang/include/clang/Sema/Scope.h
@@ -159,6 +159,9 @@ class Scope {
 
 /// This is a scope of type alias declaration.
 TypeAliasScope = 0x2000,
+
+/// This is a scope of friend declaration.
+FriendScope = 0x4000,
   };
 
 private:
@@ -586,6 +589,9 @@ class Scope {
   /// Determine whether this scope is a type alias scope.
   bool isTypeAliasScope() const { return getFlags() & Scope::TypeAliasScope; }
 
+  /// Determine whether this scope is a friend scope.
+  bool isFriendScope() const { return getFlags() & Scope::FriendScope; }
+
   /// Returns if rhs has a higher scope depth than this.
   ///
   /// The caller is responsible for calling this only if one of the two scopes
diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index 4e4b05b21383e..78a81c77f48c6 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -4332,9 +4332,12 @@ void Parser::ParseDeclarationSpecifiers(
 
 // friend
 case tok::kw_friend:
-  if (DSContext == DeclSpecContext::DSC_class)
+  if (DSContext == DeclSpecContext::DSC_class) {
 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
-  else {
+Scope *CurS = getCurScope();
+if (!isInvalid && CurS)
+  CurS->setFlags(CurS->getFlags() | Scope::FriendScope);
+  } else {
 PrevSpec = ""; // not actually used by the diagnostic
 DiagID = diag::err_friend_invalid_in_context;
 isInvalid = true;
diff --git a/clang/lib/Sema/Scope.cpp b/clang/lib/Sema/Scope.cpp
index 11a41753a1bda..780aa898b1085 100644
--- a/clang/lib/Sema/Scope.cpp
+++ b/clang/lib/Sema/Scope.cpp
@@ -229,6 +229,8 @@ void Scope::dumpImpl(raw_ostream ) const {
   {ClassInheritanceScope, "ClassInheritanceScope"},
   {CatchScope, "CatchScope"},
   {OpenACCComputeConstructScope, "OpenACCComputeConstructScope"},
+  {TypeAliasScope, "TypeAliasScope"},
+  {FriendScope, "FriendScope"},
   };
 
   for (auto Info : FlagInfo) {
diff --git a/clang/lib/Sema/SemaAccess.cpp b/clang/lib/Sema/SemaAccess.cpp
index 6a707eeb66d01..ded88da510525 100644
--- a/clang/lib/Sema/SemaAccess.cpp
+++ b/clang/lib/Sema/SemaAccess.cpp
@@ -1476,7 +1476,13 @@ static Sema::AccessResult CheckAccess(Sema , 
SourceLocation Loc,
   // Or we might be parsing something that will turn out to be a friend:
   //   void foo(A::private_type);
   //   void B::foo(A::private_type);
-  if (S.DelayedDiagnostics.shouldDelayDiagnostics()) {
+  Scope *TS = S.getCurScope();
+  bool IsFriendDeclaration = false;
+  while (TS && !IsFriendDeclaration) {
+IsFriendDeclaration = TS->isFriendScope();
+TS = TS->getParent();
+  }
+  if (S.DelayedDiagnostics.shouldDelayDiagnostics() && !IsFriendDeclaration) {
 S.DelayedDiagnostics.add(DelayedDiagnostic::makeAccess(Loc, Entity));
 return Sema::AR_delayed;
   }
diff --git a/clang/test/SemaCXX/PR12361.cpp b/clang/test/SemaCXX/PR12361.cpp
new file mode 100644
index 0..95ceb45b7ba04
--- /dev/null
+++ b/clang/test/SemaCXX/PR12361.cpp
@@ -0,0 +1,30 @@
+ // RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
+ // RUN: %clang_cc1 -fsyntax-only -verify -std=c++17 %s
+ 
+class D {
+class E{
+class F{}; // expected-note{{implicitly declared private here}}
+friend  void 

[clang] [Clang][Sema] Improve support for explicit specializations of constrained member functions & member function templates (PR #88963)

2024-05-03 Thread Qizhi Hu via cfe-commits


@@ -275,6 +275,13 @@ Response HandleFunction(Sema , const FunctionDecl 
*Function,
  TemplateArgs->asArray(),
  /*Final=*/false);
 
+if (RelativeToPrimary &&
+(Function->getTemplateSpecializationKind() ==
+ TSK_ExplicitSpecialization ||

jcsxky wrote:

@sdkrystian but gcc fails on the testcase you provided.

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


[clang] [Clang][Sema] fix a bug on constraint check with template friend function (PR #90646)

2024-05-02 Thread Qizhi Hu via cfe-commits

jcsxky wrote:

> This still doesn't handle cases such as:
> 
> ```c++
> template
> concept E = sizeof(T) == sizeof(U) && sizeof(V) == sizeof(W);
> 
> template // T = char
> struct A
> {
> template // U = signed char
> struct B
> {
> template // V = int, W = int, X = 
> short
> requires E // incorrectly substitutes T = int, U = short, 
> V = int, V = int
> static void f();
> };
> };
> 
> 
> template // T = int, U = int
> struct C
> {
> template // V = short
> struct D
> {
> friend void A::B::f();
> };
> };
> 
> template struct C::D;
> 
> extern template void A::B::f();
> ```
> 
> (which is handled by 
> [be79079](https://github.com/llvm/llvm-project/commit/be79079507ffbd9b29683498f405dc2c32dd8ba7))

Thanks for your patience and testcases! They really make me understand these 
related issues more clearly.

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


[clang] [Clang][Sema] Improve support for explicit specializations of constrained member functions & member function templates (PR #88963)

2024-05-02 Thread Qizhi Hu via cfe-commits


@@ -275,6 +275,13 @@ Response HandleFunction(Sema , const FunctionDecl 
*Function,
  TemplateArgs->asArray(),
  /*Final=*/false);
 
+if (RelativeToPrimary &&
+(Function->getTemplateSpecializationKind() ==
+ TSK_ExplicitSpecialization ||

jcsxky wrote:

This testcase runs OK before 
[be79079](https://github.com/llvm/llvm-project/pull/88963/commits/be79079507ffbd9b29683498f405dc2c32dd8ba7)
 applied and it may not be related to the condition 
`Function->getTemplateSpecializationKind() == TSK_ExplicitSpecialization`.

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


[clang] [Clang][Sema] fix a bug on constraint check with template friend function (PR #90646)

2024-05-02 Thread Qizhi Hu via cfe-commits

jcsxky wrote:

> I agree with @sdkrystian, even though the test crashes for maybe yet another 
> reason, it demonstrates you can friend a function from a different template 
> context, so comparing the depths from different branches is not helpful.

@mizvekov I missed the case which friend function is declared in an inner 
class, and 
[3d27f11](https://github.com/llvm/llvm-project/pull/90646/commits/3d27f11d79b00608bf9135bcc4b4cb859027933a)
 demonstrates the comparison works fine.

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


[clang] [Clang][Sema] fix a bug on constraint check with template friend function (PR #90646)

2024-05-02 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky updated 
https://github.com/llvm/llvm-project/pull/90646

>From 50aa5b64f6d2cf98706bfb4792f274bd071e3b9c Mon Sep 17 00:00:00 2001
From: Qizhi Hu <836744...@qq.com>
Date: Wed, 1 May 2024 02:25:04 +0800
Subject: [PATCH 1/2] [Clang][Sema] fix a bug on constraint check with template
 friend function

---
 clang/docs/ReleaseNotes.rst|  1 +
 clang/lib/Sema/SemaTemplateInstantiate.cpp | 14 +++
 clang/test/SemaCXX/PR90349.cpp | 43 ++
 3 files changed, 58 insertions(+)
 create mode 100644 clang/test/SemaCXX/PR90349.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 2c5308fbcb319a..ac90e3933798cb 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -630,6 +630,7 @@ Bug Fixes to C++ Support
 - Fix a bug on template partial specialization with issue on deduction of 
nontype template parameter
   whose type is `decltype(auto)`. Fixes (#GH68885).
 - Clang now correctly treats the noexcept-specifier of a friend function to be 
a complete-class context.
+- Fix a bug on constraint check with template friend function. Fixes 
(#GH90349).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp 
b/clang/lib/Sema/SemaTemplateInstantiate.cpp
index 3a9fd906b7af86..1805f8f6e5ad90 100644
--- a/clang/lib/Sema/SemaTemplateInstantiate.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp
@@ -281,6 +281,20 @@ Response HandleFunction(Sema , const FunctionDecl 
*Function,
 if (Function->getPrimaryTemplate()->isMemberSpecialization())
   return Response::Done();
 
+if (Function->getFriendObjectKind())
+  if (const ClassTemplateSpecializationDecl *TD =
+  dyn_cast(
+  Function->getLexicalDeclContext())) {
+const CXXRecordDecl *TemplatePattern =
+TD->getTemplateInstantiationPattern();
+const FunctionDecl *FunctionPattern =
+Function->getTemplateInstantiationPattern();
+if (TemplatePattern && FunctionPattern &&
+TemplatePattern->getTemplateDepth() ==
+FunctionPattern->getTemplateDepth())
+  return Response::Done();
+  }
+
 // If this function is a generic lambda specialization, we are done.
 if (!ForConstraintInstantiation &&
 isGenericLambdaCallOperatorOrStaticInvokerSpecialization(Function)) {
diff --git a/clang/test/SemaCXX/PR90349.cpp b/clang/test/SemaCXX/PR90349.cpp
new file mode 100644
index 00..6a4b5c21e88f3b
--- /dev/null
+++ b/clang/test/SemaCXX/PR90349.cpp
@@ -0,0 +1,43 @@
+// RUN: %clang_cc1 -verify -std=c++20 -fsyntax-only %s
+
+// expected-no-diagnostics
+
+namespace std {
+template
+concept floating_point = __is_same(T,double) || __is_same(T,float);
+
+template
+concept integral = __is_same(T,int);
+
+}
+
+template
+class Blob;
+
+template
+Blob MakeBlob();
+
+template
+class Blob {
+private:
+Blob() {}
+
+friend Blob MakeBlob();
+};
+
+template
+Blob MakeBlob()
+{
+return Blob();
+}
+
+template
+Blob FindBlobs()
+{
+return MakeBlob();
+}
+
+int main(int argc, const char * argv[]) {
+FindBlobs();
+return 0;
+}

>From 3d27f11d79b00608bf9135bcc4b4cb859027933a Mon Sep 17 00:00:00 2001
From: Qizhi Hu <836744...@qq.com>
Date: Thu, 2 May 2024 20:05:10 +0800
Subject: [PATCH 2/2] continue to handle context instead of finish

---
 clang/lib/Sema/SemaTemplateInstantiate.cpp |  2 +-
 clang/test/SemaCXX/PR90349.cpp | 24 ++
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp 
b/clang/lib/Sema/SemaTemplateInstantiate.cpp
index 1805f8f6e5ad90..22789c72b2c90f 100644
--- a/clang/lib/Sema/SemaTemplateInstantiate.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp
@@ -292,7 +292,7 @@ Response HandleFunction(Sema , const FunctionDecl 
*Function,
 if (TemplatePattern && FunctionPattern &&
 TemplatePattern->getTemplateDepth() ==
 FunctionPattern->getTemplateDepth())
-  return Response::Done();
+  return Response::UseNextDecl(Function);
   }
 
 // If this function is a generic lambda specialization, we are done.
diff --git a/clang/test/SemaCXX/PR90349.cpp b/clang/test/SemaCXX/PR90349.cpp
index 6a4b5c21e88f3b..570a49fd2073bb 100644
--- a/clang/test/SemaCXX/PR90349.cpp
+++ b/clang/test/SemaCXX/PR90349.cpp
@@ -41,3 +41,27 @@ int main(int argc, const char * argv[]) {
 FindBlobs();
 return 0;
 }
+
+template
+concept D = sizeof(T) == sizeof(U);
+
+template
+struct A
+{
+template requires D
+static void f();
+};
+
+template
+struct B
+{
+template
+struct C
+{
+friend void A::f();
+};
+};
+
+template struct B::C;
+
+extern template void A::f(); // crash here

___
cfe-commits mailing list
cfe-commits@lists.llvm.org

[clang] [Clang][Sema] Improve support for explicit specializations of constrained member functions & member function templates (PR #88963)

2024-05-02 Thread Qizhi Hu via cfe-commits


@@ -275,6 +275,13 @@ Response HandleFunction(Sema , const FunctionDecl 
*Function,
  TemplateArgs->asArray(),
  /*Final=*/false);
 
+if (RelativeToPrimary &&
+(Function->getTemplateSpecializationKind() ==
+ TSK_ExplicitSpecialization ||

jcsxky wrote:

Is this condition `Function->getTemplateSpecializationKind() == 
TSK_ExplicitSpecialization` necessary? I removed it and all testcases passed. 
Could you please add a testcase to cover it which would fail without this 
condition?

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


[clang] [Clang][Sema] fix a bug on constraint check with template friend function (PR #90646)

2024-05-01 Thread Qizhi Hu via cfe-commits

jcsxky wrote:

> ```c++
> template
> concept D = sizeof(T) == sizeof(U);
> 
> template
> struct A
> {
> template requires D
> static void f();
> };
> 
> template
> struct B
> {
> template
> struct C
> {
> friend void A::f();
> };
> };
> 
> template struct B::C;
> 
> extern template void A::f(); // crash here
> ```
> 
> @jcsxky causes crash with and without this patch applied.

Thanks for your feedback! This may be another issue. clang trunk crashes with 
this case. 

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


[clang] [Clang][Sema] fix a bug on constraint check with template friend function (PR #90646)

2024-05-01 Thread Qizhi Hu via cfe-commits

jcsxky wrote:

Windows CI failed with some unrelated files.

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


[clang] [Clang][Sema] fix a bug on constraint check with template friend function (PR #90646)

2024-04-30 Thread Qizhi Hu via cfe-commits

jcsxky wrote:

> I'm actually working on constraint checking for function template 
> specializations in #88963. I don't think this patch is quite right... this 
> will cause a crash if the befriended function is a member of a class template 
> specialization. Relative to the changes in #88963, I believe the correct fix 
> would be to change line 278 to:
> 
> ```c++
> if (RelativeToPrimary &&
> (Function->getTemplateSpecializationKind() ==
>  TSK_ExplicitSpecialization ||
>  (Function->getFriendObjectKind() &&
>   !Function->getPrimaryTemplate()->getFriendObjectKind(
>   return Response::UseNextDecl(Function);
> ```
> 
> I added a commit to #88963 which makes this change 
> ([be79079](https://github.com/llvm/llvm-project/commit/be79079507ffbd9b29683498f405dc2c32dd8ba7))
> 
> cc @erichkeane

Only when the friend function doesn't use any other new template parameters, 
i.e. their depth is equal can we skip to add the outer arguments to `MTAL`.

> this will cause a crash if the befriended function is a member of a class 
> template specialization

Could you please provide a testcase?

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


[clang] [Clang][Sema] fix a bug on constraint check with template friend function (PR #90646)

2024-04-30 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky created 
https://github.com/llvm/llvm-project/pull/90646

attempt to fix https://github.com/llvm/llvm-project/issues/90349
Skip to add outer class template arguments to `MTAL` when the friend function 
has the same depth with its lexical context(`CXXRecordDecl`).

>From e2785999ed38cd3b0addcf28d9122ecf8100d908 Mon Sep 17 00:00:00 2001
From: Qizhi Hu <836744...@qq.com>
Date: Wed, 1 May 2024 02:25:04 +0800
Subject: [PATCH] [Clang][Sema] fix a bug on constraint check with template
 friend function

---
 clang/docs/ReleaseNotes.rst|  1 +
 clang/lib/Sema/SemaTemplateInstantiate.cpp | 14 +++
 clang/test/SemaCXX/PR90349.cpp | 43 ++
 3 files changed, 58 insertions(+)
 create mode 100644 clang/test/SemaCXX/PR90349.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index c11f117cd6e8b4..ec10c82a3a5403 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -627,6 +627,7 @@ Bug Fixes to C++ Support
 - Fix a bug on template partial specialization with issue on deduction of 
nontype template parameter
   whose type is `decltype(auto)`. Fixes (#GH68885).
 - Clang now correctly treats the noexcept-specifier of a friend function to be 
a complete-class context.
+- Fix a bug on constraint check with template friend function. Fixes 
(#GH90349).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp 
b/clang/lib/Sema/SemaTemplateInstantiate.cpp
index 3a9fd906b7af86..1805f8f6e5ad90 100644
--- a/clang/lib/Sema/SemaTemplateInstantiate.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp
@@ -281,6 +281,20 @@ Response HandleFunction(Sema , const FunctionDecl 
*Function,
 if (Function->getPrimaryTemplate()->isMemberSpecialization())
   return Response::Done();
 
+if (Function->getFriendObjectKind())
+  if (const ClassTemplateSpecializationDecl *TD =
+  dyn_cast(
+  Function->getLexicalDeclContext())) {
+const CXXRecordDecl *TemplatePattern =
+TD->getTemplateInstantiationPattern();
+const FunctionDecl *FunctionPattern =
+Function->getTemplateInstantiationPattern();
+if (TemplatePattern && FunctionPattern &&
+TemplatePattern->getTemplateDepth() ==
+FunctionPattern->getTemplateDepth())
+  return Response::Done();
+  }
+
 // If this function is a generic lambda specialization, we are done.
 if (!ForConstraintInstantiation &&
 isGenericLambdaCallOperatorOrStaticInvokerSpecialization(Function)) {
diff --git a/clang/test/SemaCXX/PR90349.cpp b/clang/test/SemaCXX/PR90349.cpp
new file mode 100644
index 00..6a4b5c21e88f3b
--- /dev/null
+++ b/clang/test/SemaCXX/PR90349.cpp
@@ -0,0 +1,43 @@
+// RUN: %clang_cc1 -verify -std=c++20 -fsyntax-only %s
+
+// expected-no-diagnostics
+
+namespace std {
+template
+concept floating_point = __is_same(T,double) || __is_same(T,float);
+
+template
+concept integral = __is_same(T,int);
+
+}
+
+template
+class Blob;
+
+template
+Blob MakeBlob();
+
+template
+class Blob {
+private:
+Blob() {}
+
+friend Blob MakeBlob();
+};
+
+template
+Blob MakeBlob()
+{
+return Blob();
+}
+
+template
+Blob FindBlobs()
+{
+return MakeBlob();
+}
+
+int main(int argc, const char * argv[]) {
+FindBlobs();
+return 0;
+}

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


[clang] [Clang][Sema] Fix a bug on template partial specialization with issue on deduction of nontype template parameter (PR #90376)

2024-04-30 Thread Qizhi Hu via cfe-commits

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


[clang] [Clang][Sema] Fix a bug on template partial specialization with issue on deduction of nontype template parameter (PR #90376)

2024-04-30 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky updated 
https://github.com/llvm/llvm-project/pull/90376

>From c5c67ed879fc58e5371de6fc8296b7b6f653a072 Mon Sep 17 00:00:00 2001
From: huqizhi 
Date: Sun, 28 Apr 2024 14:24:30 +0800
Subject: [PATCH] [Clang][Sema] Fix a bug on template partial specialization
 with issue on deduction of nontype tempalte parameter

---
 clang/docs/ReleaseNotes.rst  |  2 ++
 clang/include/clang/Sema/Sema.h  | 10 ++
 clang/lib/Sema/SemaTemplate.cpp  | 19 +++
 clang/lib/Sema/SemaTemplateDeduction.cpp | 10 ++
 clang/test/SemaCXX/PR68885.cpp   | 21 +
 5 files changed, 50 insertions(+), 12 deletions(-)
 create mode 100644 clang/test/SemaCXX/PR68885.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 98c80b6017f604..1abc00a25f1f42 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -612,6 +612,8 @@ Bug Fixes to C++ Support
 - Fix CTAD for ``std::initializer_list``. This allows 
``std::initializer_list{1, 2, 3}`` to be deduced as
   ``std::initializer_list`` as intended.
 - Fix a bug on template partial specialization whose template parameter is 
`decltype(auto)`.
+- Fix a bug on template partial specialization with issue on deduction of 
nontype template parameter
+  whose type is `decltype(auto)`. Fixes (#GH68885).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 1ca523ec88c2f9..809b9c4498f697 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -9249,7 +9249,8 @@ class Sema final : public SemaBase {
   void NoteTemplateParameterLocation(const NamedDecl );
 
   ExprResult BuildExpressionFromDeclTemplateArgument(
-  const TemplateArgument , QualType ParamType, SourceLocation Loc);
+  const TemplateArgument , QualType ParamType, SourceLocation Loc,
+  NamedDecl *TemplateParam = nullptr);
   ExprResult
   BuildExpressionFromNonTypeTemplateArgument(const TemplateArgument ,
  SourceLocation Loc);
@@ -9572,9 +9573,10 @@ class Sema final : public SemaBase {
 
   bool isSameOrCompatibleFunctionType(QualType Param, QualType Arg);
 
-  TemplateArgumentLoc getTrivialTemplateArgumentLoc(const TemplateArgument 
,
-QualType NTTPType,
-SourceLocation Loc);
+  TemplateArgumentLoc
+  getTrivialTemplateArgumentLoc(const TemplateArgument , QualType NTTPType,
+SourceLocation Loc,
+NamedDecl *TemplateParam = nullptr);
 
   /// Get a template argument mapping the given template parameter to itself,
   /// e.g. for X in \c template, this would return an expression 
template
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index ed5507c0ec0100..3c2a5a4ac47e69 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -8438,10 +8438,9 @@ void Sema::NoteTemplateParameterLocation(const NamedDecl 
) {
 /// declaration and the type of its corresponding non-type template
 /// parameter, produce an expression that properly refers to that
 /// declaration.
-ExprResult
-Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument ,
-  QualType ParamType,
-  SourceLocation Loc) {
+ExprResult Sema::BuildExpressionFromDeclTemplateArgument(
+const TemplateArgument , QualType ParamType, SourceLocation Loc,
+NamedDecl *TemplateParam) {
   // C++ [temp.param]p8:
   //
   //   A non-type template-parameter of type "array of T" or
@@ -8508,6 +8507,18 @@ Sema::BuildExpressionFromDeclTemplateArgument(const 
TemplateArgument ,
   } else {
 assert(ParamType->isReferenceType() &&
"unexpected type for decl template argument");
+if (NonTypeTemplateParmDecl *NTTP =
+dyn_cast_if_present(TemplateParam)) {
+  QualType TemplateParamType = NTTP->getType();
+  const AutoType *AT = TemplateParamType->getAs();
+  if (AT && AT->isDecltypeAuto()) {
+RefExpr = new (getASTContext()) SubstNonTypeTemplateParmExpr(
+ParamType->getPointeeType(), RefExpr.get()->getValueKind(),
+RefExpr.get()->getExprLoc(), RefExpr.get(), VD, NTTP->getIndex(),
+/*PackIndex=*/std::nullopt,
+/*RefParam=*/true);
+  }
+}
   }
 
   // At this point we should have the right value category.
diff --git a/clang/lib/Sema/SemaTemplateDeduction.cpp 
b/clang/lib/Sema/SemaTemplateDeduction.cpp
index c3815bca038554..e93f7bd842e444 100644
--- a/clang/lib/Sema/SemaTemplateDeduction.cpp
+++ b/clang/lib/Sema/SemaTemplateDeduction.cpp
@@ -2639,7 +2639,8 @@ static bool isSameTemplateArg(ASTContext ,
 /// argument.
 TemplateArgumentLoc
 

[clang] [Clang][Sema] fix a bug on template partial specialization (PR #89862)

2024-04-30 Thread Qizhi Hu via cfe-commits

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


[clang] [Clang][Sema] fix a bug on template partial specialization (PR #89862)

2024-04-30 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky updated 
https://github.com/llvm/llvm-project/pull/89862

>From 242b88a37f08bb66bcdde5e5b30c43553107d29c Mon Sep 17 00:00:00 2001
From: huqizhi 
Date: Wed, 24 Apr 2024 09:37:53 +0800
Subject: [PATCH] [Clang][Sema] fix a bug on template partial specialization

---
 clang/docs/ReleaseNotes.rst |  1 +
 clang/lib/Sema/SemaTemplate.cpp |  2 +-
 ...dentical-type-primary-partial-specialization.cpp | 13 +
 3 files changed, 15 insertions(+), 1 deletion(-)
 create mode 100644 
clang/test/SemaCXX/identical-type-primary-partial-specialization.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 4c0fe5bcf6b122..98c80b6017f604 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -611,6 +611,7 @@ Bug Fixes to C++ Support
   immediate function context.
 - Fix CTAD for ``std::initializer_list``. This allows 
``std::initializer_list{1, 2, 3}`` to be deduced as
   ``std::initializer_list`` as intended.
+- Fix a bug on template partial specialization whose template parameter is 
`decltype(auto)`.
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index bbcb7c33a98579..ed5507c0ec0100 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -7706,7 +7706,7 @@ ExprResult 
Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
 // FIXME: The language rules don't say what happens in this case.
 // FIXME: We get an opaque dependent type out of decltype(auto) if the
 // expression is merely instantiation-dependent; is this enough?
-if (CTAK == CTAK_Deduced && Arg->isTypeDependent()) {
+if (Arg->isTypeDependent()) {
   auto *AT = dyn_cast(DeducedT);
   if (AT && AT->isDecltypeAuto()) {
 SugaredConverted = TemplateArgument(Arg);
diff --git 
a/clang/test/SemaCXX/identical-type-primary-partial-specialization.cpp 
b/clang/test/SemaCXX/identical-type-primary-partial-specialization.cpp
new file mode 100644
index 00..ad51ca8252ef50
--- /dev/null
+++ b/clang/test/SemaCXX/identical-type-primary-partial-specialization.cpp
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++17 %s
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++20 %s
+
+template 
+struct S { // expected-note {{previous definition is here}}
+static constexpr int i = 42;
+};
+
+template 
+struct S { // expected-error {{class template partial specialization does 
not specialize any template argument; to define the primary template, remove 
the template argument list}} \
+  // expected-error {{redefinition of 'S'}}
+static constexpr int i = 0;
+};

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


[clang] [Clang][Sema] Fix a bug on template partial specialization with issue on deduction of nontype tempalte parameter (PR #90376)

2024-04-28 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky updated 
https://github.com/llvm/llvm-project/pull/90376

>From 947096950049ac7047a26335a993e48c1fa5c16d Mon Sep 17 00:00:00 2001
From: huqizhi 
Date: Sun, 28 Apr 2024 14:24:30 +0800
Subject: [PATCH] [Clang][Sema] Fix a bug on template partial specialization
 with issue on deduction of nontype tempalte parameter

---
 clang/docs/ReleaseNotes.rst  |  2 ++
 clang/include/clang/Sema/Sema.h  | 10 ++
 clang/lib/Sema/SemaTemplate.cpp  | 19 +++
 clang/lib/Sema/SemaTemplateDeduction.cpp | 10 ++
 clang/test/SemaCXX/PR68885.cpp   | 21 +
 5 files changed, 50 insertions(+), 12 deletions(-)
 create mode 100644 clang/test/SemaCXX/PR68885.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 64a523a6f25fc2..64533f94815eb3 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -590,6 +590,8 @@ Bug Fixes to C++ Support
 - Fixed a use-after-free bug in parsing of type constraints with default 
arguments that involve lambdas. (#GH67235)
 - Fixed bug in which the body of a consteval lambda within a template was not 
parsed as within an
   immediate function context.
+- Fix a bug on template partial specialization with issue on deduction of 
nontype tempalte parameter
+  whose type is `decltype(auto)`. Fixes (#GH68885).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 1ca523ec88c2f9..809b9c4498f697 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -9249,7 +9249,8 @@ class Sema final : public SemaBase {
   void NoteTemplateParameterLocation(const NamedDecl );
 
   ExprResult BuildExpressionFromDeclTemplateArgument(
-  const TemplateArgument , QualType ParamType, SourceLocation Loc);
+  const TemplateArgument , QualType ParamType, SourceLocation Loc,
+  NamedDecl *TemplateParam = nullptr);
   ExprResult
   BuildExpressionFromNonTypeTemplateArgument(const TemplateArgument ,
  SourceLocation Loc);
@@ -9572,9 +9573,10 @@ class Sema final : public SemaBase {
 
   bool isSameOrCompatibleFunctionType(QualType Param, QualType Arg);
 
-  TemplateArgumentLoc getTrivialTemplateArgumentLoc(const TemplateArgument 
,
-QualType NTTPType,
-SourceLocation Loc);
+  TemplateArgumentLoc
+  getTrivialTemplateArgumentLoc(const TemplateArgument , QualType NTTPType,
+SourceLocation Loc,
+NamedDecl *TemplateParam = nullptr);
 
   /// Get a template argument mapping the given template parameter to itself,
   /// e.g. for X in \c template, this would return an expression 
template
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index bbcb7c33a98579..04f87fff550370 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -8438,10 +8438,9 @@ void Sema::NoteTemplateParameterLocation(const NamedDecl 
) {
 /// declaration and the type of its corresponding non-type template
 /// parameter, produce an expression that properly refers to that
 /// declaration.
-ExprResult
-Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument ,
-  QualType ParamType,
-  SourceLocation Loc) {
+ExprResult Sema::BuildExpressionFromDeclTemplateArgument(
+const TemplateArgument , QualType ParamType, SourceLocation Loc,
+NamedDecl *TemplateParam) {
   // C++ [temp.param]p8:
   //
   //   A non-type template-parameter of type "array of T" or
@@ -8508,6 +8507,18 @@ Sema::BuildExpressionFromDeclTemplateArgument(const 
TemplateArgument ,
   } else {
 assert(ParamType->isReferenceType() &&
"unexpected type for decl template argument");
+if (NonTypeTemplateParmDecl *NTTP =
+dyn_cast_if_present(TemplateParam)) {
+  QualType TemplateParamType = NTTP->getType();
+  const AutoType *AT = TemplateParamType->getAs();
+  if (AT && AT->isDecltypeAuto()) {
+RefExpr = new (getASTContext()) SubstNonTypeTemplateParmExpr(
+ParamType->getPointeeType(), RefExpr.get()->getValueKind(),
+RefExpr.get()->getExprLoc(), RefExpr.get(), VD, NTTP->getIndex(),
+/*PackIndex=*/std::nullopt,
+/*RefParam=*/true);
+  }
+}
   }
 
   // At this point we should have the right value category.
diff --git a/clang/lib/Sema/SemaTemplateDeduction.cpp 
b/clang/lib/Sema/SemaTemplateDeduction.cpp
index c3815bca038554..e93f7bd842e444 100644
--- a/clang/lib/Sema/SemaTemplateDeduction.cpp
+++ b/clang/lib/Sema/SemaTemplateDeduction.cpp
@@ -2639,7 +2639,8 @@ static bool isSameTemplateArg(ASTContext ,
 /// argument.
 TemplateArgumentLoc
 

[clang] [Clang][Sema] Fix a bug on template partial specialization with issue on deduction of nontype tempalte parameter (PR #90376)

2024-04-28 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky updated 
https://github.com/llvm/llvm-project/pull/90376

>From cb7acd79aea4af3e92b5317e55fb43cabc7ebb40 Mon Sep 17 00:00:00 2001
From: huqizhi 
Date: Sun, 28 Apr 2024 14:24:30 +0800
Subject: [PATCH] [Clang][Sema] Fix a bug on template partial specialization
 with issue on deduction of nontype tempalte parameter

---
 clang/docs/ReleaseNotes.rst  |  2 ++
 clang/include/clang/Sema/Sema.h  | 10 ++
 clang/lib/Sema/SemaTemplate.cpp  | 19 +++
 clang/lib/Sema/SemaTemplateDeduction.cpp | 10 ++
 clang/test/SemaCXX/PR68885.cpp   | 21 +
 5 files changed, 50 insertions(+), 12 deletions(-)
 create mode 100644 clang/test/SemaCXX/PR68885.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 64a523a6f25fc2..64533f94815eb3 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -590,6 +590,8 @@ Bug Fixes to C++ Support
 - Fixed a use-after-free bug in parsing of type constraints with default 
arguments that involve lambdas. (#GH67235)
 - Fixed bug in which the body of a consteval lambda within a template was not 
parsed as within an
   immediate function context.
+- Fix a bug on template partial specialization with issue on deduction of 
nontype tempalte parameter
+  whose type is `decltype(auto)`. Fixes (#GH68885).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 1ca523ec88c2f9..809b9c4498f697 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -9249,7 +9249,8 @@ class Sema final : public SemaBase {
   void NoteTemplateParameterLocation(const NamedDecl );
 
   ExprResult BuildExpressionFromDeclTemplateArgument(
-  const TemplateArgument , QualType ParamType, SourceLocation Loc);
+  const TemplateArgument , QualType ParamType, SourceLocation Loc,
+  NamedDecl *TemplateParam = nullptr);
   ExprResult
   BuildExpressionFromNonTypeTemplateArgument(const TemplateArgument ,
  SourceLocation Loc);
@@ -9572,9 +9573,10 @@ class Sema final : public SemaBase {
 
   bool isSameOrCompatibleFunctionType(QualType Param, QualType Arg);
 
-  TemplateArgumentLoc getTrivialTemplateArgumentLoc(const TemplateArgument 
,
-QualType NTTPType,
-SourceLocation Loc);
+  TemplateArgumentLoc
+  getTrivialTemplateArgumentLoc(const TemplateArgument , QualType NTTPType,
+SourceLocation Loc,
+NamedDecl *TemplateParam = nullptr);
 
   /// Get a template argument mapping the given template parameter to itself,
   /// e.g. for X in \c template, this would return an expression 
template
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index bbcb7c33a98579..c91002592781c8 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -8438,10 +8438,9 @@ void Sema::NoteTemplateParameterLocation(const NamedDecl 
) {
 /// declaration and the type of its corresponding non-type template
 /// parameter, produce an expression that properly refers to that
 /// declaration.
-ExprResult
-Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument ,
-  QualType ParamType,
-  SourceLocation Loc) {
+ExprResult Sema::BuildExpressionFromDeclTemplateArgument(
+const TemplateArgument , QualType ParamType, SourceLocation Loc,
+NamedDecl *TemplateParam) {
   // C++ [temp.param]p8:
   //
   //   A non-type template-parameter of type "array of T" or
@@ -8508,6 +8507,18 @@ Sema::BuildExpressionFromDeclTemplateArgument(const 
TemplateArgument ,
   } else {
 assert(ParamType->isReferenceType() &&
"unexpected type for decl template argument");
+if (NonTypeTemplateParmDecl *NTTP =
+dyn_cast_if_present(TemplateParam)) {
+  QualType TemplateParamType = NTTP->getType();
+  const AutoType *AT = TemplateParamType->getAs();
+  if (AT && AT->isDecltypeAuto()) {
+RefExpr = new (getASTContext()) SubstNonTypeTemplateParmExpr(
+ParamType->getPointeeType(), RefExpr.get()->getValueKind(),
+RefExpr.get()->getExprLoc(), RefExpr.get(), VD, /*Index=*/0,
+/*PackIndex=*/std::nullopt,
+/*RefParam=*/true);
+  }
+}
   }
 
   // At this point we should have the right value category.
diff --git a/clang/lib/Sema/SemaTemplateDeduction.cpp 
b/clang/lib/Sema/SemaTemplateDeduction.cpp
index c3815bca038554..e93f7bd842e444 100644
--- a/clang/lib/Sema/SemaTemplateDeduction.cpp
+++ b/clang/lib/Sema/SemaTemplateDeduction.cpp
@@ -2639,7 +2639,8 @@ static bool isSameTemplateArg(ASTContext ,
 /// argument.
 TemplateArgumentLoc
 

[clang] [Clang][Sema] Fix a bug on template partial specialization with issue on deduction of nontype tempalte parameter (PR #90376)

2024-04-28 Thread Qizhi Hu via cfe-commits

jcsxky wrote:

Windows CI failed with an unrelated file.

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


[clang] [Clang][Sema] Fix a bug on template partial specialization with issue on deduction of nontype tempalte parameter (PR #90376)

2024-04-28 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky updated 
https://github.com/llvm/llvm-project/pull/90376

>From f28eba548ae942ab3e567e7b2550a461e8fd5eac Mon Sep 17 00:00:00 2001
From: huqizhi 
Date: Sun, 28 Apr 2024 14:24:30 +0800
Subject: [PATCH] [Clang][Sema] Fix a bug on template partial specialization
 with issue on deduction of nontype tempalte parameter

---
 clang/docs/ReleaseNotes.rst  |  2 ++
 clang/include/clang/Sema/Sema.h  | 10 ++
 clang/lib/Sema/SemaTemplate.cpp  | 18 ++
 clang/lib/Sema/SemaTemplateDeduction.cpp | 10 ++
 clang/test/SemaCXX/PR68885.cpp   | 21 +
 5 files changed, 49 insertions(+), 12 deletions(-)
 create mode 100644 clang/test/SemaCXX/PR68885.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index a1390d6536b28c..8c1ba2a50b921b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -588,6 +588,8 @@ Bug Fixes to C++ Support
 - Fixed a use-after-free bug in parsing of type constraints with default 
arguments that involve lambdas. (#GH67235)
 - Fixed bug in which the body of a consteval lambda within a template was not 
parsed as within an
   immediate function context.
+- Fix a bug on template partial specialization with issue on deduction of 
nontype tempalte parameter
+  whose type is `decltype(auto)`. Fixes (#GH68885).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 1ca523ec88c2f9..809b9c4498f697 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -9249,7 +9249,8 @@ class Sema final : public SemaBase {
   void NoteTemplateParameterLocation(const NamedDecl );
 
   ExprResult BuildExpressionFromDeclTemplateArgument(
-  const TemplateArgument , QualType ParamType, SourceLocation Loc);
+  const TemplateArgument , QualType ParamType, SourceLocation Loc,
+  NamedDecl *TemplateParam = nullptr);
   ExprResult
   BuildExpressionFromNonTypeTemplateArgument(const TemplateArgument ,
  SourceLocation Loc);
@@ -9572,9 +9573,10 @@ class Sema final : public SemaBase {
 
   bool isSameOrCompatibleFunctionType(QualType Param, QualType Arg);
 
-  TemplateArgumentLoc getTrivialTemplateArgumentLoc(const TemplateArgument 
,
-QualType NTTPType,
-SourceLocation Loc);
+  TemplateArgumentLoc
+  getTrivialTemplateArgumentLoc(const TemplateArgument , QualType NTTPType,
+SourceLocation Loc,
+NamedDecl *TemplateParam = nullptr);
 
   /// Get a template argument mapping the given template parameter to itself,
   /// e.g. for X in \c template, this would return an expression 
template
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index bbcb7c33a98579..6e5f7bd61a03a2 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -8438,10 +8438,9 @@ void Sema::NoteTemplateParameterLocation(const NamedDecl 
) {
 /// declaration and the type of its corresponding non-type template
 /// parameter, produce an expression that properly refers to that
 /// declaration.
-ExprResult
-Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument ,
-  QualType ParamType,
-  SourceLocation Loc) {
+ExprResult Sema::BuildExpressionFromDeclTemplateArgument(
+const TemplateArgument , QualType ParamType, SourceLocation Loc,
+NamedDecl *TemplateParam) {
   // C++ [temp.param]p8:
   //
   //   A non-type template-parameter of type "array of T" or
@@ -8508,6 +8507,17 @@ Sema::BuildExpressionFromDeclTemplateArgument(const 
TemplateArgument ,
   } else {
 assert(ParamType->isReferenceType() &&
"unexpected type for decl template argument");
+if (NonTypeTemplateParmDecl *NTTP =
+dyn_cast_if_present(TemplateParam)) {
+  QualType TemplateParamType = NTTP->getType();
+  const AutoType *AT = TemplateParamType->getAs();
+  if (AT && AT->isDecltypeAuto()) {
+RefExpr = new (getASTContext()) SubstNonTypeTemplateParmExpr(
+ParamType->getPointeeType(), RefExpr.get()->getValueKind(),
+RefExpr.get()->getExprLoc(), RefExpr.get(), VD, 0, std::nullopt,
+true);
+  }
+}
   }
 
   // At this point we should have the right value category.
diff --git a/clang/lib/Sema/SemaTemplateDeduction.cpp 
b/clang/lib/Sema/SemaTemplateDeduction.cpp
index c3815bca038554..e93f7bd842e444 100644
--- a/clang/lib/Sema/SemaTemplateDeduction.cpp
+++ b/clang/lib/Sema/SemaTemplateDeduction.cpp
@@ -2639,7 +2639,8 @@ static bool isSameTemplateArg(ASTContext ,
 /// argument.
 TemplateArgumentLoc
 Sema::getTrivialTemplateArgumentLoc(const TemplateArgument ,
-   

[clang] [Clang][Sema] Fix a bug on template partial specialization with issue on deduction of nontype tempalte parameter (PR #90376)

2024-04-28 Thread Qizhi Hu via cfe-commits

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


[clang] [Clang][Sema] Fix a bug on template partial specialization with issue on deduction of nontype tempalte parameter (PR #90376)

2024-04-28 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky updated 
https://github.com/llvm/llvm-project/pull/90376

>From 641f3e695c4bf7cd67e9aff0e0d345b59ad88685 Mon Sep 17 00:00:00 2001
From: huqizhi 
Date: Sun, 28 Apr 2024 14:24:30 +0800
Subject: [PATCH] [Clang][Sema] Fix a bug on template partial specialization
 with issue on deduction of nontype tempalte parameter

---
 clang/docs/ReleaseNotes.rst  |  2 ++
 clang/include/clang/Sema/Sema.h  | 10 ++
 clang/lib/Sema/SemaTemplate.cpp  | 18 ++
 clang/lib/Sema/SemaTemplateDeduction.cpp | 10 ++
 clang/test/SemaCXX/PR68885.cpp   | 21 +
 5 files changed, 49 insertions(+), 12 deletions(-)
 create mode 100644 clang/test/SemaCXX/PR68885.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index a1390d6536b28c..8c1ba2a50b921b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -588,6 +588,8 @@ Bug Fixes to C++ Support
 - Fixed a use-after-free bug in parsing of type constraints with default 
arguments that involve lambdas. (#GH67235)
 - Fixed bug in which the body of a consteval lambda within a template was not 
parsed as within an
   immediate function context.
+- Fix a bug on template partial specialization with issue on deduction of 
nontype tempalte parameter
+  whose type is `decltype(auto)`. Fixes (#GH68885).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 1ca523ec88c2f9..809b9c4498f697 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -9249,7 +9249,8 @@ class Sema final : public SemaBase {
   void NoteTemplateParameterLocation(const NamedDecl );
 
   ExprResult BuildExpressionFromDeclTemplateArgument(
-  const TemplateArgument , QualType ParamType, SourceLocation Loc);
+  const TemplateArgument , QualType ParamType, SourceLocation Loc,
+  NamedDecl *TemplateParam = nullptr);
   ExprResult
   BuildExpressionFromNonTypeTemplateArgument(const TemplateArgument ,
  SourceLocation Loc);
@@ -9572,9 +9573,10 @@ class Sema final : public SemaBase {
 
   bool isSameOrCompatibleFunctionType(QualType Param, QualType Arg);
 
-  TemplateArgumentLoc getTrivialTemplateArgumentLoc(const TemplateArgument 
,
-QualType NTTPType,
-SourceLocation Loc);
+  TemplateArgumentLoc
+  getTrivialTemplateArgumentLoc(const TemplateArgument , QualType NTTPType,
+SourceLocation Loc,
+NamedDecl *TemplateParam = nullptr);
 
   /// Get a template argument mapping the given template parameter to itself,
   /// e.g. for X in \c template, this would return an expression 
template
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index bbcb7c33a98579..5425d862a06c82 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -8438,10 +8438,9 @@ void Sema::NoteTemplateParameterLocation(const NamedDecl 
) {
 /// declaration and the type of its corresponding non-type template
 /// parameter, produce an expression that properly refers to that
 /// declaration.
-ExprResult
-Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument ,
-  QualType ParamType,
-  SourceLocation Loc) {
+ExprResult Sema::BuildExpressionFromDeclTemplateArgument(
+const TemplateArgument , QualType ParamType, SourceLocation Loc,
+NamedDecl *TemplateParam) {
   // C++ [temp.param]p8:
   //
   //   A non-type template-parameter of type "array of T" or
@@ -8508,6 +8507,17 @@ Sema::BuildExpressionFromDeclTemplateArgument(const 
TemplateArgument ,
   } else {
 assert(ParamType->isReferenceType() &&
"unexpected type for decl template argument");
+  if (NonTypeTemplateParmDecl *NTTP =
+  dyn_cast_if_present(TemplateParam)) {
+QualType TemplateParamType = NTTP->getType();
+const AutoType *AT = TemplateParamType->getAs();
+if (AT && AT->isDecltypeAuto()) {
+RefExpr = new (getASTContext()) SubstNonTypeTemplateParmExpr(
+ParamType->getPointeeType(), RefExpr.get()->getValueKind(),
+RefExpr.get()->getExprLoc(), RefExpr.get(), VD, 0, std::nullopt,
+true);
+}
+  }
   }
 
   // At this point we should have the right value category.
diff --git a/clang/lib/Sema/SemaTemplateDeduction.cpp 
b/clang/lib/Sema/SemaTemplateDeduction.cpp
index c3815bca038554..e93f7bd842e444 100644
--- a/clang/lib/Sema/SemaTemplateDeduction.cpp
+++ b/clang/lib/Sema/SemaTemplateDeduction.cpp
@@ -2639,7 +2639,8 @@ static bool isSameTemplateArg(ASTContext ,
 /// argument.
 TemplateArgumentLoc
 Sema::getTrivialTemplateArgumentLoc(const TemplateArgument ,
- 

[clang] [Clang][Sema] Fix a bug on template partial specialization with issue on deduction of nontype tempalte parameter (PR #90376)

2024-04-28 Thread Qizhi Hu via cfe-commits


@@ -8508,6 +8507,16 @@ Sema::BuildExpressionFromDeclTemplateArgument(const 
TemplateArgument ,
   } else {
 assert(ParamType->isReferenceType() &&
"unexpected type for decl template argument");
+if (ParamType->isLValueReferenceType())
+  if (NonTypeTemplateParmDecl *NTTP =
+  dyn_cast_if_present(TemplateParam)) {
+QualType TemplateParamType = NTTP->getType();
+const AutoType *AT = TemplateParamType->getAs();
+if (AT && AT->isDecltypeAuto())
+  RefExpr = new (getASTContext())
+  ParenExpr(RefExpr.get()->getBeginLoc(),

jcsxky wrote:

Ah, thanks for your remind! `SubstNonTypeTemplateParmExpr` is more suitable 
here.

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


[clang] [Clang][Sema] Fix a bug on template partial specialization with issue on deduction of nontype tempalte parameter (PR #90376)

2024-04-28 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky created 
https://github.com/llvm/llvm-project/pull/90376

Fix https://github.com/llvm/llvm-project/issues/68885
When build expression from a deduced argument whose kind is `Declaration` and 
`NTTPType`(which declared as `decltype(auto)`) is deduced as a reference type, 
`BuildExpressionFromDeclTemplateArgument` just create a `DeclRef`. This is 
incorrect while we get type from the expression since we can't get the original 
reference type from `DeclRef`. Creating a `ParenExpr` expression and make the 
deduction correct. `ParenExpr` expression just helps the deduction and may not 
be same with the original expression.

>From 44d96874e8c2b905843166c08a8a002daa6ee99d Mon Sep 17 00:00:00 2001
From: huqizhi 
Date: Sun, 28 Apr 2024 14:24:30 +0800
Subject: [PATCH] [Clang][Sema] Fix a bug on template partial specialization
 with issue on deduction of nontype tempalte parameter

---
 clang/docs/ReleaseNotes.rst  |  2 ++
 clang/include/clang/Sema/Sema.h  | 10 ++
 clang/lib/Sema/SemaTemplate.cpp  | 17 +
 clang/lib/Sema/SemaTemplateDeduction.cpp | 10 ++
 clang/test/SemaCXX/PR68885.cpp   | 21 +
 5 files changed, 48 insertions(+), 12 deletions(-)
 create mode 100644 clang/test/SemaCXX/PR68885.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index a1390d6536b28c..8c1ba2a50b921b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -588,6 +588,8 @@ Bug Fixes to C++ Support
 - Fixed a use-after-free bug in parsing of type constraints with default 
arguments that involve lambdas. (#GH67235)
 - Fixed bug in which the body of a consteval lambda within a template was not 
parsed as within an
   immediate function context.
+- Fix a bug on template partial specialization with issue on deduction of 
nontype tempalte parameter
+  whose type is `decltype(auto)`. Fixes (#GH68885).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 1ca523ec88c2f9..809b9c4498f697 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -9249,7 +9249,8 @@ class Sema final : public SemaBase {
   void NoteTemplateParameterLocation(const NamedDecl );
 
   ExprResult BuildExpressionFromDeclTemplateArgument(
-  const TemplateArgument , QualType ParamType, SourceLocation Loc);
+  const TemplateArgument , QualType ParamType, SourceLocation Loc,
+  NamedDecl *TemplateParam = nullptr);
   ExprResult
   BuildExpressionFromNonTypeTemplateArgument(const TemplateArgument ,
  SourceLocation Loc);
@@ -9572,9 +9573,10 @@ class Sema final : public SemaBase {
 
   bool isSameOrCompatibleFunctionType(QualType Param, QualType Arg);
 
-  TemplateArgumentLoc getTrivialTemplateArgumentLoc(const TemplateArgument 
,
-QualType NTTPType,
-SourceLocation Loc);
+  TemplateArgumentLoc
+  getTrivialTemplateArgumentLoc(const TemplateArgument , QualType NTTPType,
+SourceLocation Loc,
+NamedDecl *TemplateParam = nullptr);
 
   /// Get a template argument mapping the given template parameter to itself,
   /// e.g. for X in \c template, this would return an expression 
template
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index bbcb7c33a98579..6a2f025521ce8e 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -8438,10 +8438,9 @@ void Sema::NoteTemplateParameterLocation(const NamedDecl 
) {
 /// declaration and the type of its corresponding non-type template
 /// parameter, produce an expression that properly refers to that
 /// declaration.
-ExprResult
-Sema::BuildExpressionFromDeclTemplateArgument(const TemplateArgument ,
-  QualType ParamType,
-  SourceLocation Loc) {
+ExprResult Sema::BuildExpressionFromDeclTemplateArgument(
+const TemplateArgument , QualType ParamType, SourceLocation Loc,
+NamedDecl *TemplateParam) {
   // C++ [temp.param]p8:
   //
   //   A non-type template-parameter of type "array of T" or
@@ -8508,6 +8507,16 @@ Sema::BuildExpressionFromDeclTemplateArgument(const 
TemplateArgument ,
   } else {
 assert(ParamType->isReferenceType() &&
"unexpected type for decl template argument");
+if (ParamType->isLValueReferenceType())
+  if (NonTypeTemplateParmDecl *NTTP =
+  dyn_cast_if_present(TemplateParam)) {
+QualType TemplateParamType = NTTP->getType();
+const AutoType *AT = TemplateParamType->getAs();
+if (AT && AT->isDecltypeAuto())
+  RefExpr = new (getASTContext())
+  ParenExpr(RefExpr.get()->getBeginLoc(),
+  

[clang] [Clang][Sema] fix a bug on template partial specialization (PR #89862)

2024-04-25 Thread Qizhi Hu via cfe-commits


@@ -7706,7 +7706,7 @@ ExprResult 
Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
 // FIXME: The language rules don't say what happens in this case.
 // FIXME: We get an opaque dependent type out of decltype(auto) if the
 // expression is merely instantiation-dependent; is this enough?
-if (CTAK == CTAK_Deduced && Arg->isTypeDependent()) {
+if (Arg->isTypeDependent()) {

jcsxky wrote:

After looking into the code a bit more, I think this won't happen since the 
type of `Arg` can't be dependent if the condition `CTAK == 
CTAK_DeducedFromArrayBound` holds. Because it has been deduced as a certain 
expression at the moment.

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


[clang] [Clang][Sema] fix a bug on template partial specialization (PR #89862)

2024-04-23 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky created 
https://github.com/llvm/llvm-project/pull/89862

attempt to fix 
https://github.com/llvm/llvm-project/issues/68885#issuecomment-1764201896
Deduction of NTTP whose type is `decltype(auto)` would create an implicit cast 
expression to dependent type and makes the type of primary template definition 
(`InjectedClassNameSpecialization`) and its partial specialization different. 
Prevent emitting cast expression to make clang knows their types are identical 
by removing `CTAK == CTAK_Deduced` when the type is `decltype(auto)`.

>From 9b3e69844033a99be0eef90e816b9dff3e64c901 Mon Sep 17 00:00:00 2001
From: huqizhi 
Date: Wed, 24 Apr 2024 09:37:53 +0800
Subject: [PATCH] [Clang][Sema] fix a bug on template partial specialization

---
 clang/docs/ReleaseNotes.rst |  1 +
 clang/lib/Sema/SemaTemplate.cpp |  2 +-
 ...dentical-type-primary-partial-specialization.cpp | 13 +
 3 files changed, 15 insertions(+), 1 deletion(-)
 create mode 100644 
clang/test/SemaCXX/identical-type-primary-partial-specialization.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 3db558a1c11a3f..27341fb0e3a772 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -561,6 +561,7 @@ Bug Fixes to C++ Support
 - Fixed a crash when trying to evaluate a user-defined ``static_assert`` 
message whose ``size()``
   function returns a large or negative value. Fixes (#GH89407).
 - Fixed a use-after-free bug in parsing of type constraints with default 
arguments that involve lambdas. (#GH67235)
+- Fix a bug on template partial specialization whose template parameter is 
`decltype(auto)`.
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index 4bda31ba67c02d..345d77849b49d0 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -7706,7 +7706,7 @@ ExprResult 
Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param,
 // FIXME: The language rules don't say what happens in this case.
 // FIXME: We get an opaque dependent type out of decltype(auto) if the
 // expression is merely instantiation-dependent; is this enough?
-if (CTAK == CTAK_Deduced && Arg->isTypeDependent()) {
+if (Arg->isTypeDependent()) {
   auto *AT = dyn_cast(DeducedT);
   if (AT && AT->isDecltypeAuto()) {
 SugaredConverted = TemplateArgument(Arg);
diff --git 
a/clang/test/SemaCXX/identical-type-primary-partial-specialization.cpp 
b/clang/test/SemaCXX/identical-type-primary-partial-specialization.cpp
new file mode 100644
index 00..ad51ca8252ef50
--- /dev/null
+++ b/clang/test/SemaCXX/identical-type-primary-partial-specialization.cpp
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++17 %s
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++20 %s
+
+template 
+struct S { // expected-note {{previous definition is here}}
+static constexpr int i = 42;
+};
+
+template 
+struct S { // expected-error {{class template partial specialization does 
not specialize any template argument; to define the primary template, remove 
the template argument list}} \
+  // expected-error {{redefinition of 'S'}}
+static constexpr int i = 0;
+};

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


[clang] [Clang][Sema] set declaration invalid earlier to prevent crash in calculating record layout (PR #87173)

2024-04-17 Thread Qizhi Hu via cfe-commits

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


[clang] [Clang][Sema] set declaration invalid earlier to prevent crash in calculating record layout (PR #87173)

2024-04-17 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky updated 
https://github.com/llvm/llvm-project/pull/87173

>From 19b2cab0c84a934910f65536a0627045d30b9729 Mon Sep 17 00:00:00 2001
From: huqizhi 
Date: Sun, 31 Mar 2024 09:38:05 +0800
Subject: [PATCH] [Clang][Sema] set declaration invalid earlier to prevent
 crash in calculating record layout

---
 clang/docs/ReleaseNotes.rst  | 2 ++
 clang/include/clang/Sema/Scope.h | 6 ++
 clang/lib/Parse/ParseDeclCXX.cpp | 5 +
 clang/lib/Sema/SemaDecl.cpp  | 7 +++
 clang/test/SemaCXX/PR75221.cpp   | 6 ++
 5 files changed, 26 insertions(+)
 create mode 100644 clang/test/SemaCXX/PR75221.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index c19ad9fba58f37..6c51c2d1f483ce 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -536,6 +536,8 @@ Bug Fixes to C++ Support
   Fixes (#GH70604), (#GH79754), (#GH84163), (#GH84425), (#GH86054), 
(#GH86398), and (#GH86399).
 - Fix a crash when deducing ``auto`` from an invalid dereference (#GH88329).
 - Fix a crash in requires expression with templated base class member 
function. Fixes (#GH84020).
+- Fix a crash caused by defined struct in a type alias template when the 
structure
+  has fields with dependent type. Fixes (#GH75221).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/include/clang/Sema/Scope.h b/clang/include/clang/Sema/Scope.h
index 099c2739e8603a..1752a25111a775 100644
--- a/clang/include/clang/Sema/Scope.h
+++ b/clang/include/clang/Sema/Scope.h
@@ -156,6 +156,9 @@ class Scope {
 /// This is the scope of an OpenACC Compute Construct, which restricts
 /// jumping into/out of it.
 OpenACCComputeConstructScope = 0x1000,
+
+/// This is a scope of type alias declaration.
+TypeAliasScope = 0x2000,
   };
 
 private:
@@ -580,6 +583,9 @@ class Scope {
   /// if/switch/while/for statement.
   bool isControlScope() const { return getFlags() & Scope::ControlScope; }
 
+  /// Determine whether this scope is a type alias scope.
+  bool isTypeAliasScope() const { return getFlags() & Scope::TypeAliasScope; }
+
   /// Returns if rhs has a higher scope depth than this.
   ///
   /// The caller is responsible for calling this only if one of the two scopes
diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index 51fd64b2d01aa7..8e0e8682482933 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -799,6 +799,11 @@ Parser::DeclGroupPtrTy Parser::ParseUsingDeclaration(
 ProhibitAttributes(PrefixAttrs);
 
 Decl *DeclFromDeclSpec = nullptr;
+Scope *CurScope = getCurScope();
+if (CurScope)
+  CurScope->setFlags(Scope::ScopeFlags::TypeAliasScope |
+ CurScope->getFlags());
+
 Decl *AD = ParseAliasDeclarationAfterDeclarator(
 TemplateInfo, UsingLoc, D, DeclEnd, AS, Attrs, );
 return Actions.ConvertDeclToDeclGroup(AD, DeclFromDeclSpec);
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 1bde99d6fce740..356abe09a5ca62 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -19529,6 +19529,13 @@ void Sema::ActOnFields(Scope *S, SourceLocation 
RecLoc, Decl *EnclosingDecl,
   // Okay, we successfully defined 'Record'.
   if (Record) {
 bool Completed = false;
+if (S) {
+  Scope *Parent = S->getParent();
+  if (Parent && Parent->isTypeAliasScope() &&
+  Parent->isTemplateParamScope())
+Record->setInvalidDecl();
+}
+
 if (CXXRecord) {
   if (!CXXRecord->isInvalidDecl()) {
 // Set access bits correctly on the directly-declared conversions.
diff --git a/clang/test/SemaCXX/PR75221.cpp b/clang/test/SemaCXX/PR75221.cpp
new file mode 100644
index 00..b342e347c5606a
--- /dev/null
+++ b/clang/test/SemaCXX/PR75221.cpp
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -verify -std=c++11 -fsyntax-only %s
+
+template  using foo = struct foo { // expected-error {{'foo' cannot 
be defined in a type alias template}}
+  T size = 0;
+};
+foo a;

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


[clang] [Clang][Sema] set declaration invalid earlier to prevent crash in calculating record layout (PR #87173)

2024-04-17 Thread Qizhi Hu via cfe-commits

jcsxky wrote:

gently ping. @erichkeane @shafik Any opinions on this pr?

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


[clang] [Clang][Sema] Fix issue on requires expression with templated base class member function (PR #85198)

2024-04-15 Thread Qizhi Hu via cfe-commits

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


[clang] [Clang][Sema] Fix issue on requires expression with templated base class member function (PR #85198)

2024-04-15 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky updated 
https://github.com/llvm/llvm-project/pull/85198

>From 0076d87897371f3467c49818a7789cedda27e485 Mon Sep 17 00:00:00 2001
From: huqizhi 
Date: Thu, 14 Mar 2024 16:32:36 +0800
Subject: [PATCH] [Clang][Sema] Fix issue on requires expression with templated
 base class member function

---
 clang/docs/ReleaseNotes.rst|  1 +
 clang/lib/Sema/SemaExpr.cpp|  3 ++-
 clang/test/SemaCXX/PR84020.cpp | 23 +++
 3 files changed, 26 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/SemaCXX/PR84020.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index dc108785f6cc99..76701dc723b6c3 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -538,6 +538,7 @@ Bug Fixes to C++ Support
   object parameter.
   Fixes (#GH70604), (#GH79754), (#GH84163), (#GH84425), (#GH86054), 
(#GH86398), and (#GH86399).
 - Fix a crash when deducing ``auto`` from an invalid dereference (#GH88329).
+- Fix a crash in requires expression with templated base class member 
function. Fixes (#GH84020).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 24f354f1c72498..189764cb4b6b08 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -7739,7 +7739,8 @@ ExprResult Sema::BuildResolvedCallExpr(Expr *Fn, 
NamedDecl *NDecl,
   }
 
   if (CXXMethodDecl *Method = dyn_cast_or_null(FDecl))
-if (Method->isImplicitObjectMemberFunction())
+if (!isa(CurContext) &&
+Method->isImplicitObjectMemberFunction())
   return ExprError(Diag(LParenLoc, diag::err_member_call_without_object)
<< Fn->getSourceRange() << 0);
 
diff --git a/clang/test/SemaCXX/PR84020.cpp b/clang/test/SemaCXX/PR84020.cpp
new file mode 100644
index 00..8ea5dcc4527ae7
--- /dev/null
+++ b/clang/test/SemaCXX/PR84020.cpp
@@ -0,0 +1,23 @@
+// RUN: %clang_cc1 -std=c++20 -verify %s
+// RUN: %clang_cc1 -std=c++23 -verify %s
+// expected-no-diagnostics
+
+struct B {
+template 
+void foo();
+
+void bar();
+};
+
+template 
+struct A : T {
+auto foo() {
+static_assert(requires { T::template foo(); });
+static_assert(requires { T::bar(); });
+}
+};
+
+int main() {
+A a;
+a.foo();
+}

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


[clang] [Clang][Sema] Fix issue on requires expression with templated base class member function (PR #85198)

2024-04-15 Thread Qizhi Hu via cfe-commits

jcsxky wrote:

> Ah, I had missed (thanks github review tool!) that the 'ImplicitObjectMember' 
> part already existed. Sorry for that.

Never mind! Partly because I didn't make it more clear due to my poor 
expression .

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


[clang] [Clang][Sema] Fix issue on requires expression with templated base class member function (PR #85198)

2024-04-14 Thread Qizhi Hu via cfe-commits

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


[clang] [Clang][Sema] Fix issue on requires expression with templated base class member function (PR #85198)

2024-04-14 Thread Qizhi Hu via cfe-commits

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


[clang] [Clang][Sema] Fix issue on requires expression with templated base class member function (PR #85198)

2024-04-14 Thread Qizhi Hu via cfe-commits

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


[clang] [Clang][Sema] Fix issue on requires expression with templated base class member function (PR #85198)

2024-04-13 Thread Qizhi Hu via cfe-commits


@@ -7735,7 +7735,8 @@ ExprResult Sema::BuildResolvedCallExpr(Expr *Fn, 
NamedDecl *NDecl,
   }
 
   if (CXXMethodDecl *Method = dyn_cast_or_null(FDecl))
-if (Method->isImplicitObjectMemberFunction())
+if (!isa(CurContext) &&

jcsxky wrote:

And if only checking whether `Method` is a static method, we failed on:
https://github.com/llvm/llvm-project/blob/ef9446bd2d362ec90cd681ae59463d16bf671fe8/clang/test/CXX/drs/dr26xx.cpp#L225-L240

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


[clang] [Clang][Sema] Fix issue on requires expression with templated base class member function (PR #85198)

2024-04-13 Thread Qizhi Hu via cfe-commits


@@ -7735,7 +7735,8 @@ ExprResult Sema::BuildResolvedCallExpr(Expr *Fn, 
NamedDecl *NDecl,
   }
 
   if (CXXMethodDecl *Method = dyn_cast_or_null(FDecl))
-if (Method->isImplicitObjectMemberFunction())
+if (!isa(CurContext) &&

jcsxky wrote:

Do you mean that checking whether `Method` is an implicit object function is 
redundant? After I removed 
https://github.com/llvm/llvm-project/blob/844b532713986999aa1ffed0883eff2d1339ec7a/clang/lib/Sema/SemaExpr.cpp#L7723-L7726
These testcase failed

https://github.com/llvm/llvm-project/blob/844b532713986999aa1ffed0883eff2d1339ec7a/clang/test/CXX/drs/dr3xx.cpp#L1084-L1095
https://github.com/llvm/llvm-project/blob/844b532713986999aa1ffed0883eff2d1339ec7a/clang/test/SemaTemplate/instantiate-using-decl.cpp#L150-L168
with no diagnose. Or the checking shouldn't be placed at current position?

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


[clang] [Clang][Sema] Fix issue on requires expression with templated base class member function (PR #85198)

2024-04-11 Thread Qizhi Hu via cfe-commits


@@ -7735,7 +7735,8 @@ ExprResult Sema::BuildResolvedCallExpr(Expr *Fn, 
NamedDecl *NDecl,
   }
 
   if (CXXMethodDecl *Method = dyn_cast_or_null(FDecl))
-if (Method->isImplicitObjectMemberFunction())
+if (!isa(CurContext) &&

jcsxky wrote:

@erichkeane I am still feeling that we have no need to check whether the method 
has a explicit object parameter in require clause. In type requirement, 
checking nested member is enough(name resolution and parameter checking has 
already completed) and we don't need object argument since it isn't a function 
call.
```cpp
struct S {
  void foo() {}
};
void bar() {
  S::foo(); // need object parameter
  foo();// name resolution failed
```
It is only doing a function call that object parameter is required.

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


[clang] [Clang][Sema] Fix issue on requires expression with templated base class member function (PR #85198)

2024-04-11 Thread Qizhi Hu via cfe-commits


@@ -7735,7 +7735,8 @@ ExprResult Sema::BuildResolvedCallExpr(Expr *Fn, 
NamedDecl *NDecl,
   }
 
   if (CXXMethodDecl *Method = dyn_cast_or_null(FDecl))
-if (Method->isImplicitObjectMemberFunction())
+if (!isa(CurContext) &&

jcsxky wrote:

Yeah, I think it's like this, just with the same name is enough.

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


[clang] [Clang][Sema] Fix issue on requires expression with templated base class member function (PR #85198)

2024-04-11 Thread Qizhi Hu via cfe-commits


@@ -7735,7 +7735,8 @@ ExprResult Sema::BuildResolvedCallExpr(Expr *Fn, 
NamedDecl *NDecl,
   }
 
   if (CXXMethodDecl *Method = dyn_cast_or_null(FDecl))
-if (Method->isImplicitObjectMemberFunction())
+if (!isa(CurContext) &&

jcsxky wrote:

In require body, name resolving just lookup function name and don't check 
function parameter without function call(I think).
```cpp
struct B {
template 
void foo(int);

void bar();
};

template 
struct A : T {
auto foo() {
static_assert(requires { T::template foo(); });
static_assert(requires { T::bar(); });
}
};

int main() {
A a;
//a.foo(0);
}
```
After add a parameter to `foo`, this code also compiles correctly without 
calling.

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


[clang] [Clang][Sema] Fix issue on requires expression with templated base class member function (PR #85198)

2024-04-11 Thread Qizhi Hu via cfe-commits

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


[clang] [Clang][Sema] Fix issue on requires expression with templated base class member function (PR #85198)

2024-04-11 Thread Qizhi Hu via cfe-commits

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


[clang] [Clang][Sema] Fix issue on requires expression with templated base class member function (PR #85198)

2024-04-11 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky updated 
https://github.com/llvm/llvm-project/pull/85198

>From a7bc05667f7280958e68fd82e01b620e18e4203c Mon Sep 17 00:00:00 2001
From: huqizhi 
Date: Thu, 14 Mar 2024 16:32:36 +0800
Subject: [PATCH] [Clang][Sema] Fix issue on requires expression with templated
 base class member function

---
 clang/docs/ReleaseNotes.rst|  1 +
 clang/lib/Sema/SemaExpr.cpp|  3 ++-
 clang/test/SemaCXX/PR84020.cpp | 23 +++
 3 files changed, 26 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/SemaCXX/PR84020.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index c4a4893aec5cd6..e40090be6213bd 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -528,6 +528,7 @@ Bug Fixes to C++ Support
 - Clang now correctly tracks type dependence of by-value captures in lambdas 
with an explicit
   object parameter.
   Fixes (#GH70604), (#GH79754), (#GH84163), (#GH84425), (#GH86054), 
(#GH86398), and (#GH86399).
+- Fix a crash in requires expression with templated base class member 
function. Fixes (#GH84020).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 4d4ef9b16381b4..99b938bb1ba26c 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -7735,7 +7735,8 @@ ExprResult Sema::BuildResolvedCallExpr(Expr *Fn, 
NamedDecl *NDecl,
   }
 
   if (CXXMethodDecl *Method = dyn_cast_or_null(FDecl))
-if (Method->isImplicitObjectMemberFunction())
+if (!isa(CurContext) &&
+Method->isImplicitObjectMemberFunction())
   return ExprError(Diag(LParenLoc, diag::err_member_call_without_object)
<< Fn->getSourceRange() << 0);
 
diff --git a/clang/test/SemaCXX/PR84020.cpp b/clang/test/SemaCXX/PR84020.cpp
new file mode 100644
index 00..8ea5dcc4527ae7
--- /dev/null
+++ b/clang/test/SemaCXX/PR84020.cpp
@@ -0,0 +1,23 @@
+// RUN: %clang_cc1 -std=c++20 -verify %s
+// RUN: %clang_cc1 -std=c++23 -verify %s
+// expected-no-diagnostics
+
+struct B {
+template 
+void foo();
+
+void bar();
+};
+
+template 
+struct A : T {
+auto foo() {
+static_assert(requires { T::template foo(); });
+static_assert(requires { T::bar(); });
+}
+};
+
+int main() {
+A a;
+a.foo();
+}

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


[clang] [Clang][Sema] Fix issue on requires expression with templated base class member function (PR #85198)

2024-04-11 Thread Qizhi Hu via cfe-commits

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


[clang] [Clang][Sema] set declaration invalid earlier to prevent crash in calculating record layout (PR #87173)

2024-04-09 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky updated 
https://github.com/llvm/llvm-project/pull/87173

>From a0c0feae5bee318bcc253e5497994bfe78f308ee Mon Sep 17 00:00:00 2001
From: huqizhi 
Date: Sun, 31 Mar 2024 09:38:05 +0800
Subject: [PATCH] [Clang][Sema] set declaration invalid earlier to prevent
 crash in calculating record layout

---
 clang/docs/ReleaseNotes.rst  | 2 ++
 clang/include/clang/Sema/Scope.h | 6 ++
 clang/lib/Parse/ParseDeclCXX.cpp | 5 +
 clang/lib/Sema/SemaDecl.cpp  | 7 +++
 clang/test/SemaCXX/PR75221.cpp   | 6 ++
 5 files changed, 26 insertions(+)
 create mode 100644 clang/test/SemaCXX/PR75221.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index f96cebbde3d825..31a0e5d4d11a12 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -529,6 +529,8 @@ Bug Fixes to C++ Support
 - Clang now correctly tracks type dependence of by-value captures in lambdas 
with an explicit
   object parameter.
   Fixes (#GH70604), (#GH79754), (#GH84163), (#GH84425), (#GH86054), 
(#GH86398), and (#GH86399).
+- Fix a crash caused by defined struct in a type alias template when the 
structure
+  has fields with dependent type. Fixes (#GH75221).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/include/clang/Sema/Scope.h b/clang/include/clang/Sema/Scope.h
index 099c2739e8603a..1752a25111a775 100644
--- a/clang/include/clang/Sema/Scope.h
+++ b/clang/include/clang/Sema/Scope.h
@@ -156,6 +156,9 @@ class Scope {
 /// This is the scope of an OpenACC Compute Construct, which restricts
 /// jumping into/out of it.
 OpenACCComputeConstructScope = 0x1000,
+
+/// This is a scope of type alias declaration.
+TypeAliasScope = 0x2000,
   };
 
 private:
@@ -580,6 +583,9 @@ class Scope {
   /// if/switch/while/for statement.
   bool isControlScope() const { return getFlags() & Scope::ControlScope; }
 
+  /// Determine whether this scope is a type alias scope.
+  bool isTypeAliasScope() const { return getFlags() & Scope::TypeAliasScope; }
+
   /// Returns if rhs has a higher scope depth than this.
   ///
   /// The caller is responsible for calling this only if one of the two scopes
diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index 861a25dc5103c1..270d09f8c9580c 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -799,6 +799,11 @@ Parser::DeclGroupPtrTy Parser::ParseUsingDeclaration(
 ProhibitAttributes(PrefixAttrs);
 
 Decl *DeclFromDeclSpec = nullptr;
+Scope *CurScope = getCurScope();
+if (CurScope)
+  CurScope->setFlags(Scope::ScopeFlags::TypeAliasScope |
+ CurScope->getFlags());
+
 Decl *AD = ParseAliasDeclarationAfterDeclarator(
 TemplateInfo, UsingLoc, D, DeclEnd, AS, Attrs, );
 return Actions.ConvertDeclToDeclGroup(AD, DeclFromDeclSpec);
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index c790dab72dd721..afe25e2a1de4ab 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -19622,6 +19622,13 @@ void Sema::ActOnFields(Scope *S, SourceLocation 
RecLoc, Decl *EnclosingDecl,
   // Okay, we successfully defined 'Record'.
   if (Record) {
 bool Completed = false;
+if (S) {
+  Scope *Parent = S->getParent();
+  if (Parent && Parent->isTypeAliasScope() &&
+  Parent->isTemplateParamScope())
+Record->setInvalidDecl();
+}
+
 if (CXXRecord) {
   if (!CXXRecord->isInvalidDecl()) {
 // Set access bits correctly on the directly-declared conversions.
diff --git a/clang/test/SemaCXX/PR75221.cpp b/clang/test/SemaCXX/PR75221.cpp
new file mode 100644
index 00..b342e347c5606a
--- /dev/null
+++ b/clang/test/SemaCXX/PR75221.cpp
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -verify -std=c++11 -fsyntax-only %s
+
+template  using foo = struct foo { // expected-error {{'foo' cannot 
be defined in a type alias template}}
+  T size = 0;
+};
+foo a;

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


[clang] [Clang][Sema] set declaration invalid earlier to prevent crash in calculating record layout (PR #87173)

2024-04-09 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky updated 
https://github.com/llvm/llvm-project/pull/87173

>From 26da477eb3633880734c096b13f89cc0d557745b Mon Sep 17 00:00:00 2001
From: huqizhi 
Date: Sun, 31 Mar 2024 09:38:05 +0800
Subject: [PATCH] [Clang][Sema] set declaration invalid earlier to prevent
 crash in calculating record layout

---
 clang/docs/ReleaseNotes.rst  | 2 ++
 clang/include/clang/Sema/Scope.h | 6 ++
 clang/lib/Parse/ParseDeclCXX.cpp | 5 +
 clang/lib/Sema/SemaDecl.cpp  | 7 +++
 clang/test/SemaCXX/PR75221.cpp   | 6 ++
 5 files changed, 26 insertions(+)
 create mode 100644 clang/test/SemaCXX/PR75221.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 28e8ddb3c41c3e..1c541af8657dbb 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -504,6 +504,8 @@ Bug Fixes to C++ Support
 - Fix crash when inheriting from a cv-qualified type. Fixes:
   (`#35603 `_)
 - Fix a crash when the using enum declaration uses an anonymous enumeration. 
Fixes (#GH86790).
+- Fix a crash caused by defined struct in a type alias template when the 
structure
+  has fields with dependent type. Fixes (#GH75221).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/include/clang/Sema/Scope.h b/clang/include/clang/Sema/Scope.h
index 099c2739e8603a..1752a25111a775 100644
--- a/clang/include/clang/Sema/Scope.h
+++ b/clang/include/clang/Sema/Scope.h
@@ -156,6 +156,9 @@ class Scope {
 /// This is the scope of an OpenACC Compute Construct, which restricts
 /// jumping into/out of it.
 OpenACCComputeConstructScope = 0x1000,
+
+/// This is a scope of type alias declaration.
+TypeAliasScope = 0x2000,
   };
 
 private:
@@ -580,6 +583,9 @@ class Scope {
   /// if/switch/while/for statement.
   bool isControlScope() const { return getFlags() & Scope::ControlScope; }
 
+  /// Determine whether this scope is a type alias scope.
+  bool isTypeAliasScope() const { return getFlags() & Scope::TypeAliasScope; }
+
   /// Returns if rhs has a higher scope depth than this.
   ///
   /// The caller is responsible for calling this only if one of the two scopes
diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index 861a25dc5103c1..270d09f8c9580c 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -799,6 +799,11 @@ Parser::DeclGroupPtrTy Parser::ParseUsingDeclaration(
 ProhibitAttributes(PrefixAttrs);
 
 Decl *DeclFromDeclSpec = nullptr;
+Scope *CurScope = getCurScope();
+if (CurScope)
+  CurScope->setFlags(Scope::ScopeFlags::TypeAliasScope |
+ CurScope->getFlags());
+
 Decl *AD = ParseAliasDeclarationAfterDeclarator(
 TemplateInfo, UsingLoc, D, DeclEnd, AS, Attrs, );
 return Actions.ConvertDeclToDeclGroup(AD, DeclFromDeclSpec);
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index c790dab72dd721..afe25e2a1de4ab 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -19622,6 +19622,13 @@ void Sema::ActOnFields(Scope *S, SourceLocation 
RecLoc, Decl *EnclosingDecl,
   // Okay, we successfully defined 'Record'.
   if (Record) {
 bool Completed = false;
+if (S) {
+  Scope *Parent = S->getParent();
+  if (Parent && Parent->isTypeAliasScope() &&
+  Parent->isTemplateParamScope())
+Record->setInvalidDecl();
+}
+
 if (CXXRecord) {
   if (!CXXRecord->isInvalidDecl()) {
 // Set access bits correctly on the directly-declared conversions.
diff --git a/clang/test/SemaCXX/PR75221.cpp b/clang/test/SemaCXX/PR75221.cpp
new file mode 100644
index 00..b342e347c5606a
--- /dev/null
+++ b/clang/test/SemaCXX/PR75221.cpp
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -verify -std=c++11 -fsyntax-only %s
+
+template  using foo = struct foo { // expected-error {{'foo' cannot 
be defined in a type alias template}}
+  T size = 0;
+};
+foo a;

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


[clang] [Clang][Sema] Fix the lambda call expression inside of a type alias declaration (PR #82310)

2024-04-08 Thread Qizhi Hu via cfe-commits

jcsxky wrote:

> > Still crash on
> > ```c++
> > template constexpr auto x = F();
> > template constexpr int a() { return 1; }
> > 
> > template 
> > struct A {
> > using Func = decltype(
> > [](T) {
> > return x<[] constexpr { return a(); }>;
> > // return x<[] constexpr { return b(); }>;
> > }.template operator()('2')
> > );
> > };
> > A::Func y;
> > ```
> 
> That is a distinct case: Func does _not_ form a `TypeAliasTemplateDecl`, and 
> thus it is _not_ supposed to be covered by this PR. Please submit a new issue 
> for it.

Consider this one,
```cpp
template constexpr auto x = F();
template constexpr int a() { return 1; }

template 
struct A {
template
using Func = decltype(
[](T) {
return x<[] constexpr { return a(); }>;
}.template operator()('2')
);
};
A::Func y;
```
`Func` is a `TypeAliasTemplateDecl` and crashes as well.

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


[clang] [Clang][Sema] Fix the lambda call expression inside of a type alias declaration (PR #82310)

2024-04-08 Thread Qizhi Hu via cfe-commits

jcsxky wrote:

Still crash on
```cpp
template constexpr auto x = F();
template constexpr int a() { return 1; }

template 
struct A {
using Func = decltype(
[](T) {
return x<[] constexpr { return a(); }>;
// return x<[] constexpr { return b(); }>;
}.template operator()('2')
);
};
A::Func y;
```

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


[clang] [Clang][Sema] set declaration invalid earlier to prevent crash in calculating record layout (PR #87173)

2024-04-08 Thread Qizhi Hu via cfe-commits

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


[clang] [Clang][Sema] set declaration invalid earlier to prevent crash in calculating record layout (PR #87173)

2024-04-08 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky updated 
https://github.com/llvm/llvm-project/pull/87173

>From 51f3dc24417eb0b74f029b85b47519b6d152 Mon Sep 17 00:00:00 2001
From: huqizhi 
Date: Sun, 31 Mar 2024 09:38:05 +0800
Subject: [PATCH] [Clang][Sema] set declaration invalid earlier to prevent
 crash in calculating record layout

---
 clang/docs/ReleaseNotes.rst  | 2 ++
 clang/include/clang/Sema/Scope.h | 6 ++
 clang/lib/Parse/ParseDeclCXX.cpp | 5 +
 clang/lib/Sema/SemaDecl.cpp  | 7 +++
 clang/test/SemaCXX/PR75221.cpp   | 6 ++
 5 files changed, 26 insertions(+)
 create mode 100644 clang/test/SemaCXX/PR75221.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 28e8ddb3c41c3e..1c541af8657dbb 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -504,6 +504,8 @@ Bug Fixes to C++ Support
 - Fix crash when inheriting from a cv-qualified type. Fixes:
   (`#35603 `_)
 - Fix a crash when the using enum declaration uses an anonymous enumeration. 
Fixes (#GH86790).
+- Fix a crash caused by defined struct in a type alias template when the 
structure
+  has fields with dependent type. Fixes (#GH75221).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/include/clang/Sema/Scope.h b/clang/include/clang/Sema/Scope.h
index 099c2739e8603a..1752a25111a775 100644
--- a/clang/include/clang/Sema/Scope.h
+++ b/clang/include/clang/Sema/Scope.h
@@ -156,6 +156,9 @@ class Scope {
 /// This is the scope of an OpenACC Compute Construct, which restricts
 /// jumping into/out of it.
 OpenACCComputeConstructScope = 0x1000,
+
+/// This is a scope of type alias declaration.
+TypeAliasScope = 0x2000,
   };
 
 private:
@@ -580,6 +583,9 @@ class Scope {
   /// if/switch/while/for statement.
   bool isControlScope() const { return getFlags() & Scope::ControlScope; }
 
+  /// Determine whether this scope is a type alias scope.
+  bool isTypeAliasScope() const { return getFlags() & Scope::TypeAliasScope; }
+
   /// Returns if rhs has a higher scope depth than this.
   ///
   /// The caller is responsible for calling this only if one of the two scopes
diff --git a/clang/lib/Parse/ParseDeclCXX.cpp b/clang/lib/Parse/ParseDeclCXX.cpp
index 861a25dc5103c1..270d09f8c9580c 100644
--- a/clang/lib/Parse/ParseDeclCXX.cpp
+++ b/clang/lib/Parse/ParseDeclCXX.cpp
@@ -799,6 +799,11 @@ Parser::DeclGroupPtrTy Parser::ParseUsingDeclaration(
 ProhibitAttributes(PrefixAttrs);
 
 Decl *DeclFromDeclSpec = nullptr;
+Scope *CurScope = getCurScope();
+if (CurScope)
+  CurScope->setFlags(Scope::ScopeFlags::TypeAliasScope |
+ CurScope->getFlags());
+
 Decl *AD = ParseAliasDeclarationAfterDeclarator(
 TemplateInfo, UsingLoc, D, DeclEnd, AS, Attrs, );
 return Actions.ConvertDeclToDeclGroup(AD, DeclFromDeclSpec);
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index c790dab72dd721..b47824ef3d8498 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -19622,6 +19622,13 @@ void Sema::ActOnFields(Scope *S, SourceLocation 
RecLoc, Decl *EnclosingDecl,
   // Okay, we successfully defined 'Record'.
   if (Record) {
 bool Completed = false;
+if (S && S->getParent()) {
+  Scope *Parent = S->getParent();
+  if (Parent && Parent->isTypeAliasScope() &&
+  Parent->isTemplateParamScope())
+Record->setInvalidDecl();
+}
+
 if (CXXRecord) {
   if (!CXXRecord->isInvalidDecl()) {
 // Set access bits correctly on the directly-declared conversions.
diff --git a/clang/test/SemaCXX/PR75221.cpp b/clang/test/SemaCXX/PR75221.cpp
new file mode 100644
index 00..b342e347c5606a
--- /dev/null
+++ b/clang/test/SemaCXX/PR75221.cpp
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -verify -std=c++11 -fsyntax-only %s
+
+template  using foo = struct foo { // expected-error {{'foo' cannot 
be defined in a type alias template}}
+  T size = 0;
+};
+foo a;

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


[clang] [Clang][Sema] Skip checking anonymous enum in using enum declaration (PR #87144)

2024-04-07 Thread Qizhi Hu via cfe-commits


@@ -1537,6 +1537,10 @@ void Sema::PushOnScopeChains(NamedDecl *D, Scope *S, 
bool AddToContext) {
   cast(D)->isFunctionTemplateSpecialization())
 return;
 
+  if (isa(D) && D->getDeclName().isEmpty()) {

jcsxky wrote:

Ah, I see. I skip those checking when `D` is `using enum`(`UsingEnumDecl`) by 
adding
```cpp
if (isa(D))
return;
```
at 
https://github.com/llvm/llvm-project/blob/cebf77fb936a7270c7e3fa5c4a7e76216321d385/clang/lib/Sema/SemaDecl.cpp#L1529
 and it makes sense after runing unit test on `check-clang` locally. Then I 
think you are right.

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


[clang] [Clang][Sema] Skip checking anonymous enum in using enum declaration (PR #87144)

2024-04-07 Thread Qizhi Hu via cfe-commits


@@ -1537,6 +1537,10 @@ void Sema::PushOnScopeChains(NamedDecl *D, Scope *S, 
bool AddToContext) {
   cast(D)->isFunctionTemplateSpecialization())
 return;
 
+  if (isa(D) && D->getDeclName().isEmpty()) {

jcsxky wrote:

These checks include name conflict and reserved identifier or not. From this 
perspective I think it's required here. It will take some time to attempt to 
skip calling `PushOnScopeChains` and run unittest to find failed cases.

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


[clang] [Clang][Sema] Skip checking anonymous enum in using enum declaration (PR #87144)

2024-04-06 Thread Qizhi Hu via cfe-commits


@@ -1537,6 +1537,10 @@ void Sema::PushOnScopeChains(NamedDecl *D, Scope *S, 
bool AddToContext) {
   cast(D)->isFunctionTemplateSpecialization())
 return;
 
+  if (isa(D) && D->getDeclName().isEmpty()) {

jcsxky wrote:

`Sema::ActOnUsingEnumDeclaration` invoke `PushOnScopeChains` when `D` is a 
`UsingEnumDecl` (anonymous or not) and do some extra works.  Just add the 
declaration would skip checking even it's not anonymous. 

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


[clang] [Clang][Sema] Skip checking anonymous enum in using enum declaration (PR #87144)

2024-04-05 Thread Qizhi Hu via cfe-commits

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


[clang] [clang][ASTImporter] fix variable inline of CXX17 (PR #87314)

2024-04-04 Thread Qizhi Hu via cfe-commits

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


[clang] [Clang][Sema] Skip checking anonymous enum in using enum declaration (PR #87144)

2024-04-04 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky updated 
https://github.com/llvm/llvm-project/pull/87144

>From c4adc0ae83294e4524f2740a40eee483c2cb Mon Sep 17 00:00:00 2001
From: huqizhi 
Date: Sat, 30 Mar 2024 14:47:00 +0800
Subject: [PATCH] [Clang][Sema] Skip checking anonymous enum in using enum
 declaration

---
 clang/docs/ReleaseNotes.rst|  1 +
 clang/lib/Sema/SemaDecl.cpp|  4 
 clang/test/SemaCXX/PR86790.cpp | 32 
 3 files changed, 37 insertions(+)
 create mode 100644 clang/test/SemaCXX/PR86790.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 8fc925350849cd..f33f67cdbd0fc5 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -494,6 +494,7 @@ Bug Fixes to C++ Support
 
 - Fix crash when inheriting from a cv-qualified type. Fixes:
   (`#35603 `_)
+- Fix a crash when the using enum declaration uses an anonymous enumeration. 
Fixes (#GH86790).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 5c1152896559b5..ba0ce433a39789 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -1537,6 +1537,10 @@ void Sema::PushOnScopeChains(NamedDecl *D, Scope *S, 
bool AddToContext) {
   cast(D)->isFunctionTemplateSpecialization())
 return;
 
+  if (isa(D) && D->getDeclName().isEmpty()) {
+S->AddDecl(D);
+return;
+  }
   // If this replaces anything in the current scope,
   IdentifierResolver::iterator I = IdResolver.begin(D->getDeclName()),
IEnd = IdResolver.end();
diff --git a/clang/test/SemaCXX/PR86790.cpp b/clang/test/SemaCXX/PR86790.cpp
new file mode 100644
index 00..09e9bb3505e1bf
--- /dev/null
+++ b/clang/test/SemaCXX/PR86790.cpp
@@ -0,0 +1,32 @@
+// RUN: %clang_cc1 -verify -std=c++20 -fsyntax-only %s
+
+enum {A, S, D, F};
+int main() {
+using asdf = decltype(A);
+using enum asdf; // this line causes the crash
+return 0;
+}
+
+namespace N1 {
+enum {A, S, D, F};
+constexpr struct T {
+using asdf = decltype(A);
+using enum asdf;
+} t;
+
+static_assert(t.D == D);
+static_assert(T::S == S);
+}
+
+namespace N2 {
+enum {A, S, D, F};
+constexpr struct T {
+struct {
+using asdf = decltype(A);
+using enum asdf;
+} inner;
+} t;
+
+static_assert(t.inner.D == D);
+static_assert(t.D == D); // expected-error {{no member named 'D' in 
'N2::T'}}
+}

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


[clang] [clang][ASTImporter] fix variable inline of CXX17 (PR #87314)

2024-04-01 Thread Qizhi Hu via cfe-commits

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


[clang] [clang][ASTImporter] fix variable inline of CXX17 (PR #87314)

2024-04-01 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky updated 
https://github.com/llvm/llvm-project/pull/87314

>From 1a1071845f2409dd07be454fb68ff42911fb62a2 Mon Sep 17 00:00:00 2001
From: huqizhi 
Date: Tue, 2 Apr 2024 13:18:14 +0800
Subject: [PATCH] [clang][ASTImporter] fix variable inline of CXX17

---
 clang/lib/AST/ASTImporter.cpp   |  7 ++-
 clang/unittests/AST/ASTImporterTest.cpp | 28 +
 2 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp
index 94a47a8f619018..2b2c7c0491dfe5 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -4579,7 +4579,12 @@ ExpectedDecl ASTNodeImporter::VisitVarDecl(VarDecl *D) {
 if (!RedeclOrErr)
   return RedeclOrErr.takeError();
   }
-
+  if (D->isInlineSpecified()) {
+ToVar->setInlineSpecified();
+  }
+  if (D->isInline()) {
+ToVar->setImplicitlyInline();
+  }
   return ToVar;
 }
 
diff --git a/clang/unittests/AST/ASTImporterTest.cpp 
b/clang/unittests/AST/ASTImporterTest.cpp
index 35ab7e3b7fe314..d57736830f0223 100644
--- a/clang/unittests/AST/ASTImporterTest.cpp
+++ b/clang/unittests/AST/ASTImporterTest.cpp
@@ -5317,6 +5317,34 @@ TEST_P(ASTImporterOptionSpecificTestBase,
   EXPECT_FALSE(ToX);
 }
 
+TEST_P(ASTImporterOptionSpecificTestBase, VarTemplateDeclInlineWithCXX17) {
+  Decl *FromTU = getTuDecl(
+  R"(
+  struct S {
+template  static constexpr bool X = true;
+  };
+  )",
+  Lang_CXX17, "input1.cc");
+  Decl *FromTU2 = getTuDecl(
+  R"(
+  struct S {
+template  static constexpr bool X = true;
+template  void get() { X; }
+  };
+  template  T qvariant_cast(const S ) { return v.get; }
+  )",
+  Lang_CXX17, "input2.cc");
+  auto *FromX = FirstDeclMatcher().match(
+  FromTU, varTemplateDecl(hasName("X")));
+  auto *ToX = Import(FromX, Lang_CXX17);
+  EXPECT_TRUE(ToX);
+  auto *FromX2 = FirstDeclMatcher().match(
+  FromTU2, varTemplateDecl(hasName("X")));
+  auto *ToX2 = Import(FromX2, Lang_CXX17);
+  EXPECT_TRUE(ToX2);
+  EXPECT_TRUE(ToX == ToX2);
+}
+
 TEST_P(ASTImporterOptionSpecificTestBase, VarTemplateParameterDeclContext) {
   constexpr auto Code =
   R"(

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


[clang] [clang][ASTImporter] fix variable inline of CXX17 (PR #87314)

2024-04-01 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky created 
https://github.com/llvm/llvm-project/pull/87314

Fix crash int the testcase from 
https://github.com/llvm/llvm-project/issues/75114#issuecomment-1872595956
Forget to set inline of variable declaration would make 
`isThisDeclarationADefinition` get incorrect result and didn't get imported 
variable. This will lead to a new `VarTemplateDecl` being created and call 
`setDescribedVarTemplate` again which produce the crash.

>From 2f579346d6955e06d8e16bb29467ed5da332451d Mon Sep 17 00:00:00 2001
From: huqizhi 
Date: Tue, 2 Apr 2024 13:18:14 +0800
Subject: [PATCH] [clang][ASTImporter] fix variable inline of CXX17

---
 clang/lib/AST/ASTImporter.cpp   |  9 ++--
 clang/unittests/AST/ASTImporterTest.cpp | 28 +
 2 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp
index 94a47a8f619018..cfdb2cb5c778e3 100644
--- a/clang/lib/AST/ASTImporter.cpp
+++ b/clang/lib/AST/ASTImporter.cpp
@@ -4579,7 +4579,12 @@ ExpectedDecl ASTNodeImporter::VisitVarDecl(VarDecl *D) {
 if (!RedeclOrErr)
   return RedeclOrErr.takeError();
   }
-
+  if (D->isInlineSpecified()) {
+ToVar->setInlineSpecified();
+  }
+  if (D->isInline()) {
+ToVar->setImplicitlyInline();
+  }
   return ToVar;
 }
 
@@ -6410,7 +6415,7 @@ ExpectedDecl 
ASTNodeImporter::VisitVarTemplateDecl(VarTemplateDecl *D) {
 }
 ToVarTD->setPreviousDecl(Recent);
   }
-
+  // Importer.MapImported(D, ToVarTD);
   return ToVarTD;
 }
 
diff --git a/clang/unittests/AST/ASTImporterTest.cpp 
b/clang/unittests/AST/ASTImporterTest.cpp
index 35ab7e3b7fe314..d57736830f0223 100644
--- a/clang/unittests/AST/ASTImporterTest.cpp
+++ b/clang/unittests/AST/ASTImporterTest.cpp
@@ -5317,6 +5317,34 @@ TEST_P(ASTImporterOptionSpecificTestBase,
   EXPECT_FALSE(ToX);
 }
 
+TEST_P(ASTImporterOptionSpecificTestBase, VarTemplateDeclInlineWithCXX17) {
+  Decl *FromTU = getTuDecl(
+  R"(
+  struct S {
+template  static constexpr bool X = true;
+  };
+  )",
+  Lang_CXX17, "input1.cc");
+  Decl *FromTU2 = getTuDecl(
+  R"(
+  struct S {
+template  static constexpr bool X = true;
+template  void get() { X; }
+  };
+  template  T qvariant_cast(const S ) { return v.get; }
+  )",
+  Lang_CXX17, "input2.cc");
+  auto *FromX = FirstDeclMatcher().match(
+  FromTU, varTemplateDecl(hasName("X")));
+  auto *ToX = Import(FromX, Lang_CXX17);
+  EXPECT_TRUE(ToX);
+  auto *FromX2 = FirstDeclMatcher().match(
+  FromTU2, varTemplateDecl(hasName("X")));
+  auto *ToX2 = Import(FromX2, Lang_CXX17);
+  EXPECT_TRUE(ToX2);
+  EXPECT_TRUE(ToX == ToX2);
+}
+
 TEST_P(ASTImporterOptionSpecificTestBase, VarTemplateParameterDeclContext) {
   constexpr auto Code =
   R"(

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


[clang] [Clang][Sema] set declaration invalid earlier to prevent crash in calculating record layout (PR #87173)

2024-04-01 Thread Qizhi Hu via cfe-commits


@@ -3899,6 +3899,9 @@ static QualType 
GetDeclSpecTypeForDeclarator(TypeProcessingState ,
   SemaRef.Diag(OwnedTagDecl->getLocation(), DiagID)
   << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
   D.setInvalidType(true);
+  OwnedTagDecl->setCompleteDefinition(false);
+  OwnedTagDecl->setInvalidDecl();
+  OwnedTagDecl->setCompleteDefinition();

jcsxky wrote:

@shafik If I understand correctly, you want to set `OwnedTagDecl` invalid 
before it is a complete definition. I will look into the code.

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


[clang] [Clang][Sema] set declaration invalid earlier to prevent crash in calculating record layout (PR #87173)

2024-04-01 Thread Qizhi Hu via cfe-commits


@@ -3899,6 +3899,9 @@ static QualType 
GetDeclSpecTypeForDeclarator(TypeProcessingState ,
   SemaRef.Diag(OwnedTagDecl->getLocation(), DiagID)
   << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
   D.setInvalidType(true);
+  OwnedTagDecl->setCompleteDefinition(false);
+  OwnedTagDecl->setInvalidDecl();
+  OwnedTagDecl->setCompleteDefinition();

jcsxky wrote:

@erichkeane Condition holds in outer `if`(line 3841) indicates `OwnedTagDecl` 
is a complete definition.

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


[clang] [Clang][Sema] set declaration invalid earlier to prevent crash in calculating record layout (PR #87173)

2024-03-30 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky updated 
https://github.com/llvm/llvm-project/pull/87173

>From 710a72c43ae9612e577172a978bfafe6553a6f9e Mon Sep 17 00:00:00 2001
From: huqizhi 
Date: Sun, 31 Mar 2024 09:38:05 +0800
Subject: [PATCH] [Clang][Sema] set declaration invalid earlier to prevent
 crash in calculating record layout

---
 clang/docs/ReleaseNotes.rst| 2 ++
 clang/lib/Sema/SemaType.cpp| 3 +++
 clang/test/SemaCXX/PR75221.cpp | 6 ++
 3 files changed, 11 insertions(+)
 create mode 100644 clang/test/SemaCXX/PR75221.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 37b843915a0dee..20578c9b60e33c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -465,6 +465,8 @@ Bug Fixes to C++ Support
   following the first `::` were ignored).
 - Fix an out-of-bounds crash when checking the validity of template partial 
specializations. (part of #GH86757).
 - Fix an issue caused by not handling invalid cases when substituting into the 
parameter mapping of a constraint. Fixes (#GH86757).
+- Fix a crash caused by defined struct in a type alias template when the 
structure
+  has fields with dependent type. Fixes (#GH75221).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index fd94caa4e1d449..973ad20c943bde 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -3899,6 +3899,9 @@ static QualType 
GetDeclSpecTypeForDeclarator(TypeProcessingState ,
   SemaRef.Diag(OwnedTagDecl->getLocation(), DiagID)
   << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
   D.setInvalidType(true);
+  OwnedTagDecl->setCompleteDefinition(false);
+  OwnedTagDecl->setInvalidDecl();
+  OwnedTagDecl->setCompleteDefinition();
 }
   }
 
diff --git a/clang/test/SemaCXX/PR75221.cpp b/clang/test/SemaCXX/PR75221.cpp
new file mode 100644
index 00..b342e347c5606a
--- /dev/null
+++ b/clang/test/SemaCXX/PR75221.cpp
@@ -0,0 +1,6 @@
+// RUN: %clang_cc1 -verify -std=c++11 -fsyntax-only %s
+
+template  using foo = struct foo { // expected-error {{'foo' cannot 
be defined in a type alias template}}
+  T size = 0;
+};
+foo a;

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


[clang] [Clang][Sema] set declaration invalid earlier to prevent crash in calculating record layout (PR #87173)

2024-03-30 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky created 
https://github.com/llvm/llvm-project/pull/87173

Try to fix https://github.com/llvm/llvm-project/issues/75221
This crash caused by calculating record layout which contains a field 
declaration with dependent type. Make it invalid when report diagnose to 
prevent this crash. Set the record declaration incomplete bypass the assertion 
and restore the status when finish setting it invalid.

>From 98c7c28b2e3484d599847f4a4046fc4ebef5a1e0 Mon Sep 17 00:00:00 2001
From: huqizhi 
Date: Sun, 31 Mar 2024 09:38:05 +0800
Subject: [PATCH] [Clang][Sema] set declaration invalid earlier to prevent
 crash in calculating record layout

---
 clang/docs/ReleaseNotes.rst| 2 ++
 clang/lib/Sema/SemaType.cpp| 3 +++
 clang/test/SemaCXX/PR75221.cpp | 7 +++
 3 files changed, 12 insertions(+)
 create mode 100644 clang/test/SemaCXX/PR75221.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 37b843915a0dee..20578c9b60e33c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -465,6 +465,8 @@ Bug Fixes to C++ Support
   following the first `::` were ignored).
 - Fix an out-of-bounds crash when checking the validity of template partial 
specializations. (part of #GH86757).
 - Fix an issue caused by not handling invalid cases when substituting into the 
parameter mapping of a constraint. Fixes (#GH86757).
+- Fix a crash caused by defined struct in a type alias template when the 
structure
+  has fields with dependent type. Fixes (#GH75221).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index fd94caa4e1d449..973ad20c943bde 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -3899,6 +3899,9 @@ static QualType 
GetDeclSpecTypeForDeclarator(TypeProcessingState ,
   SemaRef.Diag(OwnedTagDecl->getLocation(), DiagID)
   << SemaRef.Context.getTypeDeclType(OwnedTagDecl);
   D.setInvalidType(true);
+  OwnedTagDecl->setCompleteDefinition(false);
+  OwnedTagDecl->setInvalidDecl();
+  OwnedTagDecl->setCompleteDefinition();
 }
   }
 
diff --git a/clang/test/SemaCXX/PR75221.cpp b/clang/test/SemaCXX/PR75221.cpp
new file mode 100644
index 00..08b7a06676a8a5
--- /dev/null
+++ b/clang/test/SemaCXX/PR75221.cpp
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 -verify -std=c++11 -fsyntax-only %s
+// expected-no-diagnostics
+
+template  using foo = struct foo {
+  T size = 0;
+};
+foo a;

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


[clang] [Clang][Sema] Skip checking anonymous enum in using enum declaration (PR #87144)

2024-03-30 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky updated 
https://github.com/llvm/llvm-project/pull/87144

>From 8a0d7c30b2e0efae395143bcd599f3de8d018394 Mon Sep 17 00:00:00 2001
From: huqizhi 
Date: Sat, 30 Mar 2024 14:47:00 +0800
Subject: [PATCH] [Clang][Sema] Skip checking anonymous enum in using enum
 declaration

---
 clang/docs/ReleaseNotes.rst| 1 +
 clang/lib/Sema/SemaDecl.cpp| 4 
 clang/test/SemaCXX/PR86790.cpp | 9 +
 3 files changed, 14 insertions(+)
 create mode 100644 clang/test/SemaCXX/PR86790.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 232de0d7d8bb73..39f896652d03d5 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -459,6 +459,7 @@ Bug Fixes to C++ Support
   following the first `::` were ignored).
 - Fix an out-of-bounds crash when checking the validity of template partial 
specializations. (part of #GH86757).
 - Fix an issue caused by not handling invalid cases when substituting into the 
parameter mapping of a constraint. Fixes (#GH86757).
+- Fix a crash when the using enum declaration uses an anonymous enumeration. 
Fixes (#GH86790).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 0bd88ece2aa544..594ebe56f4f4a8 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -1537,6 +1537,10 @@ void Sema::PushOnScopeChains(NamedDecl *D, Scope *S, 
bool AddToContext) {
   cast(D)->isFunctionTemplateSpecialization())
 return;
 
+  if (isa(D) && D->getDeclName().isEmpty()) {
+S->AddDecl(D);
+return;
+  }
   // If this replaces anything in the current scope,
   IdentifierResolver::iterator I = IdResolver.begin(D->getDeclName()),
IEnd = IdResolver.end();
diff --git a/clang/test/SemaCXX/PR86790.cpp b/clang/test/SemaCXX/PR86790.cpp
new file mode 100644
index 00..1e9f3e1f467e5f
--- /dev/null
+++ b/clang/test/SemaCXX/PR86790.cpp
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -verify -std=c++20 -fsyntax-only %s
+// expected-no-diagnostics
+
+enum {A, S, D, F};
+int main() {
+using asdf = decltype(A);
+using enum asdf; // this line causes the crash
+return 0;
+}

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


[clang] [Clang][Sema] Skip checking anonymous enum in using enum declaration (PR #87144)

2024-03-30 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky updated 
https://github.com/llvm/llvm-project/pull/87144

>From 92da0db6815e07419256ea7fd531d0785a47d8fc Mon Sep 17 00:00:00 2001
From: huqizhi 
Date: Sat, 30 Mar 2024 14:47:00 +0800
Subject: [PATCH] [Clang][Sema] Skip checking anonymous enum in using enum
 declaration

---
 clang/docs/ReleaseNotes.rst| 1 +
 clang/lib/Sema/SemaDecl.cpp| 4 
 clang/test/SemaCXX/PR86790.cpp | 9 +
 3 files changed, 14 insertions(+)
 create mode 100644 clang/test/SemaCXX/PR86790.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 232de0d7d8bb73..39f896652d03d5 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -459,6 +459,7 @@ Bug Fixes to C++ Support
   following the first `::` were ignored).
 - Fix an out-of-bounds crash when checking the validity of template partial 
specializations. (part of #GH86757).
 - Fix an issue caused by not handling invalid cases when substituting into the 
parameter mapping of a constraint. Fixes (#GH86757).
+- Fix a crash when the using enum declaration uses an anonymous enumeration. 
Fixes (#GH86790).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 0bd88ece2aa544..848cadd437fa16 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -1537,6 +1537,10 @@ void Sema::PushOnScopeChains(NamedDecl *D, Scope *S, 
bool AddToContext) {
   cast(D)->isFunctionTemplateSpecialization())
 return;
 
+  if (D->getDeclName().isEmpty()) {
+S->AddDecl(D);
+return;
+  }
   // If this replaces anything in the current scope,
   IdentifierResolver::iterator I = IdResolver.begin(D->getDeclName()),
IEnd = IdResolver.end();
diff --git a/clang/test/SemaCXX/PR86790.cpp b/clang/test/SemaCXX/PR86790.cpp
new file mode 100644
index 00..1e9f3e1f467e5f
--- /dev/null
+++ b/clang/test/SemaCXX/PR86790.cpp
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -verify -std=c++20 -fsyntax-only %s
+// expected-no-diagnostics
+
+enum {A, S, D, F};
+int main() {
+using asdf = decltype(A);
+using enum asdf; // this line causes the crash
+return 0;
+}

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


[clang] [Clang][Sema] Skip checking anonymous enum in using enum declaration (PR #87144)

2024-03-30 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky updated 
https://github.com/llvm/llvm-project/pull/87144

>From 6a0557097548d1a83592185a72c6609ee9ae0a17 Mon Sep 17 00:00:00 2001
From: huqizhi 
Date: Sat, 30 Mar 2024 14:47:00 +0800
Subject: [PATCH] [Clang][Sema] Skip checking anonymous enum in using enum
 declaration

---
 clang/docs/ReleaseNotes.rst| 1 +
 clang/lib/Sema/SemaDecl.cpp| 4 
 clang/test/SemaCXX/PR86790.cpp | 9 +
 3 files changed, 14 insertions(+)
 create mode 100644 clang/test/SemaCXX/PR86790.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 232de0d7d8bb73..39f896652d03d5 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -459,6 +459,7 @@ Bug Fixes to C++ Support
   following the first `::` were ignored).
 - Fix an out-of-bounds crash when checking the validity of template partial 
specializations. (part of #GH86757).
 - Fix an issue caused by not handling invalid cases when substituting into the 
parameter mapping of a constraint. Fixes (#GH86757).
+- Fix a crash when the using enum declaration uses an anonymous enumeration. 
Fixes (#GH86790).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 0bd88ece2aa544..dda0b08d93bc75 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -1537,6 +1537,10 @@ void Sema::PushOnScopeChains(NamedDecl *D, Scope *S, 
bool AddToContext) {
   cast(D)->isFunctionTemplateSpecialization())
 return;
 
+  if (isa(D) && D->getDeclName().isEmpty()) {
+S->AddDecl(D);
+return;
+  }
   // If this replaces anything in the current scope,
   IdentifierResolver::iterator I = IdResolver.begin(D->getDeclName()),
IEnd = IdResolver.end();
diff --git a/clang/test/SemaCXX/PR86790.cpp b/clang/test/SemaCXX/PR86790.cpp
new file mode 100644
index 00..1e9f3e1f467e5f
--- /dev/null
+++ b/clang/test/SemaCXX/PR86790.cpp
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -verify -std=c++20 -fsyntax-only %s
+// expected-no-diagnostics
+
+enum {A, S, D, F};
+int main() {
+using asdf = decltype(A);
+using enum asdf; // this line causes the crash
+return 0;
+}

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


[clang] [Clang][Sema] Skip checking anonymous enum in using enum declaration (PR #87144)

2024-03-30 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky created 
https://github.com/llvm/llvm-project/pull/87144

Try to fix https://github.com/llvm/llvm-project/issues/86790
Skip checking anonymous enumeration in using enum declaration.

>From bff555ed0466a74a4e23d7d4feca2940ee04b719 Mon Sep 17 00:00:00 2001
From: huqizhi 
Date: Sat, 30 Mar 2024 14:47:00 +0800
Subject: [PATCH] [Clang][Sema] Skip checking anonymous enum in using enum
 declaration

---
 clang/docs/ReleaseNotes.rst| 1 +
 clang/lib/Sema/SemaDecl.cpp| 4 
 clang/test/SemaCXX/PR86790.cpp | 9 +
 3 files changed, 14 insertions(+)
 create mode 100644 clang/test/SemaCXX/PR86790.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 232de0d7d8bb73..39f896652d03d5 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -459,6 +459,7 @@ Bug Fixes to C++ Support
   following the first `::` were ignored).
 - Fix an out-of-bounds crash when checking the validity of template partial 
specializations. (part of #GH86757).
 - Fix an issue caused by not handling invalid cases when substituting into the 
parameter mapping of a constraint. Fixes (#GH86757).
+- Fix a crash when the using enum declaration uses an anonymous enumeration. 
Fixes (#GH86790).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 0bd88ece2aa544..dda0b08d93bc75 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -1537,6 +1537,10 @@ void Sema::PushOnScopeChains(NamedDecl *D, Scope *S, 
bool AddToContext) {
   cast(D)->isFunctionTemplateSpecialization())
 return;
 
+  if (isa(D) && D->getDeclName().isEmpty()) {
+S->AddDecl(D);
+return;
+  }
   // If this replaces anything in the current scope,
   IdentifierResolver::iterator I = IdResolver.begin(D->getDeclName()),
IEnd = IdResolver.end();
diff --git a/clang/test/SemaCXX/PR86790.cpp b/clang/test/SemaCXX/PR86790.cpp
new file mode 100644
index 00..65403baea32c01
--- /dev/null
+++ b/clang/test/SemaCXX/PR86790.cpp
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -verify -std=c++20 -fsyntax-only %s
+// expected-no-diagnostics
+
+enum {A, S, D, F};
+int main() {
+using asdf = decltype(A);
+using enum asdf; // this line causes the crash
+return 0;
+}
\ No newline at end of file

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


[clang] [Clang][Sema] Fix a crash in lambda instantiation (PR #85565)

2024-03-19 Thread Qizhi Hu via cfe-commits

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


[clang] [Clang][Sema] Fix a crash in lambda instantiation (PR #85565)

2024-03-18 Thread Qizhi Hu via cfe-commits


@@ -13714,6 +13714,12 @@ TreeTransform::TransformLambdaExpr(LambdaExpr 
*E) {
 
 // Capturing 'this' is trivial.
 if (C->capturesThis()) {
+  // We need ThisType when build capture in CheckCXXThisCapture.

jcsxky wrote:

Thanks for your patience!

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


[clang] [Clang][Sema] Fix a crash in lambda instantiation (PR #85565)

2024-03-18 Thread Qizhi Hu via cfe-commits


@@ -13714,6 +13714,12 @@ TreeTransform::TransformLambdaExpr(LambdaExpr 
*E) {
 
 // Capturing 'this' is trivial.
 if (C->capturesThis()) {
+  // We need ThisType when build capture in CheckCXXThisCapture.

jcsxky wrote:

Ah, I see. I didn't realize to combine test case with the comment to have a 
better illustration. Sorry for my poor English and expression! I have pushed 
your suggestion.

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


[clang] [Clang][Sema] Fix a crash in lambda instantiation (PR #85565)

2024-03-18 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky updated 
https://github.com/llvm/llvm-project/pull/85565

>From b286dcfb2ae59d650e6b49fee97f159e2e958dcc Mon Sep 17 00:00:00 2001
From: huqizhi 
Date: Sun, 17 Mar 2024 17:48:05 +0800
Subject: [PATCH] [Clang][Sema] Fix a crash in lambda instantiation

---
 clang/docs/ReleaseNotes.rst|  1 +
 clang/lib/Sema/TreeTransform.h | 10 ++
 clang/test/Sema/PR85343.cpp| 22 ++
 3 files changed, 33 insertions(+)
 create mode 100644 clang/test/Sema/PR85343.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 125d51c42d507f..a446fd203e0f8c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -394,6 +394,7 @@ Bug Fixes to C++ Support
   expression references to an entity declared outside of the lambda. (#GH64808)
 - Clang's __builtin_bit_cast will now produce a constant value for records 
with empty bases. See:
   (#GH82383)
+- Fix a crash when instantiating a lambda that captures ``this`` outside of 
its context. Fixes (#GH85343).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index 2d22692f3ab750..f2f7d7ab9c7c38 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -13714,6 +13714,16 @@ TreeTransform::TransformLambdaExpr(LambdaExpr 
*E) {
 
 // Capturing 'this' is trivial.
 if (C->capturesThis()) {
+  // If this is a lambda that is part of a default member initialiser
+  // and which we're instantiating outside the class that 'this' is
+  // supposed to refer to, adjust the type of 'this' accordingly.
+  //
+  // Otherwise, leave the type of 'this' as-is.
+  Sema::CXXThisScopeRAII ThisScope(
+  getSema(),
+  dyn_cast_if_present(
+  getSema().getFunctionLevelDeclContext()),
+  Qualifiers());
   getSema().CheckCXXThisCapture(C->getLocation(), C->isExplicit(),
 /*BuildAndDiagnose*/ true, nullptr,
 C->getCaptureKind() == LCK_StarThis);
diff --git a/clang/test/Sema/PR85343.cpp b/clang/test/Sema/PR85343.cpp
new file mode 100644
index 00..d90ef19d423455
--- /dev/null
+++ b/clang/test/Sema/PR85343.cpp
@@ -0,0 +1,22 @@
+// RUN: %clang_cc1 -std=c++14 -verify %s
+// expected-no-diagnostics
+
+template  auto ab() -> c ;
+
+template  struct e {};
+
+template  struct ac {
+  template  static e()(ab))> i;
+  decltype(i) j;
+};
+
+struct d {
+  template 
+  d(f) { 
+ac a;
+  }
+};
+struct a {
+  d b = [=](auto) { (void)[this] {}; };
+};
+void b() { new a; }

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


[clang] [Clang][Sema] Fix a crash in lambda instantiation (PR #85565)

2024-03-18 Thread Qizhi Hu via cfe-commits

jcsxky wrote:

@Sirraide Very thankful for your comments and it really makes the description 
more clear and easy to be understood! I have updated this patch following your 
suggestion and please take another look.

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


[clang] [Clang][Sema] Fix a crash in lambda instantiation (PR #85565)

2024-03-18 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky updated 
https://github.com/llvm/llvm-project/pull/85565

>From e94505fa77155cb0bbdf3ef92a426ef070cb3833 Mon Sep 17 00:00:00 2001
From: huqizhi 
Date: Sun, 17 Mar 2024 17:48:05 +0800
Subject: [PATCH] [Clang][Sema] Fix a crash in lambda instantiation

---
 clang/docs/ReleaseNotes.rst|  1 +
 clang/lib/Sema/TreeTransform.h |  6 ++
 clang/test/Sema/PR85343.cpp| 22 ++
 3 files changed, 29 insertions(+)
 create mode 100644 clang/test/Sema/PR85343.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 125d51c42d507f..a446fd203e0f8c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -394,6 +394,7 @@ Bug Fixes to C++ Support
   expression references to an entity declared outside of the lambda. (#GH64808)
 - Clang's __builtin_bit_cast will now produce a constant value for records 
with empty bases. See:
   (#GH82383)
+- Fix a crash when instantiating a lambda that captures ``this`` outside of 
its context. Fixes (#GH85343).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index 2d22692f3ab750..92a08bae03b485 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -13714,6 +13714,12 @@ TreeTransform::TransformLambdaExpr(LambdaExpr 
*E) {
 
 // Capturing 'this' is trivial.
 if (C->capturesThis()) {
+  // We need ThisType when build capture in CheckCXXThisCapture.
+  Sema::CXXThisScopeRAII ThisScope(
+  getSema(),
+  dyn_cast_if_present(
+  getSema().getFunctionLevelDeclContext()),
+  Qualifiers());
   getSema().CheckCXXThisCapture(C->getLocation(), C->isExplicit(),
 /*BuildAndDiagnose*/ true, nullptr,
 C->getCaptureKind() == LCK_StarThis);
diff --git a/clang/test/Sema/PR85343.cpp b/clang/test/Sema/PR85343.cpp
new file mode 100644
index 00..d90ef19d423455
--- /dev/null
+++ b/clang/test/Sema/PR85343.cpp
@@ -0,0 +1,22 @@
+// RUN: %clang_cc1 -std=c++14 -verify %s
+// expected-no-diagnostics
+
+template  auto ab() -> c ;
+
+template  struct e {};
+
+template  struct ac {
+  template  static e()(ab))> i;
+  decltype(i) j;
+};
+
+struct d {
+  template 
+  d(f) { 
+ac a;
+  }
+};
+struct a {
+  d b = [=](auto) { (void)[this] {}; };
+};
+void b() { new a; }

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


[clang] [Clang][Sema] Fix a crash in lambda instantiation (PR #85565)

2024-03-17 Thread Qizhi Hu via cfe-commits

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


[clang] [Clang][Sema] Fix a crash in lambda instantiation (PR #85565)

2024-03-17 Thread Qizhi Hu via cfe-commits

https://github.com/jcsxky updated 
https://github.com/llvm/llvm-project/pull/85565

>From 535617d786799c7657155e6e2cfa34fd3070f840 Mon Sep 17 00:00:00 2001
From: huqizhi 
Date: Sun, 17 Mar 2024 17:48:05 +0800
Subject: [PATCH] [Clang][Sema] Fix a crash in lambda instantiation

---
 clang/docs/ReleaseNotes.rst|  1 +
 clang/lib/Sema/TreeTransform.h |  6 ++
 clang/test/Sema/PR85343.cpp| 22 ++
 3 files changed, 29 insertions(+)
 create mode 100644 clang/test/Sema/PR85343.cpp

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 125d51c42d507f..2587c9d002c2c9 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -394,6 +394,7 @@ Bug Fixes to C++ Support
   expression references to an entity declared outside of the lambda. (#GH64808)
 - Clang's __builtin_bit_cast will now produce a constant value for records 
with empty bases. See:
   (#GH82383)
+- Fix a crash in lambda instantiation that missing set ``ThisType`` when 
checking capture. Fixes (#GH85343).
 
 Bug Fixes to AST Handling
 ^
diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index 2d22692f3ab750..9d638d27d5f51c 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -13714,6 +13714,12 @@ TreeTransform::TransformLambdaExpr(LambdaExpr 
*E) {
 
 // Capturing 'this' is trivial.
 if (C->capturesThis()) {
+  // We need ThisType here.
+  Sema::CXXThisScopeRAII ThisScope(
+  getSema(),
+  dyn_cast_or_null(
+  getSema().getFunctionLevelDeclContext()),
+  Qualifiers());
   getSema().CheckCXXThisCapture(C->getLocation(), C->isExplicit(),
 /*BuildAndDiagnose*/ true, nullptr,
 C->getCaptureKind() == LCK_StarThis);
diff --git a/clang/test/Sema/PR85343.cpp b/clang/test/Sema/PR85343.cpp
new file mode 100644
index 00..d90ef19d423455
--- /dev/null
+++ b/clang/test/Sema/PR85343.cpp
@@ -0,0 +1,22 @@
+// RUN: %clang_cc1 -std=c++14 -verify %s
+// expected-no-diagnostics
+
+template  auto ab() -> c ;
+
+template  struct e {};
+
+template  struct ac {
+  template  static e()(ab))> i;
+  decltype(i) j;
+};
+
+struct d {
+  template 
+  d(f) { 
+ac a;
+  }
+};
+struct a {
+  d b = [=](auto) { (void)[this] {}; };
+};
+void b() { new a; }

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


  1   2   3   4   5   >