Skip to content

Commit b8e4841

Browse files
committed
source-mgr: remove static buffer used in source_mgr.loc2str()
* pass destination buffer instead of using `local` buffer
1 parent 600f43c commit b8e4841

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

common/source_mgr.c2

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,12 @@ public fn Location SourceMgr.locate(SourceMgr* sm, SrcLoc loc) {
277277
return l;
278278
}
279279

280-
public fn const char* SourceMgr.loc2str(SourceMgr* sm, SrcLoc sloc) {
281-
local char[256] tmp;
280+
public fn char* SourceMgr.loc2str(SourceMgr* sm, SrcLoc sloc, char* tmp, usize tmp_size) {
282281
Location loc = sm.locate(sloc);
283-
tmp[0] = '-';
284-
tmp[1] = '\0';
285282
if (loc.line_start) {
286-
stdio.snprintf(tmp, elemsof(tmp), "%s:%d:%d", loc.filename, loc.line, loc.column);
283+
stdio.snprintf(tmp, tmp_size, "%s:%d:%d", loc.filename, loc.line, loc.column);
284+
} else {
285+
stdio.snprintf(tmp, tmp_size, "-");
287286
}
288287
return tmp;
289288
}

compiler/c2recipe_parser.c2

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,21 +195,26 @@ fn void Parser.error(Parser* p, const char* format @(printf_format), ...) @(nore
195195
char[128] msg;
196196
va_list args;
197197
va_start(args, format);
198-
vsnprintf(msg, sizeof(msg)-1, format, args);
198+
vsnprintf(msg, sizeof(msg), format, args);
199199
va_end(args);
200200

201+
char[256] locstr;
202+
p.sm.loc2str(p.token.loc, locstr, elemsof(locstr));
203+
201204
if (color.useColor()) {
202-
fprintf(stderr, "%s: %serror:%s %s\n", p.sm.loc2str(p.token.loc), color.Red, color.Normal, msg);
205+
fprintf(stderr, "%s: %serror:%s %s\n", locstr, color.Red, color.Normal, msg);
203206
} else {
204-
fprintf(stderr, "%s: error: %s\n", p.sm.loc2str(p.token.loc), msg);
207+
fprintf(stderr, "%s: error: %s\n", locstr, msg);
205208
}
206209
longjmp(&p.jmpbuf, 1);
207210
}
208211

209212
fn void Parser.consumeToken(Parser* p) {
210213
p.lex(&p.token);
211214
#if 0
212-
printf(" %s %s", p.sm.loc2str(p.token.loc), kind_names[p.token.kind]);
215+
char[256] locstr;
216+
p.sm.loc2str(p.token.loc, locstr, elemsof(locstr));
217+
printf(" %s %s", locstr, kind_names[p.token.kind]);
213218
switch (p.token.kind) {
214219
case PluginOptions:
215220
case Text:

parser/c2_parser.c2

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,9 @@ fn void Parser.dump_token(Parser* p, const Token* tok) @(unused) {
798798
out.print("%12s", tok.kind.str());
799799
if (tok.kind.isKeyword())
800800
out.color(color.Normal);
801-
out.print(" %6d %s ", tok.loc, p.sm.loc2str(tok.loc));
801+
char[256] locstr;
802+
p.sm.loc2str(tok.loc, locstr, elemsof(locstr));
803+
out.print(" %6d %s ", tok.loc, locstr);
802804

803805
out.color(color.Cyan);
804806
switch (tok.kind) {

0 commit comments

Comments
 (0)