Commit 51783510b9

Matthew McAllister <matthew.mcallister.0@gmail.com>
2019-02-15 00:54:37
Deduplicate compile log statement warnings
1 parent de18ece
src/all_types.hpp
@@ -648,6 +648,7 @@ struct AstNodeFnCallExpr {
     ZigList<AstNode *> params;
     bool is_builtin;
     bool is_async;
+    bool seen; // used by @compileLog
     AstNode *async_allocator;
 };
 
src/ir.cpp
@@ -17194,9 +17194,13 @@ static IrInstruction *ir_analyze_instruction_compile_log(IrAnalyze *ira, IrInstr
     }
     fprintf(stderr, "\n");
 
-    // Here we bypass higher level functions such as ir_add_error because we do not want
-    // invalidate_exec to be called.
-    add_node_error(ira->codegen, instruction->base.source_node, buf_sprintf("found compile log statement"));
+    auto *expr = &instruction->base.source_node->data.fn_call_expr;
+    if (!expr->seen) {
+        // Here we bypass higher level functions such as ir_add_error because we do not want
+        // invalidate_exec to be called.
+        add_node_error(ira->codegen, instruction->base.source_node, buf_sprintf("found compile log statement"));
+    }
+    expr->seen = true;
 
     return ir_const_void(ira, &instruction->base);
 }
src/parser.cpp
@@ -2739,6 +2739,7 @@ static AstNode *ast_parse_async_prefix(ParseContext *pc) {
 
     AstNode *res = ast_create_node(pc, NodeTypeFnCallExpr, async);
     res->data.fn_call_expr.is_async = true;
+    res->data.fn_call_expr.seen = false;
     if (eat_token_if(pc, TokenIdCmpLessThan) != nullptr) {
         AstNode *prefix_expr = ast_expect(pc, ast_parse_prefix_expr);
         expect_token(pc, TokenIdCmpGreaterThan);
@@ -2759,6 +2760,7 @@ static AstNode *ast_parse_fn_call_argumnets(ParseContext *pc) {
 
     AstNode *res = ast_create_node(pc, NodeTypeFnCallExpr, paren);
     res->data.fn_call_expr.params = params;
+    res->data.fn_call_expr.seen = false;
     return res;
 }
 
test/compile_errors.zig
@@ -137,6 +137,20 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
         ".tmp_source.zig:3:15: error: C pointers cannot point to non-C-ABI-compatible type 'Foo'",
     );
 
+    cases.addTest(
+        "compile log statement warning deduplication in generic fn",
+        \\export fn entry() void {
+        \\    inner(1);
+        \\    inner(2);
+        \\}
+        \\fn inner(comptime n: usize) void {
+        \\    comptime var i = 0;
+        \\    inline while (i < n) : (i += 1) { @compileLog("!@#$"); }
+        \\}
+    ,
+        ".tmp_source.zig:7:39: error: found compile log statement",
+    );
+
     cases.addTest(
         "@truncate undefined value",
         \\export fn entry() void {
@@ -4920,7 +4934,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
 
     cases.add(
         "non-printable invalid character",
-        "\xff\xfe" ++            
+        "\xff\xfe" ++
             \\fn test() bool {\r
             \\    true\r
             \\}
@@ -5480,7 +5494,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
         \\    Baz: void,
         \\};
         \\comptime {
-        \\    var foo = Foo {.Baz = {}};    
+        \\    var foo = Foo {.Baz = {}};
         \\    const bar_val = foo.Bar;
         \\}
     ,