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