Skip to content

Commit 825f018

Browse files
chqrliebvdberg
authored andcommitted
C-generator: fix missing statement if asserts are disabled
* output null statement and comment instead of nothing if asserts are disabled * trim some emopty lines
1 parent 9f7fe27 commit 825f018

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

generator/c/c_generator.c2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ fn bool Generator.emitGlobalVarDecl(Generator* gen, string_buffer.Buf* out, Decl
613613
// auto-initialize (only required for embedded targets)
614614
gen.emitAutoInit(out, d.getType());
615615
}
616-
out.add(";\n\n");
616+
out.add(";\n");
617617
}
618618
return false; // put this part in .c file
619619
}

generator/c/c_generator_stmt.c2

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ fn void Generator.emitStmt(Generator* gen, Stmt* s, u32 indent, bool newline) {
105105
if (!out.endsWith('\n')) out.add1(';');
106106
}
107107
}
108-
out.newline();
108+
if (!out.endsWith('\n')) out.newline();
109109
if (is_decl) {
110110
indent--;
111111
out.indent(indent);
@@ -237,22 +237,24 @@ fn void Generator.emitStmt(Generator* gen, Stmt* s, u32 indent, bool newline) {
237237
gen.emitAsmStmt(cast<AsmStmt*>(s), indent);
238238
break;
239239
case Assert:
240-
if (!gen.enable_asserts) break;
241-
240+
if (!gen.enable_asserts) out.print(";//assert");
242241
AssertStmt* a = cast<AssertStmt*>(s);
243-
source_mgr.Location loc = gen.sm.locate(s.getLoc());
244-
const char* funcname = gen.cur_function.asDecl().getFullName();
245-
246242
out.add1('(');
247243
Expr* inner = a.getInner();
248244
gen.emitExpr(out, inner);
249-
out.print(") || c2_assert(\"%s\", %d, \"%s\", \"", loc.filename, loc.line, funcname);
250-
// encode expression as a string
251-
string_buffer.Buf* str = string_buffer.create(128, false, 0);
252-
inner.printLiteral(str);
253-
out.encodeBytes(str.data(), str.size(), '"');
254-
str.free();
255-
out.add("\");\n");
245+
out.add1(')');
246+
if (gen.enable_asserts) {
247+
source_mgr.Location loc = gen.sm.locate(s.getLoc());
248+
const char* funcname = gen.cur_function.asDecl().getFullName();
249+
out.print(" || c2_assert(\"%s\", %d, \"%s\", \"", loc.filename, loc.line, funcname);
250+
// encode expression as a string
251+
string_buffer.Buf* str = string_buffer.create(128, false, 0);
252+
inner.printLiteral(str);
253+
out.encodeBytes(str.data(), str.size(), '"');
254+
str.free();
255+
out.add("\")");
256+
}
257+
out.add(";\n");
256258
break;
257259
}
258260
}

0 commit comments

Comments
 (0)