LLDB mainline
RealpathPrefixes.cpp
Go to the documentation of this file.
1//===-- RealpathPrefixes.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
10
14#include "lldb/Utility/Log.h"
16
17using namespace lldb_private;
18
20 const FileSpecList &file_spec_list,
21 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs)
22 : m_fs(fs) {
23 m_prefixes.reserve(file_spec_list.GetSize());
24 for (const FileSpec &file_spec : file_spec_list) {
25 m_prefixes.emplace_back(file_spec.GetPath());
26 }
27}
28
29std::optional<FileSpec>
31 if (m_prefixes.empty())
32 return std::nullopt;
33
34 // Test if `b` is a *path* prefix of `a` (not just *string* prefix).
35 // E.g. "/foo/bar" is a path prefix of "/foo/bar/baz" but not "/foo/barbaz".
36 auto is_path_prefix = [](llvm::StringRef a, llvm::StringRef b,
37 bool case_sensitive,
38 llvm::sys::path::Style style) -> bool {
39 if (case_sensitive ? a.consume_front(b) : a.consume_front_insensitive(b))
40 // If `b` isn't "/", then it won't end with "/" because it comes from
41 // `FileSpec`. After `a` consumes `b`, `a` should either be empty (i.e.
42 // `a` == `b`) or end with "/" (the remainder of `a` is a subdirectory).
43 return b == "/" || a.empty() ||
44 llvm::sys::path::is_separator(a[0], style);
45 return false;
46 };
47 std::string file_spec_path = file_spec.GetPath();
48 for (const std::string &prefix : m_prefixes) {
49 if (is_path_prefix(file_spec_path, prefix, file_spec.IsCaseSensitive(),
50 file_spec.GetPathStyle())) {
51 // Stats and logging.
54 LLDB_LOGF(log, "Realpath'ing support file %s", file_spec_path.c_str());
55
56 // One prefix matched. Try to realpath.
57 PathSmallString buff;
58 std::error_code ec = m_fs->getRealPath(file_spec_path, buff);
59 if (ec)
60 return std::nullopt;
61 FileSpec realpath(buff, file_spec.GetPathStyle());
62
63 // Only return realpath if it is different from the original file_spec.
64 if (realpath != file_spec)
65 return realpath;
66 return std::nullopt;
67 }
68 }
69 // No prefix matched
70 return std::nullopt;
71}
#define LLDB_LOGF(log,...)
Definition: Log.h:376
A file collection class.
Definition: FileSpecList.h:91
size_t GetSize() const
Get the number of files in the file list.
A file utility class.
Definition: FileSpec.h:56
bool IsCaseSensitive() const
Case sensitivity of path.
Definition: FileSpec.h:205
Style GetPathStyle() const
Definition: FileSpec.cpp:333
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:367
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > m_fs
std::optional< FileSpec > ResolveSymlinks(const FileSpec &file_spec)
std::vector< std::string > m_prefixes
RealpathPrefixes(const FileSpecList &file_spec_list, llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > fs=llvm::vfs::getRealFileSystem())
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:332
llvm::SmallString< 256 > PathSmallString