From 535d4195907de8a7e494b0480c394634d4c6034a Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 21 Apr 2019 19:46:34 -0400 Subject: [PATCH] translate-c: self-hosted implementation can detect C errors See #1964 --- src-self-hosted/clang.zig | 8 ++++++++ src-self-hosted/stage1.zig | 9 +++++++-- src-self-hosted/translate_c.zig | 12 ++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src-self-hosted/clang.zig b/src-self-hosted/clang.zig index a9cf06f8a538..15e480919ba6 100644 --- a/src-self-hosted/clang.zig +++ b/src-self-hosted/clang.zig @@ -959,3 +959,11 @@ pub const Stage2ErrorMsg = extern struct { offset: c_uint, }; pub extern fn ZigClangErrorMsg_delete(ptr: [*c]Stage2ErrorMsg, len: usize) void; + +pub extern fn ZigClangLoadFromCommandLine( + args_begin: [*]?[*]const u8, + args_end: [*]?[*]const u8, + errors_ptr: *[*]Stage2ErrorMsg, + errors_len: *usize, + resources_path: [*c]const u8, +) ?*ZigClangASTUnit; diff --git a/src-self-hosted/stage1.zig b/src-self-hosted/stage1.zig index 53bc1eeb690a..e28e9c21c9a5 100644 --- a/src-self-hosted/stage1.zig +++ b/src-self-hosted/stage1.zig @@ -88,10 +88,15 @@ export fn stage2_translate_c( out_ast.* = translate_c.translate(args_begin, args_end, switch (mode) { .import => translate_c.Mode.import, .translate => translate_c.Mode.translate, - }, &errors) catch |err| switch (err) { + }, &errors, resources_path) catch |err| switch (err) { error.Unimplemented => return Error.Unimplemented, + error.SemanticAnalyzeFail => { + out_errors_ptr.* = errors.ptr; + out_errors_len.* = errors.len; + return Error.CCompileErrors; + }, + error.OutOfMemory => return Error.OutOfMemory, }; - return Error.None; } diff --git a/src-self-hosted/translate_c.zig b/src-self-hosted/translate_c.zig index b2dcfffeb340..99d7a3c2a011 100644 --- a/src-self-hosted/translate_c.zig +++ b/src-self-hosted/translate_c.zig @@ -17,7 +17,19 @@ pub fn translate( args_end: [*]?[*]const u8, mode: Mode, errors: *[]ClangErrMsg, + resources_path: [*]const u8, ) !*ast.Tree { + const ast_unit = ZigClangLoadFromCommandLine( + args_begin, + args_end, + &errors.ptr, + &errors.len, + resources_path, + ) orelse { + if (errors.len == 0) return error.OutOfMemory; + return error.SemanticAnalyzeFail; + }; + return error.Unimplemented; }