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 | c00cf4a | 2014-08-15 22:04:21 +0000 | [diff] [blame] | 13 | |
Jonas Devlieghere | ad8d48f | 2018-06-13 16:23:21 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/SmallString.h" |
| 15 | #include "llvm/ADT/SmallVector.h" |
Caroline Tice | 391a960 | 2010-09-12 00:10:52 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringRef.h" |
Jonas Devlieghere | ad8d48f | 2018-06-13 16:23:21 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/Triple.h" |
| 18 | #include "llvm/ADT/Twine.h" |
| 19 | #include "llvm/Support/ErrorOr.h" |
Zachary Turner | 3f55974 | 2014-08-07 17:33:36 +0000 | [diff] [blame] | 20 | #include "llvm/Support/FileSystem.h" |
Greg Clayton | 38a6140 | 2010-12-02 23:20:03 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Program.h" |
Jonas Devlieghere | ad8d48f | 2018-06-13 16:23:21 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Zachary Turner | 4479ac1 | 2017-04-06 18:12:24 +0000 | [diff] [blame] | 23 | |
Jonas Devlieghere | 672d2c1 | 2018-11-11 23:16:43 +0000 | [diff] [blame] | 24 | #include <algorithm> |
| 25 | #include <system_error> |
| 26 | #include <vector> |
Zachary Turner | 4479ac1 | 2017-04-06 18:12:24 +0000 | [diff] [blame] | 27 | |
Jonas Devlieghere | 672d2c1 | 2018-11-11 23:16:43 +0000 | [diff] [blame] | 28 | #include <assert.h> |
| 29 | #include <limits.h> |
| 30 | #include <stdio.h> |
| 31 | #include <string.h> |
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 | |
Pavel Labath | 2cb7cf8 | 2018-05-14 14:52:47 +0000 | [diff] [blame] | 69 | FileSpec::FileSpec() : m_style(GetNativeStyle()) {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 70 | |
| 71 | //------------------------------------------------------------------ |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 72 | // 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] | 73 | //------------------------------------------------------------------ |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 74 | FileSpec::FileSpec(llvm::StringRef path, Style style) : m_style(style) { |
| 75 | SetFile(path, style); |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 78 | FileSpec::FileSpec(llvm::StringRef path, const llvm::Triple &Triple) |
| 79 | : FileSpec{path, Triple.isOSWindows() ? Style::windows : Style::posix} {} |
Chaoren Lin | f34f410 | 2015-05-09 01:21:32 +0000 | [diff] [blame] | 80 | |
Jim Ingham | 0909e5f | 2010-09-16 00:57:33 +0000 | [diff] [blame] | 81 | //------------------------------------------------------------------ |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 82 | // Copy constructor |
| 83 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 84 | FileSpec::FileSpec(const FileSpec &rhs) |
| 85 | : m_directory(rhs.m_directory), m_filename(rhs.m_filename), |
Pavel Labath | 2cb7cf8 | 2018-05-14 14:52:47 +0000 | [diff] [blame] | 86 | m_is_resolved(rhs.m_is_resolved), m_style(rhs.m_style) {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 87 | |
| 88 | //------------------------------------------------------------------ |
| 89 | // Copy constructor |
| 90 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 91 | FileSpec::FileSpec(const FileSpec *rhs) : m_directory(), m_filename() { |
| 92 | if (rhs) |
| 93 | *this = *rhs; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | //------------------------------------------------------------------ |
Bruce Mitchener | d93c4a3 | 2014-07-01 21:22:11 +0000 | [diff] [blame] | 97 | // Virtual destructor in case anyone inherits from this class. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 98 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 99 | FileSpec::~FileSpec() {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 100 | |
Jonas Devlieghere | c1cc317 | 2018-06-25 10:11:53 +0000 | [diff] [blame] | 101 | namespace { |
| 102 | //------------------------------------------------------------------ |
| 103 | /// Safely get a character at the specified index. |
| 104 | /// |
| 105 | /// @param[in] path |
| 106 | /// A full, partial, or relative path to a file. |
| 107 | /// |
| 108 | /// @param[in] i |
| 109 | /// An index into path which may or may not be valid. |
| 110 | /// |
| 111 | /// @return |
| 112 | /// The character at index \a i if the index is valid, or 0 if |
| 113 | /// the index is not valid. |
| 114 | //------------------------------------------------------------------ |
| 115 | inline char safeCharAtIndex(const llvm::StringRef &path, size_t i) { |
| 116 | if (i < path.size()) |
| 117 | return path[i]; |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | //------------------------------------------------------------------ |
| 122 | /// Check if a path needs to be normalized. |
| 123 | /// |
| 124 | /// Check if a path needs to be normalized. We currently consider a |
| 125 | /// path to need normalization if any of the following are true |
| 126 | /// - path contains "/./" |
| 127 | /// - path contains "/../" |
| 128 | /// - path contains "//" |
| 129 | /// - path ends with "/" |
| 130 | /// Paths that start with "./" or with "../" are not considered to |
| 131 | /// need normalization since we aren't trying to resolve the path, |
| 132 | /// we are just trying to remove redundant things from the path. |
| 133 | /// |
| 134 | /// @param[in] path |
| 135 | /// A full, partial, or relative path to a file. |
| 136 | /// |
| 137 | /// @return |
| 138 | /// Returns \b true if the path needs to be normalized. |
| 139 | //------------------------------------------------------------------ |
| 140 | bool needsNormalization(const llvm::StringRef &path) { |
| 141 | if (path.empty()) |
| 142 | return false; |
| 143 | // We strip off leading "." values so these paths need to be normalized |
| 144 | if (path[0] == '.') |
| 145 | return true; |
| 146 | for (auto i = path.find_first_of("\\/"); i != llvm::StringRef::npos; |
| 147 | i = path.find_first_of("\\/", i + 1)) { |
| 148 | const auto next = safeCharAtIndex(path, i+1); |
| 149 | switch (next) { |
| 150 | case 0: |
| 151 | // path separator char at the end of the string which should be |
| 152 | // stripped unless it is the one and only character |
| 153 | return i > 0; |
| 154 | case '/': |
| 155 | case '\\': |
| 156 | // two path separator chars in the middle of a path needs to be |
| 157 | // normalized |
| 158 | if (i > 0) |
| 159 | return true; |
| 160 | ++i; |
| 161 | break; |
| 162 | |
| 163 | case '.': { |
| 164 | const auto next_next = safeCharAtIndex(path, i+2); |
| 165 | switch (next_next) { |
| 166 | default: break; |
| 167 | case 0: return true; // ends with "/." |
| 168 | case '/': |
| 169 | case '\\': |
| 170 | return true; // contains "/./" |
| 171 | case '.': { |
| 172 | const auto next_next_next = safeCharAtIndex(path, i+3); |
| 173 | switch (next_next_next) { |
| 174 | default: break; |
| 175 | case 0: return true; // ends with "/.." |
| 176 | case '/': |
| 177 | case '\\': |
| 178 | return true; // contains "/../" |
| 179 | } |
| 180 | break; |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | break; |
| 185 | |
| 186 | default: |
| 187 | break; |
| 188 | } |
| 189 | } |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | |
| 194 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 195 | //------------------------------------------------------------------ |
| 196 | // Assignment operator. |
| 197 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 198 | const FileSpec &FileSpec::operator=(const FileSpec &rhs) { |
| 199 | if (this != &rhs) { |
| 200 | m_directory = rhs.m_directory; |
| 201 | m_filename = rhs.m_filename; |
| 202 | m_is_resolved = rhs.m_is_resolved; |
Pavel Labath | 2cb7cf8 | 2018-05-14 14:52:47 +0000 | [diff] [blame] | 203 | m_style = rhs.m_style; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 204 | } |
| 205 | return *this; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 208 | void FileSpec::SetFile(llvm::StringRef pathname) { SetFile(pathname, m_style); } |
Jonas Devlieghere | 937348c | 2018-06-13 22:08:14 +0000 | [diff] [blame] | 209 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 210 | //------------------------------------------------------------------ |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 211 | // Update the contents of this object with a new path. The path will be split |
| 212 | // up into a directory and filename and stored as uniqued string values for |
| 213 | // quick comparison and efficient memory usage. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 214 | //------------------------------------------------------------------ |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 215 | void FileSpec::SetFile(llvm::StringRef pathname, Style style) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 216 | m_filename.Clear(); |
| 217 | m_directory.Clear(); |
| 218 | m_is_resolved = false; |
Pavel Labath | 2cb7cf8 | 2018-05-14 14:52:47 +0000 | [diff] [blame] | 219 | m_style = (style == Style::native) ? GetNativeStyle() : style; |
Zachary Turner | df62f20 | 2014-08-07 17:33:07 +0000 | [diff] [blame] | 220 | |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 221 | if (pathname.empty()) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 222 | return; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 223 | |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 224 | llvm::SmallString<128> resolved(pathname); |
Zachary Turner | c7a17ed | 2014-12-01 23:13:32 +0000 | [diff] [blame] | 225 | |
Greg Clayton | 776cd7a | 2018-04-27 15:45:58 +0000 | [diff] [blame] | 226 | // Normalize the path by removing ".", ".." and other redundant components. |
Jonas Devlieghere | c1cc317 | 2018-06-25 10:11:53 +0000 | [diff] [blame] | 227 | if (needsNormalization(resolved)) |
| 228 | llvm::sys::path::remove_dots(resolved, true, m_style); |
Greg Clayton | 776cd7a | 2018-04-27 15:45:58 +0000 | [diff] [blame] | 229 | |
| 230 | // Normalize back slashes to forward slashes |
Pavel Labath | 2cb7cf8 | 2018-05-14 14:52:47 +0000 | [diff] [blame] | 231 | if (m_style == Style::windows) |
Greg Clayton | 776cd7a | 2018-04-27 15:45:58 +0000 | [diff] [blame] | 232 | std::replace(resolved.begin(), resolved.end(), '\\', '/'); |
Pavel Labath | a212c58 | 2016-04-14 09:38:06 +0000 | [diff] [blame] | 233 | |
Greg Clayton | 39d50b7 | 2018-05-17 16:12:38 +0000 | [diff] [blame] | 234 | if (resolved.empty()) { |
| 235 | // If we have no path after normalization set the path to the current |
| 236 | // directory. This matches what python does and also a few other path |
Jonas Devlieghere | ad8d48f | 2018-06-13 16:23:21 +0000 | [diff] [blame] | 237 | // utilities. |
Greg Clayton | 39d50b7 | 2018-05-17 16:12:38 +0000 | [diff] [blame] | 238 | m_filename.SetString("."); |
| 239 | return; |
| 240 | } |
| 241 | |
Jonas Devlieghere | ad8d48f | 2018-06-13 16:23:21 +0000 | [diff] [blame] | 242 | // Split path into filename and directory. We rely on the underlying char |
| 243 | // pointer to be nullptr when the components are empty. |
| 244 | llvm::StringRef filename = llvm::sys::path::filename(resolved, m_style); |
Jonas Devlieghere | c1cc317 | 2018-06-25 10:11:53 +0000 | [diff] [blame] | 245 | if(!filename.empty()) |
Jonas Devlieghere | ad8d48f | 2018-06-13 16:23:21 +0000 | [diff] [blame] | 246 | m_filename.SetString(filename); |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 247 | |
Jonas Devlieghere | ad8d48f | 2018-06-13 16:23:21 +0000 | [diff] [blame] | 248 | llvm::StringRef directory = llvm::sys::path::parent_path(resolved, m_style); |
Jonas Devlieghere | c1cc317 | 2018-06-25 10:11:53 +0000 | [diff] [blame] | 249 | if(!directory.empty()) |
Jonas Devlieghere | ad8d48f | 2018-06-13 16:23:21 +0000 | [diff] [blame] | 250 | m_directory.SetString(directory); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 253 | void FileSpec::SetFile(llvm::StringRef path, const llvm::Triple &Triple) { |
| 254 | return SetFile(path, Triple.isOSWindows() ? Style::windows : Style::posix); |
Chaoren Lin | 44145d7 | 2015-05-29 19:52:37 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 257 | //---------------------------------------------------------------------- |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 258 | // Convert to pointer operator. This allows code to check any FileSpec objects |
| 259 | // to see if they contain anything valid using code such as: |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 260 | // |
| 261 | // if (file_spec) |
| 262 | // {} |
| 263 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 264 | FileSpec::operator bool() const { return m_filename || m_directory; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 265 | |
| 266 | //---------------------------------------------------------------------- |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 267 | // Logical NOT operator. This allows code to check any FileSpec objects to see |
| 268 | // if they are invalid using code such as: |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 269 | // |
| 270 | // if (!file_spec) |
| 271 | // {} |
| 272 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 273 | bool FileSpec::operator!() const { return !m_directory && !m_filename; } |
| 274 | |
| 275 | bool FileSpec::DirectoryEquals(const FileSpec &rhs) const { |
| 276 | const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive(); |
| 277 | return ConstString::Equals(m_directory, rhs.m_directory, case_sensitive); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 280 | bool FileSpec::FileEquals(const FileSpec &rhs) const { |
| 281 | const bool case_sensitive = IsCaseSensitive() || rhs.IsCaseSensitive(); |
| 282 | return ConstString::Equals(m_filename, rhs.m_filename, case_sensitive); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 285 | //------------------------------------------------------------------ |
| 286 | // Equal to operator |
| 287 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 288 | bool FileSpec::operator==(const FileSpec &rhs) const { |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 289 | return FileEquals(rhs) && DirectoryEquals(rhs); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | //------------------------------------------------------------------ |
| 293 | // Not equal to operator |
| 294 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 295 | bool FileSpec::operator!=(const FileSpec &rhs) const { return !(*this == rhs); } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 296 | |
| 297 | //------------------------------------------------------------------ |
| 298 | // Less than operator |
| 299 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 300 | bool FileSpec::operator<(const FileSpec &rhs) const { |
| 301 | return FileSpec::Compare(*this, rhs, true) < 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | //------------------------------------------------------------------ |
| 305 | // Dump a FileSpec object to a stream |
| 306 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 307 | Stream &lldb_private::operator<<(Stream &s, const FileSpec &f) { |
| 308 | f.Dump(&s); |
| 309 | return s; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | //------------------------------------------------------------------ |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 313 | // Clear this object by releasing both the directory and filename string values |
| 314 | // and making them both the empty string. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 315 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 316 | void FileSpec::Clear() { |
| 317 | m_directory.Clear(); |
| 318 | m_filename.Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | //------------------------------------------------------------------ |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 322 | // Compare two FileSpec objects. If "full" is true, then both the directory and |
| 323 | // the filename must match. If "full" is false, then the directory names for |
| 324 | // "a" and "b" are only compared if they are both non-empty. This allows a |
| 325 | // FileSpec object to only contain a filename and it can match FileSpec objects |
| 326 | // that have matching filenames with different paths. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 327 | // |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 328 | // Return -1 if the "a" is less than "b", 0 if "a" is equal to "b" and "1" if |
| 329 | // "a" is greater than "b". |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 330 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 331 | int FileSpec::Compare(const FileSpec &a, const FileSpec &b, bool full) { |
| 332 | int result = 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 333 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 334 | // case sensitivity of compare |
| 335 | const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive(); |
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 | // 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] | 338 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 339 | // If full is false, then if either directory is empty, then we match on the |
| 340 | // basename only, and if both directories have valid values, we still do a |
| 341 | // full compare. This allows for matching when we just have a filename in one |
| 342 | // of the FileSpec objects. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 343 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 344 | if (full || (a.m_directory && b.m_directory)) { |
| 345 | result = ConstString::Compare(a.m_directory, b.m_directory, case_sensitive); |
| 346 | if (result) |
| 347 | return result; |
| 348 | } |
| 349 | return ConstString::Compare(a.m_filename, b.m_filename, case_sensitive); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Greg Clayton | 776cd7a | 2018-04-27 15:45:58 +0000 | [diff] [blame] | 352 | bool FileSpec::Equal(const FileSpec &a, const FileSpec &b, bool full) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 353 | // case sensitivity of equality test |
| 354 | const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive(); |
Jonas Devlieghere | ad8d48f | 2018-06-13 16:23:21 +0000 | [diff] [blame] | 355 | |
Jonas Devlieghere | c1cc317 | 2018-06-25 10:11:53 +0000 | [diff] [blame] | 356 | const bool filenames_equal = ConstString::Equals(a.m_filename, |
| 357 | b.m_filename, |
| 358 | case_sensitive); |
Jim Ingham | 97e4f47 | 2017-03-27 19:12:25 +0000 | [diff] [blame] | 359 | |
Greg Clayton | 776cd7a | 2018-04-27 15:45:58 +0000 | [diff] [blame] | 360 | if (!filenames_equal) |
Jonas Devlieghere | ad8d48f | 2018-06-13 16:23:21 +0000 | [diff] [blame] | 361 | return false; |
Jim Ingham | 97e4f47 | 2017-03-27 19:12:25 +0000 | [diff] [blame] | 362 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 363 | if (!full && (a.GetDirectory().IsEmpty() || b.GetDirectory().IsEmpty())) |
Jim Ingham | 97e4f47 | 2017-03-27 19:12:25 +0000 | [diff] [blame] | 364 | return filenames_equal; |
Pavel Labath | 218770b | 2016-10-31 16:22:07 +0000 | [diff] [blame] | 365 | |
Greg Clayton | 776cd7a | 2018-04-27 15:45:58 +0000 | [diff] [blame] | 366 | return a == b; |
Jim Ingham | 96a1596 | 2014-11-15 01:54:26 +0000 | [diff] [blame] | 367 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 368 | |
| 369 | //------------------------------------------------------------------ |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 370 | // Dump the object to the supplied stream. If the object contains a valid |
| 371 | // directory name, it will be displayed followed by a directory delimiter, and |
| 372 | // the filename. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 373 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 374 | void FileSpec::Dump(Stream *s) const { |
| 375 | if (s) { |
| 376 | std::string path{GetPath(true)}; |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 377 | s->PutCString(path); |
Pavel Labath | 2cb7cf8 | 2018-05-14 14:52:47 +0000 | [diff] [blame] | 378 | char path_separator = GetPreferredPathSeparator(m_style); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 379 | if (!m_filename && !path.empty() && path.back() != path_separator) |
| 380 | s->PutChar(path_separator); |
| 381 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Pavel Labath | 2cb7cf8 | 2018-05-14 14:52:47 +0000 | [diff] [blame] | 384 | FileSpec::Style FileSpec::GetPathStyle() const { return m_style; } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 385 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 386 | //------------------------------------------------------------------ |
| 387 | // Directory string get accessor. |
| 388 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 389 | ConstString &FileSpec::GetDirectory() { return m_directory; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 390 | |
| 391 | //------------------------------------------------------------------ |
| 392 | // Directory string const get accessor. |
| 393 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 394 | const ConstString &FileSpec::GetDirectory() const { return m_directory; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 395 | |
| 396 | //------------------------------------------------------------------ |
| 397 | // Filename string get accessor. |
| 398 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 399 | ConstString &FileSpec::GetFilename() { return m_filename; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 400 | |
| 401 | //------------------------------------------------------------------ |
| 402 | // Filename string const get accessor. |
| 403 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 404 | const ConstString &FileSpec::GetFilename() const { return m_filename; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 405 | |
| 406 | //------------------------------------------------------------------ |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 407 | // Extract the directory and path into a fixed buffer. This is needed as the |
| 408 | // directory and path are stored in separate string values. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 409 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 410 | size_t FileSpec::GetPath(char *path, size_t path_max_len, |
| 411 | bool denormalize) const { |
| 412 | if (!path) |
| 413 | return 0; |
Zachary Turner | b6d9924 | 2014-08-08 23:54:35 +0000 | [diff] [blame] | 414 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 415 | std::string result = GetPath(denormalize); |
| 416 | ::snprintf(path, path_max_len, "%s", result.c_str()); |
| 417 | return std::min(path_max_len - 1, result.length()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 420 | std::string FileSpec::GetPath(bool denormalize) const { |
| 421 | llvm::SmallString<64> result; |
| 422 | GetPath(result, denormalize); |
| 423 | return std::string(result.begin(), result.end()); |
Jason Molenda | a7ae467 | 2013-04-29 09:46:43 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 426 | const char *FileSpec::GetCString(bool denormalize) const { |
Jonas Devlieghere | 65e5e278 | 2018-12-13 00:15:17 +0000 | [diff] [blame] | 427 | return ConstString{GetPath(denormalize)}.AsCString(nullptr); |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 430 | void FileSpec::GetPath(llvm::SmallVectorImpl<char> &path, |
| 431 | bool denormalize) const { |
| 432 | path.append(m_directory.GetStringRef().begin(), |
| 433 | m_directory.GetStringRef().end()); |
Greg Clayton | 776cd7a | 2018-04-27 15:45:58 +0000 | [diff] [blame] | 434 | // Since the path was normalized and all paths use '/' when stored in these |
| 435 | // objects, we don't need to look for the actual syntax specific path |
| 436 | // separator, we just look for and insert '/'. |
| 437 | if (m_directory && m_filename && m_directory.GetStringRef().back() != '/' && |
| 438 | m_filename.GetStringRef().back() != '/') |
| 439 | path.insert(path.end(), '/'); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 440 | path.append(m_filename.GetStringRef().begin(), |
| 441 | m_filename.GetStringRef().end()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 442 | if (denormalize && !path.empty()) |
Pavel Labath | 2cb7cf8 | 2018-05-14 14:52:47 +0000 | [diff] [blame] | 443 | Denormalize(path, m_style); |
Zachary Turner | 4e8ddf5 | 2015-04-09 18:08:50 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 446 | ConstString FileSpec::GetFileNameExtension() const { |
Jonas Devlieghere | 9c1a645 | 2018-06-13 16:36:07 +0000 | [diff] [blame] | 447 | return ConstString( |
| 448 | llvm::sys::path::extension(m_filename.GetStringRef(), m_style)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | ConstString FileSpec::GetFileNameStrippingExtension() const { |
Jonas Devlieghere | 9c1a645 | 2018-06-13 16:36:07 +0000 | [diff] [blame] | 452 | return ConstString(llvm::sys::path::stem(m_filename.GetStringRef(), m_style)); |
Enrico Granata | a9dbf43 | 2011-10-17 21:45:27 +0000 | [diff] [blame] | 453 | } |
| 454 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 455 | //------------------------------------------------------------------ |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 456 | // Return the size in bytes that this object takes in memory. This returns the |
| 457 | // 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] | 458 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 459 | size_t FileSpec::MemorySize() const { |
| 460 | return m_filename.MemorySize() + m_directory.MemorySize(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 461 | } |
| 462 | |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 463 | FileSpec |
| 464 | FileSpec::CopyByAppendingPathComponent(llvm::StringRef component) const { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 465 | FileSpec ret = *this; |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 466 | ret.AppendPathComponent(component); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 467 | return ret; |
Chaoren Lin | 0c5a9c1 | 2015-06-05 00:28:06 +0000 | [diff] [blame] | 468 | } |
| 469 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 470 | FileSpec FileSpec::CopyByRemovingLastPathComponent() const { |
Jonas Devlieghere | 24bd63c4 | 2018-06-24 10:18:01 +0000 | [diff] [blame] | 471 | llvm::SmallString<64> current_path; |
| 472 | GetPath(current_path, false); |
| 473 | if (llvm::sys::path::has_parent_path(current_path, m_style)) |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 474 | return FileSpec(llvm::sys::path::parent_path(current_path, m_style), |
Jonas Devlieghere | 24bd63c4 | 2018-06-24 10:18:01 +0000 | [diff] [blame] | 475 | m_style); |
| 476 | return *this; |
Chaoren Lin | 0c5a9c1 | 2015-06-05 00:28:06 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 479 | ConstString FileSpec::GetLastPathComponent() const { |
Jonas Devlieghere | 24bd63c4 | 2018-06-24 10:18:01 +0000 | [diff] [blame] | 480 | llvm::SmallString<64> current_path; |
| 481 | GetPath(current_path, false); |
| 482 | return ConstString(llvm::sys::path::filename(current_path, m_style)); |
Pavel Labath | 59d725c | 2017-01-16 10:07:02 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 485 | void FileSpec::PrependPathComponent(llvm::StringRef component) { |
Jonas Devlieghere | 24bd63c4 | 2018-06-24 10:18:01 +0000 | [diff] [blame] | 486 | llvm::SmallString<64> new_path(component); |
| 487 | llvm::SmallString<64> current_path; |
| 488 | GetPath(current_path, false); |
| 489 | llvm::sys::path::append(new_path, |
| 490 | llvm::sys::path::begin(current_path, m_style), |
| 491 | llvm::sys::path::end(current_path), m_style); |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 492 | SetFile(new_path, m_style); |
Chaoren Lin | d3173f3 | 2015-05-29 19:52:29 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 495 | void FileSpec::PrependPathComponent(const FileSpec &new_path) { |
| 496 | return PrependPathComponent(new_path.GetPath(false)); |
Chaoren Lin | 0c5a9c1 | 2015-06-05 00:28:06 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Zachary Turner | fe83ad8 | 2016-09-27 20:48:37 +0000 | [diff] [blame] | 499 | void FileSpec::AppendPathComponent(llvm::StringRef component) { |
Jonas Devlieghere | 24bd63c4 | 2018-06-24 10:18:01 +0000 | [diff] [blame] | 500 | llvm::SmallString<64> current_path; |
| 501 | GetPath(current_path, false); |
| 502 | llvm::sys::path::append(current_path, m_style, component); |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 503 | SetFile(current_path, m_style); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | void FileSpec::AppendPathComponent(const FileSpec &new_path) { |
| 507 | return AppendPathComponent(new_path.GetPath(false)); |
| 508 | } |
| 509 | |
Jonas Devlieghere | df8e291 | 2018-05-30 13:03:16 +0000 | [diff] [blame] | 510 | bool FileSpec::RemoveLastPathComponent() { |
| 511 | llvm::SmallString<64> current_path; |
| 512 | GetPath(current_path, false); |
| 513 | if (llvm::sys::path::has_parent_path(current_path, m_style)) { |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 514 | SetFile(llvm::sys::path::parent_path(current_path, m_style)); |
Jonas Devlieghere | df8e291 | 2018-05-30 13:03:16 +0000 | [diff] [blame] | 515 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 516 | } |
Jonas Devlieghere | df8e291 | 2018-05-30 13:03:16 +0000 | [diff] [blame] | 517 | return false; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 518 | } |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 519 | //------------------------------------------------------------------ |
| 520 | /// Returns true if the filespec represents an implementation source |
| 521 | /// file (files with a ".c", ".cpp", ".m", ".mm" (many more) |
| 522 | /// extension). |
| 523 | /// |
| 524 | /// @return |
| 525 | /// \b true if the filespec represents an implementation source |
| 526 | /// file, \b false otherwise. |
| 527 | //------------------------------------------------------------------ |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 528 | bool FileSpec::IsSourceImplementationFile() const { |
| 529 | ConstString extension(GetFileNameExtension()); |
Zachary Turner | 95eae42 | 2016-09-21 16:01:28 +0000 | [diff] [blame] | 530 | if (!extension) |
| 531 | return false; |
| 532 | |
| 533 | static RegularExpression g_source_file_regex(llvm::StringRef( |
Jonas Devlieghere | ad8d48f | 2018-06-13 16:23:21 +0000 | [diff] [blame] | 534 | "^.([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] | 535 | "cC][pP]|[sS]|[aA][sS][mM]|[fF]|[fF]77|[fF]90|[fF]95|[fF]03|[fF][oO][" |
| 536 | "rR]|[fF][tT][nN]|[fF][pP][pP]|[aA][dD][aA]|[aA][dD][bB]|[aA][dD][sS])" |
| 537 | "$")); |
| 538 | return g_source_file_regex.Execute(extension.GetStringRef()); |
Greg Clayton | 1f74607 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Jonas Devlieghere | c1cc317 | 2018-06-25 10:11:53 +0000 | [diff] [blame] | 541 | bool FileSpec::IsRelative() const { |
| 542 | return !IsAbsolute(); |
| 543 | } |
Chaoren Lin | 372e906 | 2015-06-09 17:54:27 +0000 | [diff] [blame] | 544 | |
Jonas Devlieghere | ad8d48f | 2018-06-13 16:23:21 +0000 | [diff] [blame] | 545 | bool FileSpec::IsAbsolute() const { |
| 546 | llvm::SmallString<64> current_path; |
| 547 | GetPath(current_path, false); |
| 548 | |
| 549 | // Early return if the path is empty. |
| 550 | if (current_path.empty()) |
| 551 | return false; |
| 552 | |
| 553 | // We consider paths starting with ~ to be absolute. |
| 554 | if (current_path[0] == '~') |
| 555 | return true; |
| 556 | |
| 557 | return llvm::sys::path::is_absolute(current_path, m_style); |
| 558 | } |
Zachary Turner | 827d5d7 | 2016-12-16 04:27:00 +0000 | [diff] [blame] | 559 | |
Pavel Labath | 7d36d72 | 2019-01-16 12:30:41 +0000 | [diff] [blame^] | 560 | void FileSpec::MakeAbsolute(const FileSpec &dir) { |
| 561 | if (IsRelative()) |
| 562 | PrependPathComponent(dir); |
| 563 | } |
| 564 | |
Zachary Turner | 827d5d7 | 2016-12-16 04:27:00 +0000 | [diff] [blame] | 565 | void llvm::format_provider<FileSpec>::format(const FileSpec &F, |
| 566 | raw_ostream &Stream, |
| 567 | StringRef Style) { |
| 568 | assert( |
| 569 | (Style.empty() || Style.equals_lower("F") || Style.equals_lower("D")) && |
| 570 | "Invalid FileSpec style!"); |
| 571 | |
| 572 | StringRef dir = F.GetDirectory().GetStringRef(); |
| 573 | StringRef file = F.GetFilename().GetStringRef(); |
| 574 | |
| 575 | if (dir.empty() && file.empty()) { |
| 576 | Stream << "(empty)"; |
| 577 | return; |
| 578 | } |
| 579 | |
| 580 | if (Style.equals_lower("F")) { |
| 581 | Stream << (file.empty() ? "(empty)" : file); |
| 582 | return; |
| 583 | } |
| 584 | |
| 585 | // Style is either D or empty, either way we need to print the directory. |
| 586 | if (!dir.empty()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 587 | // Directory is stored in normalized form, which might be different than |
| 588 | // preferred form. In order to handle this, we need to cut off the |
| 589 | // filename, then denormalize, then write the entire denorm'ed directory. |
Zachary Turner | 827d5d7 | 2016-12-16 04:27:00 +0000 | [diff] [blame] | 590 | llvm::SmallString<64> denormalized_dir = dir; |
Pavel Labath | 2cb7cf8 | 2018-05-14 14:52:47 +0000 | [diff] [blame] | 591 | Denormalize(denormalized_dir, F.GetPathStyle()); |
Zachary Turner | 827d5d7 | 2016-12-16 04:27:00 +0000 | [diff] [blame] | 592 | Stream << denormalized_dir; |
Pavel Labath | 2cb7cf8 | 2018-05-14 14:52:47 +0000 | [diff] [blame] | 593 | Stream << GetPreferredPathSeparator(F.GetPathStyle()); |
Zachary Turner | 827d5d7 | 2016-12-16 04:27:00 +0000 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | if (Style.equals_lower("D")) { |
| 597 | // We only want to print the directory, so now just exit. |
| 598 | if (dir.empty()) |
| 599 | Stream << "(empty)"; |
| 600 | return; |
| 601 | } |
| 602 | |
| 603 | if (!file.empty()) |
| 604 | Stream << file; |
| 605 | } |