[clang] [libcxx] [clang] Preserve Qualifiers and type sugar in TemplateNames (PR #93433)

2024-06-07 Thread Matheus Izvekov via cfe-commits

mizvekov wrote:

> so after this patch, clang seems to be crashing on:

Thanks for the reproducer. Yeah this is about default argument deduction, which 
doesn't involve default arguments as written in source.

I confirm the crash and it's indeed caused by changes in this patch. The fix is 
very simple and I will be posting a patch shortly.



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


[clang] [libcxx] [clang] Preserve Qualifiers and type sugar in TemplateNames (PR #93433)

2024-06-07 Thread kadir çetinkaya via cfe-commits

kadircet wrote:

so after this patch, clang seems to be crashing on:
```cpp
template  struct map {};
template  class foo {};

template  class MapType, typename Value>
Value bar(MapType map);

template  class MapType, typename Value>
Value bar(MapType> map);

void aux() {
  map> input;
  bar(input);
}
```

stack trace:
```
$ ./bin/clang -fsyntax-only -xc++ -std=c++20 -Wno-everything foo.cc
clang: 
/usr/local/google/home/kadircet/repos/llvm/clang/lib/Sema/SemaTemplateDeduction.cpp:596:
 TemplateDeductionResult DeduceTemplateArguments(Sema &, TemplateParameterList 
*, TemplateName, TemplateName, TemplateDeductionInfo &, 
ArrayRef, SmallVectorImpl &): 
Assertion `DefaultArguments.size() <= As->size()' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and 
include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0.  Program arguments: ./bin/clang -fsyntax-only -xc++ -std=c++20 
-Wno-everything myreduce.cc
1.  myreduce.cc:12:12: current parser token ')'
2.  myreduce.cc:10:12: parsing function body 'aux'
3.  myreduce.cc:10:12: in compound statement ('{}')
 #0 0x561942c22558 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) 
/usr/local/google/home/kadircet/repos/llvm/llvm/lib/Support/Unix/Signals.inc:723:13
 #1 0x561942c2011e llvm::sys::RunSignalHandlers() 
/usr/local/google/home/kadircet/repos/llvm/llvm/lib/Support/Signals.cpp:106:18
 #2 0x561942b8c686 HandleCrash 
/usr/local/google/home/kadircet/repos/llvm/llvm/lib/Support/CrashRecoveryContext.cpp:73:5
 #3 0x561942b8c686 CrashRecoverySignalHandler(int) 
/usr/local/google/home/kadircet/repos/llvm/llvm/lib/Support/CrashRecoveryContext.cpp:390:51
 #4 0x7fa5f6e5a510 (/lib/x86_64-linux-gnu/libc.so.6+0x3c510)
 #5 0x7fa5f6ea816c __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
 #6 0x7fa5f6e5a472 raise ./signal/../sysdeps/posix/raise.c:27:6
 #7 0x7fa5f6e444b2 abort ./stdlib/abort.c:81:7
 #8 0x7fa5f6e443d5 _nl_load_domain ./intl/loadmsgcat.c:1177:9
 #9 0x7fa5f6e533a2 (/lib/x86_64-linux-gnu/libc.so.6+0x353a2)
#10 0x561945cbacbc set_size 
/usr/local/google/home/kadircet/repos/llvm/llvm/include/llvm/ADT/SmallVector.h:102:5
#11 0x561945cbacbc resizeImpl 
/usr/local/google/home/kadircet/repos/llvm/llvm/include/llvm/ADT/SmallVector.h:647:11
#12 0x561945cbacbc resize 
/usr/local/google/home/kadircet/repos/llvm/llvm/include/llvm/ADT/SmallVector.h:651:30
#13 0x561945cbacbc SmallVector 
/usr/local/google/home/kadircet/repos/llvm/llvm/include/llvm/ADT/SmallVector.h:1220:11
#14 0x561945cbacbc DeduceTemplateArguments(clang::Sema&, 
clang::TemplateParameterList*, clang::TemplateName, clang::TemplateName, 
clang::sema::TemplateDeductionInfo&, llvm::ArrayRef, 
llvm::SmallVectorImpl&) 
/usr/local/google/home/kadircet/repos/llvm/clang/lib/Sema/SemaTemplateDeduction.cpp:597:37
#15 0x561945cbde14 DeduceTemplateSpecArguments(clang::Sema&, 
clang::TemplateParameterList*, clang::QualType, clang::QualType, 
clang::sema::TemplateDeductionInfo&, 
llvm::SmallVectorImpl&) 
/usr/local/google/home/kadircet/repos/llvm/clang/lib/Sema/SemaTemplateDeduction.cpp:717:13
#16 0x561945caf095 DeduceTemplateArgumentsByTypeMatch(clang::Sema&, 
clang::TemplateParameterList*, clang::QualType, clang::QualType, 
clang::sema::TemplateDeductionInfo&, 
llvm::SmallVectorImpl&, unsigned int, bool, 
bool) 
/usr/local/google/home/kadircet/repos/llvm/clang/lib/Sema/SemaTemplateDeduction.cpp:0:16
#17 0x561945cbd8e2 DeduceTemplateArguments(clang::Sema&, 
clang::TemplateParameterList*, clang::QualType const*, unsigned int, 
clang::QualType const*, unsigned int, clang::sema::TemplateDeductionInfo&, 
llvm::SmallVectorImpl&, unsigned int, bool) 
/usr/local/google/home/kadircet/repos/llvm/clang/lib/Sema/SemaTemplateDeduction.cpp:1223:44
#18 0x561945cb5910 isAtLeastAsSpecializedAs(clang::Sema&, 
clang::SourceLocation, clang::FunctionTemplateDecl const*, 
clang::FunctionTemplateDecl const*, clang::TemplatePartialOrderingContext, 
bool, llvm::SmallVector const&, 
llvm::SmallVector const&) 
/usr/local/google/home/kadircet/repos/llvm/clang/lib/Sema/SemaTemplateDeduction.cpp:0:0
#19 0x561945cb45df 
clang::Sema::getMoreSpecializedTemplate(clang::FunctionTemplateDecl*, 
clang::FunctionTemplateDecl*, clang::SourceLocation, 
clang::TemplatePartialOrderingContext, unsigned int, clang::QualType, 
clang::QualType, bool) 
/usr/local/google/home/kadircet/repos/llvm/clang/lib/Sema/SemaTemplateDeduction.cpp:0:18
#20 0x561945aef302 clang::isBetterOverloadCandidate(clang::Sema&, 
clang::OverloadCandidate const&, clang::OverloadCandidate const&, 
clang::SourceLocation, clang::OverloadCandidateSet::CandidateSetKind) 
/usr/local/google/home/kadircet/repos/llvm/clang/lib/Sema/SemaOverload.cpp:0:50
#21 0x561945ae0836 
clang::OverloadCandidateSet::BestViableFunction(clang::Sema&, 
clang::SourceLocation, clang::OverloadCandidate*&) 

[clang] [libcxx] [clang] Preserve Qualifiers and type sugar in TemplateNames (PR #93433)

2024-05-31 Thread via cfe-commits

pcc wrote:

> @pcc #93926 should fix it, can you double check that it gets all issues?

Yes, the bot is green now, thanks.

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


[clang] [libcxx] [clang] Preserve Qualifiers and type sugar in TemplateNames (PR #93433)

2024-05-31 Thread Matheus Izvekov via cfe-commits

mizvekov wrote:

@pcc https://github.com/llvm/llvm-project/pull/93926 should fix it, can you 
double check that it gets all issues?

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


[clang] [libcxx] [clang] Preserve Qualifiers and type sugar in TemplateNames (PR #93433)

2024-05-30 Thread via cfe-commits

pcc wrote:

This commit broke the UBSan buildbot: 
https://lab.llvm.org/buildbot/#/builders/85/builds/24378

Can you please take a look?

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


[clang] [libcxx] [clang] Preserve Qualifiers and type sugar in TemplateNames (PR #93433)

2024-05-30 Thread Matheus Izvekov via cfe-commits

mizvekov wrote:

> Could you please take another look?

Sure, but let's take the discussion to the other PR.

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


[clang] [libcxx] [clang] Preserve Qualifiers and type sugar in TemplateNames (PR #93433)

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

jcsxky wrote:

@mizvekov After I apply you changes in this patch, 
https://github.com/llvm/llvm-project/issues/91677 is still crash(although it 
makes sense with `-ast-dump` option). I put it below for explanation clearly:
```cpp
template  struct t1 {
  template 
  struct t2 {};
};

template 
t1::template t2 f1();

void f2() {
  f1();
}
```

The reason seems that we transform `TemplateDecl` to the original one and do 
nothing and then crash in codegen stage. For `t1::t2`, we have 
transformed qualifier(`t1`) in `TransformElaboratedType`. But, we transform 
qualifier in `TransformTemplateName` for `t2` again because of transforming 
`TransformTemplateSpecializationType`. This leads to the  transformation of the 
`TemplateDecl` as I described in the beginning.
I think we can reuse the transformed qualifier(`t1`) in 
`TransformElaboratedType` and this is what I did in 
https://github.com/llvm/llvm-project/pull/93411. Could you please take another 
look?

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


[clang] [libcxx] [clang] Preserve Qualifiers and type sugar in TemplateNames (PR #93433)

2024-05-30 Thread Matheus Izvekov via cfe-commits

mizvekov wrote:

@nikic Thanks for the heads-up. I expected some impact. There are some ideas to 
regain that loss in follow up work.

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


[clang] [libcxx] [clang] Preserve Qualifiers and type sugar in TemplateNames (PR #93433)

2024-05-30 Thread Nikita Popov via cfe-commits

nikic wrote:

This change seems to have some compile-time overhead. I'm not seeing a lot on 
CTMark, but during the clang bootstrap there are many files with ~1% 
regressions 
(http://llvm-compile-time-tracker.com/compare_clang.php?from=ec8fe598a94d2826f8e4f79367a5a45a6b32d284=9c4a716c1292096fcbdf415b63b7b0122b03310f=instructions%3Au=interestingness).
 Just an FYI in case this is unexpected.

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


[clang] [libcxx] [clang] Preserve Qualifiers and type sugar in TemplateNames (PR #93433)

2024-05-29 Thread Matheus Izvekov via cfe-commits

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


[clang] [libcxx] [clang] Preserve Qualifiers and type sugar in TemplateNames (PR #93433)

2024-05-29 Thread via cfe-commits

github-actions[bot] wrote:




:warning: C/C++ code formatter, clang-format found issues in your code. 
:warning:



You can test this locally with the following command:


``bash
git-clang-format --diff 1a2f3309765fdc143fdc3809211fb85d2e2ca341 
8322ce12520d1d4ab96b58195bd91f62a7b95c06 -- 
clang/include/clang/AST/TemplateName.h clang/include/clang/Sema/Sema.h 
clang/lib/AST/ASTContext.cpp clang/lib/AST/DeclTemplate.cpp 
clang/lib/AST/ODRHash.cpp clang/lib/AST/TemplateBase.cpp 
clang/lib/AST/TemplateName.cpp clang/lib/AST/TextNodeDumper.cpp 
clang/lib/AST/Type.cpp clang/lib/AST/TypePrinter.cpp 
clang/lib/Sema/SemaDecl.cpp clang/lib/Sema/SemaDeclCXX.cpp 
clang/lib/Sema/SemaExpr.cpp clang/lib/Sema/SemaExprMember.cpp 
clang/lib/Sema/SemaTemplate.cpp clang/lib/Sema/SemaTemplateDeduction.cpp 
clang/lib/Sema/SemaType.cpp clang/lib/Sema/TreeTransform.h 
clang/test/AST/ast-dump-ctad-alias.cpp clang/test/AST/ast-dump-decl.cpp 
clang/test/AST/ast-dump-expr.cpp clang/test/AST/ast-dump-template-decls.cpp 
clang/test/AST/ast-dump-template-name.cpp 
clang/test/AST/ast-dump-using-template.cpp clang/test/CXX/drs/cwg1xx.cpp 
clang/test/CXX/over/over.match/over.match.funcs/over.match.oper/p3-2a.cpp 
clang/test/CXX/temp/temp.fct.spec/temp.deduct/temp.deduct.type/p9-0x.cpp 
clang/test/Index/print-type.cpp clang/test/OpenMP/declare_mapper_messages.cpp 
clang/test/Parser/cxx-template-template-recovery.cpp 
clang/test/SemaCXX/cxx1y-variable-templates_in_class.cpp 
clang/test/SemaTemplate/cwg2398.cpp 
clang/test/SemaTemplate/instantiate-requires-expr.cpp 
clang/test/SemaTemplate/nested-implicit-deduction-guides.cpp 
clang/unittests/AST/TemplateNameTest.cpp 
libcxx/test/std/containers/associative/map/map.cons/deduct.verify.cpp 
libcxx/test/std/containers/associative/multimap/multimap.cons/deduct.verify.cpp 
libcxx/test/std/containers/associative/multiset/multiset.cons/deduct.verify.cpp 
libcxx/test/std/containers/associative/set/set.cons/deduct.verify.cpp 
libcxx/test/std/containers/container.adaptors/priority.queue/priqueue.cons/deduct.verify.cpp
 
libcxx/test/std/containers/container.adaptors/queue/queue.cons/deduct.verify.cpp
 
libcxx/test/std/containers/container.adaptors/stack/stack.cons/deduct.verify.cpp
 libcxx/test/std/containers/sequences/array/array.cons/deduct.verify.cpp 
libcxx/test/std/containers/sequences/deque/deque.cons/deduct.verify.cpp 
libcxx/test/std/containers/sequences/forwardlist/forwardlist.cons/deduct.verify.cpp
 libcxx/test/std/containers/sequences/list/list.cons/deduct.verify.cpp 
libcxx/test/std/containers/sequences/vector/vector.cons/deduct.verify.cpp 
libcxx/test/std/containers/unord/unord.map/unord.map.cnstr/deduct.verify.cpp 
libcxx/test/std/containers/unord/unord.multimap/unord.multimap.cnstr/deduct.verify.cpp
 
libcxx/test/std/containers/unord/unord.multiset/unord.multiset.cnstr/deduct.verify.cpp
 libcxx/test/std/containers/unord/unord.set/unord.set.cnstr/deduct.verify.cpp 
libcxx/test/std/ranges/range.adaptors/range.join/ctad.verify.cpp 
libcxx/test/std/re/re.regex/re.regex.construct/deduct.verify.cpp 
libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/deduct.verify.cpp
``





View the diff from clang-format here.


``diff
diff --git 
a/libcxx/test/std/containers/associative/map/map.cons/deduct.verify.cpp 
b/libcxx/test/std/containers/associative/map/map.cons/deduct.verify.cpp
index b314e6fba6..8dfc015685 100644
--- a/libcxx/test/std/containers/associative/map/map.cons/deduct.verify.cpp
+++ b/libcxx/test/std/containers/associative/map/map.cons/deduct.verify.cpp
@@ -42,63 +42,65 @@ int main(int, char**)
 {
 {
 // cannot deduce Key and T from nothing
-std::map m; // expected-error-re {{no viable constructor or deduction 
guide for deduction of template arguments of '{{(std::)?}}map'}}
+std::map
+m; // expected-error-re {{no viable constructor or deduction guide 
for deduction of template arguments of '{{(std::)?}}map'}}
 }
 {
 // cannot deduce Key and T from just (Compare)
 std::map m(std::less{});
-// expected-error-re@-1{{no viable constructor or deduction guide 
for deduction of template arguments of '{{(std::)?}}map'}}
+// expected-error-re@-1{{no viable constructor or deduction guide for 
deduction of template arguments of '{{(std::)?}}map'}}
 }
 {
 // cannot deduce Key and T from just (Compare, Allocator)
 std::map m(std::less{}, std::allocator{});
-// expected-error-re@-1{{no viable constructor or deduction guide 
for deduction of template arguments of '{{(std::)?}}map'}}
+// expected-error-re@-1{{no viable constructor or deduction guide for 
deduction of template arguments of '{{(std::)?}}map'}}
 }
 {
 // cannot deduce Key and T from just (Allocator)
 std::map m(std::allocator{});
-// expected-error-re@-1{{no viable constructor or deduction guide 
for deduction of template arguments 

[clang] [libcxx] [clang] Preserve Qualifiers and type sugar in TemplateNames (PR #93433)

2024-05-29 Thread Kazu Hirata via cfe-commits

https://github.com/kazutakahirata updated 
https://github.com/llvm/llvm-project/pull/93433

>From 8322ce12520d1d4ab96b58195bd91f62a7b95c06 Mon Sep 17 00:00:00 2001
From: Matheus Izvekov 
Date: Sat, 25 May 2024 13:57:39 -0300
Subject: [PATCH] [clang] Preserve Qualifiers and type sugar in TemplateNames

This patch improves the preservation of qualifiers
and loss of type sugar in TemplateNames.

This problem is analogous to https://reviews.llvm.org/D112374
and this patch takes a very similar approach to that patch,
except the impact here is much lesser.

When a TemplateName was written bare, without qualifications,
we wouldn't produce a QualifiedTemplate which could be used
to disambiguate it from a Canonical TemplateName. This had
effects in the TemplateName printer, which had workarounds
to deal with this, and wouldn't print the TemplateName
as-written in most situations.

There are also some related fixes to help preserve this type
sugar along the way into diagnostics, so that this patch can
be properly tested.

- Fix dropping the template keyword.
- Fix type deduction to preserve sugar in TST TemplateNames.
---
 clang/docs/ReleaseNotes.rst   |  2 +
 clang/include/clang/AST/TemplateName.h| 19 +++---
 clang/include/clang/Sema/Sema.h   |  3 +
 clang/lib/AST/ASTContext.cpp  | 16 ++---
 clang/lib/AST/DeclTemplate.cpp|  7 +-
 clang/lib/AST/ODRHash.cpp |  9 ++-
 clang/lib/AST/TemplateBase.cpp|  2 +-
 clang/lib/AST/TemplateName.cpp| 64 +--
 clang/lib/AST/TextNodeDumper.cpp  |  4 +-
 clang/lib/AST/Type.cpp|  3 +-
 clang/lib/AST/TypePrinter.cpp |  4 +-
 clang/lib/Sema/SemaDecl.cpp   | 15 ++---
 clang/lib/Sema/SemaDeclCXX.cpp| 12 ++--
 clang/lib/Sema/SemaExpr.cpp   |  4 +-
 clang/lib/Sema/SemaExprMember.cpp |  3 +-
 clang/lib/Sema/SemaTemplate.cpp   | 25 +---
 clang/lib/Sema/SemaTemplateDeduction.cpp  | 62 +-
 clang/lib/Sema/SemaType.cpp   | 14 ++--
 clang/lib/Sema/TreeTransform.h|  8 +--
 clang/test/AST/ast-dump-ctad-alias.cpp|  6 +-
 clang/test/AST/ast-dump-decl.cpp  |  8 +--
 clang/test/AST/ast-dump-expr.cpp  |  2 +-
 clang/test/AST/ast-dump-template-decls.cpp|  6 +-
 clang/test/AST/ast-dump-template-name.cpp |  4 +-
 clang/test/AST/ast-dump-using-template.cpp|  6 +-
 clang/test/CXX/drs/cwg1xx.cpp |  4 +-
 .../over.match.oper/p3-2a.cpp |  4 +-
 .../temp.deduct/temp.deduct.type/p9-0x.cpp|  4 +-
 clang/test/Index/print-type.cpp   |  2 +-
 clang/test/OpenMP/declare_mapper_messages.cpp |  2 +-
 .../Parser/cxx-template-template-recovery.cpp |  4 +-
 .../cxx1y-variable-templates_in_class.cpp | 10 +--
 clang/test/SemaTemplate/cwg2398.cpp   |  2 +-
 .../instantiate-requires-expr.cpp |  4 +-
 .../nested-implicit-deduction-guides.cpp  |  2 +-
 clang/unittests/AST/TemplateNameTest.cpp  | 40 ++--
 .../map/map.cons/deduct.verify.cpp| 24 +++
 .../multimap/multimap.cons/deduct.verify.cpp  | 22 +++
 .../multiset/multiset.cons/deduct.verify.cpp  | 10 +--
 .../set/set.cons/deduct.verify.cpp| 10 +--
 .../priqueue.cons/deduct.verify.cpp   | 10 +--
 .../queue/queue.cons/deduct.verify.cpp|  6 +-
 .../stack/stack.cons/deduct.verify.cpp|  6 +-
 .../array/array.cons/deduct.verify.cpp|  2 +-
 .../deque/deque.cons/deduct.verify.cpp|  2 +-
 .../forwardlist.cons/deduct.verify.cpp|  2 +-
 .../list/list.cons/deduct.verify.cpp  |  2 +-
 .../vector/vector.cons/deduct.verify.cpp  |  2 +-
 .../unord.map.cnstr/deduct.verify.cpp | 16 ++---
 .../unord.multimap.cnstr/deduct.verify.cpp| 16 ++---
 .../unord.multiset.cnstr/deduct.verify.cpp| 16 ++---
 .../unord.set.cnstr/deduct.verify.cpp | 16 ++---
 .../range.adaptors/range.join/ctad.verify.cpp |  2 +-
 .../re.regex.construct/deduct.verify.cpp  |  4 +-
 .../optional.object.ctor/deduct.verify.cpp|  2 +-
 55 files changed, 316 insertions(+), 240 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index e1c6d55eeeacd..44035f48cb3f9 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -819,6 +819,8 @@ Bug Fixes to AST Handling
 - Clang now properly preserves ``FoundDecls`` within a ``ConceptReference``. 
(#GH82628)
 - The presence of the ``typename`` keyword is now stored in 
``TemplateTemplateParmDecl``.
 - Fixed malformed AST generated for anonymous union access in templates. 
(#GH90842)
+- Improved preservation of qualifiers and sugar in `TemplateNames`, including
+  template keyword.
 
 Miscellaneous Bug Fixes
 ^^^
diff --git 

[clang] [libcxx] [clang] Preserve Qualifiers and type sugar in TemplateNames (PR #93433)

2024-05-29 Thread Matheus Izvekov via cfe-commits

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