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