This is an archive of the discontinued LLVM Phabricator instance.

Improve ".." handling in FileSpec normalization
ClosedPublic

Authored by labath on Oct 28 2016, 8:40 AM.

Details

Summary

.. handling for windows path was completely broken because the function was
expecting \ as path separators, but we were passing it normalized file paths,
where these have been replaced by forward slashes. Apart from this, the function
was incorrect for posix paths as well in some corner cases, as well as being
generally hard to follow.

The corner cases were:

  • /../bar -> should be same as /bar
  • /bar/.. -> should be same as / (slightly dodgy as the former depends on /bar actually existing, but since we're doing it in an abstract way, I think the transformation is reasonable)

I rewrite the function to fix these corner cases and handle windows paths more
correctly. The function should now handle the posix paths (modulo symlinks, but
we cannot really do anything about that without a real filesystem). For windows
paths, there are a couple of corner cases left, mostly to do with drive letter
handling, which cannot be fixed until the rest of the class understands drive
letters better.

Diff Detail

Repository
rL LLVM

Event Timeline

labath updated this revision to Diff 76201.Oct 28 2016, 8:40 AM
labath retitled this revision from to Improve ".." handling in FileSpec normalization.
labath updated this object.
labath added reviewers: clayborg, zturner.
labath added a subscriber: lldb-commits.
amccarth added inline comments.
source/Host/common/FileSpec.cpp
550 ↗(On Diff #76201)

Do we have to worry about an unnecessary single dot in the directory, like /foo/./bar/? Are those handled when the FileSpec is constructed?

576 ↗(On Diff #76201)

Ah, here we're skipping the unnecessary single dots, but the short-circuit at the top would prevent us from getting here unless the directory also contained a /...

unittests/Host/FileSpecTest.cpp
144 ↗(On Diff #76201)

How about a test to make sure C:/foo/./bar is the same as C:/foo/bar?

212 ↗(On Diff #76201)

Here, too, I'd like to see test cases for paths with single dots.

clayborg accepted this revision.Oct 28 2016, 9:00 AM
clayborg edited edge metadata.
This revision is now accepted and ready to land.Oct 28 2016, 9:00 AM

Good point about the single dots. I'll add the tests and make sure they work properly.

labath added inline comments.Oct 31 2016, 9:27 AM
source/Host/common/FileSpec.cpp
550 ↗(On Diff #76201)

Good catch. The fast path was too optimistic. I've replaced it with a much more conservative one. It can be beefed up, if we find that this is actually a performance problem.

unittests/Host/FileSpecTest.cpp
144 ↗(On Diff #76201)

I added a bunch more tests with single dots in various places. If you see more interesting corner cases, let me know.

labath updated this revision to Diff 76426.Oct 31 2016, 9:27 AM
labath edited edge metadata.

Fix handling of single dots and add tests for that.

labath updated this revision to Diff 76428.Oct 31 2016, 9:29 AM

Fix typo in the test

This revision was automatically updated to reflect the committed changes.