[lldb] Make the ClassTemplateDecl merging logic in TypeSystemClang respect template...
authorRaphael Isemann <teemperor@gmail.com>
Tue, 15 Jun 2021 17:23:51 +0000 (19:23 +0200)
committerRaphael Isemann <teemperor@gmail.com>
Tue, 15 Jun 2021 17:25:07 +0000 (19:25 +0200)
commitb8567559cf3872d0cc2a0ed24a171da8b1ff400f
treea33983ccf584bdc576ef828118fd9dedfd9400bd
parent93aa445e16f7e27df08f69204263b618ca0448a1
[lldb] Make the ClassTemplateDecl merging logic in TypeSystemClang respect template parameters

DWARF doesn't describe templates itself but only actual template instantiations.
Because of that LLDB has to infer the parameters of the class template
declarations from the actual instantiations when creating the internal Clang AST
from debug info

Because there is no dedicated DIE for the class template, LLDB also creates the
`ClassTemplateDecl` implicitly when parsing a template instantiation. To avoid
creating one ClassTemplateDecls for every instantiation,
`TypeSystemClang::CreateClassTemplateDecl` will check if there is already a
`ClassTemplateDecl` in the requested `DeclContext` and will reuse a found
fitting declaration.

The logic that checks if a found class template fits to an instantiation is
currently just comparing the name of the template. So right now we map
`template<typename T> struct S;` to an instantiation with the values `S<1, 2,
3>` even though they clearly don't belong together.

This causes crashes later on when for example the Itanium mangler's
`TemplateArgManglingInfo::needExactType` method tries to find fitting the class
template parameter that fits to an instantiation value. In the example above it
will try to find the parameter for the value `2` but will just trigger a
boundary check when retrieving the parameter with index 1 from the class
template.

There are two ways we can end up with an instantiation that doesn't fit to a
class template with the same name:

1. We have two TUs with two templates that have the same name and internal
   linkage.
2. A forward declared template instantiation is emitted by GCC and Clang
   without an empty list of parameter values.

This patch makes the check for whether a class template declaration can be
reused more sophisticated by also comparing whether the parameter values can fit
to the found class template. If we can't find a fitting class template we
justcreate a second class template with the fitting parameters.

Fixes rdar://76592821

Reviewed By: kastiglione

Differential Revision: https://reviews.llvm.org/D100662
lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.h
lldb/test/API/lang/cpp/forward-declared-template-specialization/Makefile [new file with mode: 0644]
lldb/test/API/lang/cpp/forward-declared-template-specialization/TestCppForwardDeclaredTemplateSpecialization.py [new file with mode: 0644]
lldb/test/API/lang/cpp/forward-declared-template-specialization/main.cpp [new file with mode: 0644]
lldb/test/API/lang/cpp/incompatible-class-templates/Makefile [new file with mode: 0644]
lldb/test/API/lang/cpp/incompatible-class-templates/TestCppIncompatibleClassTemplates.py [new file with mode: 0644]
lldb/test/API/lang/cpp/incompatible-class-templates/main.cpp [new file with mode: 0644]
lldb/test/API/lang/cpp/incompatible-class-templates/other.cpp [new file with mode: 0644]
lldb/unittests/Symbol/TestTypeSystemClang.cpp