This is an archive of the discontinued LLVM Phabricator instance.

[Darwin] Add tests for OS version checking
ClosedPublic

Authored by yln on May 28 2020, 2:35 PM.

Details

Summary
  • Extract ParseVersion helper function for testing

Diff Detail

Event Timeline

yln created this revision.May 28 2020, 2:35 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 28 2020, 2:35 PM
Herald added subscribers: Restricted Project, mgorny. · View Herald Transcript
yln marked 2 inline comments as done.May 28 2020, 2:38 PM
yln added inline comments.
compiler-rt/lib/sanitizer_common/tests/sanitizer_mac_test.cpp
17

I include the implementation file here so that I can directly test the static ParseVersion helper function. Is this okay?

46

This essentially re-implements what we have already. Happy to follow any pointers on how to improve this.

delcypher requested changes to this revision.Jun 1 2020, 12:50 PM
delcypher added inline comments.
compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
632

Does your use of references here pass the sanitizer lint check? IIRC this is disallowed by the linter.

compiler-rt/lib/sanitizer_common/tests/sanitizer_mac_test.cpp
17

I don't think this is a good idea. Sanitizer functions aren't usually tested this way. IIRC the unit tests get linked against the sanitizer library object files so you should be able to call the function you need from the linked object file.

Can't you just remove the static on __sanitizer::ParseVersion(...) and call it directly? You might need to add a proto-type to lib/sanitizer_common/sanitizer_mac.h or in sanitizer_mac_test.cpp.

This revision now requires changes to proceed.Jun 1 2020, 12:50 PM
yln updated this revision to Diff 267759.Jun 1 2020, 4:18 PM
yln marked 2 inline comments as done.

Make helper non-static and forward declare it in test instead of including implementation file.

yln marked an inline comment as done.Jun 1 2020, 4:28 PM
yln added inline comments.
compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
632

ninja SanitizerLintCheck comes back good for. Is there a pattern that it rejects so I can counter-check if it's running properly?

Just out of curiosity: the linter is executed when compiling sanitizer tests (e.g., check-sanitizer, check-asan), right?! I usually don't run it explicitly.

compiler-rt/lib/sanitizer_common/tests/sanitizer_mac_test.cpp
17

The reason this was that I tried to avoid making this helper (implementation detail) non-static just for the purpose of testing. I will remove static and forward declare in the test file.

delcypher requested changes to this revision.Jun 2 2020, 7:49 PM
delcypher added inline comments.
compiler-rt/lib/sanitizer_common/tests/sanitizer_mac_test.cpp
23

Test's aren't normally declared inside the __sanitizer namespace.

Maybe do something like this?

// Prototypes
namespace __sanitizer {
  void ParseVersion(const char *vers, u16 &major, u16 &minor);
}

using namespace __sanitizer;

TEST(SanitizerMac, GetMacosAlignedVersion) {
  ...
}
45

Nit: I think it's okay to use C++ in the unit tests so you could use std::ostringstream to avoid using snprintf for simpler code.

This revision now requires changes to proceed.Jun 2 2020, 7:49 PM
delcypher added inline comments.Jun 2 2020, 8:01 PM
compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
632

IIRC I hit this from the old google C++ style guide once

Parameters are either input to the function, output from the function, or both. Input parameters are usually values or const references, while output and input/output parameters will be pointers to non-const.

https://drake.mit.edu/styleguide/cppguide.html#Output_Parameters

which I think the linter script tries to enforce (at least partially).

If you're passing SanitizerLintCheck then you're probably fine.

If you want to check if it's working IIRC the linter doesn't like FIXME: something and instead wants something like FIXME(yln): something.

yln updated this revision to Diff 268265.Jun 3 2020, 11:32 AM

Use std::ostringstream instead of snprintf in test code.

yln marked 6 inline comments as done.Jun 3 2020, 11:37 AM
yln added inline comments.
compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp
632

Hmm, I can't get it to trigger at all. Investigating...

compiler-rt/lib/sanitizer_common/tests/sanitizer_mac_test.cpp
23

The other tests in this directory (e.g., sanitizer_linux_test.cpp, sanitizer_symbolizer_test.cpp, etc.) follow the same pattern, so I would prefer to leave this as is.

45

Dreaming of std::format() ;)

yln marked 3 inline comments as done.Jun 3 2020, 11:45 AM
delcypher accepted this revision.Jun 3 2020, 12:21 PM

LGTM but please to make sure the linter is working correctly on your system before you land this.

compiler-rt/lib/sanitizer_common/tests/sanitizer_mac_test.cpp
23

Yeah some do, some don't. E.g. sanitizer_bvgraph_test.cpp and sanitizer_bitvector_test.cpp follow the pattern I suggest and others follow what you've done in this patch. Yay for inconsistency :(

I'll let you decide which you want to follow.

This revision is now accepted and ready to land.Jun 3 2020, 12:21 PM
This revision was automatically updated to reflect the committed changes.