Skip to content

Commit 8836b23

Browse files
authored
[lldb] add de-duplication and working dir support for fmodule-file (#4039)
This PR adds correct handling of the `-fmodule-file=` flag. This flag is of the form `-fmodule-file=[<name>=]<file>`, so we need to split on the final = to get the path component in all cases.
1 parent 2d04cca commit 8836b23

File tree

2 files changed

+87
-14
lines changed

2 files changed

+87
-14
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,19 +1407,29 @@ bool ConsumeIncludeOption(StringRef &arg, StringRef &prefix) {
14071407
"-isystem",
14081408
"-isysroot",
14091409
"-ivfsoverlay"};
1410+
14101411
for (StringRef &option : options)
14111412
if (arg.consume_front(option)) {
14121413
prefix = option;
14131414
return true;
14141415
}
1416+
1417+
// We need special handling for -fmodule-file as we need to
1418+
// split on the final = after the module name.
1419+
if (arg.startswith("-fmodule-file=")) {
1420+
prefix = arg.substr(0, arg.rfind("=") + 1);
1421+
arg.consume_front(prefix);
1422+
return true;
1423+
}
1424+
14151425
return false;
14161426
}
14171427

14181428
std::array<StringRef, 2> macro_flags = { "-D", "-U" };
14191429
std::array<StringRef, 5> multi_arg_flags =
14201430
{ "-D", "-U", "-I", "-F", "-working-directory" };
1421-
std::array<StringRef, 5> args_to_unique =
1422-
{ "-D", "-U", "-I", "-F", "-fmodule-map-file=" };
1431+
std::array<StringRef, 6> args_to_unique = {
1432+
"-D", "-U", "-I", "-F", "-fmodule-file=", "-fmodule-map-file="};
14231433

14241434
bool IsMultiArgClangFlag(StringRef arg) {
14251435
for (auto &flag : multi_arg_flags)
@@ -7358,8 +7368,8 @@ CompilerType SwiftASTContext::GetUnboundGenericType(opaque_compiler_type_t type,
73587368
auto *nominal_type_decl = unbound_generic_type->getDecl();
73597369
swift::GenericSignature generic_sig =
73607370
nominal_type_decl->getGenericSignature();
7361-
swift::TypeArrayView<swift::GenericTypeParamType> depView
7362-
= generic_sig.getGenericParams();
7371+
swift::TypeArrayView<swift::GenericTypeParamType> depView =
7372+
generic_sig.getGenericParams();
73637373
swift::Type depTy = depView[idx];
73647374
return ToCompilerType({nominal_type_decl->mapTypeIntoContext(depTy)
73657375
->castTo<swift::ArchetypeType>()});

lldb/unittests/Symbol/TestSwiftASTContext.cpp

Lines changed: 73 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -268,19 +268,82 @@ TEST_F(TestSwiftASTContext, ApplyWorkingDir) {
268268
EXPECT_EQ(
269269
single_arg_rel_path,
270270
llvm::SmallString<128>("-fmodule-map-file=rel/dir/module.modulemap"));
271+
272+
// fmodule-file needs to handle different cases:
273+
// -fmodule-file=path/to/pcm
274+
// -fmodule-file=name=path/to/pcm
275+
llvm::SmallString<128> module_file_abs_path(
276+
"-fmodule-file=/some/dir/module.pcm");
277+
SwiftASTContext::ApplyWorkingDir(module_file_abs_path, abs_working_dir);
278+
EXPECT_EQ(module_file_abs_path,
279+
llvm::SmallString<128>("-fmodule-file=/some/dir/module.pcm"));
280+
281+
llvm::SmallString<128> module_file_rel_path(
282+
"-fmodule-file=relpath/module.pcm");
283+
SwiftASTContext::ApplyWorkingDir(module_file_rel_path, abs_working_dir);
284+
EXPECT_EQ(
285+
module_file_rel_path,
286+
llvm::SmallString<128>("-fmodule-file=/abs/dir/relpath/module.pcm"));
287+
288+
llvm::SmallString<128> module_file_with_name_abs_path(
289+
"-fmodule-file=modulename=/some/dir/module.pcm");
290+
SwiftASTContext::ApplyWorkingDir(module_file_with_name_abs_path,
291+
abs_working_dir);
292+
EXPECT_EQ(
293+
module_file_with_name_abs_path,
294+
llvm::SmallString<128>("-fmodule-file=modulename=/some/dir/module.pcm"));
295+
296+
llvm::SmallString<128> module_file_with_name_rel_path(
297+
"-fmodule-file=modulename=relpath/module.pcm");
298+
SwiftASTContext::ApplyWorkingDir(module_file_with_name_rel_path,
299+
abs_working_dir);
300+
EXPECT_EQ(module_file_with_name_rel_path,
301+
llvm::SmallString<128>(
302+
"-fmodule-file=modulename=/abs/dir/relpath/module.pcm"));
271303
}
272304

273305
namespace {
274-
const std::vector<std::string> duplicated_flags = {
275-
"-DMACRO1", "-D", "MACRO1", "-UMACRO2", "-U", "MACRO2",
276-
"-I/path1", "-I", "/path1", "-F/path2", "-F", "/path2",
277-
"-fmodule-map-file=/path3", "-fmodule-map-file=/path3",
278-
"-F/path2", "-F", "/path2", "-I/path1", "-I", "/path1",
279-
"-UMACRO2", "-U", "MACRO2", "-DMACRO1", "-D", "MACRO1",
280-
};
281-
const std::vector<std::string> uniqued_flags = {
282-
"-DMACRO1", "-UMACRO2", "-I/path1", "-F/path2", "-fmodule-map-file=/path3"
283-
};
306+
const std::vector<std::string> duplicated_flags = {
307+
"-DMACRO1",
308+
"-D",
309+
"MACRO1",
310+
"-UMACRO2",
311+
"-U",
312+
"MACRO2",
313+
"-I/path1",
314+
"-I",
315+
"/path1",
316+
"-F/path2",
317+
"-F",
318+
"/path2",
319+
"-fmodule-map-file=/path3",
320+
"-fmodule-map-file=/path3",
321+
"-F/path2",
322+
"-F",
323+
"/path2",
324+
"-I/path1",
325+
"-I",
326+
"/path1",
327+
"-UMACRO2",
328+
"-U",
329+
"MACRO2",
330+
"-DMACRO1",
331+
"-D",
332+
"MACRO1",
333+
"-fmodule-file=/path/to/pcm",
334+
"-fmodule-file=/path/to/pcm",
335+
"-fmodule-file=modulename=/path/to/pcm",
336+
"-fmodule-file=modulename=/path/to/pcm",
337+
};
338+
const std::vector<std::string> uniqued_flags = {
339+
"-DMACRO1",
340+
"-UMACRO2",
341+
"-I/path1",
342+
"-F/path2",
343+
"-fmodule-map-file=/path3",
344+
"-fmodule-file=/path/to/pcm",
345+
"-fmodule-file=modulename=/path/to/pcm",
346+
};
284347
} // namespace
285348

286349
TEST(ClangArgs, UniquingCollisionWithExistingFlags) {

0 commit comments

Comments
 (0)