Skip to content

Commit 76fb5a7

Browse files
chqrliebvdberg
authored andcommitted
debug: use common string_buffer for dump functions
* add `ast.globals.dump_buf` for dump functions * add `ast.getDumpBuf()` and `ast.flushDumpBuf()` for dump functions
1 parent fa2e6f5 commit 76fb5a7

File tree

10 files changed

+43
-56
lines changed

10 files changed

+43
-56
lines changed

ast/decl.c2

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,9 @@ public fn void Decl.setGenerated(Decl* d) { d.declBits.is_generated = 1; }
250250
public fn void Decl.clearGenerated(Decl* d) { d.declBits.is_generated = 0; }
251251

252252
public fn void Decl.dump(const Decl* d) {
253-
string_buffer.Buf* out = string_buffer.create(10*4096, useColor(), 2);
253+
string_buffer.Buf* out = ast.getDumpBuf();
254254
d.print(out, 0);
255-
out.color(col_Normal);
256-
stdio.puts(out.data());
257-
out.free();
255+
ast.flushDumpBuf(out);
258256
}
259257

260258
public fn bool Decl.isTypeDecl(const Decl* d) {

ast/expr.c2

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ module ast;
1717

1818
import src_loc local;
1919
import string_buffer;
20-
import stdio;
2120

2221
public type ExprKind enum u8 {
2322
IntegerLiteral,
@@ -500,11 +499,9 @@ public fn bool Expr.needsSemi(const Expr* e) {
500499
}
501500

502501
public fn void Expr.dump(const Expr* e) {
503-
string_buffer.Buf* out = string_buffer.create(10*4096, useColor(), 2);
502+
string_buffer.Buf* out = ast.getDumpBuf();
504503
e.print(out, 0);
505-
out.color(col_Normal);
506-
stdio.puts(out.data());
507-
out.free();
504+
ast.flushDumpBuf(out);
508505
}
509506

510507
fn void Expr.print(const Expr* e, string_buffer.Buf* out, u32 indent) {

ast/member_expr.c2

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
module ast;
1717

1818
import ast_context;
19-
import string_buffer;
2019
import src_loc local;
21-
import stdio;
20+
import string_buffer;
21+
2222
import string;
2323

2424
public const u32 MemberExprMaxDepth = 7;
@@ -324,7 +324,7 @@ fn void MemberExpr.printLiteral(const MemberExpr* e, string_buffer.Buf* out) {
324324
}
325325

326326
public fn void MemberExpr.dump(const MemberExpr* m) @(unused) {
327-
string_buffer.Buf* out = string_buffer.create(4096, useColor(), 2);
327+
string_buffer.Buf* out = ast.getDumpBuf();
328328
out.color(col_Expr);
329329
out.print("MemberExpr expr %d ref %d/%d\n", m.hasExpr(), m.base.base.memberExprBits.num_decls, m.getNumRefs());
330330
const MemberExprBits* bits = &m.base.base.memberExprBits;
@@ -353,8 +353,6 @@ public fn void MemberExpr.dump(const MemberExpr* m) @(unused) {
353353
}
354354

355355
}
356-
out.color(col_Normal);
357-
stdio.puts(out.data());
358-
out.free();
356+
ast.flushDumpBuf(out);
359357
}
360358

ast/qualtype.c2

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
module ast;
1717

18-
import color;
1918
import string_buffer;
2019
import stdio local;
2120

@@ -422,18 +421,15 @@ public fn const char* QualType.diagNameBare(const QualType* qt) {
422421
}
423422

424423
public fn void QualType.dump(const QualType* qt) @(unused) {
425-
string_buffer.Buf* out = string_buffer.create(512, useColor(), 2);
424+
string_buffer.Buf* out = ast.getDumpBuf();
426425
qt.printInner(out, false, true, false);
427-
out.color(color.Normal);
428-
stdio.puts(out.data());
429-
out.free();
426+
ast.flushDumpBuf(out);
430427
}
431428

432429
public fn void QualType.dump_full(const QualType* qt) {
433-
string_buffer.Buf* out = string_buffer.create(512, useColor(), 1);
430+
string_buffer.Buf* out = ast.getDumpBuf();
434431
qt.fullPrint(out, 0);
435-
stdio.puts(out.data());
436-
out.free();
432+
ast.flushDumpBuf(out);
437433
}
438434

439435
fn void QualType.printQuoted(const QualType* qt, string_buffer.Buf* out) {

ast/stmt.c2

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@
1515

1616
module ast;
1717

18-
import string_buffer;
1918
import src_loc local;
20-
21-
import stdio;
19+
import string_buffer;
2220

2321
public type StmtKind enum u8 {
2422
Return,
@@ -169,11 +167,9 @@ public fn SrcLoc Stmt.getLoc(const Stmt* s) {
169167
}
170168

171169
public fn void Stmt.dump(const Stmt* s) {
172-
string_buffer.Buf* out = string_buffer.create(10*4096, useColor(), 2);
170+
string_buffer.Buf* out = ast.getDumpBuf();
173171
s.print(out, 0);
174-
out.color(col_Normal);
175-
stdio.puts(out.data());
176-
out.free();
172+
ast.flushDumpBuf(out);
177173
}
178174

179175
fn void Stmt.print(const Stmt* s, string_buffer.Buf* out, u32 indent) {

ast/symbol_table.c2

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@
1515

1616
module ast;
1717

18-
import string_buffer;
1918
import color;
19+
import string_buffer;
2020

21-
import stdio local;
2221
import stdlib;
2322
import string;
2423

@@ -144,7 +143,7 @@ public fn void SymbolTable.print(const SymbolTable* t, string_buffer.Buf* out) {
144143
}
145144

146145
public fn void SymbolTable.dump(const SymbolTable* t) @(unused) {
147-
string_buffer.Buf* out = string_buffer.create(4096, useColor(), 2);
146+
string_buffer.Buf* out = ast.getDumpBuf();
148147
u32 count = t.num_public + t.num_private;
149148

150149
out.add("Symbols:\n");
@@ -153,7 +152,6 @@ public fn void SymbolTable.dump(const SymbolTable* t) @(unused) {
153152
const char* name = ast.idx2name(name_idx);
154153
out.print(" [%2d] %6d %s\n", i, name_idx, name);
155154
}
156-
fputs(out.data(), stdout);
157-
out.free();
155+
ast.flushDumpBuf(out);
158156
}
159157

ast/type.c2

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515

1616
module ast;
1717

18-
import color;
1918
import string_buffer;
20-
import stdio;
2119

2220
public type TypeKind enum u8 {
2321
Builtin,
@@ -109,11 +107,10 @@ public fn bool Type.isVoidType(const Type* t) {
109107
}
110108

111109
public fn void Type.dump(const Type* t) @(unused) {
112-
string_buffer.Buf* out = string_buffer.create(256, useColor(), 2);
110+
string_buffer.Buf* out = ast.getDumpBuf();
113111
t.print(out);
114-
out.color(color.Normal);
115-
stdio.printf("%s\n", out.data());
116-
out.free();
112+
out.newline();
113+
ast.flushDumpBuf(out);
117114
}
118115

119116
fn u32 Type.getAlignment(const Type* t) {

ast/type_ref.c2

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -187,18 +187,16 @@ public fn void TypeRefHolder.setPrefix(TypeRefHolder* h, SrcLoc loc, u32 name_id
187187
}
188188

189189
public fn void TypeRefHolder.dump(const TypeRefHolder* h) @(unused) {
190+
string_buffer.Buf* out = ast.getDumpBuf();
190191
const TypeRef* r = (TypeRef*)&h.ref;
191-
string_buffer.Buf* out = string_buffer.create(128, useColor(), 2);
192192
r.print(out, false);
193193
for (u32 i=0; i<r.getNumArrays(); i++) {
194194
out.add1('[');
195195
Expr* size = h.arrays[i];
196196
if (size) h.arrays[i].printLiteral(out);
197197
out.add1(']');
198198
}
199-
out.color(col_Normal);
200-
stdio.puts(out.data());
201-
out.free();
199+
ast.flushDumpBuf(out);
202200
}
203201

204202

@@ -532,16 +530,14 @@ fn void TypeRef.print(const TypeRef* r, string_buffer.Buf* out, bool filled) {
532530

533531
// Note: only use on filled TypeRef
534532
public fn void TypeRef.dump(const TypeRef* r) @(unused) {
535-
string_buffer.Buf* out = string_buffer.create(128, useColor(), 2);
533+
string_buffer.Buf* out = ast.getDumpBuf();
536534
r.print(out, true);
537-
out.color(col_Normal);
538-
stdio.puts(out.data());
539-
out.free();
535+
ast.flushDumpBuf(out);
540536
}
541537

542538
// Note: only use on filled TypeRef
543539
public fn void TypeRef.dump_full(const TypeRef* r) @(unused) {
544-
string_buffer.Buf* out = string_buffer.create(1024, useColor(), 2);
540+
string_buffer.Buf* out = ast.getDumpBuf();
545541
out.add("TypeRef:\n");
546542
out.indent(1);
547543
out.add("flags:");
@@ -560,10 +556,7 @@ public fn void TypeRef.dump_full(const TypeRef* r) @(unused) {
560556
const Ref* ref = &r.refs[i];
561557
out.print("ref[%d] loc %d name_idx %d decl %p\n", i, ref.loc, ref.name_idx, ref.decl);
562558
}
563-
564-
out.color(col_Normal);
565-
stdio.puts(out.data());
566-
out.free();
559+
ast.flushDumpBuf(out);
567560
}
568561

569562
public fn const char* TypeRef.diagName(const TypeRef* r) {

ast/utils.c2

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ module ast;
1818
import attr;
1919
import color;
2020
import ast_context local;
21+
import string_buffer;
2122
import string_pool;
2223

24+
import stdio;
2325
import stdlib;
2426
import string;
2527

@@ -111,6 +113,7 @@ public type Globals struct @(opaque) {
111113

112114
u32[elemsof(attr.AttrKind)] attr_name_indexes;
113115

116+
string_buffer.Buf* dump_buf;
114117
#if AstStatistics
115118
Stats stats;
116119
#endif
@@ -141,6 +144,7 @@ public fn void initialize(Context* c, string_pool.Pool* astPool, u32 wordsize, b
141144
globals.ast_count = 1;
142145
globals.ast_capacity = 0;
143146
globals.ast_list = nil;
147+
globals.dump_buf = string_buffer.create(4096, use_color, 2);
144148
#if AstStatistics
145149
globals.stats.reset();
146150
#endif
@@ -192,13 +196,14 @@ public fn void deinit(bool print_stats) {
192196
globals.string_types.clear();
193197
stdlib.free(globals);
194198
stdlib.free(builtins);
199+
globals.dump_buf.free();
195200
}
196201

197202
public fn u32 getWordSize() {
198203
return globals.wordsize;
199204
}
200205

201-
fn bool useColor() {
206+
public fn bool useColor() @(unused) {
202207
return globals.use_color;
203208
}
204209

@@ -251,6 +256,16 @@ fn AST* idx2ast(u32 idx) {
251256
return globals.ast_list[idx];
252257
}
253258

259+
fn string_buffer.Buf* getDumpBuf() {
260+
return globals.dump_buf;
261+
}
262+
263+
fn void flushDumpBuf(string_buffer.Buf* out) {
264+
out.color(col_Normal);
265+
stdio.puts(out.data());
266+
out.clear();
267+
}
268+
254269
// Note: qt must be valid
255270
public fn void setTypePublicUsed(QualType qt) {
256271
const Type* t = qt.getType();

common/component.c2

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ public fn void Component.print(const Component* c, bool show_funcs) {
192192
for (u32 i=0; i<c.mods.length(); i++) {
193193
mods[i].print(out, show_funcs);
194194
}
195-
196195
out.color(color.Normal);
197196
stdio.puts(out.data());
198197
out.free();

0 commit comments

Comments
 (0)