Skip to content

[lldb] add de-duplication and working dir support for fmodule-file #4039

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
Mar 9, 2022
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
18 changes: 14 additions & 4 deletions lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1407,19 +1407,29 @@ bool ConsumeIncludeOption(StringRef &arg, StringRef &prefix) {
"-isystem",
"-isysroot",
"-ivfsoverlay"};

for (StringRef &option : options)
if (arg.consume_front(option)) {
prefix = option;
return true;
}

// We need special handling for -fmodule-file as we need to
// split on the final = after the module name.
if (arg.startswith("-fmodule-file=")) {
prefix = arg.substr(0, arg.rfind("=") + 1);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this is safe, even if arg is either "foo", or "foo="?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two valid cases are -fmodule-file=some/path and -fmodule-file=modulename=some/path. We know the first = is present as we checked the prefix above, so this will split correctly in both cases. There is no form -fmodule-file some/path so we don't need to handle that case.

arg.consume_front(prefix);
return true;
}

return false;
}

std::array<StringRef, 2> macro_flags = { "-D", "-U" };
std::array<StringRef, 5> multi_arg_flags =
{ "-D", "-U", "-I", "-F", "-working-directory" };
std::array<StringRef, 5> args_to_unique =
{ "-D", "-U", "-I", "-F", "-fmodule-map-file=" };
std::array<StringRef, 6> args_to_unique = {
"-D", "-U", "-I", "-F", "-fmodule-file=", "-fmodule-map-file="};

bool IsMultiArgClangFlag(StringRef arg) {
for (auto &flag : multi_arg_flags)
Expand Down Expand Up @@ -7358,8 +7368,8 @@ CompilerType SwiftASTContext::GetUnboundGenericType(opaque_compiler_type_t type,
auto *nominal_type_decl = unbound_generic_type->getDecl();
swift::GenericSignature generic_sig =
nominal_type_decl->getGenericSignature();
swift::TypeArrayView<swift::GenericTypeParamType> depView
= generic_sig.getGenericParams();
swift::TypeArrayView<swift::GenericTypeParamType> depView =
generic_sig.getGenericParams();
swift::Type depTy = depView[idx];
return ToCompilerType({nominal_type_decl->mapTypeIntoContext(depTy)
->castTo<swift::ArchetypeType>()});
Expand Down
83 changes: 73 additions & 10 deletions lldb/unittests/Symbol/TestSwiftASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,19 +268,82 @@ TEST_F(TestSwiftASTContext, ApplyWorkingDir) {
EXPECT_EQ(
single_arg_rel_path,
llvm::SmallString<128>("-fmodule-map-file=rel/dir/module.modulemap"));

// fmodule-file needs to handle different cases:
// -fmodule-file=path/to/pcm
// -fmodule-file=name=path/to/pcm

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here it says -fmodule-file=name=path/to/pcm but all the tests use -fmodule-file=modulename=path/to/pcm is that a tricky test for the edge-case of passing in something that ends with name= or a typo?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name string can be anything, it is used to indicate the name of the module that you are providing the path to. I can make these the same if you think it is clearer? It shouldn't make any difference to the flag splitting logic though.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, this isn't a key=value pair whre the key is "name", it's [name of the module]=[file name]!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly, yes. I believe the idea is that the compiler can avoid loading pcm files until they are imported.

llvm::SmallString<128> module_file_abs_path(
"-fmodule-file=/some/dir/module.pcm");
SwiftASTContext::ApplyWorkingDir(module_file_abs_path, abs_working_dir);
EXPECT_EQ(module_file_abs_path,
llvm::SmallString<128>("-fmodule-file=/some/dir/module.pcm"));

llvm::SmallString<128> module_file_rel_path(
"-fmodule-file=relpath/module.pcm");
SwiftASTContext::ApplyWorkingDir(module_file_rel_path, abs_working_dir);
EXPECT_EQ(
module_file_rel_path,
llvm::SmallString<128>("-fmodule-file=/abs/dir/relpath/module.pcm"));

llvm::SmallString<128> module_file_with_name_abs_path(
"-fmodule-file=modulename=/some/dir/module.pcm");
SwiftASTContext::ApplyWorkingDir(module_file_with_name_abs_path,
abs_working_dir);
EXPECT_EQ(
module_file_with_name_abs_path,
llvm::SmallString<128>("-fmodule-file=modulename=/some/dir/module.pcm"));

llvm::SmallString<128> module_file_with_name_rel_path(
"-fmodule-file=modulename=relpath/module.pcm");
SwiftASTContext::ApplyWorkingDir(module_file_with_name_rel_path,
abs_working_dir);
EXPECT_EQ(module_file_with_name_rel_path,
llvm::SmallString<128>(
"-fmodule-file=modulename=/abs/dir/relpath/module.pcm"));
}

namespace {
const std::vector<std::string> duplicated_flags = {
"-DMACRO1", "-D", "MACRO1", "-UMACRO2", "-U", "MACRO2",
"-I/path1", "-I", "/path1", "-F/path2", "-F", "/path2",
"-fmodule-map-file=/path3", "-fmodule-map-file=/path3",
"-F/path2", "-F", "/path2", "-I/path1", "-I", "/path1",
"-UMACRO2", "-U", "MACRO2", "-DMACRO1", "-D", "MACRO1",
};
const std::vector<std::string> uniqued_flags = {
"-DMACRO1", "-UMACRO2", "-I/path1", "-F/path2", "-fmodule-map-file=/path3"
};
const std::vector<std::string> duplicated_flags = {
"-DMACRO1",
"-D",
"MACRO1",
"-UMACRO2",
"-U",
"MACRO2",
"-I/path1",
"-I",
"/path1",
"-F/path2",
"-F",
"/path2",
"-fmodule-map-file=/path3",
"-fmodule-map-file=/path3",
"-F/path2",
"-F",
"/path2",
"-I/path1",
"-I",
"/path1",
"-UMACRO2",
"-U",
"MACRO2",
"-DMACRO1",
"-D",
"MACRO1",
"-fmodule-file=/path/to/pcm",
"-fmodule-file=/path/to/pcm",
"-fmodule-file=modulename=/path/to/pcm",
"-fmodule-file=modulename=/path/to/pcm",
};
const std::vector<std::string> uniqued_flags = {
"-DMACRO1",
"-UMACRO2",
"-I/path1",
"-F/path2",
"-fmodule-map-file=/path3",
"-fmodule-file=/path/to/pcm",
"-fmodule-file=modulename=/path/to/pcm",
};
} // namespace

TEST(ClangArgs, UniquingCollisionWithExistingFlags) {
Expand Down