[PATCH] D113716: sys::path: Detect posix paths starting with ~ as absolute

Pavel Labath via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 11 23:07:09 PST 2021


labath added a comment.

I don't believe we should be treating these paths as absolute at such a deep level. ~ is is a purely a userspace artifact used by shells and programs wishing to provide similar UI convenience. It is not recognized by any system calls, nor by any filesystem library that I am aware of. Normally most programs do not even see it, as it will be expanded by the shell. However, if you convince the shell to not expand it, the programs will happily operate on it, as `~` is a valid filename on most filesystems (not that I recommend using it):

  $ touch ~/a.cc
  $ touch '~/a.cc'
  touch: cannot touch '~/a.cc': No such file or directory
  $ clang '~/a.cc'
  clang-12: error: no such file or directory: '~/a.cc'
  clang-12: error: no input files
  $ clang ./~/a.cc
  clang-12: error: no such file or directory: './~/a.cc'
  clang-12: error: no input files
  $ clang ~/a.cc
  /usr/bin/x86_64-pc-linux-gnu-ld: /usr/lib/gcc/x86_64-pc-linux-gnu/11.2.0/../../../../lib64/crt1.o: in function `_start':
  (.text+0x20): undefined reference to `main'
  clang-12: error: linker command failed with exit code 1 (use -v to see invocation)
  $ mkdir '~'
  $ touch '~/a.cc'
  $ ls '~'
  a.cc

By adding our own special handling of ~ we would be messing with that mechanism. The case of `./~/a.cc` is particularly amusing because then `sys::path::remove_leading_dotslash` will be turning a relative path into an absolute one.

I agree that there are contexts where we would want to treat ~ specially, but this should be opt-in only and based on the context from which that path is coming from. Maybe DW_AT_comp_dir could be one of those contexts. Strictly speaking, it wouldn't be correct, but we do funny things with dwarf paths anyway. IIRC our dwarf code already has some funky logic to detect absolute windows paths on non-windows hosts. Maybe that could be extended to handle ~ as well?


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D113716/new/

https://reviews.llvm.org/D113716



More information about the llvm-commits mailing list