mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-19 16:47:28 +01:00
Add warnings as another status code category
This commit is contained in:
1 parent
54e5ea7f15
commit
9c04249bfe
12 files changed
+67
-45
No files matched your search
@@ -99,10 +99,10 @@ typedef struct {
|
||||
|
||||
// --------
|
||||
|
||||
// A status is either NULL (meaning OK or no error) or string message. That
|
||||
// message is human-readable, for programmers, but it is not for end users. It
|
||||
// is not localized, and does not contain additional contextual information
|
||||
// such as a source filename.
|
||||
// A status is either NULL (meaning OK) or a string message. That message is
|
||||
// human-readable, for programmers, but it is not for end users. It is not
|
||||
// localized, and does not contain additional contextual information such as a
|
||||
// source filename.
|
||||
//
|
||||
// Status strings are statically allocated and should never be free'd. They can
|
||||
// be compared by the == operator and not just by strcmp.
|
||||
@@ -112,7 +112,7 @@ typedef const char* wuffs_base__status;
|
||||
|
||||
static inline bool //
|
||||
wuffs_base__status__is_error(wuffs_base__status z) {
|
||||
return z && (*z != '$');
|
||||
return z && (*z == '?');
|
||||
}
|
||||
|
||||
static inline bool //
|
||||
@@ -125,6 +125,11 @@ wuffs_base__status__is_suspension(wuffs_base__status z) {
|
||||
return z && (*z == '$');
|
||||
}
|
||||
|
||||
static inline bool //
|
||||
wuffs_base__status__is_warning(wuffs_base__status z) {
|
||||
return z && (*z != '$') && (*z != '?');
|
||||
}
|
||||
|
||||
// --------
|
||||
|
||||
// Flicks are a unit of time. One flick (frame-tick) is 1 / 705_600_000 of a
|
||||
|
||||
@@ -116,9 +116,11 @@ func Do(args []string) error {
|
||||
if z == "" {
|
||||
continue
|
||||
}
|
||||
pre := "error"
|
||||
pre := "warning"
|
||||
if z[0] == '$' {
|
||||
pre = "suspension"
|
||||
} else if z[0] == '?' {
|
||||
pre = "error"
|
||||
}
|
||||
b.printf("const char* wuffs_base__%s__%s = \"%sbase: %s\";\n",
|
||||
pre, cName(z, ""), z[:1], z[1:])
|
||||
@@ -185,7 +187,15 @@ type status struct {
|
||||
}
|
||||
|
||||
func statusMsgIsError(msg string) bool {
|
||||
return (len(msg) == 0) || (msg[0] != '$')
|
||||
return (len(msg) != 0) && (msg[0] == '?')
|
||||
}
|
||||
|
||||
func statusMsgIsSuspension(msg string) bool {
|
||||
return (len(msg) != 0) && (msg[0] == '$')
|
||||
}
|
||||
|
||||
func statusMsgIsWarning(msg string) bool {
|
||||
return (len(msg) == 0) || (msg[0] != '$' && msg[0] != '?')
|
||||
}
|
||||
|
||||
type buffer []byte
|
||||
@@ -247,8 +257,10 @@ func insertBasePublicH(buf *buffer) error {
|
||||
if err := expandBangBangInsert(buf, baseBasePublicH, map[string]func(*buffer) error{
|
||||
"// !! INSERT wuffs_base__status names.\n": func(b *buffer) error {
|
||||
for _, z := range builtin.StatusList {
|
||||
pre := "error"
|
||||
if !statusMsgIsError(z) {
|
||||
pre := "warning"
|
||||
if statusMsgIsError(z) {
|
||||
pre = "error"
|
||||
} else if statusMsgIsSuspension(z) {
|
||||
pre = "suspension"
|
||||
}
|
||||
b.printf("extern const char* wuffs_base__%s__%s;\n", pre, cName(z, ""))
|
||||
|
||||
@@ -22,7 +22,7 @@ const baseBasePublicH = "" +
|
||||
"nt.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__WARN_UNUSED_RESULT __attribute__((warn_unused_result))\n#else\n#define WUFFS_BASE__WARN_UNUSED_RESULT\n#endif\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\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" +
|
||||
"" +
|
||||
"// --------\n\n// A status is either NULL (meaning OK or no error) or string message. That\n// message is human-readable, for programmers, but it is not for end users. It\n// is not localized, and does not contain additional contextual information\n// such as a source filename.\n//\n// Status strings are statically allocated and should never be free'd. They can\n// be compared by the == operator and not just by strcmp.\ntypedef const char* wuffs_base__status;\n\n// !! INSERT wuffs_base__status names.\n\nstatic inline bool //\nwuffs_base__status__is_error(wuffs_base__status z) {\n return z && (*z != '$');\n}\n\nstatic inline bool //\nwuffs_base__status__is_ok(wuffs_base__status z) {\n return z == NULL;\n}\n\nstatic inline bool //\nwuffs_base__status__is_suspension(wuffs_base__status z) {\n return z && (*z == '$');\n}\n\n" +
|
||||
"// --------\n\n// A status is either NULL (meaning OK) or a string message. That message is\n// human-readable, for programmers, but it is not for end users. It is not\n// localized, and does not contain additional contextual information such as a\n// source filename.\n//\n// Status strings are statically allocated and should never be free'd. They can\n// be compared by the == operator and not just by strcmp.\ntypedef const char* wuffs_base__status;\n\n// !! INSERT wuffs_base__status names.\n\nstatic inline bool //\nwuffs_base__status__is_error(wuffs_base__status z) {\n return z && (*z == '?');\n}\n\nstatic inline bool //\nwuffs_base__status__is_ok(wuffs_base__status z) {\n return z == NULL;\n}\n\nstatic inline bool //\nwuffs_base__status__is_suspension(wuffs_base__status z) {\n return z && (*z == '$');\n}\n\nstatic inline bool //\nwuffs_base__status__is_warning(wuffs_base__status z) {\n return z && (*z != '$') && (*z != '?');\n}\n\n" +
|
||||
"" +
|
||||
"// --------\n\n// Flicks are a unit of time. One flick (frame-tick) is 1 / 705_600_000 of a\n// second. See https://github.com/OculusVR/Flicks\ntypedef int64_t wuffs_base__flicks;\n\n#define WUFFS_BASE__FLICKS_PER_SECOND ((uint64_t)705600000)\n#define WUFFS_BASE__FLICKS_PER_MILLISECOND ((uint64_t)705600)\n\n" +
|
||||
"" +
|
||||
|
||||
@@ -219,8 +219,10 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, depth u
|
||||
b.writes("wuffs_base__")
|
||||
if statusMsgIsError(msg) {
|
||||
b.writes("error__")
|
||||
} else {
|
||||
} else if statusMsgIsSuspension(msg) {
|
||||
b.writes("suspension__")
|
||||
} else {
|
||||
b.writes("warning__")
|
||||
}
|
||||
b.writes(cName(msg, ""))
|
||||
}
|
||||
|
||||
@@ -315,6 +315,7 @@ func (g *gen) writeStatementRet(b *buffer, n *a.Ret, depth uint32) error {
|
||||
if retExpr.Operator() == t.IDStatus {
|
||||
msg, _ := t.Unescape(retExpr.Ident().Str(g.tm))
|
||||
isError = statusMsgIsError(msg)
|
||||
isOK = statusMsgIsWarning(msg)
|
||||
}
|
||||
// TODO: check that retExpr has no call-suspendibles.
|
||||
if err := g.writeExpr(
|
||||
@@ -335,9 +336,10 @@ func (g *gen) writeStatementRet(b *buffer, n *a.Ret, depth uint32) error {
|
||||
b.writes("goto ok;")
|
||||
} else {
|
||||
g.currFunk.hasGotoOK = true
|
||||
b.writes("if (!status) { goto ok; }" +
|
||||
// TODO: the "goto exit"s can be "goto ok".
|
||||
b.writes("if (wuffs_base__status__is_error(status)) { goto exit; }" +
|
||||
"else if (wuffs_base__status__is_suspension(status)) { " +
|
||||
"status = wuffs_base__error__cannot_return_a_suspension; } goto exit;")
|
||||
"status = wuffs_base__error__cannot_return_a_suspension; goto exit; } goto ok;")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user