From d8b5128c09cf04bd98baa5001bc69fb5247c2a3b Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 7 Apr 2019 16:16:14 +1000 Subject: [PATCH] Compile cleanly with clang++ --- build-all.sh | 4 ++-- internal/cgen/base/core-public.h | 6 ++++++ internal/cgen/cgen.go | 2 +- internal/cgen/data.go | 7 ++++--- release/c/wuffs-unsupported-snapshot.c | 20 +++++++++++++------- 5 files changed, 26 insertions(+), 13 deletions(-) diff --git a/build-all.sh b/build-all.sh index 2a0acee1..15499874 100755 --- a/build-all.sh +++ b/build-all.sh @@ -60,10 +60,10 @@ $CC -c -DWUFFS_IMPLEMENTATION \ echo "Checking snapshot compiles cleanly (as C++)" $CXX -c \ -Wall -Werror -Wpedantic -Wconversion -std=c++11 \ - release/c/wuffs-unsupported-snapshot.c -o /dev/null + -x c++ release/c/wuffs-unsupported-snapshot.c -o /dev/null $CXX -c -DWUFFS_IMPLEMENTATION \ -Wall -Werror -Wpedantic -Wconversion -std=c++11 \ - release/c/wuffs-unsupported-snapshot.c -o /dev/null + -x c++ release/c/wuffs-unsupported-snapshot.c -o /dev/null wuffs genlib -skipgen wuffs test -skipgen -mimic diff --git a/internal/cgen/base/core-public.h b/internal/cgen/base/core-public.h index 8c686c4d..dc7e7fb6 100644 --- a/internal/cgen/base/core-public.h +++ b/internal/cgen/base/core-public.h @@ -57,6 +57,12 @@ #define WUFFS_BASE__MAYBE_STATIC #endif +#if defined(__clang__) +#define WUFFS_BASE__POTENTIALLY_UNUSED_FIELD __attribute__((unused)) +#else +#define WUFFS_BASE__POTENTIALLY_UNUSED_FIELD +#endif + // Clang also defines "__GNUC__". #if defined(__GNUC__) #define WUFFS_BASE__POTENTIALLY_UNUSED __attribute__((unused)) diff --git a/internal/cgen/cgen.go b/internal/cgen/cgen.go index 65ec5ac0..52f9b3cb 100644 --- a/internal/cgen/cgen.go +++ b/internal/cgen/cgen.go @@ -873,7 +873,7 @@ func (g *gen) writeStruct(b *buffer, n *a.Struct) error { b.writes("union {\n") b.writes("uint32_t align_as_per_magic_field;\n") b.writes("uint8_t placeholder[1073741824]; // 1 GiB.\n") - b.writes("} private_impl;\n\n") + b.writes("} private_impl WUFFS_BASE__POTENTIALLY_UNUSED_FIELD;\n\n") b.writes("public:\n") b.writex(wiEnd) diff --git a/internal/cgen/data.go b/internal/cgen/data.go index 5d222b57..7e445929 100644 --- a/internal/cgen/data.go +++ b/internal/cgen/data.go @@ -60,9 +60,10 @@ const baseCorePrivateH = "" + const baseCorePublicH = "" + "// Wuffs assumes that:\n// - converting a uint32_t to a size_t will never overflow.\n// - converting a size_t to a uint64_t will never overflow.\n#ifdef __WORDSIZE\n#if (__WORDSIZE != 32) && (__WORDSIZE != 64)\n#error \"Wuffs requires a word size of either 32 or 64 bits\"\n#endif\n#endif\n\n// WUFFS_VERSION is the major.minor.patch version, as per https://semver.org/,\n// as a uint64_t. The major number is the high 32 bits. The minor number is the\n// middle 16 bits. The patch number is the low 16 bits. The pre-release label\n// and build metadata are part of the string representation (such as\n// \"1.2.3-beta+456.20181231\") but not the uint64_t representation.\n//\n// WUFFS_VERSION_PRE_RELEASE_LABEL (such as \"\", \"beta\" or \"rc.1\") being\n// non-empty denotes a developer preview, not a release version, and has no\n// backwards or forwards compatibility guarantees.\n//\n// WUFFS_VERSION_BUILD_METADATA_XXX, if non-zero, are the number of commits and\n// the last commit date in the repository used to build this library. Within\n// eac" + - "h major.minor branch, the commit count should increase monotonically.\n//\n// !! Some code generation programs can override WUFFS_VERSION.\n#define WUFFS_VERSION ((uint64_t)0)\n#define WUFFS_VERSION_MAJOR ((uint64_t)0)\n#define WUFFS_VERSION_MINOR ((uint64_t)0)\n#define WUFFS_VERSION_PATCH ((uint64_t)0)\n#define WUFFS_VERSION_PRE_RELEASE_LABEL \"work.in.progress\"\n#define WUFFS_VERSION_BUILD_METADATA_COMMIT_COUNT 0\n#define WUFFS_VERSION_BUILD_METADATA_COMMIT_DATE 0\n#define WUFFS_VERSION_STRING \"0.0.0+0.00000000\"\n\n// Define WUFFS_CONFIG__STATIC_FUNCTIONS to make all of Wuffs' functions have\n// static storage. The motivation is discussed in the \"ALLOW STATIC\n// IMPLEMENTATION\" section of\n// https://raw.githubusercontent.com/nothings/stb/master/docs/stb_howto.txt\n#ifdef WUFFS_CONFIG__STATIC_FUNCTIONS\n#define WUFFS_BASE__MAYBE_STATIC static\n#else\n#define WUFFS_BASE__MAYBE_STATIC\n#endif\n\n// Clang also defines \"__GNUC__\".\n#if defined(__GNUC__)\n#define WUFFS_BASE__POTENTIALLY_UNUSED __attribute__((unused))\n#define WUFFS_BASE" + - "__WARN_UNUSED_RESULT __attribute__((warn_unused_result))\n#else\n#define WUFFS_BASE__POTENTIALLY_UNUSED\n#define WUFFS_BASE__WARN_UNUSED_RESULT\n#endif\n\n// Flags for wuffs_foo__bar__initialize functions.\n\n#define WUFFS_INITIALIZE__DEFAULT_OPTIONS ((uint32_t)0x00000000)\n\n// WUFFS_INITIALIZE__ALREADY_ZEROED means that the \"self\" receiver struct value\n// has already been set to all zeroes.\n#define WUFFS_INITIALIZE__ALREADY_ZEROED ((uint32_t)0x00000001)\n\n// WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED means that, absent\n// WUFFS_INITIALIZE__ALREADY_ZEROED, only some of the \"self\" receiver struct\n// value will be set to all zeroes. Internal buffers, which tend to be a large\n// proportion of the struct's size, will be left uninitialized. Internal means\n// that the buffer is contained by the receiver struct, as opposed to being\n// passed as a separately allocated \"work buffer\".\n//\n// With or without this bit set, the Wuffs compiler still enforces that no\n// reads or writes will overflow internal buffers' bound" + - "s. Even with this bit\n// set, the Wuffs standard library also considers reading from an uninitialized\n// buffer to be a bug, and strives to never do so, but unlike buffer overflows,\n// it is not a bug class that the Wuffs compiler eliminates.\n//\n// For those paranoid about security, leave this bit unset, so that\n// wuffs_foo__bar__initialize will initialize the entire struct value to zeroes\n// (unless WUFFS_INITIALIZE__ALREADY_ZEROED is set).\n//\n// Setting this bit gives a small absolute improvement on micro-benchmarks, but\n// this can be a large relative effect, up to 2x faster, when the actual work\n// to be done is also small, such as decompressing small input. See git commit\n// 438fc105 \"Move some struct fields to private_data\" for some numbers and a\n// discussion, noting that its commit message was written before this\n// WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED option was defined.\n#define WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED \\\n ((uint32_t)0x00000002)\n\n" + + "h major.minor branch, the commit count should increase monotonically.\n//\n// !! Some code generation programs can override WUFFS_VERSION.\n#define WUFFS_VERSION ((uint64_t)0)\n#define WUFFS_VERSION_MAJOR ((uint64_t)0)\n#define WUFFS_VERSION_MINOR ((uint64_t)0)\n#define WUFFS_VERSION_PATCH ((uint64_t)0)\n#define WUFFS_VERSION_PRE_RELEASE_LABEL \"work.in.progress\"\n#define WUFFS_VERSION_BUILD_METADATA_COMMIT_COUNT 0\n#define WUFFS_VERSION_BUILD_METADATA_COMMIT_DATE 0\n#define WUFFS_VERSION_STRING \"0.0.0+0.00000000\"\n\n// Define WUFFS_CONFIG__STATIC_FUNCTIONS to make all of Wuffs' functions have\n// static storage. The motivation is discussed in the \"ALLOW STATIC\n// IMPLEMENTATION\" section of\n// https://raw.githubusercontent.com/nothings/stb/master/docs/stb_howto.txt\n#ifdef WUFFS_CONFIG__STATIC_FUNCTIONS\n#define WUFFS_BASE__MAYBE_STATIC static\n#else\n#define WUFFS_BASE__MAYBE_STATIC\n#endif\n\n#if defined(__clang__)\n#define WUFFS_BASE__POTENTIALLY_UNUSED_FIELD __attribute__((unused))\n#else\n#define WUFFS_BASE__POTENTIALLY_UNUSED_" + + "FIELD\n#endif\n\n// Clang also defines \"__GNUC__\".\n#if defined(__GNUC__)\n#define WUFFS_BASE__POTENTIALLY_UNUSED __attribute__((unused))\n#define WUFFS_BASE__WARN_UNUSED_RESULT __attribute__((warn_unused_result))\n#else\n#define WUFFS_BASE__POTENTIALLY_UNUSED\n#define WUFFS_BASE__WARN_UNUSED_RESULT\n#endif\n\n// Flags for wuffs_foo__bar__initialize functions.\n\n#define WUFFS_INITIALIZE__DEFAULT_OPTIONS ((uint32_t)0x00000000)\n\n// WUFFS_INITIALIZE__ALREADY_ZEROED means that the \"self\" receiver struct value\n// has already been set to all zeroes.\n#define WUFFS_INITIALIZE__ALREADY_ZEROED ((uint32_t)0x00000001)\n\n// WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED means that, absent\n// WUFFS_INITIALIZE__ALREADY_ZEROED, only some of the \"self\" receiver struct\n// value will be set to all zeroes. Internal buffers, which tend to be a large\n// proportion of the struct's size, will be left uninitialized. Internal means\n// that the buffer is contained by the receiver struct, as opposed to being\n// passed as a separately allocate" + + "d \"work buffer\".\n//\n// With or without this bit set, the Wuffs compiler still enforces that no\n// reads or writes will overflow internal buffers' bounds. Even with this bit\n// set, the Wuffs standard library also considers reading from an uninitialized\n// buffer to be a bug, and strives to never do so, but unlike buffer overflows,\n// it is not a bug class that the Wuffs compiler eliminates.\n//\n// For those paranoid about security, leave this bit unset, so that\n// wuffs_foo__bar__initialize will initialize the entire struct value to zeroes\n// (unless WUFFS_INITIALIZE__ALREADY_ZEROED is set).\n//\n// Setting this bit gives a small absolute improvement on micro-benchmarks, but\n// this can be a large relative effect, up to 2x faster, when the actual work\n// to be done is also small, such as decompressing small input. See git commit\n// 438fc105 \"Move some struct fields to private_data\" for some numbers and a\n// discussion, noting that its commit message was written before this\n// WUFFS_INITIALIZE__LEAVE_INTERNAL_BUF" + + "FERS_UNINITIALIZED option was defined.\n#define WUFFS_INITIALIZE__LEAVE_INTERNAL_BUFFERS_UNINITIALIZED \\\n ((uint32_t)0x00000002)\n\n" + "" + "// --------\n\n// wuffs_base__empty_struct is used when a Wuffs function returns an empty\n// struct. In C, if a function f returns void, you can't say \"x = f()\", but in\n// Wuffs, if a function g returns empty, you can say \"y = g()\".\ntypedef struct {\n // private_impl is a placeholder field. It isn't explicitly used, except that\n // without it, the sizeof a struct with no fields can differ across C/C++\n // compilers, and it is undefined behavior in C99. For example, gcc says that\n // the sizeof an empty struct is 0, and g++ says that it is 1. This leads to\n // ABI incompatibility if a Wuffs .c file is processed by one compiler and\n // its .h file with another compiler.\n //\n // Instead, we explicitly insert an otherwise unused field, so that the\n // sizeof this struct is always 1.\n uint8_t private_impl;\n} wuffs_base__empty_struct;\n\nstatic inline wuffs_base__empty_struct //\nwuffs_base__make_empty_struct() {\n wuffs_base__empty_struct ret;\n ret.private_impl = 0;\n return ret;\n}\n\n// wuffs_base__utility is" + " a placeholder receiver type. It enables what Java\n// calls static methods, as opposed to regular methods.\ntypedef struct {\n // private_impl is a placeholder field. It isn't explicitly used, except that\n // without it, the sizeof a struct with no fields can differ across C/C++\n // compilers, and it is undefined behavior in C99. For example, gcc says that\n // the sizeof an empty struct is 0, and g++ says that it is 1. This leads to\n // ABI incompatibility if a Wuffs .c file is processed by one compiler and\n // its .h file with another compiler.\n //\n // Instead, we explicitly insert an otherwise unused field, so that the\n // sizeof this struct is always 1.\n uint8_t private_impl;\n} wuffs_base__utility;\n\n" + diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 92dba51f..d5035717 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -79,6 +79,12 @@ extern "C" { #define WUFFS_BASE__MAYBE_STATIC #endif +#if defined(__clang__) +#define WUFFS_BASE__POTENTIALLY_UNUSED_FIELD __attribute__((unused)) +#else +#define WUFFS_BASE__POTENTIALLY_UNUSED_FIELD +#endif + // Clang also defines "__GNUC__". #if defined(__GNUC__) #define WUFFS_BASE__POTENTIALLY_UNUSED __attribute__((unused)) @@ -2683,7 +2689,7 @@ struct wuffs_adler32__hasher__struct { union { uint32_t align_as_per_magic_field; uint8_t placeholder[1073741824]; // 1 GiB. - } private_impl; + } private_impl WUFFS_BASE__POTENTIALLY_UNUSED_FIELD; public: @@ -2799,7 +2805,7 @@ struct wuffs_crc32__ieee_hasher__struct { union { uint32_t align_as_per_magic_field; uint8_t placeholder[1073741824]; // 1 GiB. - } private_impl; + } private_impl WUFFS_BASE__POTENTIALLY_UNUSED_FIELD; public: @@ -2991,7 +2997,7 @@ struct wuffs_deflate__decoder__struct { union { uint32_t align_as_per_magic_field; uint8_t placeholder[1073741824]; // 1 GiB. - } private_impl; + } private_impl WUFFS_BASE__POTENTIALLY_UNUSED_FIELD; public: @@ -3156,7 +3162,7 @@ struct wuffs_lzw__decoder__struct { union { uint32_t align_as_per_magic_field; uint8_t placeholder[1073741824]; // 1 GiB. - } private_impl; + } private_impl WUFFS_BASE__POTENTIALLY_UNUSED_FIELD; public: @@ -3422,7 +3428,7 @@ struct wuffs_gif__decoder__struct { union { uint32_t align_as_per_magic_field; uint8_t placeholder[1073741824]; // 1 GiB. - } private_impl; + } private_impl WUFFS_BASE__POTENTIALLY_UNUSED_FIELD; public: @@ -3617,7 +3623,7 @@ struct wuffs_gzip__decoder__struct { union { uint32_t align_as_per_magic_field; uint8_t placeholder[1073741824]; // 1 GiB. - } private_impl; + } private_impl WUFFS_BASE__POTENTIALLY_UNUSED_FIELD; public: @@ -3775,7 +3781,7 @@ struct wuffs_zlib__decoder__struct { union { uint32_t align_as_per_magic_field; uint8_t placeholder[1073741824]; // 1 GiB. - } private_impl; + } private_impl WUFFS_BASE__POTENTIALLY_UNUSED_FIELD; public: