Skip to content

Commit ba4a518

Browse files
committed
Revise STDC defines, support -std=iso9899:
1 parent aee4a24 commit ba4a518

File tree

5 files changed

+36
-8
lines changed

5 files changed

+36
-8
lines changed

codegen.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ static bool in_imm_range(int64_t val) {
333333
}
334334

335335
static bool export_fn(Obj *fn) {
336-
if (fn->is_gnu_inline || opt_std == STD_C89 || opt_gnu89_inline)
336+
if (fn->is_gnu_inline || opt_gnu89_inline || opt_std < STD_C99)
337337
return fn->export_fn_gnu;
338338
return fn->export_fn;
339339
}

main.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,26 @@ static void set_std(bool is_iso, const char *arg) {
210210
error("unknown c standard");
211211
}
212212

213+
static void set_std_iso(const char *arg) {
214+
char *end;
215+
int val = strtoul(arg, &end, 10);
216+
217+
if (end != arg) {
218+
is_iso_std = true;
219+
220+
switch (val) {
221+
case 1990: opt_std = STD_C89; return;
222+
case 199409: opt_std = STD_C94; return;
223+
case 1999: opt_std = STD_C99; return;
224+
case 2011: opt_std = STD_C11; return;
225+
case 2017:
226+
case 2018: opt_std = STD_C17; return;
227+
case 2024: opt_std = STD_C23; return;
228+
}
229+
}
230+
error("unknown c standard");
231+
}
232+
213233
void set_fpic(const char *lvl) {
214234
opt_fpic = true;
215235
opt_fpie = false;
@@ -246,11 +266,18 @@ static void build_macros(MacroChangeArr *arr, bool is_asm_pp) {
246266
define_macro("__STRICT_ANSI__", "1");
247267

248268
switch (opt_std) {
269+
case STD_C94: define_macro("__STDC_VERSION__", "199409L"); break;
249270
case STD_C99: define_macro("__STDC_VERSION__", "199901L"); break;
250271
case STD_C11: define_macro("__STDC_VERSION__", "201112L"); break;
251272
case STD_C17: define_macro("__STDC_VERSION__", "201710L"); break;
252273
case STD_C23: define_macro("__STDC_VERSION__", "202311L"); break;
253274
}
275+
276+
if (opt_std >= STD_C99)
277+
define_macro("__GNUC_STDC_INLINE__", "1");
278+
279+
define_macro("__STDC_UTF_16__", "1");
280+
define_macro("__STDC_UTF_32__", "1");
254281
}
255282

256283
for (int i = 0; i < arr->len; i++) {
@@ -594,6 +621,8 @@ static void parse_args(int argc, char **argv, bool *run_ld, bool *no_fork) {
594621
set_std(true, arg);
595622
else if (startswith(arg, &arg, "gnu"))
596623
set_std(false, arg);
624+
else if (startswith(arg, &arg, "iso9899:"))
625+
set_std_iso(arg);
597626
else
598627
error("unknown c standard");
599628
continue;

parse.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ static Scope *base_scope(void) {
313313

314314
static Scope *decl_scope(void) {
315315
Scope *sc = scope;
316-
while (sc->is_temporary || (sc->is_stmt && opt_std == STD_C89))
316+
while (sc->is_temporary || (sc->is_stmt && opt_std < STD_C99))
317317
sc = sc->parent;
318318
return sc;
319319
}
@@ -1259,7 +1259,7 @@ static Type *declspec(Token **rest, Token *tok, VarAttr *attr, StorageClass ctx)
12591259
error_tok(tok, "invalid storage class");
12601260

12611261
if (!ty) {
1262-
if (opt_std == STD_C89) {
1262+
if (opt_std < STD_C99) {
12631263
ty = ty_int;
12641264
} else if (opt_std >= STD_C23 && (attr->strg & SC_AUTO)) {
12651265
ty = new_type(TY_AUTO, -1, 0);
@@ -5529,7 +5529,7 @@ static Node *primary(Token **rest, Token *tok) {
55295529
if (node)
55305530
return node;
55315531

5532-
if (opt_std == STD_C89) {
5532+
if (opt_std < STD_C99) {
55335533
Type *ty = func_type(ty_int, tok);
55345534
ty->is_oldstyle = true;
55355535
return new_var_node(func_prototype2(ty, &(VarAttr){0}, tok), tok);

preprocess.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,11 +1773,10 @@ void init_macros(void) {
17731773
define_macro("__STDC_EMBED_FOUND__", "1");
17741774
define_macro("__STDC_EMBED_NOT_FOUND__", "0");
17751775
define_macro("__STDC_HOSTED__", "1");
1776-
define_macro("__STDC_NO_COMPLEX__", "1");
1777-
define_macro("__STDC_UTF_16__", "1");
1778-
define_macro("__STDC_UTF_32__", "1");
17791776
define_macro("__STDC__", "1");
17801777

1778+
define_macro("__STDC_NO_COMPLEX__", "1");
1779+
17811780
define_macro("__STDC_DEFER_TS25755__", "1");
17821781

17831782
define_macro("__C99_MACRO_WITH_VA_ARGS", "1");

slimcc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ void run_linker(StringArray *paths, StringArray *inputs, const char *output);
912912
// main.c
913913
//
914914

915-
typedef enum { STD_C89, STD_C99, STD_C11, STD_C17, STD_C23 } StdVer;
915+
typedef enum { STD_C89, STD_C94, STD_C99, STD_C11, STD_C17, STD_C23 } StdVer;
916916

917917
void cleanup_exit(int status) NORETURN;
918918
bool file_exists(const char *path);

0 commit comments

Comments
 (0)