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