This is an archive of the discontinued LLVM Phabricator instance.

[AST] Fix the EndLoc calculation for ObjCObjectPointer
ClosedPublic

Authored by lpetre on Oct 19 2021, 3:22 PM.

Details

Summary

There is an issue where the AST code does not compute the correct SourceRange for a ObjCObjectPointer.

From Richard Smith (ie @zygoloid) in discord:

I think the problem is that we set an invalid location for the * (because there isn't one): https://github.com/llvm/llvm-project/blob/main/clang/lib/Sema/SemaType.cpp#L1121
And then we use the default getLocalSourceRangeImpl for a PointerLikeTypeLoc that just assumes the * location is the type's end location: https://github.com/llvm/llvm-project/blob/main/clang/include/clang/AST/TypeLoc.h#L1293
Possibly we should be special-casing that here: https://github.com/llvm/llvm-project/blob/main/clang/lib/AST/TypeLoc.cpp#L228

My change:

  • introduces a AST dump test to show the issue in the first commit
  • special cases ObjCObjectPointerType in the second commit to correctly compute the end location

Diff Detail

Event Timeline

lpetre created this revision.Oct 19 2021, 3:22 PM
lpetre updated this revision to Diff 381958.Oct 25 2021, 5:54 AM

Updating diff with full commit range

My initial arc diff only picked up the final commit, now running for all commits

lpetre updated this revision to Diff 381960.Oct 25 2021, 5:58 AM

Trying again to include both commits

lpetre published this revision for review.Oct 25 2021, 6:04 AM
lpetre retitled this revision from Fix the EndLoc calculation for ObjCObjectPointer to [AST] Fix the EndLoc calculation for ObjCObjectPointer.
lpetre edited the summary of this revision. (Show Details)
lpetre added a reviewer: rsmith.
lpetre added a project: Restricted Project.
lpetre added a subscriber: bruno.
aaron.ballman accepted this revision.Oct 25 2021, 12:23 PM

LGTM! Do you need someone to commit on your behalf? If so, let me know what name and email address you would like me to use for patch attribution and I can land it after giving a day or two for @rsmith to review.

This revision is now accepted and ready to land.Oct 25 2021, 12:23 PM

I've commit on your behalf in a9db0a804a5335b6534995c54ab9d6fcef06e739, thank you for the fix!

I think this is still not quite right: if the ObjCObjectPointerType has a valid star location, we should use that. We should only be falling back on the inner location's end when there is no star. For example:

@interface X @end;
using ObjectPointer = X*;

... should have an end loc that points at the *, not the X.

I think this is still not quite right

Fixed in rG68ffcd521347e3c8b97ca233239d503beff239f4.

I think this is still not quite right

Fixed in rG68ffcd521347e3c8b97ca233239d503beff239f4.

Thanks for the quick fix!