From 302e24671942051d70707586cf8c605a5815edac Mon Sep 17 00:00:00 2001 From: lemon Date: Mon, 15 Dec 2025 22:39:52 +0100 Subject: create distinct interned string type Interned strings are used pervasively, so it's a good idea to add a layer of type safety to differentiate them from general cstrs and avoid potential bugs from comparing non-interned and interned strings. Not that that's happened so far that I can remember, but it could. I'm 90% sure it's legal to alias `struct {char c;}` pointers with `char` pointers. This specific typedef gives type safety but with a simple one-way `internstr -> const char *` typecast (with `&istr->c`). Converting the other way around is more intentional: a straight up cast `(internstr)cstr` which sticks out as unchecked and probably wrong, or calling the intern(cstr) function, which is the right way. --- io.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'io.c') diff --git a/io.c b/io.c index 1661e12..8d9af5c 100644 --- a/io.c +++ b/io.c @@ -226,7 +226,7 @@ pritypebefore(struct wbuf *buf, union type ty, int qual) case TYSTRUCT: s = "struct"; Tagged: - n = bfmt(buf, "%s %s", s, (s2 = ttypenames[typedata[ty.dat].id]) ? s2 : "(anonymous)"); + n = bfmt(buf, "%s %s", s, (s2 = &ttypenames[typedata[ty.dat].id]->c) ? s2 : "(anonymous)"); return n + priquals(buf, qual); case TYUNION: s = "union"; @@ -530,7 +530,7 @@ vbfmt(struct wbuf *out, const char *fmt, va_list ap) case TKPPMACARG: case TKIDENT: if (quote) n += bputc(buf, '`'); - n += bfmt(buf, "%s", tok->s); + n += bfmt(buf, "%s", tok->name); if (quote) n += bputc(buf, '\''); break; case TKEOF: @@ -562,7 +562,7 @@ vbfmt(struct wbuf *out, const char *fmt, va_list ap) default: if (quote) n += bputc(buf, '`'); if (in_range(tok->t, TKWBEGIN_, TKWEND_)) { - iowrite(buf, tok->s, tok->len); + iowrite(buf, tok->name, tok->len); n += tok->len; } else if (aisprint(tok->t)) { n += bputc(buf, tok->t); @@ -809,7 +809,7 @@ static struct file { vec_of(struct linemap) linemap; bool once; bool seen; - const char *guardmac; + internstr guardmac; } *fileht[1<guardmac; @@ -979,7 +979,7 @@ isoncefile(int id, const char **guard) } void -markfileonce(int id, const char *guard) +markfileonce(int id, internstr guard) { assert(id < countof(fileht) && fileht[id]); fileht[id]->once = 1; -- cgit v1.2.3