Skip to content

Commit

Permalink
Revert using __builtin_dump_struct as a fallback to print object de…
Browse files Browse the repository at this point in the history
…scription

This call is unsafe and cannot be made safe unfortunately.
  • Loading branch information
gershnik committed Jan 21, 2024
1 parent e03b687 commit e0d7d95
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 25 deletions.
3 changes: 0 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## Unreleased

### Changed
- `XCTestUtil.h`: default output for printing C++ objects changed to use [`__builtin_dump_struct`](https://clang.llvm.org/docs/LanguageExtensions.html#builtin-dump-struct) clang intrinsic.

## [3.0] - 2024-01-17

### Changed
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ That, in the case of failure, try to obtain description using the following meth
- If there is an ADL call `testDescription(obj)` that produces `NSString *`, use that.
- Otherwise, if there is an ADL call `to_string(obj)` in `using std::to_string` scope, use that
- Otherwise, if it is possible to do `ostream << obj`, use that
- Finally produce use the output of [`__builtin_dump_struct`](https://clang.llvm.org/docs/LanguageExtensions.html#builtin-dump-struct) clang intrinsic.
- Finally produce `"<full name of the type> object"` string.
Thus if an object is printable using the typical means those will be automatically used. You can also make your own objects printable using either of the means above. The `testDescription` approach specifically exists to allow you to print something different for tests than in normal code.
Expand Down
30 changes: 10 additions & 20 deletions include/objc-helpers/XCTestUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
#import <XCTest/XCTest.h>

#include <sstream>
#include <string>
#include <stdarg.h>
#include <cxxabi.h>

namespace TestUtil {
using std::to_string;
Expand All @@ -41,21 +40,14 @@ namespace TestUtil {
{ str << obj };
};

auto sprintf(std::string & res, const char * format, ...) -> int {
va_list vl;
va_start(vl, format);
const int size = vsnprintf(0, 0, format, vl);
va_end(vl);
if (size <= 0)
return size;
size_t appendPos = res.size();
size_t appendSize = size_t(size) + 1;
res.resize(appendPos + appendSize);
va_start(vl, format);
const int ret = vsnprintf(&res[appendPos], appendSize, format, vl);
va_end(vl);
res.resize(appendPos + size_t(ret > 0 ? ret : 0));
return ret;
inline auto demangle(const char * name) -> std::string {

int status = 0;
std::unique_ptr<char, void(*)(void*)> res {
abi::__cxa_demangle(name, nullptr, nullptr, &status),
std::free
};
return (status==0) ? res.get() : name ;
}

template<class T>
Expand All @@ -72,9 +64,7 @@ namespace TestUtil {
str << val;
return @(str.str().c_str());
} else {
std::string res;
__builtin_dump_struct(&val, sprintf, res);
return @(res.c_str());
return [NSString stringWithFormat:@"%s object", demangle(typeid(T).name()).c_str()];
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/XCTestUtilTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ void _XCTFailureHandler(XCTestCase * _Nullable test, BOOL expected, const char *
TEST_CASE( "Generic" ) {
failures.clear();
XCTAssertCppEqual(bar{1}, bar{2});
CHECK_FAILURE(__LINE__ - 1, true, "((bar{1}) equal to (bar{2})) failed: (\"(anonymous namespace)::bar {\n int i = 1\n}\n\") is not equal to (\"(anonymous namespace)::bar {\n int i = 2\n}\n\")", "");
CHECK_FAILURE(__LINE__ - 1, true, "((bar{1}) equal to (bar{2})) failed: (\"(anonymous namespace)::bar object\") is not equal to (\"(anonymous namespace)::bar object\")", "");
}

namespace {
Expand Down

0 comments on commit e0d7d95

Please sign in to comment.