[libc-commits] [PATCH] D85790: [libc][NFC] Extend <ASSERT|EXPECT>_STR* macros to compare with nullptr.

Siva Chandra via Phabricator via libc-commits libc-commits at lists.llvm.org
Wed Aug 12 08:30:04 PDT 2020

sivachandra added inline comments.

Comment at: libc/utils/UnitTest/Test.cpp:256
                      unsigned long Line) {
+  if (LHS == nullptr)
+    LHS = "<nullptr>";
sivachandra wrote:
> cgyurgyik wrote:
> > 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.
> 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.
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.

  rG LLVM Github Monorepo



More information about the libc-commits mailing list