@@ -54,30 +54,17 @@ void UnifiedPath::makeCanonical(const std::string &CWD) {
5454 llvm::SmallString<512 > RealPath;
5555 // We need make sure the input `Path` for llvm::sys::fs::real_path is
5656 // exsiting, or else the behavior of real_path() is unexpected.
57- if (llvm::sys::fs::exists (Path)) {
58- llvm::sys::fs::real_path (Path, RealPath, true );
59- } else {
60- llvm::SmallString<512 > Suffix;
61- if (llvm::sys::path::has_filename (Path)) {
62- Suffix = llvm::sys::path::filename (Path).str ();
63- llvm::sys::path::remove_filename (Path);
64- }
65- while (!llvm::sys::fs::exists (Path)) {
66- if (!llvm::sys::path::has_parent_path (Path)) {
67- assert (0 && " no real directory found" );
68- return ;
69- }
70- llvm::sys::path::reverse_iterator RI =
71- llvm::sys::path::rbegin (llvm::StringRef (Path));
72- llvm::SmallString<512 > SuffixTemp (*RI );
73- llvm::sys::path::append (SuffixTemp, llvm::sys::path::Style::native,
74- Suffix);
75- Suffix = SuffixTemp;
76- Path = llvm::SmallString<512 >(llvm::sys::path::parent_path (Path).str ());
57+ llvm::StringRef ParentPath = Path;
58+ while (!llvm::sys::fs::exists (ParentPath)) {
59+ ParentPath = llvm::sys::path::parent_path (ParentPath);
60+ if (ParentPath.empty ()) {
61+ assert (0 && " no real directory found" );
62+ return ;
7763 }
78- llvm::sys::fs::real_path (Path, RealPath, true );
79- llvm::sys::path::append (RealPath, llvm::sys::path::Style::native, Suffix);
8064 }
65+ llvm::sys::fs::real_path (ParentPath, RealPath, true );
66+ if (auto Suffix = Path.substr (ParentPath.size ()); !Suffix.empty ())
67+ llvm::sys::path::append (RealPath, llvm::sys::path::Style::native, Suffix);
8168 _CanonicalPath = RealPath.str ();
8269#if defined(_WIN32)
8370 if (_CanonicalPath.size () >= 3 &&
0 commit comments