This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Use the localSourceRange as the default claimed range for typelocs.
Needs ReviewPublic

Authored by hokein on Jan 4 2022, 12:41 PM.

Details

Reviewers
sammccall
Summary

It also fixes a bug where SelectionTree doesn't select the right AST node on
int (*Fun(OuterT^ype))(InnerType);.

Diff Detail

Event Timeline

hokein created this revision.Jan 4 2022, 12:41 PM
hokein requested review of this revision.Jan 4 2022, 12:41 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 4 2022, 12:41 PM
hokein added inline comments.Jan 4 2022, 12:43 PM
clang-tools-extra/clangd/unittests/SelectionTests.cpp
234

these two new behavior changes seem to be more appropriate.

This is neat, and I we're getting closer to the principled answer.
After staring at this patch for a while, and seeing what arrays, functions, and now parens have in common, I finally understand. The problem is *declarators*.

Declarators are inside-out: ((*x)[](double) says that x is a pointer to an array to a function from double.
Whereas types that are decl-specifiers nest the other way: that would be written pointer<array<function<double>>>, just like decls or stmts nest.

So for decl-specifier TypeLocs the usual getSourceRange should apply. (Modulo hacks like decltype() where the range is just wrong).

For typelocs that are part of the declarator, there are two approaches that seem principled:

  • claim their full range, but in the opposite order (parents before children)
  • claim only the specific tokens used, in which case order doesn't matter

These are basically equivalent. declarator types have a "before" and an "after" chunk (see TypePrinter). They can claim these by claiming their full range, but the "gap" was claimed earlier, or they can claim the two ends separately.
If we can make it work, claiming the full range is probably better because it will handle unexpected stray tokens in a more natural way.
Either way we should be able to unify this with the DeclaratorDecl hack.

I'll have a play with this idea...

  • claim their full range, but in the opposite order (parents before children)

To refine this a little more, for declarator type nodes:

  • first, we should recurse into non-declarator children (such as array bounds and function parameters) and let them claim tokens
  • then we claim the range
  • then we recurse into declarator children

That said I think it's probably not worth it, there are only a few declarator nodes and the approach of not claiming the gap seems simpler.

OK, I hacked up this and your other patch into https://reviews.llvm.org/D116630.

The TypeLoc part seems a little more principled there, because:

  • most types are treated the same as any other nodes
  • the types that are treated differently have a precise reason, and we can be confident the list is exhaustive

Feel free to do what you like with that patch - review it, adopt it, discard it.

nridge added a subscriber: nridge.Jan 12 2022, 8:52 PM