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

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

merged 1 commit into from
Jul 8, 2021

Conversation

rmaz
Copy link

@rmaz rmaz commented Jun 30, 2021

This change makes it possible to use relative working directories. Prior
to this change the path::append will put a / between the clang_argument
and the cur_working_dir, which will not correctly resolve if
cur_working_dir is relative. This change joins the path components
separately then concatenates with the clang_argument.

@bulbazord
Copy link

@adrian-prantl
Copy link

Would you mind turning ApplyWorkingDir into a static function and adding a unit test to TestSwiftASTContext.cpp, so we can document the expected behavior?

@kastiglione
Copy link

which will not correctly resolve if cur_working_dir is relative

when does this happen?

@rmaz
Copy link
Author

rmaz commented Jun 30, 2021

when does this happen?

At the moment it doesn't, the compiler will always prefix the current directory on a relative working directory to make it absolute. This is the first part of a series of PRs to prefix the serialized debug info in swiftmodule files. This requires treating the working directory as a relative path, as it is generally replaced with either . or SRCROOT or similar.

I'm still verifying the upstream Swift change, but you can see most of it here: rmaz/swift@2c5d792

@rmaz
Copy link
Author

rmaz commented Jul 1, 2021

Would you mind turning ApplyWorkingDir into a static function and adding a unit test to TestSwiftASTContext.cpp, so we can document the expected behavior?

Not quite sure what you mean by static in this context, but I moved the function out of the anonymous namespace and added a unit test for it. Is this what you had in mind?

#ifndef NDEBUG
/// Provided only for unit tests.
template <typename SmallString>
void ApplyWorkingDir(SmallString &clang_argument, llvm::StringRef cur_working_dir);

Choose a reason for hiding this comment

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

c++ question: should this have an explicit instantiation? or does it not matter?

Choose a reason for hiding this comment

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

this was discussed offline, the conclusion has been: explicit instantiation not needed

Choose a reason for hiding this comment

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

This works. I originally had in mind a static member function of SwiftASTContext, but this is equivalent.

Instead of making this a template, I think we can use the base class SmallStringImpl & here.

Choose a reason for hiding this comment

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

Actually, this doesn't work. Now release builds won't be able to build the unit test target. Can we make it a static member of Swift ASTContext instead?

Copy link
Author

Choose a reason for hiding this comment

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

This was based on the existing code further up the header:

https://github.com/apple/llvm-project/blob/swift/main/lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.h#L176-L179

Would you rather the NDEBUG guard is not present? I'll update the function to be a static member fn and remove the template.

Choose a reason for hiding this comment

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

Yes, but the test that depends on this constructor is also guarded by a #ifndef NDEBUG (line 195).
If we make the function a static member of SwiftASTContext, we can leave the tests unguarded. I think that's the cleanest approach.

Copy link
Author

Choose a reason for hiding this comment

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

I don't see a SmallStringImpl base class anywhere though, it appears to inherit directly from SmallVector. Fine with keeping this templated for now?

Choose a reason for hiding this comment

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

You're right, it should have been SmallVectorImpl, not SmallStringImpl. Here's an example API that works like this:
https://www.llvm.org/doxygen/namespacellvm_1_1sys_1_1fs.html#a2363fbfbd0a348f5d81fc3d3223ecae3

If using the underlying SmallVectorImpl is too cumbersome, I would just hardcode the buffer size instead of using a template. But it looks like we are only converting clang_argument to a StringRef and append characters to it.

Copy link
Author

Choose a reason for hiding this comment

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

I see, yes, that works out fine, I've updated it now.

Copy link

@kastiglione kastiglione left a comment

Choose a reason for hiding this comment

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

Looks good to me, but let's see give @adrian-prantl a chance to look at the tests since he asked for them.

@kastiglione kastiglione requested a review from adrian-prantl July 6, 2021 17:30

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

Choose a reason for hiding this comment

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

This looks great, thanks!

This change makes it possible to use relative working directories. Prior
to this change the `path::append` will put a / between the `clang_argument`
and the `cur_working_dir`, which will not correctly resolve if
`cur_working_dir` is relative. This change joins the path components
separately.
Copy link

@adrian-prantl adrian-prantl left a comment

Choose a reason for hiding this comment

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

Thanks! Please don't forget to open a second PR that cherry-picks this patch to swift/next!

@kastiglione
Copy link

@swift-ci test

@kastiglione kastiglione merged commit 9e9c7b2 into swiftlang:swift/main Jul 8, 2021
@kastiglione
Copy link

Thanks Richard. Could you open a matching PR against the swift/next branch too? thanks

@rmaz
Copy link
Author

rmaz commented Jul 8, 2021

Thanks Richard. Could you open a matching PR against the swift/next branch too? thanks

Will do, thanks for merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants