From b517af6738b5900b10dd5397a918760d6c14f76f Mon Sep 17 00:00:00 2001 From: Matthew Fernandez Date: Tue, 12 Apr 2022 20:48:18 -0700 Subject: [PATCH] remove 'sprint' helpers MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Since these were added a year ago in 08786bbb19bbe0fdf28adcbb305b02f54a237edf, they have only been used once in 6437e1535c7da1cb97fd215131c4bd92c60021fe in code that was subsequently deleted in c2d3526427a7259aa656ab72c83b43232ebfd806. At this point, it seems safe to call “time” on these and judge them as not useful in practice. To further reinforce this, it is worth observing that they were not usable in the CMake build system. 5b620771b7e5f07529ff7a41177ae27048c91865, despite correcting an absence of sprint.h, failed to also correct the absence of sprint.c. Any attempt to use these functions resulted in link failures in the CMake build. This commit effectively reverts 5b620771b7e5f07529ff7a41177ae27048c91865 and 08786bbb19bbe0fdf28adcbb305b02f54a237edf. --- ci/clang_format.py | 2 - lib/cgraph/CMakeLists.txt | 1 - lib/cgraph/Makefile.am | 4 +- lib/cgraph/cgraph.vcxproj | 2 - lib/cgraph/cgraph.vcxproj.filters | 6 -- lib/cgraph/sprint.c | 67 --------------- lib/cgraph/sprint.h | 76 ----------------- lib/cgraph/test_sprint.c | 137 ------------------------------ rtest/test_c_utils.py | 2 +- 9 files changed, 3 insertions(+), 294 deletions(-) delete mode 100644 lib/cgraph/sprint.c delete mode 100644 lib/cgraph/sprint.h delete mode 100644 lib/cgraph/test_sprint.c diff --git a/ci/clang_format.py b/ci/clang_format.py index 9649cc640..e4a3cead1 100644 --- a/ci/clang_format.py +++ b/ci/clang_format.py @@ -287,8 +287,6 @@ EXCLUDE = ( "lib/cgraph/prisize_t.h", "lib/cgraph/rec.c", "lib/cgraph/refstr.c", - "lib/cgraph/sprint.c", - "lib/cgraph/sprint.h", "lib/cgraph/subg.c", "lib/cgraph/utils.c", "lib/cgraph/write.c", diff --git a/lib/cgraph/CMakeLists.txt b/lib/cgraph/CMakeLists.txt index 1380c4784..0287f9259 100644 --- a/lib/cgraph/CMakeLists.txt +++ b/lib/cgraph/CMakeLists.txt @@ -20,7 +20,6 @@ add_library(cgraph SHARED itos.h likely.h prisize_t.h - sprint.h stack.h strcasecmp.h unreachable.h diff --git a/lib/cgraph/Makefile.am b/lib/cgraph/Makefile.am index 9075016ef..8c906b435 100644 --- a/lib/cgraph/Makefile.am +++ b/lib/cgraph/Makefile.am @@ -13,7 +13,7 @@ endif pkginclude_HEADERS = cgraph.h noinst_HEADERS = agxbuf.h alloc.h bitarray.h cghdr.h exit.h itos.h likely.h \ - prisize_t.h sprint.h stack.h strcasecmp.h unreachable.h + prisize_t.h stack.h strcasecmp.h unreachable.h noinst_LTLIBRARIES = libcgraph_C.la lib_LTLIBRARIES = libcgraph.la pkgconfig_DATA = libcgraph.pc @@ -34,7 +34,7 @@ endif libcgraph_C_la_SOURCES = agerror.c agxbuf.c apply.c attr.c edge.c \ flatten.c graph.c grammar.y id.c imap.c io.c mem.c node.c \ - obj.c pend.c rec.c refstr.c scan.l sprint.c subg.c utils.c write.c + obj.c pend.c rec.c refstr.c scan.l subg.c utils.c write.c libcgraph_la_LDFLAGS = -version-info $(CGRAPH_VERSION) -no-undefined libcgraph_la_SOURCES = $(libcgraph_C_la_SOURCES) diff --git a/lib/cgraph/cgraph.vcxproj b/lib/cgraph/cgraph.vcxproj index 272f365d5..ff4902d15 100644 --- a/lib/cgraph/cgraph.vcxproj +++ b/lib/cgraph/cgraph.vcxproj @@ -106,7 +106,6 @@ win_flex -oscan.c scan.l - @@ -130,7 +129,6 @@ win_flex -oscan.c scan.l - diff --git a/lib/cgraph/cgraph.vcxproj.filters b/lib/cgraph/cgraph.vcxproj.filters index c51a3dae1..99c78a77f 100644 --- a/lib/cgraph/cgraph.vcxproj.filters +++ b/lib/cgraph/cgraph.vcxproj.filters @@ -42,9 +42,6 @@ Header Files - - Header Files - Header Files @@ -110,9 +107,6 @@ Source Files - - Source Files - Source Files diff --git a/lib/cgraph/sprint.c b/lib/cgraph/sprint.c deleted file mode 100644 index 316bebd01..000000000 --- a/lib/cgraph/sprint.c +++ /dev/null @@ -1,67 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#ifdef __clang__ -#define NONNULL _Nonnull -#define NULLABLE _Nullable -#else -#define NONNULL /* nothing */ -#define NULLABLE /* nothing */ -#endif - -static char *NULLABLE print(const char *NONNULL format, va_list ap) { - assert(format != NULL); - - va_list ap2; - va_copy(ap2, ap); - - // how many bytes do we need to construct this string? - int r = vsnprintf(NULL, 0, format, ap2); - va_end(ap2); - if (r < 0) { - // bad format string - return NULL; - } - size_t len = (size_t)r + 1 /* for NUL */; - - // allocate space to construct the string - char *s = malloc(len); - if (s == NULL) { - return NULL; - } - - // construct it - (void)vsnprintf(s, len, format, ap); - - return s; -} - -char *NULLABLE gv_sprint(const char *NONNULL format, ...) { - assert(format != NULL); - - va_list ap; - va_start(ap, format); - char *s = print(format, ap); - va_end(ap); - return s; -} - -char *NONNULL gv_sprint_or_exit(const char *NONNULL format, ...) { - assert(format != NULL); - - va_list ap; - va_start(ap, format); - char *s = print(format, ap); - va_end(ap); - - if (s == NULL) { - fprintf(stderr, "gv_sprint failed\n"); - graphviz_exit(EXIT_FAILURE); - } - - return s; -} diff --git a/lib/cgraph/sprint.h b/lib/cgraph/sprint.h deleted file mode 100644 index 2584defa1..000000000 --- a/lib/cgraph/sprint.h +++ /dev/null @@ -1,76 +0,0 @@ -#pragma once - -#ifdef GVDLL -#ifdef EXPORT_CGRAPH -#define SPRINT_API __declspec(dllexport) -#else -#define SPRINT_API __declspec(dllimport) -#endif -#endif - -#ifndef SPRINT_API -#define SPRINT_API /* nothing */ -#endif - -#ifdef __clang__ -#define NONNULL _Nonnull -#define NULLABLE _Nullable -#else -#define NONNULL /* nothing */ -#define NULLABLE /* nothing */ -#endif - -#ifdef __GNUC__ -#define PRINTF_LIKE(fmt, args) __attribute__((format(printf, fmt, args))) -#else -#define PRINTF_LIKE(fmt, args) /* nothing */ -#endif - -#ifdef __GNUC__ -#if defined(__clang__) || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9) -#define RETURNS_NONNULL __attribute__((returns_nonnull)) -#else -#define RETURNS_NONNULL /* nothing */ -#endif -#else -#define RETURNS_NONNULL /* nothing */ -#endif - -#ifdef __GNUC__ -#define WUR __attribute__((warn_unused_result)) -#else -#define WUR -#endif - -/** sprintf-alike - * - * This function constructs a dynamically allocated string based on its - * printf-style arguments and returns a pointer to this string. It is intended - * to be used as a safe alternative to sprintf. - * - * @param format Printf-style format string - * @param ... Format arguments, if any - * @returns A pointer to the constructed string or NULL on failure - */ -SPRINT_API PRINTF_LIKE(1, 2) WUR -char *NULLABLE gv_sprint(const char *NONNULL format, ...); - -/** gv_sprint wrapper, calling exit if NULL is returned - * - * This alternative is provided for callers who have no reasonable way to handle - * a NULL return from gv_sprint. However, it is always preferable to call - * gv_sprint and handle the error gracefully for composability. - * - * @param format Printf-style format string - * @param ... Format arguments, if any - * @returns A pointer to the constructed string - */ -SPRINT_API PRINTF_LIKE(1, 2) RETURNS_NONNULL WUR -char *NONNULL gv_sprint_or_exit(const char *NONNULL format, ...); - -#undef WUR -#undef RETURNS_NONNULL -#undef PRINTF_LIKE -#undef NONNULL -#undef NULLABLE -#undef SPRINT_API diff --git a/lib/cgraph/test_sprint.c b/lib/cgraph/test_sprint.c deleted file mode 100644 index 8ead3bb0e..000000000 --- a/lib/cgraph/test_sprint.c +++ /dev/null @@ -1,137 +0,0 @@ -// basic unit tester for gv_sprint - -#ifdef NDEBUG -#error this is not intended to be compiled with assertions off -#endif - -#include -#include -#include -#include -#include -#include - -// include sprint.c so we can be compiled standalone -#include - -// print "" -static void test_empty_string(void) { -// For some reason, GCC thinks this deserves a compiler warning because -// `printf("")` is a no-op. Apparently our annotations that tell it `gv_sprint` -// is `printf` like lead it to decide it is a little _too_ `printf` like. -#ifdef __GNUC__ -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wformat-zero-length" -#endif - char *s = gv_sprint(""); -#ifdef __GNUC__ -#pragma GCC diagnostic pop -#endif - assert(strcmp(s, "") == 0); - free(s); -} - -// print a basic string -static void test_simple(void) { - char *s = gv_sprint("hello world"); - assert(strcmp(s, "hello world") == 0); - free(s); -} - -// print something large -static void test_large(void) { - static const char text[] = - "Sed ut perspiciatis, unde omnis iste natus error sit voluptatem " - "accusantium doloremque laudantium, totam rem aperiam eaque ipsa, quae " - "ab " - "illo inventore veritatis et quasi architecto beatae vitae dicta sunt, " - "explicabo. Nemo enim ipsam voluptatem, quia voluptas sit, aspernatur " - "aut " - "odit aut fugit, sed quia consequuntur magni dolores eos, qui ratione " - "voluptatem sequi nesciunt, neque porro quisquam est, qui do lorem " - "ipsum, " - "quia dolor sit amet consectetur adipisci[ng] velit, sed quia non " - "numquam " - "[do] eius modi tempora inci[di]dunt, ut labore et dolore magnam aliquam " - "quaerat voluptatem. Ut enim ad minima veniam, quis nostrum[d] " - "exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex " - "ea " - "commodi consequatur? [D]Quis autem vel eum iure reprehenderit, qui in " - "ea " - "voluptate velit esse, quam nihil molestiae consequatur, vel illum, qui " - "dolorem eum fugiat, quo voluptas nulla pariatur? [33] At vero eos et " - "accusamus et iusto odio dignissimos ducimus, qui blanditiis praesentium " - "voluptatum deleniti atque corrupti, quos dolores et quas molestias " - "excepturi sint, obcaecati cupiditate non provident, similique sunt in " - "culpa, qui officia deserunt mollitia animi, id est laborum et dolorum " - "fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam " - "libero tempore, cum soluta nobis est eligendi optio, cumque nihil " - "impedit, quo minus id, quod maxime placeat, facere possimus, omnis " - "voluptas assumenda est, omnis dolor repellendus. Temporibus autem " - "quibusdam et aut officiis debitis aut rerum necessitatibus saepe " - "eveniet, " - "ut et voluptates repudiandae sint et molestiae non recusandae. Itaque " - "earum rerum hic tenetur a sapiente delectus, ut aut reiciendis " - "voluptatibus maiores alias consequatur aut perferendis doloribus " - "asperiores repellat."; - char *s = gv_sprint(text); - assert(strcmp(s, text) == 0); - free(s); -} - -// print some non-ASCII text -static void test_utf8(void) { - static const char text[] = "Ça roule?"; - char *s = gv_sprint(text); - assert(strcmp(s, text) == 0); - free(s); -} - -// print various numbers -static void test_int(void) { - char *s = - gv_sprint("%d is a nice number, more than %" PRId8 ", less than %lu", 7, - (int8_t)INT8_C(-1), 42ul); - assert(strcmp(s, "7 is a nice number, more than -1, less than 42") == 0); - free(s); -} - -// print some floating-point values -static void test_float(void) { - char *s = gv_sprint("%.4f as a double or %.4f as a float, " - "can be truncated to %.3f", - 1.2345, 1.2345f, 1.2345); - assert(strcmp(s, "1.2345 as a double or 1.2345 as a float, " - "can be truncated to 1.234") == 0); - free(s); -} - -// escaping % -static void test_percent(void) { - char *s = gv_sprint("this is a percent: %%."); - assert(strcmp(s, "this is a percent: %.") == 0); - free(s); -} - -int main(void) { - -#define RUN(t) \ - do { \ - printf("running test_%s... ", #t); \ - fflush(stdout); \ - test_##t(); \ - printf("OK\n"); \ - } while (0) - - RUN(empty_string); - RUN(simple); - RUN(large); - RUN(utf8); - RUN(int); - RUN(float); - RUN(percent); - -#undef RUN - - return EXIT_SUCCESS; -} diff --git a/rtest/test_c_utils.py b/rtest/test_c_utils.py index 0fbe1df5a..10740475c 100644 --- a/rtest/test_c_utils.py +++ b/rtest/test_c_utils.py @@ -9,7 +9,7 @@ import pytest sys.path.append(os.path.dirname(__file__)) from gvtest import run_c #pylint: disable=C0413 -@pytest.mark.parametrize("utility", ("bitarray", "sprint", "stack")) +@pytest.mark.parametrize("utility", ("bitarray", "stack")) def test_utility(utility: str): """run the given utility’s unit tests""" -- 2.40.0