1 //===-- RealpathPrefixes.cpp ----------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Utility/RealpathPrefixes.h"
11 #include "lldb/Utility/FileSpec.h"
12 #include "lldb/Utility/FileSpecList.h"
13 #include "lldb/Utility/LLDBLog.h"
14 #include "lldb/Utility/Log.h"
15 #include "lldb/lldb-private-types.h"
17 using namespace lldb_private
;
19 RealpathPrefixes::RealpathPrefixes(
20 const FileSpecList
&file_spec_list
,
21 llvm::IntrusiveRefCntPtr
<llvm::vfs::FileSystem
> 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());
29 std::optional
<FileSpec
>
30 RealpathPrefixes::ResolveSymlinks(const FileSpec
&file_spec
) {
31 if (m_prefixes
.empty())
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
,
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
);
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())) {
52 IncreaseSourceRealpathAttemptCount();
53 Log
*log
= GetLog(LLDBLog::Source
);
54 LLDB_LOGF(log
, "Realpath'ing support file %s", file_spec_path
.c_str());
56 // One prefix matched. Try to realpath.
58 std::error_code ec
= m_fs
->getRealPath(file_spec_path
, buff
);
61 FileSpec
realpath(buff
, file_spec
.GetPathStyle());
63 // Only return realpath if it is different from the original file_spec.
64 if (realpath
!= file_spec
)