Skip to content

Commit

Permalink
Fix type printing with GCC.
Browse files Browse the repository at this point in the history
FUTURE_COPYBARA_INTEGRATE_REVIEW=#1448 from aleksisch:unify_gcc_clang_output 27612be
PiperOrigin-RevId: 695538025
  • Loading branch information
aleksisch authored and copybara-github committed Nov 12, 2024
1 parent 4aa4462 commit 03cb763
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 18 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/cmake_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ jobs:
-D CMAKE_BUILD_TYPE=RelWithDebug \
-D FUZZTEST_BUILD_TESTING=on \
&& cmake --build build_gcc -j $(nproc)
# TODO: Rewrite helper `PrintValue` to obtain same output under clang and gcc
# && ctest --test-dir build_gcc -j $(nproc) --output-on-failure
&& ctest --test-dir build_gcc -j $(nproc) --output-on-failure
- name: Run end-to-end tests in fuzzing mode
if: matrix.mode == 'fuzzing'
run: |
Expand Down
15 changes: 15 additions & 0 deletions e2e_tests/functional_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,9 @@ void ExpectStackLimitExceededMessage(absl::string_view std_err,
}

TEST_F(UnitTestModeTest, StackLimitWorks) {
#if defined(__GNUC__)
GTEST_SKIP() << "No coverage instrumentation for GCC yet: skipping tests.";
#endif
#if defined(__has_feature)
#if !__has_feature(coverage_sanitizer)
GTEST_SKIP() << "No coverage instrumentation: skipping the stack limit test "
Expand Down Expand Up @@ -676,6 +679,10 @@ class FuzzingModeCommandLineInterfaceTest
protected:
void SetUp() override {
GenericCommandLineInterfaceTest::SetUp();
#if defined(__GNUC__)
GTEST_SKIP() << "No coverage instrumentation for GNU compiler yet: "
"skipping tests in fuzzing mode.";
#endif
#if defined(__has_feature)
#if !__has_feature(coverage_sanitizer)
GTEST_SKIP() << "No coverage instrumentation: skipping the fuzzing mode "
Expand Down Expand Up @@ -1211,6 +1218,10 @@ class FuzzingModeFixtureTest
: public ::testing::TestWithParam<ExecutionModelParam> {
protected:
void SetUp() override {
#if defined(__GNUC__)
GTEST_SKIP() << "No coverage instrumentation for GNU compiler yet: "
"skipping tests in fuzzing mode.";
#endif
#if defined(__has_feature)
#if !__has_feature(coverage_sanitizer)
GTEST_SKIP() << "No coverage instrumentation: skipping the fuzzing mode "
Expand Down Expand Up @@ -1346,6 +1357,10 @@ class FuzzingModeCrashFindingTest
: public ::testing::TestWithParam<ExecutionModelParam> {
protected:
void SetUp() override {
#if defined(__GNUC__)
GTEST_SKIP() << "No coverage instrumentation for GNU compiler yet: "
"skipping tests in fuzzing mode.";
#endif
#if defined(__has_feature)
#if !__has_feature(coverage_sanitizer)
GTEST_SKIP() << "No coverage instrumentation: skipping the fuzzing mode "
Expand Down
4 changes: 2 additions & 2 deletions fuzztest/domain_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ class DomainBuilder {
iter.second != nullptr && iter.second->has_value(),
"Some domain is not set yet!");
}
return OwningDomain<T>(GetIndirect<T>(name)->template GetAs<Domain<T>>(),
std::move(domain_lookup_table_));
auto domain = GetIndirect<T>(name)->template GetAs<Domain<T>>();
return OwningDomain<T>(std::move(domain), std::move(domain_lookup_table_));
}

private:
Expand Down
38 changes: 24 additions & 14 deletions fuzztest/internal/type_support.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,31 +56,41 @@ constexpr bool HasKnownPrinter();

// If `needle` is present in `haystack`, consume everything until `needle` and
// return true. Otherwise, return false.
inline bool ConsumePrefixUntil(absl::string_view& haystack,
absl::string_view needle) {
constexpr bool ConsumePrefixUntil(absl::string_view& haystack,
absl::string_view needle) {
size_t pos = haystack.find(needle);
if (pos == haystack.npos) return false;
haystack.remove_prefix(pos + needle.size());
return true;
}

inline void SkipAnonymous(absl::string_view& in) {
while (ConsumePrefixUntil(in, "(anonymous namespace)::")) {
constexpr bool ConsumeAnonymousPrefix(absl::string_view& name) {
constexpr absl::string_view prefixes[] = {
"{anonymous}::", "(anonymous namespace)::", "<unnamed>::", "()::"};
for (absl::string_view p : prefixes) {
if (ConsumePrefixUntil(name, p)) return true;
}
return false;
}

template <typename T>
absl::string_view GetTypeName() {
#if defined(__clang__)
// Format "std::string_view GetTypeName() [T = int]"
absl::string_view v = __PRETTY_FUNCTION__;
ConsumePrefixUntil(v, "[T = ");
SkipAnonymous(v);
absl::ConsumeSuffix(&v, "]");
return v;
constexpr auto GetTypeName() {
absl::string_view name, prefix, suffix;
#ifdef __clang__
name = __PRETTY_FUNCTION__;
prefix = "GetTypeName() [T = ";
suffix = "]";
#elif defined(__GNUC__)
name = __PRETTY_FUNCTION__;
prefix = "GetTypeName() [with T = ";
suffix = "]";
#else
return "<TYPE>";
return "<TYPE>"
#endif
ConsumePrefixUntil(name, prefix);
ConsumeAnonymousPrefix(name);
absl::ConsumeSuffix(&name, suffix);
return name;
}

template <typename T>
Expand Down Expand Up @@ -371,7 +381,7 @@ std::string GetFunctionName(const F& f, absl::string_view default_name) {
absl::string_view v = buffer;
absl::ConsumeSuffix(&v, "()");
ConsumeFileAndLineNumber(v);
SkipAnonymous(v);
ConsumeAnonymousPrefix(v);
return std::string(v);
}
}
Expand Down

0 comments on commit 03cb763

Please sign in to comment.