This is an archive of the discontinued LLVM Phabricator instance.

[libc][NFC] Extend <ASSERT|EXPECT>_STR* macros to compare with nullptr.
AcceptedPublic

Authored by sivachandra on Aug 11 2020, 3:09 PM.

Details

Summary

In future, pointer comparison might also need to be extended to compare
with nullptr values. That can be taken up when needed.

Diff Detail

Event Timeline

sivachandra created this revision.Aug 11 2020, 3:09 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 11 2020, 3:09 PM
sivachandra requested review of this revision.Aug 11 2020, 3:09 PM
cgyurgyik accepted this revision.Aug 11 2020, 3:18 PM
This revision is now accepted and ready to land.Aug 11 2020, 3:18 PM
abrachet added inline comments.Aug 11 2020, 3:28 PM
libc/utils/UnitTest/Test.cpp
245–246

These should use internal::test(Ctx, Cond_EQ, LHS, RHS, ...) so we get nice error messages and are actually reported as failures.

sivachandra added inline comments.Aug 11 2020, 4:04 PM
libc/utils/UnitTest/Test.cpp
245–246

Huh! I am not sure why this diff got uploaded. I am on a different machine today and probably did arc diff without committing the changes. I used this diff to find tests which would fail but I have a made more changes over it. Will upload the right diff now.

Correct diff (hopefully) with a fix to strstr included.

@cgyurgyik - PTAL. The latest diff looks correct to me (as in, what I intended to upload) and includes a fix to strstr. Without the fix, a strstr test fails with the nullptr change to the tests.

@cgyurgyik - PTAL. The latest diff looks correct to me (as in, what I intended to upload) and includes a fix to strstr. Without the fix, a strstr test fails with the nullptr change to the tests.

strstr change LGTM. Not familiar enough with test suite to make that call so I'll let abrachet LGTM this.

@cgyurgyik - PTAL. The latest diff looks correct to me (as in, what I intended to upload) and includes a fix to strstr. Without the fix, a strstr test fails with the nullptr change to the tests.

strstr change LGTM. Not familiar enough with test suite to make that call so I'll let abrachet LGTM this.

Thanks.

@abrachet, I think we can do something smarter here. For example, I don't like using "<nullptr>". But I am not sure if the complexity is worth it. If required, we can always extend this. Feel free to let me know if you can think of a better approach without too much of moving things around.

cgyurgyik added inline comments.Aug 12 2020, 4:52 AM
libc/utils/UnitTest/Test.cpp
256

So if I'm not mistaken, this will just compare "<nullptr>" with the other string, which will (falsely) return true if the other string is also the string literal "<nullptr>"? Maybe we can put something else a bit longer, such as ___<nullptr>___ ?
Or, just simply check here if the other string is actually the string literal "<nullptr>" to catch that case.

Just blanket suggestions, please let me know if I'm mistaken here.

sivachandra added inline comments.Aug 12 2020, 7:36 AM
libc/utils/UnitTest/Test.cpp
256

No you are not mistaken. And yes, using a "default" string is not perfect. Other things like you suggest (check iff the real string is equal to the actual string) etc can be done but not sure if that is worth it. I would ideally want llvm::StringRef to take of this. But, it is very specifically designed to allow equal comparison of null and nullpointer: https://github.com/llvm/llvm-project/blob/master/llvm/include/llvm/ADT/StringRef.h#L74

The best solution would be to use a class with has std::string_view semantics.

cgyurgyik added inline comments.Aug 12 2020, 8:25 AM
libc/utils/UnitTest/Test.cpp
256

Got it. Thanks for the insight.

sivachandra added inline comments.Aug 12 2020, 8:30 AM
libc/utils/UnitTest/Test.cpp
256

I am wrong: std::string_view cannot handle nullptr values. May the best solution is to implement a standalone equivalent of std::string_view with slightly differing semantics. Not sure if it is all worth it at this time.

abrachet accepted this revision.Aug 12 2020, 8:55 AM
abrachet added inline comments.
libc/utils/UnitTest/Test.cpp
256

What about

struct StrCompare : public llvm::StringRef {
   bool operator==(llvm::StringRef other) {
       if (!data() || !other.data()) return data() == other.data();
       return other == *this;
   }
};

And pass StrCompare(LHS) instead of llvm::StringRef(LHS)

But I think "<nullptr>" works too. We are never going to have the string "<nullptr>" so it's not worth worrying about.