[lldb] Make the ClassTemplateDecl merging logic in TypeSystemClang respect…

Authored by teemperor on Jun 15 2021, 10:23 AM.


[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

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