This is an archive of the discontinued LLVM Phabricator instance.

[clangd] don't insert new includes if either original header or canonical header is already included.
ClosedPublic

Authored by ioeric on Feb 20 2018, 6:31 AM.

Details

Summary

Changes:
o Store both the original header and the canonical header in LSP command.
o Also check that both original and canonical headers are not already included
by comparing both resolved header path and written literal includes.

This addresses the use case where private IWYU pragma is defined in a private
header while it would still be preferrable to include the private header, in the
internal implementation file. If we have seen that the priviate header is already
included, we don't try to insert the canonical include.

Diff Detail

Repository
rL LLVM

Event Timeline

ioeric created this revision.Feb 20 2018, 6:31 AM
sammccall added inline comments.Feb 20 2018, 6:41 AM
clangd/ClangdServer.h
239 ↗(On Diff #135045)

I really do want to know what this parameter does, but I don't understand it from this comment.

243 ↗(On Diff #135045)

"this has the same semantic as OriginalHeader" contradicts "[OriginalHeader] may be different from preferredHeader" :-)

Mostly nits around naming/doc: you've convinced me that the two headers that are paths or uris or <headers> at different times is the right thing, but we need to be really clear about it.

clangd/ClangdServer.cpp
320 ↗(On Diff #135045)

For readability, I'd suggest pulling this out into a real function, and returning something like
struct HeaderFile { std::string File; bool Verbatim; } which could be passed to the Headers.h functions.

The fold of "literal or uri" -> "literal or file" all typed as 'string' is a bit too much I think.
But maybe more explicit names and comments would moke this clearer.

clangd/ClangdServer.h
239 ↗(On Diff #135045)

Discussed offline - this is the header file (either URI or <header>) that directly declares the symbol.
I'd suggest calling this DeclaringHeader.

240 ↗(On Diff #135045)

the "this may be different" probably belongs after the doc of PreferredHeader (backreferences are easier to follow than forward ones)

243 ↗(On Diff #135045)

T think InsertedHeader might be clearer about its role.

243 ↗(On Diff #135045)

I'd suggest being explicit, since this is different to above: "if this is a URI, insertInclude will determine how to spell it. If this is a quoted string, it will be inserted verbatim".

244 ↗(On Diff #135045)

Drop the "empty" special case, the caller should always pass this in.

246 ↗(On Diff #135045)

Worth pointing out at the end "Both OriginalHeader and InsertedHeader will be considered to determine whether an include needs to be added".

clangd/Headers.cpp
72 ↗(On Diff #135045)

how could this happen?

126 ↗(On Diff #135045)

oops, missed this in the previous commit - this isn't an error condition, but a "return empty string" condition.

Can we add a test for this?

clangd/Headers.h
23 ↗(On Diff #135045)

only used in headers.cpp, make static?

29 ↗(On Diff #135045)

again, this special case isn't needed as copying a stringref is cheap

33 ↗(On Diff #135045)

when would originalheader be empty?

37 ↗(On Diff #135045)

again - not quite true.

clangd/Protocol.h
434 ↗(On Diff #135045)

(also update comments here)

ioeric updated this revision to Diff 135074.Feb 20 2018, 9:02 AM
ioeric marked 16 inline comments as done.
  • Addressed review comments.
ioeric added inline comments.Feb 20 2018, 9:02 AM
clangd/ClangdServer.h
243 ↗(On Diff #135045)

Bad wording... sorry about that!

clangd/Headers.cpp
126 ↗(On Diff #135045)

There are tests for this in HeadersTests.cpp. It's just that in the tests, we treat both errors and empty insertion as no insertion.

clangd/Headers.h
23 ↗(On Diff #135045)

Oops, the same logic appeared in ClangdServer. I wanted to share this but forgot to...

sammccall accepted this revision.Feb 23 2018, 5:59 AM

Thanks, and sorry for the delay!

clangd/Headers.cpp
45 ↗(On Diff #135074)

nit: StringSet? std::set is pretty bad unless we need ordering

This revision is now accepted and ready to land.Feb 23 2018, 5:59 AM
ioeric updated this revision to Diff 135857.Feb 26 2018, 12:29 AM
ioeric marked an inline comment as done.
  • Merge with origin/master
  • Use llvm::StringSet
This revision was automatically updated to reflect the committed changes.