This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Improve hover scopes for Objective-C code
ClosedPublic

Authored by dgoldman on Oct 7 2019, 2:05 PM.

Details

Summary
  • Instead of AppDelegate::application:didFinishLaunchingWithOptions: you will now see -[AppDelegate application:didFinishLaunchingWithOptions:]
  • Also include categories in the name when printing the scopes, e.g. Class(Category) and -[Class(Category) method]

Diff Detail

Event Timeline

dgoldman created this revision.Oct 7 2019, 2:05 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 7 2019, 2:05 PM

Thanks! This generally looks good, just need to find the right home for some of the logic.

clangd/XRefs.cpp
455 ↗(On Diff #223646)

XRefs.cpp isn't really the right place for this.
If there isn't somewhere appropriate in clangAST, then making this a case of printName in AST.h is probably best.

It should have unit tests (rather than testing it all through hover)

532 ↗(On Diff #223646)

Maybe "skip over Type/Function/ObjC methods, these are part of the local scope instead".

(Not sure about "objc decl" as it seems to cover a lot of other things too)

clangd/unittests/XRefsTests.cpp
1029 ↗(On Diff #223646)

combine test cases with above.
Do we need so many? I think 2 plus some unit-tests of the "pretty-print a decl" code should be enough

1177 ↗(On Diff #223646)

you've got a loop here but only one test case. Combine with the other loop or just assert directly?

kadircet added inline comments.Oct 18 2019, 12:57 AM
clangd/XRefs.cpp
461 ↗(On Diff #223646)

it looks like DeclPrinter::VisitObjCMethodDecl already has a similar handling.
would it provide value if you printed class and category names in the general case? If so it might make sense to update the logic in DeclPrinter.
If not, it looks like printing selector via getAsString is not enough, there's some special handling in DeclPrinter. Maybe move it into a public
place and make use of the same logic also in here to be consistent?

470 ↗(On Diff #223646)

again there are printers in for ObjCCategory{Impl}Decls that don't respect TerseOutput option in printing policies. It might be sensible to update them instead to print an output like what you propose in here, in the presence of TerseOutput option. WDYT?

509 ↗(On Diff #223646)

instead of saying what you do, could you say why you do it. IIUC, it is because the relevant bits of the container is already printed in getNameForObjCMethod right?

dgoldman marked 3 inline comments as done.Nov 4 2019, 12:10 PM

Will revisit this once more critical fixes are in (crash fixes), I'm still not sure where this sort of stuff should belong

clangd/XRefs.cpp
461 ↗(On Diff #223646)

DeclPrinter::VisitObjCMethodDecl prints the declaration itself, e.g.:

- (void)someMethod:(int)param;

it wouldn't make sense to instead of that do

-[MyClass someMethod:] for the declaration itself

470 ↗(On Diff #223646)

What method names are you looking at? I wasn't able to find them

We'll also want to do something similar for DocumentSymbols, see here, which will lead to Objective-C categories showing up as either (anonymous) or <category name>

dgoldman updated this revision to Diff 309611.Dec 4 2020, 12:15 PM
  • Rebase + refactor, sorry for the long delay
dgoldman retitled this revision from [clangd] Improve hover support for Objective-C to [clangd] Improve hover scopes for Objective-C code.Dec 4 2020, 12:20 PM
dgoldman edited the summary of this revision. (Show Details)
dgoldman added a comment.EditedDec 4 2020, 12:27 PM

I think there's still some more work to be done after this which might move some of this around:

  • DocumentSymbol support: Currently categories show up as (anonymous) or <category name> (see here).
  • QualifiedNames for ObjC, see the conversation here, possibly MyClass.property and MyClass(Category)->Ivar? Someone from Apple might know more. The current :: behavior is from here
dgoldman updated this revision to Diff 309983.Dec 7 2020, 12:08 PM

Swap to isa

dgoldman updated this revision to Diff 309987.Dec 7 2020, 12:16 PM

Add protocol test

I messed around with qualified name changes - lots of things internally rely upon the qualified names so that will be best for a separate change. I could potentially handle DocumentSymbol fixes in here though - LMK if you think it makes sense to move some of this logic in AST.cpp (clangd). objcContainerLocalScope seems like it would be useful to generalize support for objc container decls by ensuring that categories becoming fully qualified with their class name.

Friendly ping, I think this is still worth merging in even without the QName changes

sammccall accepted this revision.Feb 10 2021, 9:23 AM

Sorry for losing this.

LMK if you think it makes sense to move some of this logic in AST.cpp (clangd). objcContainerLocalScope seems like it would be useful to generalize support for objc container decls by ensuring that categories becoming fully qualified with their class name

Yes, please move these to AST.cpp, maybe rename to printObjCMethod and printObjCContainer.

clang-tools-extra/clangd/Hover.cpp
64

this function's name doesn't really describe what it's for

it has 3 callsites and is only needed in 2, just inline it?

71

looking at the implementation, this is null iff the method is part of a protocol.
<<error-type>> doesn't seem appropriatefor that case

78

prefer CID->getName(), the operator<< is really surprising here

80

nit: Method->getSelector().print(OS << ' ');

(yeah, the implementation is dumb, but maybe they'll fix it)

85

you're neither flushing nor destroying the OS, this is fragile.

(From playing with godbolt, it looks like this works as long as move-elision kicks in, but adding if (false) return "" at the top prevents the stream from being flushed here)

96

can't you return objcContainerLocalScope(CI->getCategoryDecl()) ?

149

nit: isa<ObjCMethodDecl, ObjCContainerDecl>(DC)

I'm not sure the comment adds much here (particularly "we return an empty string") - maybe remove?

This revision is now accepted and ready to land.Feb 10 2021, 9:23 AM
dgoldman updated this revision to Diff 323141.Feb 11 2021, 1:53 PM
dgoldman marked 7 inline comments as done.
  • Address review comments

Will move over to AST.cpp in next update

clang-tools-extra/clangd/Hover.cpp
64

Worth keeping, see below, Open to naming suggestions =)

71

It can also be NULL for invalid code (in category).

Since we only call this for Decls in this decl context protocols can't ever appear as protocols can't have method definitions. Do you think it's still worth handling?

96

As noted in getCategoryDecl() the class interface can be NULL when working with invalid code.

sammccall accepted this revision.Feb 11 2021, 2:04 PM
sammccall added inline comments.
clang-tools-extra/clangd/Hover.cpp
64

nameOrError?

71

ah, I see...

yes I think the protocol case is worth handling in order to give this a simple signature in AST.h (prettyprint an ObjC method, without any surprising caveats)

dgoldman updated this revision to Diff 323151.Feb 11 2021, 2:14 PM
  • Move over to AST.cpp
dgoldman updated this revision to Diff 323156.Feb 11 2021, 2:25 PM
dgoldman marked an inline comment as done.
  • Support protocols
dgoldman marked an inline comment as done.Feb 11 2021, 2:26 PM
dgoldman edited the summary of this revision. (Show Details)
This revision was automatically updated to reflect the committed changes.