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