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