]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Utility/FileSpec.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Utility / FileSpec.cpp
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
10 #include "lldb/Utility/FileSpec.h"
11 #include "lldb/Utility/RegularExpression.h"
12 #include "lldb/Utility/Stream.h"
13
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/ADT/Triple.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/Support/ErrorOr.h"
20 #include "llvm/Support/FileSystem.h"
21 #include "llvm/Support/Program.h"
22 #include "llvm/Support/raw_ostream.h"
23
24 #include <algorithm>
25 #include <system_error>
26 #include <vector>
27
28 #include <assert.h>
29 #include <limits.h>
30 #include <stdio.h>
31 #include <string.h>
32
33 using namespace lldb;
34 using namespace lldb_private;
35
36 namespace {
37
38 static constexpr FileSpec::Style GetNativeStyle() {
39 #if defined(_WIN32)
40   return FileSpec::Style::windows;
41 #else
42   return FileSpec::Style::posix;
43 #endif
44 }
45
46 bool PathStyleIsPosix(FileSpec::Style style) {
47   return (style == FileSpec::Style::posix ||
48           (style == FileSpec::Style::native &&
49            GetNativeStyle() == FileSpec::Style::posix));
50 }
51
52 const char *GetPathSeparators(FileSpec::Style style) {
53   return llvm::sys::path::get_separator(style).data();
54 }
55
56 char GetPreferredPathSeparator(FileSpec::Style style) {
57   return GetPathSeparators(style)[0];
58 }
59
60 void Denormalize(llvm::SmallVectorImpl<char> &path, FileSpec::Style style) {
61   if (PathStyleIsPosix(style))
62     return;
63
64   std::replace(path.begin(), path.end(), '/', '\\');
65 }
66
67 } // end anonymous namespace
68
69 FileSpec::FileSpec() : m_style(GetNativeStyle()) {}
70
71 //------------------------------------------------------------------
72 // Default constructor that can take an optional full path to a file on disk.
73 //------------------------------------------------------------------
74 FileSpec::FileSpec(llvm::StringRef path, Style style) : m_style(style) {
75   SetFile(path, style);
76 }
77
78 FileSpec::FileSpec(llvm::StringRef path, const llvm::Triple &Triple)
79     : FileSpec{path, Triple.isOSWindows() ? Style::windows : Style::posix} {}
80
81 //------------------------------------------------------------------
82 // Copy constructor
83 //------------------------------------------------------------------
84 FileSpec::FileSpec(const FileSpec &rhs)
85     : m_directory(rhs.m_directory), m_filename(rhs.m_filename),
86       m_is_resolved(rhs.m_is_resolved), m_style(rhs.m_style) {}
87
88 //------------------------------------------------------------------
89 // Copy constructor
90 //------------------------------------------------------------------
91 FileSpec::FileSpec(const FileSpec *rhs) : m_directory(), m_filename() {
92   if (rhs)
93     *this = *rhs;
94 }
95
96 //------------------------------------------------------------------
97 // Virtual destructor in case anyone inherits from this class.
98 //------------------------------------------------------------------
99 FileSpec::~FileSpec() {}
100
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 }
195 //------------------------------------------------------------------
196 // Assignment operator.
197 //------------------------------------------------------------------
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;
203     m_style = rhs.m_style;
204   }
205   return *this;
206 }
207
208 void FileSpec::SetFile(llvm::StringRef pathname) { SetFile(pathname, m_style); }
209
210 //------------------------------------------------------------------
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.
214 //------------------------------------------------------------------
215 void FileSpec::SetFile(llvm::StringRef pathname, Style style) {
216   m_filename.Clear();
217   m_directory.Clear();
218   m_is_resolved = false;
219   m_style = (style == Style::native) ? GetNativeStyle() : style;
220
221   if (pathname.empty())
222     return;
223
224   llvm::SmallString<128> resolved(pathname);
225
226   // Normalize the path by removing ".", ".." and other redundant components.
227   if (needsNormalization(resolved))
228     llvm::sys::path::remove_dots(resolved, true, m_style);
229
230   // Normalize back slashes to forward slashes
231   if (m_style == Style::windows)
232     std::replace(resolved.begin(), resolved.end(), '\\', '/');
233
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
237     // utilities.
238     m_filename.SetString(".");
239     return;
240   }
241
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);
245   if(!filename.empty())
246     m_filename.SetString(filename);
247
248   llvm::StringRef directory = llvm::sys::path::parent_path(resolved, m_style);
249   if(!directory.empty())
250     m_directory.SetString(directory);
251 }
252
253 void FileSpec::SetFile(llvm::StringRef path, const llvm::Triple &Triple) {
254   return SetFile(path, Triple.isOSWindows() ? Style::windows : Style::posix);
255 }
256
257 //----------------------------------------------------------------------
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:
260 //
261 //  if (file_spec)
262 //  {}
263 //----------------------------------------------------------------------
264 FileSpec::operator bool() const { return m_filename || m_directory; }
265
266 //----------------------------------------------------------------------
267 // Logical NOT operator. This allows code to check any FileSpec objects to see
268 // if they are invalid using code such as:
269 //
270 //  if (!file_spec)
271 //  {}
272 //----------------------------------------------------------------------
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);
278 }
279
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);
283 }
284
285 //------------------------------------------------------------------
286 // Equal to operator
287 //------------------------------------------------------------------
288 bool FileSpec::operator==(const FileSpec &rhs) const {
289   return FileEquals(rhs) && DirectoryEquals(rhs);
290 }
291
292 //------------------------------------------------------------------
293 // Not equal to operator
294 //------------------------------------------------------------------
295 bool FileSpec::operator!=(const FileSpec &rhs) const { return !(*this == rhs); }
296
297 //------------------------------------------------------------------
298 // Less than operator
299 //------------------------------------------------------------------
300 bool FileSpec::operator<(const FileSpec &rhs) const {
301   return FileSpec::Compare(*this, rhs, true) < 0;
302 }
303
304 //------------------------------------------------------------------
305 // Dump a FileSpec object to a stream
306 //------------------------------------------------------------------
307 Stream &lldb_private::operator<<(Stream &s, const FileSpec &f) {
308   f.Dump(&s);
309   return s;
310 }
311
312 //------------------------------------------------------------------
313 // Clear this object by releasing both the directory and filename string values
314 // and making them both the empty string.
315 //------------------------------------------------------------------
316 void FileSpec::Clear() {
317   m_directory.Clear();
318   m_filename.Clear();
319 }
320
321 //------------------------------------------------------------------
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.
327 //
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".
330 //------------------------------------------------------------------
331 int FileSpec::Compare(const FileSpec &a, const FileSpec &b, bool full) {
332   int result = 0;
333
334   // case sensitivity of compare
335   const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive();
336
337   // If full is true, then we must compare both the directory and filename.
338
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.
343
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);
350 }
351
352 bool FileSpec::Equal(const FileSpec &a, const FileSpec &b, bool full) {
353   // case sensitivity of equality test
354   const bool case_sensitive = a.IsCaseSensitive() || b.IsCaseSensitive();
355
356   const bool filenames_equal = ConstString::Equals(a.m_filename,
357                                                    b.m_filename,
358                                                    case_sensitive);
359
360   if (!filenames_equal)
361     return false;
362
363   if (!full && (a.GetDirectory().IsEmpty() || b.GetDirectory().IsEmpty()))
364     return filenames_equal;
365
366   return a == b;
367 }
368
369 //------------------------------------------------------------------
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.
373 //------------------------------------------------------------------
374 void FileSpec::Dump(Stream *s) const {
375   if (s) {
376     std::string path{GetPath(true)};
377     s->PutCString(path);
378     char path_separator = GetPreferredPathSeparator(m_style);
379     if (!m_filename && !path.empty() && path.back() != path_separator)
380       s->PutChar(path_separator);
381   }
382 }
383
384 FileSpec::Style FileSpec::GetPathStyle() const { return m_style; }
385
386 //------------------------------------------------------------------
387 // Directory string get accessor.
388 //------------------------------------------------------------------
389 ConstString &FileSpec::GetDirectory() { return m_directory; }
390
391 //------------------------------------------------------------------
392 // Directory string const get accessor.
393 //------------------------------------------------------------------
394 const ConstString &FileSpec::GetDirectory() const { return m_directory; }
395
396 //------------------------------------------------------------------
397 // Filename string get accessor.
398 //------------------------------------------------------------------
399 ConstString &FileSpec::GetFilename() { return m_filename; }
400
401 //------------------------------------------------------------------
402 // Filename string const get accessor.
403 //------------------------------------------------------------------
404 const ConstString &FileSpec::GetFilename() const { return m_filename; }
405
406 //------------------------------------------------------------------
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.
409 //------------------------------------------------------------------
410 size_t FileSpec::GetPath(char *path, size_t path_max_len,
411                          bool denormalize) const {
412   if (!path)
413     return 0;
414
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());
418 }
419
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());
424 }
425
426 const char *FileSpec::GetCString(bool denormalize) const {
427   return ConstString{GetPath(denormalize)}.AsCString(nullptr);
428 }
429
430 void FileSpec::GetPath(llvm::SmallVectorImpl<char> &path,
431                        bool denormalize) const {
432   path.append(m_directory.GetStringRef().begin(),
433               m_directory.GetStringRef().end());
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(), '/');
440   path.append(m_filename.GetStringRef().begin(),
441               m_filename.GetStringRef().end());
442   if (denormalize && !path.empty())
443     Denormalize(path, m_style);
444 }
445
446 ConstString FileSpec::GetFileNameExtension() const {
447   return ConstString(
448       llvm::sys::path::extension(m_filename.GetStringRef(), m_style));
449 }
450
451 ConstString FileSpec::GetFileNameStrippingExtension() const {
452   return ConstString(llvm::sys::path::stem(m_filename.GetStringRef(), m_style));
453 }
454
455 //------------------------------------------------------------------
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.
458 //------------------------------------------------------------------
459 size_t FileSpec::MemorySize() const {
460   return m_filename.MemorySize() + m_directory.MemorySize();
461 }
462
463 FileSpec
464 FileSpec::CopyByAppendingPathComponent(llvm::StringRef component) const {
465   FileSpec ret = *this;
466   ret.AppendPathComponent(component);
467   return ret;
468 }
469
470 FileSpec FileSpec::CopyByRemovingLastPathComponent() const {
471   llvm::SmallString<64> current_path;
472   GetPath(current_path, false);
473   if (llvm::sys::path::has_parent_path(current_path, m_style))
474     return FileSpec(llvm::sys::path::parent_path(current_path, m_style),
475                     m_style);
476   return *this;
477 }
478
479 ConstString FileSpec::GetLastPathComponent() const {
480   llvm::SmallString<64> current_path;
481   GetPath(current_path, false);
482   return ConstString(llvm::sys::path::filename(current_path, m_style));
483 }
484
485 void FileSpec::PrependPathComponent(llvm::StringRef component) {
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);
492   SetFile(new_path, m_style);
493 }
494
495 void FileSpec::PrependPathComponent(const FileSpec &new_path) {
496   return PrependPathComponent(new_path.GetPath(false));
497 }
498
499 void FileSpec::AppendPathComponent(llvm::StringRef component) {
500   llvm::SmallString<64> current_path;
501   GetPath(current_path, false);
502   llvm::sys::path::append(current_path, m_style, component);
503   SetFile(current_path, m_style);
504 }
505
506 void FileSpec::AppendPathComponent(const FileSpec &new_path) {
507   return AppendPathComponent(new_path.GetPath(false));
508 }
509
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)) {
514     SetFile(llvm::sys::path::parent_path(current_path, m_style));
515     return true;
516   }
517   return false;
518 }
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 //------------------------------------------------------------------
528 bool FileSpec::IsSourceImplementationFile() const {
529   ConstString extension(GetFileNameExtension());
530   if (!extension)
531     return false;
532
533   static RegularExpression g_source_file_regex(llvm::StringRef(
534       "^.([cC]|[mM]|[mM][mM]|[cC][pP][pP]|[cC]\\+\\+|[cC][xX][xX]|[cC][cC]|["
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());
539 }
540
541 bool FileSpec::IsRelative() const {
542   return !IsAbsolute();
543 }
544
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 }
559
560 void llvm::format_provider<FileSpec>::format(const FileSpec &F,
561                                              raw_ostream &Stream,
562                                              StringRef Style) {
563   assert(
564       (Style.empty() || Style.equals_lower("F") || Style.equals_lower("D")) &&
565       "Invalid FileSpec style!");
566
567   StringRef dir = F.GetDirectory().GetStringRef();
568   StringRef file = F.GetFilename().GetStringRef();
569
570   if (dir.empty() && file.empty()) {
571     Stream << "(empty)";
572     return;
573   }
574
575   if (Style.equals_lower("F")) {
576     Stream << (file.empty() ? "(empty)" : file);
577     return;
578   }
579
580   // Style is either D or empty, either way we need to print the directory.
581   if (!dir.empty()) {
582     // Directory is stored in normalized form, which might be different than
583     // preferred form.  In order to handle this, we need to cut off the
584     // filename, then denormalize, then write the entire denorm'ed directory.
585     llvm::SmallString<64> denormalized_dir = dir;
586     Denormalize(denormalized_dir, F.GetPathStyle());
587     Stream << denormalized_dir;
588     Stream << GetPreferredPathSeparator(F.GetPathStyle());
589   }
590
591   if (Style.equals_lower("D")) {
592     // We only want to print the directory, so now just exit.
593     if (dir.empty())
594       Stream << "(empty)";
595     return;
596   }
597
598   if (!file.empty())
599     Stream << file;
600 }