Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- FileSpec.cpp --------------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 10 | #include "lldb/Host/FileSpec.h" |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 11 | #include "lldb/Utility/CleanUp.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 12 | #include "lldb/Utility/RegularExpression.h" |
| 13 | #include "lldb/Utility/Stream.h" |
| 14 | #include "lldb/Utility/StreamString.h" |
Zachary Turner | 573ab90 | 2017-03-21 18:25:04 +0000 | [diff] [blame] | 15 | #include "lldb/Utility/StringList.h" |
Zachary Turner | 8d48cd6 | 2017-03-22 17:33:23 +0000 | [diff] [blame^] | 16 | #include "lldb/Utility/TildeExpressionResolver.h" |
Zachary Turner | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 17 | |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringExtras.h" |
Caroline Tice | 391a960 | 2010-09-12 00:10:52 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/StringRef.h" |
Zachary Turner | 190fadc | 2016-03-22 17:58:09 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ConvertUTF.h" |
Zachary Turner | 3f55974 | 2014-08-07 17:33:36 +0000 | [diff] [blame] | 21 | #include "llvm/Support/FileSystem.h" |
Greg Clayton | 38a6140 | 2010-12-02 23:20:03 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Path.h" |
| 23 | #include "llvm/Support/Program.h" |
Caroline Tice | 391a960 | 2010-09-12 00:10:52 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 25 | using namespace lldb; |
| 26 | using namespace lldb_private; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 27 | |
Chaoren Lin | 1c614fe | 2015-05-28 17:02:45 +0000 | [diff] [blame] | 28 | namespace { |
| 29 | |
Zachary Turner | 7d86ee5 | 2017-03-08 17:56:08 +0000 | [diff] [blame] | 30 | static constexpr FileSpec::PathSyntax GetNativeSyntax() { |
| 31 | #if defined(LLVM_ON_WIN32) |
| 32 | return FileSpec::ePathSyntaxWindows; |
| 33 | #else |
| 34 | return FileSpec::ePathSyntaxPosix; |
| 35 | #endif |
| 36 | } |
| 37 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 38 | bool PathSyntaxIsPosix(FileSpec::PathSyntax syntax) { |
| 39 | return (syntax == FileSpec::ePathSyntaxPosix || |
| 40 | (syntax == FileSpec::ePathSyntaxHostNative && |
Zachary Turner | 7d86ee5 | 2017-03-08 17:56:08 +0000 | [diff] [blame] | 41 | GetNativeSyntax() == FileSpec::ePathSyntaxPosix)); |
Chaoren Lin | 1c614fe | 2015-05-28 17:02:45 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 44 | const char *GetPathSeparators(FileSpec::PathSyntax syntax) { |
| 45 | return PathSyntaxIsPosix(syntax) ? "/" : "\\/"; |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 48 | char GetPreferredPathSeparator(FileSpec::PathSyntax syntax) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 49 | return GetPathSeparators(syntax)[0]; |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 52 | bool IsPathSeparator(char value, FileSpec::PathSyntax syntax) { |
| 53 | return value == '/' || (!PathSyntaxIsPosix(syntax) && value == '\\'); |
Chaoren Lin | 1c614fe | 2015-05-28 17:02:45 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 56 | void Normalize(llvm::SmallVectorImpl<char> &path, FileSpec::PathSyntax syntax) { |
| 57 | if (PathSyntaxIsPosix(syntax)) |
| 58 | return; |
Chaoren Lin | 1c614fe | 2015-05-28 17:02:45 +0000 | [diff] [blame] | 59 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 60 | std::replace(path.begin(), path.end(), '\\', '/'); |
| 61 | // Windows path can have \\ slashes which can be changed by replace |
| 62 | // call above to //. Here we remove the duplicate. |
| 63 | auto iter = std::unique(path.begin(), path.end(), [](char &c1, char &c2) { |
| 64 | return (c1 == '/' && c2 == '/'); |
| 65 | }); |
| 66 | path.erase(iter, path.end()); |
Chaoren Lin | 1c614fe | 2015-05-28 17:02:45 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 69 | void Denormalize(llvm::SmallVectorImpl<char> &path, |
| 70 | FileSpec::PathSyntax syntax) { |
| 71 | if (PathSyntaxIsPosix(syntax)) |
| 72 | return; |
Chaoren Lin | 1c614fe | 2015-05-28 17:02:45 +0000 | [diff] [blame] | 73 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 74 | std::replace(path.begin(), path.end(), '/', '\\'); |
Chaoren Lin | 1c614fe | 2015-05-28 17:02:45 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 77 | size_t FilenamePos(llvm::StringRef str, FileSpec::PathSyntax syntax) { |
| 78 | if (str.size() == 2 && IsPathSeparator(str[0], syntax) && str[0] == str[1]) |
| 79 | return 0; |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 80 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 81 | if (str.size() > 0 && IsPathSeparator(str.back(), syntax)) |
| 82 | return str.size() - 1; |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 83 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 84 | size_t pos = str.find_last_of(GetPathSeparators(syntax), str.size() - 1); |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 85 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 86 | if (!PathSyntaxIsPosix(syntax) && pos == llvm::StringRef::npos) |
| 87 | pos = str.find_last_of(':', str.size() - 2); |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 88 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 89 | if (pos == llvm::StringRef::npos || |
| 90 | (pos == 1 && IsPathSeparator(str[0], syntax))) |
| 91 | return 0; |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 92 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 93 | return pos + 1; |
Chaoren Lin | 1c614fe | 2015-05-28 17:02:45 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 96 | size_t RootDirStart(llvm::StringRef str, FileSpec::PathSyntax syntax) { |
| 97 | // case "c:/" |
| 98 | if (!PathSyntaxIsPosix(syntax) && |
| 99 | (str.size() > 2 && str[1] == ':' && IsPathSeparator(str[2], syntax))) |
| 100 | return 2; |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 101 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 102 | // case "//" |
| 103 | if (str.size() == 2 && IsPathSeparator(str[0], syntax) && str[0] == str[1]) |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 104 | return llvm::StringRef::npos; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 105 | |
| 106 | // case "//net" |
| 107 | if (str.size() > 3 && IsPathSeparator(str[0], syntax) && str[0] == str[1] && |
| 108 | !IsPathSeparator(str[2], syntax)) |
| 109 | return str.find_first_of(GetPathSeparators(syntax), 2); |
| 110 | |
| 111 | // case "/" |
| 112 | if (str.size() > 0 && IsPathSeparator(str[0], syntax)) |
| 113 | return 0; |
| 114 | |
| 115 | return llvm::StringRef::npos; |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 118 | size_t ParentPathEnd(llvm::StringRef path, FileSpec::PathSyntax syntax) { |
| 119 | size_t end_pos = FilenamePos(path, syntax); |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 120 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 121 | bool filename_was_sep = |
| 122 | path.size() > 0 && IsPathSeparator(path[end_pos], syntax); |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 123 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 124 | // Skip separators except for root dir. |
| 125 | size_t root_dir_pos = RootDirStart(path.substr(0, end_pos), syntax); |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 126 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 127 | while (end_pos > 0 && (end_pos - 1) != root_dir_pos && |
| 128 | IsPathSeparator(path[end_pos - 1], syntax)) |
| 129 | --end_pos; |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 130 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 131 | if (end_pos == 1 && root_dir_pos == 0 && filename_was_sep) |
| 132 | return llvm::StringRef::npos; |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 133 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 134 | return end_pos; |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | } // end anonymous namespace |
| 138 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 139 | void FileSpec::Resolve(llvm::SmallVectorImpl<char> &path) { |
| 140 | if (path.empty()) |
| 141 | return; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 142 | |
Zachary Turner | 8d48cd6 | 2017-03-22 17:33:23 +0000 | [diff] [blame^] | 143 | llvm::SmallString<32> Source(path.begin(), path.end()); |
| 144 | StandardTildeExpressionResolver Resolver; |
| 145 | Resolver.ResolveFullPath(Source, path); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 146 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 147 | // Save a copy of the original path that's passed in |
| 148 | llvm::SmallString<128> original_path(path.begin(), path.end()); |
Jason Molenda | 671a29d | 2015-02-25 02:35:25 +0000 | [diff] [blame] | 149 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 150 | llvm::sys::fs::make_absolute(path); |
| 151 | if (!llvm::sys::fs::exists(path)) { |
| 152 | path.clear(); |
| 153 | path.append(original_path.begin(), original_path.end()); |
| 154 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Zachary Turner | 7d86ee5 | 2017-03-08 17:56:08 +0000 | [diff] [blame] | 157 | FileSpec::FileSpec() : m_syntax(GetNativeSyntax()) {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 158 | |
| 159 | //------------------------------------------------------------------ |
| 160 | // Default constructor that can take an optional full path to a |
| 161 | // file on disk. |
| 162 | //------------------------------------------------------------------ |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 163 | FileSpec::FileSpec(llvm::StringRef path, bool resolve_path, PathSyntax syntax) |
Sam McCall | 6f43d9d | 2016-11-15 10:58:16 +0000 | [diff] [blame] | 164 | : m_syntax(syntax) { |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 165 | SetFile(path, resolve_path, syntax); |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Zachary Turner | 8c6b546 | 2017-03-06 23:42:44 +0000 | [diff] [blame] | 168 | FileSpec::FileSpec(llvm::StringRef path, bool resolve_path, |
| 169 | const llvm::Triple &Triple) |
| 170 | : FileSpec{path, resolve_path, |
| 171 | Triple.isOSWindows() ? ePathSyntaxWindows : ePathSyntaxPosix} {} |
Chaoren Lin | f34f410 | 2015-05-09 01:21:32 +0000 | [diff] [blame] | 172 | |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 173 | //------------------------------------------------------------------ |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 174 | // Copy constructor |
| 175 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 176 | FileSpec::FileSpec(const FileSpec &rhs) |
| 177 | : m_directory(rhs.m_directory), m_filename(rhs.m_filename), |
| 178 | m_is_resolved(rhs.m_is_resolved), m_syntax(rhs.m_syntax) {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 179 | |
| 180 | //------------------------------------------------------------------ |
| 181 | // Copy constructor |
| 182 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 183 | FileSpec::FileSpec(const FileSpec *rhs) : m_directory(), m_filename() { |
| 184 | if (rhs) |
| 185 | *this = *rhs; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | //------------------------------------------------------------------ |
Bruce Mitchener | d93c4a3 | 2014-07-01 21:22:11 +0000 | [diff] [blame] | 189 | // Virtual destructor in case anyone inherits from this class. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 190 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 191 | FileSpec::~FileSpec() {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 192 | |
| 193 | //------------------------------------------------------------------ |
| 194 | // Assignment operator. |
| 195 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 196 | const FileSpec &FileSpec::operator=(const FileSpec &rhs) { |
| 197 | if (this != &rhs) { |
| 198 | m_directory = rhs.m_directory; |
| 199 | m_filename = rhs.m_filename; |
| 200 | m_is_resolved = rhs.m_is_resolved; |
| 201 | m_syntax = rhs.m_syntax; |
| 202 | } |
| 203 | return *this; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 206 | //------------------------------------------------------------------ |
| 207 | // Update the contents of this object with a new path. The path will |
| 208 | // be split up into a directory and filename and stored as uniqued |
| 209 | // string values for quick comparison and efficient memory usage. |
| 210 | //------------------------------------------------------------------ |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 211 | void FileSpec::SetFile(llvm::StringRef pathname, bool resolve, |
| 212 | PathSyntax syntax) { |
| 213 | // CLEANUP: Use StringRef for string handling. This function is kind of a |
| 214 | // mess and the unclear semantics of RootDirStart and ParentPathEnd make |
| 215 | // it very difficult to understand this function. There's no reason this |
| 216 | // function should be particularly complicated or difficult to understand. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 217 | m_filename.Clear(); |
| 218 | m_directory.Clear(); |
| 219 | m_is_resolved = false; |
Zachary Turner | 7d86ee5 | 2017-03-08 17:56:08 +0000 | [diff] [blame] | 220 | m_syntax = (syntax == ePathSyntaxHostNative) ? GetNativeSyntax() : syntax; |
Zachary Turner | df62f20 | 2014-08-07 17:33:07 +0000 | [diff] [blame] | 221 | |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 222 | if (pathname.empty()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 223 | return; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 224 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 225 | llvm::SmallString<64> resolved(pathname); |
Zachary Turner | df62f20 | 2014-08-07 17:33:07 +0000 | [diff] [blame] | 226 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 227 | if (resolve) { |
| 228 | FileSpec::Resolve(resolved); |
| 229 | m_is_resolved = true; |
| 230 | } |
Zachary Turner | c7a17ed | 2014-12-01 23:13:32 +0000 | [diff] [blame] | 231 | |
Zachary Turner | 827d5d7 | 2016-12-16 04:27:00 +0000 | [diff] [blame] | 232 | Normalize(resolved, m_syntax); |
Pavel Labath | a212c58 | 2016-04-14 09:38:06 +0000 | [diff] [blame] | 233 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 234 | llvm::StringRef resolve_path_ref(resolved.c_str()); |
Zachary Turner | 827d5d7 | 2016-12-16 04:27:00 +0000 | [diff] [blame] | 235 | size_t dir_end = ParentPathEnd(resolve_path_ref, m_syntax); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 236 | if (dir_end == 0) { |
| 237 | m_filename.SetString(resolve_path_ref); |
| 238 | return; |
| 239 | } |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 240 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 241 | m_directory.SetString(resolve_path_ref.substr(0, dir_end)); |
Pavel Labath | 144119b | 2016-04-04 14:39:12 +0000 | [diff] [blame] | 242 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 243 | size_t filename_begin = dir_end; |
Zachary Turner | 827d5d7 | 2016-12-16 04:27:00 +0000 | [diff] [blame] | 244 | size_t root_dir_start = RootDirStart(resolve_path_ref, m_syntax); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 245 | while (filename_begin != llvm::StringRef::npos && |
| 246 | filename_begin < resolve_path_ref.size() && |
| 247 | filename_begin != root_dir_start && |
Zachary Turner | 827d5d7 | 2016-12-16 04:27:00 +0000 | [diff] [blame] | 248 | IsPathSeparator(resolve_path_ref[filename_begin], m_syntax)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 249 | ++filename_begin; |
| 250 | m_filename.SetString((filename_begin == llvm::StringRef::npos || |
| 251 | filename_begin >= resolve_path_ref.size()) |
| 252 | ? "." |
| 253 | : resolve_path_ref.substr(filename_begin)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Zachary Turner | 8c6b546 | 2017-03-06 23:42:44 +0000 | [diff] [blame] | 256 | void FileSpec::SetFile(llvm::StringRef path, bool resolve, |
| 257 | const llvm::Triple &Triple) { |
| 258 | return SetFile(path, resolve, |
| 259 | Triple.isOSWindows() ? ePathSyntaxWindows : ePathSyntaxPosix); |
Chaoren Lin | 44145d7 | 2015-05-29 19:52:37 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 262 | //---------------------------------------------------------------------- |
| 263 | // Convert to pointer operator. This allows code to check any FileSpec |
| 264 | // objects to see if they contain anything valid using code such as: |
| 265 | // |
| 266 | // if (file_spec) |
| 267 | // {} |
| 268 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 269 | FileSpec::operator bool() const { return m_filename || m_directory; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 270 | |
| 271 | //---------------------------------------------------------------------- |
| 272 | // Logical NOT operator. This allows code to check any FileSpec |
| 273 | // objects to see if they are invalid using code such as: |
| 274 | // |
| 275 | // if (!file_spec) |
| 276 | // {} |
| 277 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 278 | bool FileSpec::operator!() const { return !m_directory && !m_filename; } |
| 279 | |
| 280 | bool FileSpec::DirectoryEquals(const FileSpec &rhs) const { |
| 281 | const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive(); |
| 282 | return ConstString::Equals(m_directory, rhs.m_directory, case_sensitive); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 285 | bool FileSpec::FileEquals(const FileSpec &rhs) const { |
| 286 | const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive(); |
| 287 | return ConstString::Equals(m_filename, rhs.m_filename, case_sensitive); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 290 | //------------------------------------------------------------------ |
| 291 | // Equal to operator |
| 292 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 293 | bool FileSpec::operator==(const FileSpec &rhs) const { |
| 294 | if (!FileEquals(rhs)) |
| 295 | return false; |
| 296 | if (DirectoryEquals(rhs)) |
| 297 | return true; |
Zachary Turner | 47c0346 | 2016-02-24 21:26:47 +0000 | [diff] [blame] | 298 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 299 | // TODO: determine if we want to keep this code in here. |
| 300 | // The code below was added to handle a case where we were |
| 301 | // trying to set a file and line breakpoint and one path |
| 302 | // was resolved, and the other not and the directory was |
| 303 | // in a mount point that resolved to a more complete path: |
| 304 | // "/tmp/a.c" == "/private/tmp/a.c". I might end up pulling |
| 305 | // this out... |
| 306 | if (IsResolved() && rhs.IsResolved()) { |
| 307 | // Both paths are resolved, no need to look further... |
| 308 | return false; |
| 309 | } |
Zachary Turner | 47c0346 | 2016-02-24 21:26:47 +0000 | [diff] [blame] | 310 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 311 | FileSpec resolved_lhs(*this); |
Zachary Turner | 47c0346 | 2016-02-24 21:26:47 +0000 | [diff] [blame] | 312 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 313 | // If "this" isn't resolved, resolve it |
| 314 | if (!IsResolved()) { |
| 315 | if (resolved_lhs.ResolvePath()) { |
| 316 | // This path wasn't resolved but now it is. Check if the resolved |
| 317 | // directory is the same as our unresolved directory, and if so, |
| 318 | // we can mark this object as resolved to avoid more future resolves |
| 319 | m_is_resolved = (m_directory == resolved_lhs.m_directory); |
| 320 | } else |
| 321 | return false; |
| 322 | } |
Zachary Turner | 47c0346 | 2016-02-24 21:26:47 +0000 | [diff] [blame] | 323 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 324 | FileSpec resolved_rhs(rhs); |
| 325 | if (!rhs.IsResolved()) { |
| 326 | if (resolved_rhs.ResolvePath()) { |
| 327 | // rhs's path wasn't resolved but now it is. Check if the resolved |
| 328 | // directory is the same as rhs's unresolved directory, and if so, |
| 329 | // we can mark this object as resolved to avoid more future resolves |
| 330 | rhs.m_is_resolved = (rhs.m_directory == resolved_rhs.m_directory); |
| 331 | } else |
| 332 | return false; |
| 333 | } |
Zachary Turner | 47c0346 | 2016-02-24 21:26:47 +0000 | [diff] [blame] | 334 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 335 | // If we reach this point in the code we were able to resolve both paths |
| 336 | // and since we only resolve the paths if the basenames are equal, then |
| 337 | // we can just check if both directories are equal... |
| 338 | return DirectoryEquals(rhs); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | //------------------------------------------------------------------ |
| 342 | // Not equal to operator |
| 343 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 344 | bool FileSpec::operator!=(const FileSpec &rhs) const { return !(*this == rhs); } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 345 | |
| 346 | //------------------------------------------------------------------ |
| 347 | // Less than operator |
| 348 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 349 | bool FileSpec::operator<(const FileSpec &rhs) const { |
| 350 | return FileSpec::Compare(*this, rhs, true) < 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | //------------------------------------------------------------------ |
| 354 | // Dump a FileSpec object to a stream |
| 355 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 356 | Stream &lldb_private::operator<<(Stream &s, const FileSpec &f) { |
| 357 | f.Dump(&s); |
| 358 | return s; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | //------------------------------------------------------------------ |
| 362 | // Clear this object by releasing both the directory and filename |
| 363 | // string values and making them both the empty string. |
| 364 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 365 | void FileSpec::Clear() { |
| 366 | m_directory.Clear(); |
| 367 | m_filename.Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | //------------------------------------------------------------------ |
| 371 | // Compare two FileSpec objects. If "full" is true, then both |
| 372 | // the directory and the filename must match. If "full" is false, |
| 373 | // then the directory names for "a" and "b" are only compared if |
| 374 | // they are both non-empty. This allows a FileSpec object to only |
| 375 | // contain a filename and it can match FileSpec objects that have |
| 376 | // matching filenames with different paths. |
| 377 | // |
| 378 | // Return -1 if the "a" is less than "b", 0 if "a" is equal to "b" |
| 379 | // and "1" if "a" is greater than "b". |
| 380 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 381 | int FileSpec::Compare(const FileSpec &a, const FileSpec &b, bool full) { |
| 382 | int result = 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 383 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 384 | // case sensitivity of compare |
| 385 | const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive(); |
Zachary Turner | 47c0346 | 2016-02-24 21:26:47 +0000 | [diff] [blame] | 386 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 387 | // If full is true, then we must compare both the directory and filename. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 388 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 389 | // If full is false, then if either directory is empty, then we match on |
| 390 | // the basename only, and if both directories have valid values, we still |
| 391 | // do a full compare. This allows for matching when we just have a filename |
| 392 | // in one of the FileSpec objects. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 393 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 394 | if (full || (a.m_directory && b.m_directory)) { |
| 395 | result = ConstString::Compare(a.m_directory, b.m_directory, case_sensitive); |
| 396 | if (result) |
| 397 | return result; |
| 398 | } |
| 399 | return ConstString::Compare(a.m_filename, b.m_filename, case_sensitive); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 402 | bool FileSpec::Equal(const FileSpec &a, const FileSpec &b, bool full, |
| 403 | bool remove_backups) { |
| 404 | // case sensitivity of equality test |
| 405 | const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive(); |
Zachary Turner | 47c0346 | 2016-02-24 21:26:47 +0000 | [diff] [blame] | 406 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 407 | if (!full && (a.GetDirectory().IsEmpty() || b.GetDirectory().IsEmpty())) |
| 408 | return ConstString::Equals(a.m_filename, b.m_filename, case_sensitive); |
Pavel Labath | 218770b | 2016-10-31 16:22:07 +0000 | [diff] [blame] | 409 | |
| 410 | if (remove_backups == false) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 411 | return a == b; |
Jim Ingham | 96a1596 | 2014-11-15 01:54:26 +0000 | [diff] [blame] | 412 | |
Pavel Labath | 218770b | 2016-10-31 16:22:07 +0000 | [diff] [blame] | 413 | if (a == b) |
| 414 | return true; |
| 415 | |
| 416 | return Equal(a.GetNormalizedPath(), b.GetNormalizedPath(), full, false); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Pavel Labath | 218770b | 2016-10-31 16:22:07 +0000 | [diff] [blame] | 419 | FileSpec FileSpec::GetNormalizedPath() const { |
| 420 | // Fast path. Do nothing if the path is not interesting. |
| 421 | if (!m_directory.GetStringRef().contains(".") && |
Pavel Labath | e6e7e6c | 2016-11-30 16:08:45 +0000 | [diff] [blame] | 422 | !m_directory.GetStringRef().contains("//") && |
| 423 | m_filename.GetStringRef() != ".." && m_filename.GetStringRef() != ".") |
Pavel Labath | 218770b | 2016-10-31 16:22:07 +0000 | [diff] [blame] | 424 | return *this; |
Greg Clayton | 5a27195 | 2015-06-02 22:43:29 +0000 | [diff] [blame] | 425 | |
Pavel Labath | 218770b | 2016-10-31 16:22:07 +0000 | [diff] [blame] | 426 | llvm::SmallString<64> path, result; |
| 427 | const bool normalize = false; |
| 428 | GetPath(path, normalize); |
| 429 | llvm::StringRef rest(path); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 430 | |
Pavel Labath | 218770b | 2016-10-31 16:22:07 +0000 | [diff] [blame] | 431 | // We will not go below root dir. |
| 432 | size_t root_dir_start = RootDirStart(path, m_syntax); |
| 433 | const bool absolute = root_dir_start != llvm::StringRef::npos; |
| 434 | if (absolute) { |
| 435 | result += rest.take_front(root_dir_start + 1); |
| 436 | rest = rest.drop_front(root_dir_start + 1); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 437 | } else { |
Pavel Labath | 218770b | 2016-10-31 16:22:07 +0000 | [diff] [blame] | 438 | if (m_syntax == ePathSyntaxWindows && path.size() > 2 && path[1] == ':') { |
| 439 | result += rest.take_front(2); |
| 440 | rest = rest.drop_front(2); |
| 441 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Pavel Labath | 218770b | 2016-10-31 16:22:07 +0000 | [diff] [blame] | 444 | bool anything_added = false; |
| 445 | llvm::SmallVector<llvm::StringRef, 0> components, processed; |
| 446 | rest.split(components, '/', -1, false); |
| 447 | processed.reserve(components.size()); |
| 448 | for (auto component : components) { |
| 449 | if (component == ".") |
| 450 | continue; // Skip these. |
| 451 | if (component != "..") { |
| 452 | processed.push_back(component); |
| 453 | continue; // Regular file name. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 454 | } |
Pavel Labath | 218770b | 2016-10-31 16:22:07 +0000 | [diff] [blame] | 455 | if (!processed.empty()) { |
| 456 | processed.pop_back(); |
| 457 | continue; // Dots. Go one level up if we can. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 458 | } |
Pavel Labath | 218770b | 2016-10-31 16:22:07 +0000 | [diff] [blame] | 459 | if (absolute) |
| 460 | continue; // We're at the top level. Cannot go higher than that. Skip. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 461 | |
Pavel Labath | 218770b | 2016-10-31 16:22:07 +0000 | [diff] [blame] | 462 | result += component; // We're a relative path. We need to keep these. |
| 463 | result += '/'; |
| 464 | anything_added = true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 465 | } |
Pavel Labath | 218770b | 2016-10-31 16:22:07 +0000 | [diff] [blame] | 466 | for (auto component : processed) { |
| 467 | result += component; |
| 468 | result += '/'; |
| 469 | anything_added = true; |
| 470 | } |
| 471 | if (anything_added) |
| 472 | result.pop_back(); // Pop last '/'. |
| 473 | else if (result.empty()) |
| 474 | result = "."; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 475 | |
Pavel Labath | 218770b | 2016-10-31 16:22:07 +0000 | [diff] [blame] | 476 | return FileSpec(result, false, m_syntax); |
Jim Ingham | 96a1596 | 2014-11-15 01:54:26 +0000 | [diff] [blame] | 477 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 478 | |
| 479 | //------------------------------------------------------------------ |
| 480 | // Dump the object to the supplied stream. If the object contains |
| 481 | // a valid directory name, it will be displayed followed by a |
| 482 | // directory delimiter, and the filename. |
| 483 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 484 | void FileSpec::Dump(Stream *s) const { |
| 485 | if (s) { |
| 486 | std::string path{GetPath(true)}; |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 487 | s->PutCString(path); |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 488 | char path_separator = GetPreferredPathSeparator(m_syntax); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 489 | if (!m_filename && !path.empty() && path.back() != path_separator) |
| 490 | s->PutChar(path_separator); |
| 491 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | //------------------------------------------------------------------ |
| 495 | // Returns true if the file exists. |
| 496 | //------------------------------------------------------------------ |
Zachary Turner | 7d86ee5 | 2017-03-08 17:56:08 +0000 | [diff] [blame] | 497 | bool FileSpec::Exists() const { return llvm::sys::fs::exists(GetPath()); } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 498 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 499 | bool FileSpec::Readable() const { |
Zachary Turner | 7d86ee5 | 2017-03-08 17:56:08 +0000 | [diff] [blame] | 500 | return GetPermissions() & llvm::sys::fs::perms::all_read; |
Greg Clayton | 5acc125 | 2014-08-15 18:00:45 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 503 | bool FileSpec::ResolveExecutableLocation() { |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 504 | // CLEANUP: Use StringRef for string handling. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 505 | if (!m_directory) { |
| 506 | const char *file_cstr = m_filename.GetCString(); |
| 507 | if (file_cstr) { |
| 508 | const std::string file_str(file_cstr); |
| 509 | llvm::ErrorOr<std::string> error_or_path = |
| 510 | llvm::sys::findProgramByName(file_str); |
| 511 | if (!error_or_path) |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 512 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 513 | std::string path = error_or_path.get(); |
| 514 | llvm::StringRef dir_ref = llvm::sys::path::parent_path(path); |
| 515 | if (!dir_ref.empty()) { |
| 516 | // FindProgramByName returns "." if it can't find the file. |
| 517 | if (strcmp(".", dir_ref.data()) == 0) |
| 518 | return false; |
| 519 | |
| 520 | m_directory.SetCString(dir_ref.data()); |
| 521 | if (Exists()) |
| 522 | return true; |
| 523 | else { |
| 524 | // If FindProgramByName found the file, it returns the directory + |
| 525 | // filename in its return results. |
| 526 | // We need to separate them. |
| 527 | FileSpec tmp_file(dir_ref.data(), false); |
| 528 | if (tmp_file.Exists()) { |
| 529 | m_directory = tmp_file.m_directory; |
| 530 | return true; |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | return false; |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 540 | bool FileSpec::ResolvePath() { |
| 541 | if (m_is_resolved) |
| 542 | return true; // We have already resolved this path |
| 543 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 544 | // SetFile(...) will set m_is_resolved correctly if it can resolve the path |
Pavel Labath | 9bd69ad | 2017-03-13 09:46:15 +0000 | [diff] [blame] | 545 | SetFile(GetPath(false), true); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 546 | return m_is_resolved; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 549 | uint64_t FileSpec::GetByteSize() const { |
Zachary Turner | 7d86ee5 | 2017-03-08 17:56:08 +0000 | [diff] [blame] | 550 | uint64_t Size = 0; |
| 551 | if (llvm::sys::fs::file_size(GetPath(), Size)) |
| 552 | return 0; |
| 553 | return Size; |
Zachary Turner | df62f20 | 2014-08-07 17:33:07 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 556 | FileSpec::PathSyntax FileSpec::GetPathSyntax() const { return m_syntax; } |
| 557 | |
Pavel Labath | 30e6cbf | 2017-03-07 13:19:15 +0000 | [diff] [blame] | 558 | uint32_t FileSpec::GetPermissions() const { |
Zachary Turner | 7d86ee5 | 2017-03-08 17:56:08 +0000 | [diff] [blame] | 559 | namespace fs = llvm::sys::fs; |
| 560 | fs::file_status st; |
| 561 | if (fs::status(GetPath(), st, false)) |
| 562 | return fs::perms::perms_not_known; |
| 563 | |
| 564 | return st.permissions(); |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 567 | //------------------------------------------------------------------ |
| 568 | // Directory string get accessor. |
| 569 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 570 | ConstString &FileSpec::GetDirectory() { return m_directory; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 571 | |
| 572 | //------------------------------------------------------------------ |
| 573 | // Directory string const get accessor. |
| 574 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 575 | const ConstString &FileSpec::GetDirectory() const { return m_directory; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 576 | |
| 577 | //------------------------------------------------------------------ |
| 578 | // Filename string get accessor. |
| 579 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 580 | ConstString &FileSpec::GetFilename() { return m_filename; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 581 | |
| 582 | //------------------------------------------------------------------ |
| 583 | // Filename string const get accessor. |
| 584 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 585 | const ConstString &FileSpec::GetFilename() const { return m_filename; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 586 | |
| 587 | //------------------------------------------------------------------ |
| 588 | // Extract the directory and path into a fixed buffer. This is |
| 589 | // needed as the directory and path are stored in separate string |
| 590 | // values. |
| 591 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 592 | size_t FileSpec::GetPath(char *path, size_t path_max_len, |
| 593 | bool denormalize) const { |
| 594 | if (!path) |
| 595 | return 0; |
Zachary Turner | b6d9924 | 2014-08-08 23:54:35 +0000 | [diff] [blame] | 596 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 597 | std::string result = GetPath(denormalize); |
| 598 | ::snprintf(path, path_max_len, "%s", result.c_str()); |
| 599 | return std::min(path_max_len - 1, result.length()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 602 | std::string FileSpec::GetPath(bool denormalize) const { |
| 603 | llvm::SmallString<64> result; |
| 604 | GetPath(result, denormalize); |
| 605 | return std::string(result.begin(), result.end()); |
Jason Molenda | a7ae467 | 2013-04-29 09:46:43 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 608 | const char *FileSpec::GetCString(bool denormalize) const { |
| 609 | return ConstString{GetPath(denormalize)}.AsCString(NULL); |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 612 | void FileSpec::GetPath(llvm::SmallVectorImpl<char> &path, |
| 613 | bool denormalize) const { |
| 614 | path.append(m_directory.GetStringRef().begin(), |
| 615 | m_directory.GetStringRef().end()); |
| 616 | if (m_directory && m_filename && |
| 617 | !IsPathSeparator(m_directory.GetStringRef().back(), m_syntax)) |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 618 | path.insert(path.end(), GetPreferredPathSeparator(m_syntax)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 619 | path.append(m_filename.GetStringRef().begin(), |
| 620 | m_filename.GetStringRef().end()); |
| 621 | Normalize(path, m_syntax); |
| 622 | if (denormalize && !path.empty()) |
| 623 | Denormalize(path, m_syntax); |
Zachary Turner | 4e8ddf5 | 2015-04-09 18:08:50 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 626 | ConstString FileSpec::GetFileNameExtension() const { |
| 627 | if (m_filename) { |
Enrico Granata | a9dbf43 | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 628 | const char *filename = m_filename.GetCString(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 629 | const char *dot_pos = strrchr(filename, '.'); |
| 630 | if (dot_pos && dot_pos[1] != '\0') |
| 631 | return ConstString(dot_pos + 1); |
| 632 | } |
| 633 | return ConstString(); |
| 634 | } |
| 635 | |
| 636 | ConstString FileSpec::GetFileNameStrippingExtension() const { |
| 637 | const char *filename = m_filename.GetCString(); |
| 638 | if (filename == NULL) |
| 639 | return ConstString(); |
| 640 | |
| 641 | const char *dot_pos = strrchr(filename, '.'); |
| 642 | if (dot_pos == NULL) |
| 643 | return m_filename; |
| 644 | |
| 645 | return ConstString(filename, dot_pos - filename); |
Enrico Granata | a9dbf43 | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 648 | //------------------------------------------------------------------ |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 649 | // Return the size in bytes that this object takes in memory. This |
| 650 | // returns the size in bytes of this object, not any shared string |
| 651 | // values it may refer to. |
| 652 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 653 | size_t FileSpec::MemorySize() const { |
| 654 | return m_filename.MemorySize() + m_directory.MemorySize(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Zachary Turner | 1f87534 | 2017-03-13 02:44:39 +0000 | [diff] [blame] | 657 | void FileSpec::EnumerateDirectory(llvm::StringRef dir_path, |
| 658 | bool find_directories, bool find_files, |
| 659 | bool find_other, |
| 660 | EnumerateDirectoryCallbackType callback, |
| 661 | void *callback_baton) { |
| 662 | namespace fs = llvm::sys::fs; |
| 663 | std::error_code EC; |
| 664 | fs::recursive_directory_iterator Iter(dir_path, EC); |
| 665 | fs::recursive_directory_iterator End; |
| 666 | for (; Iter != End && !EC; Iter.increment(EC)) { |
| 667 | const auto &Item = *Iter; |
| 668 | fs::file_status Status; |
Pavel Labath | 9bd69ad | 2017-03-13 09:46:15 +0000 | [diff] [blame] | 669 | if ((EC = Item.status(Status))) |
Zachary Turner | 1f87534 | 2017-03-13 02:44:39 +0000 | [diff] [blame] | 670 | break; |
| 671 | if (!find_files && fs::is_regular_file(Status)) |
| 672 | continue; |
| 673 | if (!find_directories && fs::is_directory(Status)) |
| 674 | continue; |
| 675 | if (!find_other && fs::is_other(Status)) |
| 676 | continue; |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 677 | |
Zachary Turner | 1f87534 | 2017-03-13 02:44:39 +0000 | [diff] [blame] | 678 | FileSpec Spec(Item.path(), false); |
| 679 | auto Result = callback(callback_baton, Status.type(), Spec); |
| 680 | if (Result == eEnumerateDirectoryResultQuit) |
| 681 | return; |
| 682 | if (Result == eEnumerateDirectoryResultNext) { |
| 683 | // Default behavior is to recurse. Opt out if the callback doesn't want |
| 684 | // this behavior. |
| 685 | Iter.no_push(); |
Greg Clayton | 58c65f0 | 2015-06-29 18:29:00 +0000 | [diff] [blame] | 686 | } |
Zachary Turner | 1f87534 | 2017-03-13 02:44:39 +0000 | [diff] [blame] | 687 | } |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 690 | FileSpec |
| 691 | FileSpec::CopyByAppendingPathComponent(llvm::StringRef component) const { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 692 | FileSpec ret = *this; |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 693 | ret.AppendPathComponent(component); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 694 | return ret; |
Chaoren Lin | 0c5a9c1 | 2015-06-05 00:28:06 +0000 | [diff] [blame] | 695 | } |
| 696 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 697 | FileSpec FileSpec::CopyByRemovingLastPathComponent() const { |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 698 | // CLEANUP: Use StringRef for string handling. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 699 | const bool resolve = false; |
| 700 | if (m_filename.IsEmpty() && m_directory.IsEmpty()) |
| 701 | return FileSpec("", resolve); |
| 702 | if (m_directory.IsEmpty()) |
| 703 | return FileSpec("", resolve); |
| 704 | if (m_filename.IsEmpty()) { |
| 705 | const char *dir_cstr = m_directory.GetCString(); |
| 706 | const char *last_slash_ptr = ::strrchr(dir_cstr, '/'); |
| 707 | |
| 708 | // check for obvious cases before doing the full thing |
| 709 | if (!last_slash_ptr) |
| 710 | return FileSpec("", resolve); |
| 711 | if (last_slash_ptr == dir_cstr) |
| 712 | return FileSpec("/", resolve); |
| 713 | |
| 714 | size_t last_slash_pos = last_slash_ptr - dir_cstr + 1; |
| 715 | ConstString new_path(dir_cstr, last_slash_pos); |
| 716 | return FileSpec(new_path.GetCString(), resolve); |
| 717 | } else |
| 718 | return FileSpec(m_directory.GetCString(), resolve); |
Chaoren Lin | 0c5a9c1 | 2015-06-05 00:28:06 +0000 | [diff] [blame] | 719 | } |
| 720 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 721 | ConstString FileSpec::GetLastPathComponent() const { |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 722 | // CLEANUP: Use StringRef for string handling. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 723 | if (m_filename) |
| 724 | return m_filename; |
| 725 | if (m_directory) { |
| 726 | const char *dir_cstr = m_directory.GetCString(); |
| 727 | const char *last_slash_ptr = ::strrchr(dir_cstr, '/'); |
| 728 | if (last_slash_ptr == NULL) |
| 729 | return m_directory; |
| 730 | if (last_slash_ptr == dir_cstr) { |
| 731 | if (last_slash_ptr[1] == 0) |
| 732 | return ConstString(last_slash_ptr); |
| 733 | else |
| 734 | return ConstString(last_slash_ptr + 1); |
| 735 | } |
| 736 | if (last_slash_ptr[1] != 0) |
| 737 | return ConstString(last_slash_ptr + 1); |
| 738 | const char *penultimate_slash_ptr = last_slash_ptr; |
| 739 | while (*penultimate_slash_ptr) { |
| 740 | --penultimate_slash_ptr; |
| 741 | if (penultimate_slash_ptr == dir_cstr) |
| 742 | break; |
| 743 | if (*penultimate_slash_ptr == '/') |
| 744 | break; |
| 745 | } |
| 746 | ConstString result(penultimate_slash_ptr + 1, |
| 747 | last_slash_ptr - penultimate_slash_ptr); |
| 748 | return result; |
| 749 | } |
| 750 | return ConstString(); |
Chaoren Lin | 0c5a9c1 | 2015-06-05 00:28:06 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Pavel Labath | 59d725c | 2017-01-16 10:07:02 +0000 | [diff] [blame] | 753 | static std::string |
| 754 | join_path_components(FileSpec::PathSyntax syntax, |
| 755 | const std::vector<llvm::StringRef> components) { |
| 756 | std::string result; |
| 757 | for (size_t i = 0; i < components.size(); ++i) { |
| 758 | if (components[i].empty()) |
| 759 | continue; |
| 760 | result += components[i]; |
| 761 | if (i != components.size() - 1 && |
| 762 | !IsPathSeparator(components[i].back(), syntax)) |
| 763 | result += GetPreferredPathSeparator(syntax); |
| 764 | } |
| 765 | |
| 766 | return result; |
| 767 | } |
| 768 | |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 769 | void FileSpec::PrependPathComponent(llvm::StringRef component) { |
| 770 | if (component.empty()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 771 | return; |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 772 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 773 | const bool resolve = false; |
| 774 | if (m_filename.IsEmpty() && m_directory.IsEmpty()) { |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 775 | SetFile(component, resolve); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 776 | return; |
| 777 | } |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 778 | |
Pavel Labath | 59d725c | 2017-01-16 10:07:02 +0000 | [diff] [blame] | 779 | std::string result = |
| 780 | join_path_components(m_syntax, {component, m_directory.GetStringRef(), |
| 781 | m_filename.GetStringRef()}); |
Pavel Labath | 238169d | 2017-01-16 12:15:42 +0000 | [diff] [blame] | 782 | SetFile(result, resolve, m_syntax); |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 783 | } |
| 784 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 785 | void FileSpec::PrependPathComponent(const FileSpec &new_path) { |
| 786 | return PrependPathComponent(new_path.GetPath(false)); |
Chaoren Lin | 0c5a9c1 | 2015-06-05 00:28:06 +0000 | [diff] [blame] | 787 | } |
| 788 | |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 789 | void FileSpec::AppendPathComponent(llvm::StringRef component) { |
| 790 | if (component.empty()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 791 | return; |
| 792 | |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 793 | component = component.drop_while( |
| 794 | [this](char c) { return IsPathSeparator(c, m_syntax); }); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 795 | |
Pavel Labath | 59d725c | 2017-01-16 10:07:02 +0000 | [diff] [blame] | 796 | std::string result = |
| 797 | join_path_components(m_syntax, {m_directory.GetStringRef(), |
| 798 | m_filename.GetStringRef(), component}); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 799 | |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 800 | SetFile(result, false, m_syntax); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | void FileSpec::AppendPathComponent(const FileSpec &new_path) { |
| 804 | return AppendPathComponent(new_path.GetPath(false)); |
| 805 | } |
| 806 | |
| 807 | void FileSpec::RemoveLastPathComponent() { |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 808 | // CLEANUP: Use StringRef for string handling. |
| 809 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 810 | const bool resolve = false; |
| 811 | if (m_filename.IsEmpty() && m_directory.IsEmpty()) { |
| 812 | SetFile("", resolve); |
| 813 | return; |
| 814 | } |
| 815 | if (m_directory.IsEmpty()) { |
| 816 | SetFile("", resolve); |
| 817 | return; |
| 818 | } |
| 819 | if (m_filename.IsEmpty()) { |
| 820 | const char *dir_cstr = m_directory.GetCString(); |
| 821 | const char *last_slash_ptr = ::strrchr(dir_cstr, '/'); |
| 822 | |
| 823 | // check for obvious cases before doing the full thing |
| 824 | if (!last_slash_ptr) { |
| 825 | SetFile("", resolve); |
| 826 | return; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 827 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 828 | if (last_slash_ptr == dir_cstr) { |
| 829 | SetFile("/", resolve); |
| 830 | return; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 831 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 832 | size_t last_slash_pos = last_slash_ptr - dir_cstr + 1; |
| 833 | ConstString new_path(dir_cstr, last_slash_pos); |
| 834 | SetFile(new_path.GetCString(), resolve); |
| 835 | } else |
| 836 | SetFile(m_directory.GetCString(), resolve); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 837 | } |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 838 | //------------------------------------------------------------------ |
| 839 | /// Returns true if the filespec represents an implementation source |
| 840 | /// file (files with a ".c", ".cpp", ".m", ".mm" (many more) |
| 841 | /// extension). |
| 842 | /// |
| 843 | /// @return |
| 844 | /// \b true if the filespec represents an implementation source |
| 845 | /// file, \b false otherwise. |
| 846 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 847 | bool FileSpec::IsSourceImplementationFile() const { |
| 848 | ConstString extension(GetFileNameExtension()); |
Zachary Turner | 95eae42 | 2016-09-21 16:01:28 +0000 | [diff] [blame] | 849 | if (!extension) |
| 850 | return false; |
| 851 | |
| 852 | static RegularExpression g_source_file_regex(llvm::StringRef( |
| 853 | "^([cC]|[mM]|[mM][mM]|[cC][pP][pP]|[cC]\\+\\+|[cC][xX][xX]|[cC][cC]|[" |
| 854 | "cC][pP]|[sS]|[aA][sS][mM]|[fF]|[fF]77|[fF]90|[fF]95|[fF]03|[fF][oO][" |
| 855 | "rR]|[fF][tT][nN]|[fF][pP][pP]|[aA][dD][aA]|[aA][dD][bB]|[aA][dD][sS])" |
| 856 | "$")); |
| 857 | return g_source_file_regex.Execute(extension.GetStringRef()); |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 860 | bool FileSpec::IsRelative() const { |
| 861 | const char *dir = m_directory.GetCString(); |
| 862 | llvm::StringRef directory(dir ? dir : ""); |
Zachary Turner | 270e99a | 2014-12-08 21:36:42 +0000 | [diff] [blame] | 863 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 864 | if (directory.size() > 0) { |
| 865 | if (PathSyntaxIsPosix(m_syntax)) { |
| 866 | // If the path doesn't start with '/' or '~', return true |
| 867 | switch (directory[0]) { |
| 868 | case '/': |
| 869 | case '~': |
| 870 | return false; |
| 871 | default: |
Greg Clayton | a0ca660 | 2012-10-18 16:33:33 +0000 | [diff] [blame] | 872 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 873 | } |
| 874 | } else { |
| 875 | if (directory.size() >= 2 && directory[1] == ':') |
| 876 | return false; |
| 877 | if (directory[0] == '/') |
| 878 | return false; |
| 879 | return true; |
Greg Clayton | a0ca660 | 2012-10-18 16:33:33 +0000 | [diff] [blame] | 880 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 881 | } else if (m_filename) { |
| 882 | // No directory, just a basename, return true |
| 883 | return true; |
| 884 | } |
| 885 | return false; |
Greg Clayton | a0ca660 | 2012-10-18 16:33:33 +0000 | [diff] [blame] | 886 | } |
Chaoren Lin | 372e906 | 2015-06-09 17:54:27 +0000 | [diff] [blame] | 887 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 888 | bool FileSpec::IsAbsolute() const { return !FileSpec::IsRelative(); } |
Zachary Turner | 827d5d7 | 2016-12-16 04:27:00 +0000 | [diff] [blame] | 889 | |
| 890 | void llvm::format_provider<FileSpec>::format(const FileSpec &F, |
| 891 | raw_ostream &Stream, |
| 892 | StringRef Style) { |
| 893 | assert( |
| 894 | (Style.empty() || Style.equals_lower("F") || Style.equals_lower("D")) && |
| 895 | "Invalid FileSpec style!"); |
| 896 | |
| 897 | StringRef dir = F.GetDirectory().GetStringRef(); |
| 898 | StringRef file = F.GetFilename().GetStringRef(); |
| 899 | |
| 900 | if (dir.empty() && file.empty()) { |
| 901 | Stream << "(empty)"; |
| 902 | return; |
| 903 | } |
| 904 | |
| 905 | if (Style.equals_lower("F")) { |
| 906 | Stream << (file.empty() ? "(empty)" : file); |
| 907 | return; |
| 908 | } |
| 909 | |
| 910 | // Style is either D or empty, either way we need to print the directory. |
| 911 | if (!dir.empty()) { |
| 912 | // Directory is stored in normalized form, which might be different |
| 913 | // than preferred form. In order to handle this, we need to cut off |
| 914 | // the filename, then denormalize, then write the entire denorm'ed |
| 915 | // directory. |
| 916 | llvm::SmallString<64> denormalized_dir = dir; |
| 917 | Denormalize(denormalized_dir, F.GetPathSyntax()); |
| 918 | Stream << denormalized_dir; |
| 919 | Stream << GetPreferredPathSeparator(F.GetPathSyntax()); |
| 920 | } |
| 921 | |
| 922 | if (Style.equals_lower("D")) { |
| 923 | // We only want to print the directory, so now just exit. |
| 924 | if (dir.empty()) |
| 925 | Stream << "(empty)"; |
| 926 | return; |
| 927 | } |
| 928 | |
| 929 | if (!file.empty()) |
| 930 | Stream << file; |
| 931 | } |