Lindenii Project Forge
Login

scdoc

scdoc mirror for performance testing
Commit info
ID
1923fb61cd71182f05d029f280d056b21f66e8a0
Author
Dimitry Andric <dimitry@andric.com>
Author date
Fri, 09 Dec 2022 17:59:32 +0100
Committer
Drew DeVault <sir@cmpwn.com>
Committer date
Mon, 19 Dec 2022 12:01:40 +0100
Actions
Fix clang 15 -Wstrict-prototypes warnings

As of https://github.com/llvm/llvm-project/commit/11da1b53d8c, clang 15
has started warning about functions which take no arguments, but are
declared or defined using "()" instead of "(void)". See also [1].

At first this was even an error by default, but before clang 15 was
released, it was again downgraded to a warning. Since scdoc builds
with both "-std=c99 -pedantic" and "-Wall -Wextra -Werror", this leads
to two compile errors:

    include/str.h:10:23: error: a function declaration without a prototype is deprecated in all versions of C [-Werror,-Wstrict-prototypes]
    struct str *str_create();
                          ^
                           void
    src/string.c:15:23: error: a function declaration without a prototype is deprecated in all versions of C [-Werror,-Wstrict-prototypes]
    struct str *str_create() {
                          ^
                           void

To fix this, use "(void)" for both the declaration and definition of the
str_create function.

[1] https://discourse.llvm.org/t/rfc-enabling-wstrict-prototypes-by-default-in-c/60521
#ifndef _SCDOC_STRING_H
#define _SCDOC_STRING_H
#include <stdint.h>

struct str {
	char *str;
	size_t len, size;
};

struct str *str_create();
struct str *str_create(void);
void str_free(struct str *str);
void str_reset(struct str *str);
int str_append_ch(struct str *str, uint32_t ch);

#endif
#include <stdlib.h>
#include <stdint.h>
#include "str.h"
#include "unicode.h"
#include "util.h"

static void ensure_capacity(struct str *str, size_t len) {
	if (len + 1 >= str->size) {
		char *new = xrealloc(str->str, str->size * 2);
		str->str = new;
		str->size *= 2;
	}
}

struct str *str_create() {
struct str *str_create(void) {
	struct str *str = xcalloc(1, sizeof(struct str));
	str->str = xcalloc(16, 1);
	str->size = 16;
	str->len = 0;
	str->str[0] = '\0';
	return str;
}

void str_free(struct str *str) {
	if (!str) return;
	free(str->str);
	free(str);
}

int str_append_ch(struct str *str, uint32_t ch) {
	int size = utf8_chsize(ch);
	if (size <= 0) {
		return -1;
	}
	ensure_capacity(str, str->len + size);
	utf8_encode(&str->str[str->len], ch);
	str->len += size;
	str->str[str->len] = '\0';
	return size;
}