LLVM currently has a mechanism for resolving symlinks and collapsing . and .. items through the openFileForRead method, but it has a couple of limitations:
- It's not convenient to call, since it requires calling an intermediate function first and interpreting the results, then cleaning up after the function (by closing the file descriptor). It would be nice to just be able to say real_path(Path, Real);
- It requires an open file descriptor, which means that particularly on Windows, there's currently no way to get the real path of a directory ("opening" a directory and opening a file require different code, and we don't expose a method to do the former)
- On non-Windows platforms, we can't currently handle tilde expressions.
This patch addresses all of this by providing a simple method with the signature std::error_code real_path(const Twine &, SmallVectorImpl<char> &). It works even with directories on Windows, and on Posix platforms it is smart enough to translate ~ into the appropriate home directory.
As a result, real_path(X) works like you would expect it to from a bash shell on Linux/Mac/etc, and real_path(X) works with directories on Windows, providing maximum portability.
I think this description is very vague. After reading this, I would expect we will handle all kinds of fancy shell expansions (${HOME//foo} anyone ?). I suggest name the argument tilde_expand, document it as performing home-directory expansion using ~ and nothing more, until we have a very good idea of what other kinds of expansions we may want to perform.