Skip to content

[lldb] handle relative working directories in swiftmodule debug info #3067

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 29 additions & 27 deletions lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
if (HasFatalErrors()) { \
return; \
} \
} while (0);
} while (0);
#define VALID_OR_RETURN_CHECK_TYPE(type, value) \
do { \
if (HasFatalErrors() || !type) { \
Expand Down Expand Up @@ -1414,29 +1414,6 @@ bool ConsumeIncludeOption(StringRef &arg, StringRef &prefix) {
return false;
}

/// Turn relative paths in clang options into absolute paths based on
/// \c cur_working_dir.
template <typename SmallString>
void ApplyWorkingDir(SmallString &clang_argument, StringRef cur_working_dir) {
StringRef arg = clang_argument.str();
StringRef prefix;
if (ConsumeIncludeOption(arg, prefix)) {
// Ignore the option part of a double-arg include option.
if (arg.empty())
return;
} else if (arg.startswith("-")) {
// Assume this is a compiler arg and not a path starting with "-".
return;
}
// There is most probably a path in arg now.
if (!llvm::sys::path::is_relative(arg))
return;
SmallString rel_path = arg;
clang_argument = prefix;
llvm::sys::path::append(clang_argument, cur_working_dir, rel_path);
llvm::sys::path::remove_dots(clang_argument);
}

std::array<StringRef, 2> macro_flags = { "-D", "-U" };
std::array<StringRef, 5> multi_arg_flags =
{ "-D", "-U", "-I", "-F", "-working-directory" };
Expand Down Expand Up @@ -1529,6 +1506,31 @@ void SwiftASTContext::AddUserClangArgs(TargetProperties &props) {
AddExtraClangArgs(user_clang_flags);
}

/// Turn relative paths in clang options into absolute paths based on
/// \c cur_working_dir.
void SwiftASTContext::ApplyWorkingDir(
llvm::SmallVectorImpl<char> &clang_argument, StringRef cur_working_dir) {
StringRef arg = StringRef(clang_argument.data(), clang_argument.size());
StringRef prefix;
if (ConsumeIncludeOption(arg, prefix)) {
// Ignore the option part of a double-arg include option.
if (arg.empty())
return;
} else if (arg.startswith("-")) {
// Assume this is a compiler arg and not a path starting with "-".
return;
}
// There is most probably a path in arg now.
if (!llvm::sys::path::is_relative(arg))
return;

llvm::SmallString<128> joined_path;
llvm::sys::path::append(joined_path, cur_working_dir, arg);
llvm::sys::path::remove_dots(joined_path);
clang_argument.resize(prefix.size());
clang_argument.append(joined_path.begin(), joined_path.end());
}

void SwiftASTContext::RemapClangImporterOptions(
const PathMappingList &path_map) {
auto &options = GetClangImporterOptions();
Expand Down Expand Up @@ -4592,7 +4594,7 @@ llvm::Optional<SwiftASTContext::TypeOrDecl>
SwiftASTContext::FindTypeOrDecl(const char *name,
swift::ModuleDecl *swift_module) {
VALID_OR_RETURN(llvm::Optional<SwiftASTContext::TypeOrDecl>());

TypesOrDecls search_results;

FindTypesOrDecls(name, swift_module, search_results, false);
Expand Down Expand Up @@ -5772,7 +5774,7 @@ SwiftASTContext::GetArrayElementType(opaque_compiler_type_t type,
}
}
}

return element_type;
}

Expand Down Expand Up @@ -5847,7 +5849,7 @@ size_t SwiftASTContext::GetNumMemberFunctions(opaque_compiler_type_t type) {
}
}
}

return num_functions;
}

Expand Down
7 changes: 6 additions & 1 deletion lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "lldb/Core/ThreadSafeDenseMap.h"
#include "lldb/Core/ThreadSafeDenseSet.h"
#include "lldb/Utility/Either.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Target/TargetOptions.h"

Expand Down Expand Up @@ -166,7 +167,7 @@ class SwiftASTContext : public TypeSystemSwift {
Target *target = nullptr);

SwiftASTContext(const SwiftASTContext &rhs) = delete;

TypeSystemSwiftTypeRef &GetTypeSystemSwiftTypeRef() override {
return m_typeref_typesystem;
}
Expand Down Expand Up @@ -552,6 +553,9 @@ class SwiftASTContext : public TypeSystemSwift {
const CompilerType &type, NonTriviallyManagedReferenceStrategy &strategy,
CompilerType *underlying_type = nullptr);

static void ApplyWorkingDir(llvm::SmallVectorImpl<char> &clang_argument,
llvm::StringRef cur_working_dir);

// AST related queries

uint32_t GetPointerByteSize() override;
Expand Down Expand Up @@ -981,4 +985,5 @@ class SwiftASTContextForExpressions : public SwiftASTContext {
};

} // namespace lldb_private

#endif // #ifndef liblldb_SwiftASTContext_h_
45 changes: 45 additions & 0 deletions lldb/unittests/Symbol/TestSwiftASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,51 @@ TEST_F(TestSwiftASTContext, SwiftFriendlyTriple) {
llvm::Triple("aarch64-unknown-linux-gnu"));
}

TEST_F(TestSwiftASTContext, ApplyWorkingDir) {
std::string abs_working_dir = "/abs/dir";
std::string rel_working_dir = "rel/dir";

// non-include option should not apply working dir
llvm::SmallString<128> non_include_flag("-non-include-flag");
SwiftASTContext::ApplyWorkingDir(non_include_flag, abs_working_dir);
EXPECT_EQ(non_include_flag, llvm::SmallString<128>("-non-include-flag"));

// absolute paths should not apply working dir
llvm::SmallString<128> abs_path("/abs/path");
SwiftASTContext::ApplyWorkingDir(abs_path, abs_working_dir);
EXPECT_EQ(abs_path, llvm::SmallString<128>("/abs/path"));

llvm::SmallString<128> single_arg_abs_path(
"-fmodule-map-file=/module/map/path");
SwiftASTContext::ApplyWorkingDir(single_arg_abs_path, abs_working_dir);
EXPECT_EQ(single_arg_abs_path,
llvm::SmallString<128>("-fmodule-map-file=/module/map/path"));

// relative paths apply working dir
llvm::SmallString<128> rel_path("rel/path");
SwiftASTContext::ApplyWorkingDir(rel_path, abs_working_dir);
EXPECT_EQ(rel_path, llvm::SmallString<128>("/abs/dir/rel/path"));

rel_path = llvm::SmallString<128>("rel/path");
SwiftASTContext::ApplyWorkingDir(rel_path, rel_working_dir);
EXPECT_EQ(rel_path, llvm::SmallString<128>("rel/dir/rel/path"));

// single arg include option applies working dir
llvm::SmallString<128> single_arg_rel_path(
"-fmodule-map-file=module.modulemap");
SwiftASTContext::ApplyWorkingDir(single_arg_rel_path, abs_working_dir);
EXPECT_EQ(
single_arg_rel_path,
llvm::SmallString<128>("-fmodule-map-file=/abs/dir/module.modulemap"));

single_arg_rel_path =
llvm::SmallString<128>("-fmodule-map-file=module.modulemap");
SwiftASTContext::ApplyWorkingDir(single_arg_rel_path, rel_working_dir);
EXPECT_EQ(
single_arg_rel_path,
llvm::SmallString<128>("-fmodule-map-file=rel/dir/module.modulemap"));
}

namespace {
const std::vector<std::string> duplicated_flags = {
"-DMACRO1", "-D", "MACRO1", "-UMACRO2", "-U", "MACRO2",
Expand Down