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