From 79db8917a7d6722f0a64d4fa912efa5e96a1a2c3 Mon Sep 17 00:00:00 2001 From: lemon Date: Thu, 11 Dec 2025 16:25:31 +0100 Subject: c: use a look-up table for isdecltok() --- c/c.c | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) (limited to 'c/c.c') diff --git a/c/c.c b/c/c.c index 1cdb68e..d5e4d9e 100644 --- a/c/c.c +++ b/c/c.c @@ -124,22 +124,26 @@ static struct decl *finddecl(struct comp *cm, const char *name); static bool isdecltok(struct comp *cm) { - struct decl *decl; struct token tk; - switch (peek(cm, &tk)) { - case TKWsigned: case TKWunsigned: case TKWshort: case TKWlong: - case TKWint: case TKWchar: case TKW_Bool: case TKWauto: - case TKWstruct: case TKWunion: case TKWenum: case TKWtypedef: - case TKWextern: case TKWstatic: case TKWinline: case TKW_Noreturn: - case TKW_Thread_local: case TKWthread_local: - case TKWconst: case TKWvolatile: case TKWvoid: case TKWfloat: - case TKWdouble: case TKWregister: case TKW_Static_assert: - case TKW__typeof__: case TKWtypeof: case TKWtypeof_unqual: - return 1; - case TKIDENT: - return (decl = finddecl(cm, tk.s)) && decl->scls == SCTYPEDEF; + if (peek(cm, &tk) == TKIDENT) { + struct decl *decl = finddecl(cm, tk.s); + return decl && decl->scls == SCTYPEDEF; + } else { + static const char kws[] = { +#define kw(x) [TKW##x-TKWBEGIN_] = 1 + kw(auto), kw(extern), kw(static), kw(register), kw(typedef), + kw(_Thread_local), kw(thread_local), kw(_Static_assert), + kw(inline), kw(_Noreturn), + kw(const), kw(volatile), kw(restrict), kw(_Atomic), + kw(void), kw(float), kw(double), + kw(signed), kw(unsigned), kw(short), kw(long), + kw(int), kw(char), kw(_Bool), kw(bool), + kw(struct), kw(union), kw(enum), + kw(__typeof__), kw(typeof), kw(typeof_unqual), +#undef _ + }; + return ((uint)tk.t-TKWBEGIN_) < arraylength(kws) && kws[tk.t-TKWBEGIN_]; } - return 0; } -- cgit v1.2.3