swift icon indicating copy to clipboard operation
swift copied to clipboard

[cxx-interop] Fix -verify-additional-file on Windows

Open j-hui opened this issue 9 months ago • 5 comments

SourceManager uses associates paths to buffer IDs using BufIdentIDMap. However, on Windows, both '' and '/' are valid path separators, so foo\bar\baz.cpp foo/bar\baz.cpp and foo/bar/baz.cpp all refer to the same file, but will be stored as separate entries in BufIdentIDMap. This led -verify-additional-file to misbehave because it would create multiple buffer ID numbers for the same path, which confused the diagnostic verifier logic.

This patch normalizes paths before looking them up in BufIdentIDMap, so that buffer IDs are uniquely associated with each file rather than each path.

j-hui avatar Apr 06 '25 05:04 j-hui

@swift-ci please smoke test

j-hui avatar Apr 06 '25 05:04 j-hui

There are other ways for paths to be denormalised than just forward-slash vs backward-slash. Do we care about those? E.g:

  • some file systems are case insensitive
  • relative vs absolute paths
  • redundant dots: Cxx/include/cxx-header.h vs ./Cxx/include/cxx-header.h vs Cxx/./include/cxx-header.h vs Cxx/../Cxx/include/cxx-header.h

hnrklssn avatar Apr 06 '25 20:04 hnrklssn

@swift-ci please smoke test

j-hui avatar Apr 07 '25 01:04 j-hui

There are other ways for paths to be denormalised than just forward-slash vs backward-slash. Do we care about those?

The intention of this change is only to resolve the issue we face in -verify, which, as far as I'm aware, is for our own test suite. So unless purposely specify path collisions like that, we probably won't run into those kinds of issues (and if we do, we can normalize as necessary).

In the meantime, I'd like not to add too much overhead/complexity to this code path.

j-hui avatar Apr 07 '25 01:04 j-hui

Can you please add a radar link to this patch @j-hui ?

fahadnayyar avatar Apr 08 '25 07:04 fahadnayyar

I forgot that the StringRef key is also saved in the BufIdentIDMap, so creating a new entry with a stack-allocated std::string causes a lifetime issue (which is what is causing CI to fail).

Lifetime issues aside: upon discussions with some other folks, I've come around to the fact that canonicalization may not be the right approach here, since it comes with other pitfalls. I'm closing this PR in favor of simply refactoring the tests to use %{fs-sep}. That should only affect the handful of tests using -verify-additional-file.

j-hui avatar Apr 09 '25 18:04 j-hui