Skip to content

Commit 9e9c7b2

Browse files
authored
Merge pull request #3067 from rmaz/relworkingdirpatch
[lldb] handle relative working directories in swiftmodule debug info
2 parents 5e810bd + 8c12e44 commit 9e9c7b2

File tree

3 files changed

+80
-28
lines changed

3 files changed

+80
-28
lines changed

lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@
143143
if (HasFatalErrors()) { \
144144
return; \
145145
} \
146-
} while (0);
146+
} while (0);
147147
#define VALID_OR_RETURN_CHECK_TYPE(type, value) \
148148
do { \
149149
if (HasFatalErrors() || !type) { \
@@ -1415,29 +1415,6 @@ bool ConsumeIncludeOption(StringRef &arg, StringRef &prefix) {
14151415
return false;
14161416
}
14171417

1418-
/// Turn relative paths in clang options into absolute paths based on
1419-
/// \c cur_working_dir.
1420-
template <typename SmallString>
1421-
void ApplyWorkingDir(SmallString &clang_argument, StringRef cur_working_dir) {
1422-
StringRef arg = clang_argument.str();
1423-
StringRef prefix;
1424-
if (ConsumeIncludeOption(arg, prefix)) {
1425-
// Ignore the option part of a double-arg include option.
1426-
if (arg.empty())
1427-
return;
1428-
} else if (arg.startswith("-")) {
1429-
// Assume this is a compiler arg and not a path starting with "-".
1430-
return;
1431-
}
1432-
// There is most probably a path in arg now.
1433-
if (!llvm::sys::path::is_relative(arg))
1434-
return;
1435-
SmallString rel_path = arg;
1436-
clang_argument = prefix;
1437-
llvm::sys::path::append(clang_argument, cur_working_dir, rel_path);
1438-
llvm::sys::path::remove_dots(clang_argument);
1439-
}
1440-
14411418
std::array<StringRef, 2> macro_flags = { "-D", "-U" };
14421419
std::array<StringRef, 5> multi_arg_flags =
14431420
{ "-D", "-U", "-I", "-F", "-working-directory" };
@@ -1530,6 +1507,31 @@ void SwiftASTContext::AddUserClangArgs(TargetProperties &props) {
15301507
AddExtraClangArgs(user_clang_flags);
15311508
}
15321509

1510+
/// Turn relative paths in clang options into absolute paths based on
1511+
/// \c cur_working_dir.
1512+
void SwiftASTContext::ApplyWorkingDir(
1513+
llvm::SmallVectorImpl<char> &clang_argument, StringRef cur_working_dir) {
1514+
StringRef arg = StringRef(clang_argument.data(), clang_argument.size());
1515+
StringRef prefix;
1516+
if (ConsumeIncludeOption(arg, prefix)) {
1517+
// Ignore the option part of a double-arg include option.
1518+
if (arg.empty())
1519+
return;
1520+
} else if (arg.startswith("-")) {
1521+
// Assume this is a compiler arg and not a path starting with "-".
1522+
return;
1523+
}
1524+
// There is most probably a path in arg now.
1525+
if (!llvm::sys::path::is_relative(arg))
1526+
return;
1527+
1528+
llvm::SmallString<128> joined_path;
1529+
llvm::sys::path::append(joined_path, cur_working_dir, arg);
1530+
llvm::sys::path::remove_dots(joined_path);
1531+
clang_argument.resize(prefix.size());
1532+
clang_argument.append(joined_path.begin(), joined_path.end());
1533+
}
1534+
15331535
void SwiftASTContext::RemapClangImporterOptions(
15341536
const PathMappingList &path_map) {
15351537
auto &options = GetClangImporterOptions();
@@ -4597,7 +4599,7 @@ llvm::Optional<SwiftASTContext::TypeOrDecl>
45974599
SwiftASTContext::FindTypeOrDecl(const char *name,
45984600
swift::ModuleDecl *swift_module) {
45994601
VALID_OR_RETURN(llvm::Optional<SwiftASTContext::TypeOrDecl>());
4600-
4602+
46014603
TypesOrDecls search_results;
46024604

46034605
FindTypesOrDecls(name, swift_module, search_results, false);
@@ -5777,7 +5779,7 @@ SwiftASTContext::GetArrayElementType(opaque_compiler_type_t type,
57775779
}
57785780
}
57795781
}
5780-
5782+
57815783
return element_type;
57825784
}
57835785

@@ -5852,7 +5854,7 @@ size_t SwiftASTContext::GetNumMemberFunctions(opaque_compiler_type_t type) {
58525854
}
58535855
}
58545856
}
5855-
5857+
58565858
return num_functions;
58575859
}
58585860

lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "lldb/Core/ThreadSafeDenseMap.h"
2222
#include "lldb/Core/ThreadSafeDenseSet.h"
2323
#include "lldb/Utility/Either.h"
24+
#include "llvm/ADT/SmallVector.h"
2425
#include "llvm/ADT/StringRef.h"
2526
#include "llvm/Target/TargetOptions.h"
2627

@@ -167,7 +168,7 @@ class SwiftASTContext : public TypeSystemSwift {
167168
Target *target = nullptr);
168169

169170
SwiftASTContext(const SwiftASTContext &rhs) = delete;
170-
171+
171172
TypeSystemSwiftTypeRef &GetTypeSystemSwiftTypeRef() override {
172173
return m_typeref_typesystem;
173174
}
@@ -555,6 +556,9 @@ class SwiftASTContext : public TypeSystemSwift {
555556
const CompilerType &type, NonTriviallyManagedReferenceStrategy &strategy,
556557
CompilerType *underlying_type = nullptr);
557558

559+
static void ApplyWorkingDir(llvm::SmallVectorImpl<char> &clang_argument,
560+
llvm::StringRef cur_working_dir);
561+
558562
// AST related queries
559563

560564
uint32_t GetPointerByteSize() override;
@@ -984,4 +988,5 @@ class SwiftASTContextForExpressions : public SwiftASTContext {
984988
};
985989

986990
} // namespace lldb_private
991+
987992
#endif // #ifndef liblldb_SwiftASTContext_h_

lldb/unittests/Symbol/TestSwiftASTContext.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,51 @@ TEST_F(TestSwiftASTContext, SwiftFriendlyTriple) {
220220
llvm::Triple("aarch64-unknown-linux-gnu"));
221221
}
222222

223+
TEST_F(TestSwiftASTContext, ApplyWorkingDir) {
224+
std::string abs_working_dir = "/abs/dir";
225+
std::string rel_working_dir = "rel/dir";
226+
227+
// non-include option should not apply working dir
228+
llvm::SmallString<128> non_include_flag("-non-include-flag");
229+
SwiftASTContext::ApplyWorkingDir(non_include_flag, abs_working_dir);
230+
EXPECT_EQ(non_include_flag, llvm::SmallString<128>("-non-include-flag"));
231+
232+
// absolute paths should not apply working dir
233+
llvm::SmallString<128> abs_path("/abs/path");
234+
SwiftASTContext::ApplyWorkingDir(abs_path, abs_working_dir);
235+
EXPECT_EQ(abs_path, llvm::SmallString<128>("/abs/path"));
236+
237+
llvm::SmallString<128> single_arg_abs_path(
238+
"-fmodule-map-file=/module/map/path");
239+
SwiftASTContext::ApplyWorkingDir(single_arg_abs_path, abs_working_dir);
240+
EXPECT_EQ(single_arg_abs_path,
241+
llvm::SmallString<128>("-fmodule-map-file=/module/map/path"));
242+
243+
// relative paths apply working dir
244+
llvm::SmallString<128> rel_path("rel/path");
245+
SwiftASTContext::ApplyWorkingDir(rel_path, abs_working_dir);
246+
EXPECT_EQ(rel_path, llvm::SmallString<128>("/abs/dir/rel/path"));
247+
248+
rel_path = llvm::SmallString<128>("rel/path");
249+
SwiftASTContext::ApplyWorkingDir(rel_path, rel_working_dir);
250+
EXPECT_EQ(rel_path, llvm::SmallString<128>("rel/dir/rel/path"));
251+
252+
// single arg include option applies working dir
253+
llvm::SmallString<128> single_arg_rel_path(
254+
"-fmodule-map-file=module.modulemap");
255+
SwiftASTContext::ApplyWorkingDir(single_arg_rel_path, abs_working_dir);
256+
EXPECT_EQ(
257+
single_arg_rel_path,
258+
llvm::SmallString<128>("-fmodule-map-file=/abs/dir/module.modulemap"));
259+
260+
single_arg_rel_path =
261+
llvm::SmallString<128>("-fmodule-map-file=module.modulemap");
262+
SwiftASTContext::ApplyWorkingDir(single_arg_rel_path, rel_working_dir);
263+
EXPECT_EQ(
264+
single_arg_rel_path,
265+
llvm::SmallString<128>("-fmodule-map-file=rel/dir/module.modulemap"));
266+
}
267+
223268
namespace {
224269
const std::vector<std::string> duplicated_flags = {
225270
"-DMACRO1", "-D", "MACRO1", "-UMACRO2", "-U", "MACRO2",

0 commit comments

Comments
 (0)