This is an archive of the discontinued LLVM Phabricator instance.

[lldb] Unwrap the type when dereferencing the value
ClosedPublic

Authored by werat on Nov 11 2021, 6:40 AM.

Details

Summary

The value type can be a typedef of a reference (e.g. typedef int& myint).
In this case GetQualType(type) will return clang::Typedef, which cannot
be casted to clang::ReferenceType.

Fix a regression introduced in https://reviews.llvm.org/D103532.

Diff Detail

Event Timeline

werat requested review of this revision.Nov 11 2021, 6:40 AM
werat created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptNov 11 2021, 6:40 AM
shafik added a subscriber: shafik.Nov 11 2021, 9:01 AM

I was trying to fix this a while back in D108717 but got distracted and did not get back to it.

teemperor accepted this revision.Nov 11 2021, 9:01 AM

Thanks a lot for fixing this, could you point to D103532 as the cause for the regression in the commit message?

This LGTM in general, but I think there could still be some obscure cases where this could fail.

So the switch statement this code is in (and which detects whether its a reference type) is defined as:

clang::QualType parent_qual_type(
    RemoveWrappingTypes(GetCanonicalQualType(type)));
switch (parent_qual_type->getTypeClass()) {

GetCanonicalQualType should strip all the outer and inner type sugar there is. And RemoveWrappingTypes removes *most* of the outer type sugar (which isn't necessary anymore as it should be all gone), but also makes types non-atomic (for simplicity reasons).

Meanwhile in our reference-case code we try to remove the outer type sugar (and atomic) via RemoveWrappingTypes. And if this doesn't end up in a ReferenceType the cast fails.

So IIUC there is still a possibility that we fail to cast here in case GetCanonicalQualType removes more outer sugar than RemoveWrappingTypes, which I think can only happen for type sugar classes not implemented by RemoveWrappingTypes (e.g., a quick grep tells me AttributedType and some template stuff is sugar that we don't handle).

So I would propose we do the following:

  1. This patch can land now as-is to fix the regression.
  2. It would be nice if we could expand the test with a few more variations of sugar. E.g., typedef to a reference of a typedef type and all that stuff.
  3. I think we can simplify RemoveWrappingTypes to remove all type sugar (not just our small list we maintain there). That should eliminate the obscure cases and we no longer need to maintain a list of type sugar there.
lldb/test/API/lang/cpp/dereferencing_references/main.cpp
10

nit: Could you give this a more unique name such as td_to_ref_type?

This revision is now accepted and ready to land.Nov 11 2021, 9:01 AM
werat updated this revision to Diff 387213.Nov 15 2021, 4:44 AM

Update commit message, rename test variable

werat edited the summary of this revision. (Show Details)Nov 15 2021, 4:46 AM
werat marked an inline comment as done.Nov 15 2021, 4:47 AM
This revision was automatically updated to reflect the committed changes.