This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Canonicalize file path in URIForFile.
ClosedPublic

Authored by ioeric on Nov 23 2018, 2:23 AM.

Details

Summary

File paths in URIForFile can come from index or local AST. Path from
index goes through URI transformation and the final path is resolved by URI
scheme and could be potentially different from the original path. Hence, we
should do the same transformation for all paths. We do this in URIForFile, which
now converts a path to URI and back to a canonicalized path.

Diff Detail

Event Timeline

ioeric created this revision.Nov 23 2018, 2:23 AM
sammccall added inline comments.Nov 26 2018, 9:54 AM
clangd/Protocol.h
74

We need to document the APIs and motivation, these are subtle.

75

I think it's no longer sufficiently clear what this constructor does - it should be a named factory function, and the HintPath should be required.
(If it's easy to implicitly skip canonicalization, then there's no advantage of changing URIForFile vs just asking people to canonicalize the inputs)

e.g. URIForFile URIForFile::canonicalize(StringRef AbsPath, StringRef HintPath)

75

As we're exposing HintPath in more far-flung places, its semantics are becoming less obvious.

I think we should make them more concrete, e.g. rename to TUPath and doc like

Files can be referred to by several paths (e.g. in the presence of links).
Which one we prefer may depend on where we're coming from.
TUPath is a hint, and should usually be the main entrypoint file we're processing.
109

Are there really *no* cases where the caller doesn't need canonicalization?

clangd/URI.cpp
229

this seems pretty indirect - including the call to create(), this function special-cases File 3 times.

Seems a little cleaner to me to remove "file" from the registry, and repeat the loop here. Up to you.

ioeric updated this revision to Diff 175466.Nov 27 2018, 5:48 AM
ioeric marked 7 inline comments as done.

address comments and rebase

Thanks for the review! PTAL

clangd/Protocol.h
75

Sounds good. Thanks!

109

As chatted offline, there seems to be no cases where we don
t need canonicalization. We didn't do this in unit test, but we should do via matchers.

sammccall accepted this revision.Nov 27 2018, 6:22 AM

Thanks!
Mostly just places that should be updated HintPath -> TUPath. Changing the whole codebase doesn't seem important, but in places that are touched...

clangd/Protocol.cpp
45

TUPath

64–67

maybe more explicit: input should be "file" or "test" scheme, which do not...

clangd/Protocol.h
88

TUPath

clangd/URI.cpp
221

TUPath

228

TUPath

clangd/URI.h
65

TUPath (and in comment)

72

TUPath

unittests/clangd/TestFS.cpp
95

TUPath

unittests/clangd/URITests.cpp
140

TUPath

This revision is now accepted and ready to land.Nov 27 2018, 6:22 AM

Mostly just places that should be updated HintPath -> TUPath. Changing the whole codebase doesn't seem important, but in places that are touched...

As chatted offline, URIForFile is closer to ClangdServer, so TUPath is a good fit. But URI is more generic, and HintPath still seems to be a good fit there.

This revision was automatically updated to reflect the committed changes.