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