From 8db8872dbefca142cb01a74bef25a84791fbc949 Mon Sep 17 00:00:00 2001 From: Sei Lisa Date: Thu, 29 Oct 2020 20:58:11 +0100 Subject: [PATCH] Import the changes to cpreproc.py from the preprocessor branch These changes make it ready for prime time, but main.py still does not expose the functionality. Changes include: - Instead of passing paths, we pass parameters, which are parsed to extract the paths (no syspaths, only regular paths) and the defines. - Errors in the preprocessor are reported to the caller (i.e. to main). --- cpreproc.py | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/cpreproc.py b/cpreproc.py index 48c368c..877fd54 100644 --- a/cpreproc.py +++ b/cpreproc.py @@ -577,31 +577,53 @@ class Evaluator(object): return result class Preproc(preprocessor.Preprocessor): - def __init__(self, input, defines=(), sysincpaths=(), incpaths=()): + def __init__(self, input, params=()): super(Preproc, self).__init__() self.auto_pragma_once_enabled = False - for define in defines: - self.define('%s %s' % define) - - for v in sysincpaths: - self.add_path(v) - for v in incpaths: - self.add_path(v) + for v in params: + if v.startswith('-I'): + self.add_path(v[2:]) + elif v.startswith('-D'): + defn = v[2:] + if '=' not in defn: + defn += '=1' + if defn.startswith('='): + self.on_error("\nError: Empty macro name in definition.\n") + return + defn = defn.replace('=', ' ', 1) + self.define(defn) + elif v.startswith('-U'): + defn = v[2:] + if defn in self.macros: + del self.macros[defn] + else: + self.on_error("\nError: Option for the internal" + " preprocessor not -D, -U or -I:\n %s\n" % v) + return self.ignore = set() self.parser = self.parsegen(input, '', '') + self.errors_present = False def get(self): + if self.errors_present: + return True, '', {} + try: import StringIO except ImportError: import io as StringIO ret = StringIO.StringIO() self.write(ret) - return (ret.getvalue(), self.macros) + return (self.errors_present, ret.getvalue(), self.macros) + + def on_error(self, *args, **kwargs): + """Flag that errors are present when called.""" + self.errors_present = True + return super(Preproc, self).on_error(*args, **kwargs) def on_include_not_found(self, is_system_include, curdir, includepath): - """Don't pass through the #include line if the file does not exist""" + """Don't pass through the #include line if the file does not exist.""" self.on_error(self.lastdirective.source, self.lastdirective.lineno, "Include file not found: %s" % includepath)