This is an archive of the discontinued LLVM Phabricator instance.

[llvm] [unittests] Remove some temporary files after they're not needed
ClosedPublic

Authored by broadwaylamb on Jul 6 2020, 7:26 AM.

Details

Summary

Some LLVM unit tests forget to clean up the temporary files and directories.

Use existing RAII classes for cleaning them up, remove duplicated code (ScopedDir, ScopedFile, ScopedLink classes) by extracting it into a common header file.

Diff Detail

Event Timeline

broadwaylamb created this revision.Jul 6 2020, 7:26 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 6 2020, 7:26 AM

Thanks, I think these will be useful helpers to have! I think we do need to consider the API though, rather than being too anchored on the existing callsites.

llvm/utils/unittest/support/llvm-test-helper.h
1 ↗(On Diff #275704)

Missing header guard

1 ↗(On Diff #275704)

Existing test helpers associated with support libraries live in llvm/Testing/Support, in namespace llvm. Can we follow that?

(Whereas the code in this directory is mostly third-party stuff).

17 ↗(On Diff #275704)

While making these more publicly visible, we should be a bit more careful about API:

I think they should mention "temporary" somewhere.
What about TempDir? I think the RAII nature can be clear from the usage and from the documentation.

We should consider hiding Path, avoiding assignment (including accidental - many sys::path functions take a mutable SmallString as inout param).
I think StringRef TempDir::path() is probably clearer than the StringRef operator, which we should consider dropping.
Another option I've had success with is std::string TempDir::path(StringRef rel="") which allows you to construct relative paths fluently.

We should add a doc comment for the class.

46 ↗(On Diff #275704)

this signature doesn't seem ergonomic for cross-platform tests (note that the current users are all #ifdef LLVM_ON_UNIX), because of the "/" vs "\" distinction.
If a pattern like ScopedLink Link(RealPath, Dir.path("foo/bar")) is expected, I'd suggest weakening const Twine& to StringRef. (Use of Twines for paths is IMO a historical mistake that we don't need to propagate further).

It's also not particularly obvious to me whether "To" is the link target or the link being written. Maybe Target/Link?

61 ↗(On Diff #275704)

the overloaded constructor is pretty confusing here: one creates a random filename based on a template and writes an empty file, the other wants the full path to be passed in and sets the contents.

If both are needed, I'd suggest something like:

class ScopedFile {
public:
  ScopedFile(StringRef Path, StringRef Contents="");
  static ScopedFile withUniquePath(StringRef NameModel="%%%%%%%%%%%%");
};

Address review comments, change some tests that didn't use the RAII objects to use them where appropriate.

@sammccall could you please take a look?

sammccall accepted this revision.Aug 31 2020, 5:23 AM

Sorry, this fell off my radar :-(

llvm/include/llvm/Testing/Support/SupportHelpers.h
122

the bool param that changes the meaning of the previous param is a little cryptic (including at callsites).
It also makes the common case of "unique temporary directory that I don't care about the name of

You could consider TempDir(Name) and TempDir::unique(). (Specifying the model doesn't seem all that useful?).

Up to you.

158

to avoid a bit of template magic, you can just accept a path separated by / and call native on it before append. I tend to find this more readable at the callsites that multiple params, up to you.

This revision is now accepted and ready to land.Aug 31 2020, 5:23 AM
broadwaylamb added inline comments.Aug 31 2020, 5:47 AM
llvm/include/llvm/Testing/Support/SupportHelpers.h
122

The name of the unique directory helps identify what went wrong if that directory isn't cleaned up. This is exactly how I've figured out which test cases need to use RAII objects.

For example, if we see that a directory isn't cleaned up, we can tell by its name which test case could crash and cause that directory to not be cleaned up.

158

Good point, I'll do it.

Make the path() member function of TempDir non-template.

This revision was landed with ongoing or failed builds.Sep 1 2020, 2:35 PM
This revision was automatically updated to reflect the committed changes.