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