diff --git a/include/llvm/Support/PathV2.h b/include/llvm/Support/PathV2.h index 3866e410e62..976fa6a7b25 100644 --- a/include/llvm/Support/PathV2.h +++ b/include/llvm/Support/PathV2.h @@ -244,6 +244,15 @@ const StringRef filename(StringRef path); /// @result The stem of \a path. const StringRef stem(StringRef path); +/// Convert path to a canonical form, resolving symbolic links and removing +/// unnecessary path elements (e.g., "foo/../", "./"). +/// +/// @param path A path that is going to be canonicalized by resolving symlinks +/// and removing unnecessary path elements (e.g., "./"). +/// +/// @param buffer The resulting canonical path. +void canonical(const char *path, SmallVectorImpl &result); + /// @brief Get extension. /// /// If filename contains a dot but not solely one or two dots, result is the diff --git a/lib/Support/Unix/PathV2.inc b/lib/Support/Unix/PathV2.inc index 03ff28367e4..7fa838b16b7 100644 --- a/lib/Support/Unix/PathV2.inc +++ b/lib/Support/Unix/PathV2.inc @@ -503,5 +503,35 @@ error_code get_magic(const Twine &path, uint32_t len, } } // end namespace fs + +namespace path { + +void canonical(const char *path, SmallVectorImpl &buffer) { + buffer.resize(PATH_MAX); + char *result = realpath(path, buffer.data()); + if (result) { + buffer.resize(strlen(result)); + return; + } + + // A common extension is to support memory allocation of the result when + // passing NULL as the second argument. + result = realpath(path, 0); + if (result) { + size_t length = strlen(result); + buffer.resize(length); + memcpy(buffer.data(), result, length); + free(result); + return buffer.data(); + } + + size_t length = strlen(path); + buffer.resize(length); + memcpy(buffer.data(), path, length); + return path; +} + +} // end namespace path + } // end namespace sys } // end namespace llvm