Skip to content

Commit f92a7a0

Browse files
dtoh: Hide checkbuf behind a dedicated debug condition (#12661)
* dtoh: Pass temporary buffer instead of C string Avoids a redundant call to toDstring for every included symbol * dtoh: Hide checkbuf behind a dedicated debug condition Some asserts were generated in a non-debug build but discarded at the end because `checkbuf` wasn't included in the final header. Hiding all validation logic behind a dedicated `debug` condition stops this waste of time/memory and and lets one enable those `assert´s without the logging.
1 parent 041ddff commit f92a7a0

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

src/dmd/dtoh.d

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ import dmd.utils;
3030

3131
//debug = Debug_DtoH;
3232

33+
// Generate asserts to validate the header
34+
//debug = Debug_DtoH_Checks;
35+
3336
/**
3437
* Generates a C++ header containing bindings for all `extern(C[++])` declarations
3538
* found in the supplied modules.
@@ -49,7 +52,6 @@ extern(C++) void genCppHdrFiles(ref Modules ms)
4952
initialize();
5053

5154
OutBuffer fwd;
52-
OutBuffer check;
5355
OutBuffer done;
5456
OutBuffer decl;
5557

@@ -58,10 +60,17 @@ extern(C++) void genCppHdrFiles(ref Modules ms)
5860
fwd.spaces = true;
5961
decl.doindent = true;
6062
decl.spaces = true;
61-
check.doindent = true;
62-
check.spaces = true;
6363

64-
scope v = new ToCppBuffer(&check, &fwd, &done, &decl);
64+
scope v = new ToCppBuffer(&fwd, &done, &decl);
65+
66+
// Conditionally include another buffer for sanity checks
67+
debug (Debug_DtoH_Checks)
68+
{
69+
OutBuffer check;
70+
check.doindent = true;
71+
check.spaces = true;
72+
v.checkbuf = ✓
73+
}
6574

6675
OutBuffer buf;
6776
buf.doindent = true;
@@ -137,7 +146,7 @@ struct _d_dynamicArray
137146
// buf.writestringln("// decl:");
138147
buf.write(&decl);
139148

140-
debug (Debug_DtoH)
149+
debug (Debug_DtoH_Checks)
141150
{
142151
// buf.writestringln("// check:");
143152
buf.writestring(`
@@ -212,7 +221,7 @@ public:
212221
OutBuffer* fwdbuf;
213222

214223
/// Buffer for integrity checks
215-
OutBuffer* checkbuf;
224+
debug (Debug_DtoH_Checks) OutBuffer* checkbuf;
216225

217226
/// Buffer for declarations that must emitted before the currently
218227
/// visited node but can't be forward declared (see `includeSymbol`)
@@ -261,9 +270,8 @@ public:
261270
Context context;
262271
alias context this;
263272

264-
this(OutBuffer* checkbuf, OutBuffer* fwdbuf, OutBuffer* donebuf, OutBuffer* buf)
273+
this(OutBuffer* fwdbuf, OutBuffer* donebuf, OutBuffer* buf)
265274
{
266-
this.checkbuf = checkbuf;
267275
this.fwdbuf = fwdbuf;
268276
this.donebuf = donebuf;
269277
this.buf = buf;
@@ -758,6 +766,7 @@ public:
758766
(tdparent && adparent.isClassDeclaration() && !(this.storageClass & AST.STC.final_ || fd.isFinal))))
759767
buf.writestring("virtual ");
760768

769+
debug (Debug_DtoH_Checks)
761770
if (adparent && !tdparent)
762771
{
763772
auto s = adparent.search(Loc.initial, fd.ident);
@@ -973,14 +982,17 @@ public:
973982
if (auto t = vd.type.isTypeStruct())
974983
includeSymbol(t.sym);
975984

976-
checkbuf.level++;
977-
const pn = adparent.ident.toChars();
978-
const vn = vd.ident.toChars();
979-
const vo = vd.offset;
980-
checkbuf.printf("assert(offsetof(%s, %s) == %d);",
981-
pn, vn, vo);
982-
checkbuf.writenl();
983-
checkbuf.level--;
985+
debug (Debug_DtoH_Checks)
986+
{
987+
checkbuf.level++;
988+
const pn = adparent.ident.toChars();
989+
const vn = vd.ident.toChars();
990+
const vo = vd.offset;
991+
checkbuf.printf("assert(offsetof(%s, %s) == %d);",
992+
pn, vn, vo);
993+
checkbuf.writenl();
994+
checkbuf.level--;
995+
}
984996
return;
985997
}
986998

@@ -1352,6 +1364,7 @@ public:
13521364
// Workaround because size triggers a forward-reference error
13531365
// for struct templates (the size is undetermined even if the
13541366
// size doesn't depend on the parameters)
1367+
debug (Debug_DtoH_Checks)
13551368
if (!tdparent)
13561369
{
13571370
checkbuf.level++;
@@ -1408,7 +1421,7 @@ public:
14081421
decl.doindent = true;
14091422
decl.spaces = true;
14101423
visitAsRoot(ds, &decl);
1411-
donebuf.writestring(decl.peekChars());
1424+
donebuf.write(&decl);
14121425
}
14131426

14141427
override void visit(AST.ClassDeclaration cd)

0 commit comments

Comments
 (0)