Expose a function to build headers and log errors
Refactors header generation and error logging into a public function
that can be used by library consumers.
diff --git a/compiler/back_end/cpp/emboss_codegen_cpp.py b/compiler/back_end/cpp/emboss_codegen_cpp.py
index 77bcb84..ed5e66e 100644
--- a/compiler/back_end/cpp/emboss_codegen_cpp.py
+++ b/compiler/back_end/cpp/emboss_codegen_cpp.py
@@ -48,16 +48,30 @@
return parser.parse_args(argv[1:])
-def _show_errors(errors, ir, flags):
+def _show_errors(errors, ir, color_output):
"""Prints errors with source code snippets."""
source_codes = {}
for module in ir.module:
source_codes[module.source_file_name] = module.source_text
- use_color = (flags.color_output == "always" or
- (flags.color_output in ("auto", "if_tty") and
+ use_color = (color_output == "always" or
+ (color_output in ("auto", "if_tty") and
os.isatty(sys.stderr.fileno())))
print(error.format_errors(errors, source_codes, use_color), file=sys.stderr)
+def generate_headers_and_log_errors(ir, color_output):
+ """Generates a C++ header and logs any errors.
+
+ Arguments:
+ ir: EmbossIr of the module.
+ color_output: "always", "never", "if_tty", "auto"
+
+ Returns:
+ A tuple of (header, errors)
+ """
+ header, errors = header_generator.generate_header(ir)
+ if errors:
+ _show_errors(errors, ir, color_output)
+ return (header, errors)
def main(flags):
if flags.input_file:
@@ -65,9 +79,8 @@
ir = ir_pb2.EmbossIr.from_json(f.read())
else:
ir = ir_pb2.EmbossIr.from_json(sys.stdin.read())
- header, errors = header_generator.generate_header(ir)
+ header, errors = generate_headers_and_log_errors(ir, flags.color_output)
if errors:
- _show_errors(errors, ir, flags)
return 1
if flags.output_file:
with open(flags.output_file, "w") as f: