From 7d6622e9f1fd274e7f6c2ff7526e4df269d36b26 Mon Sep 17 00:00:00 2001 From: Siarhei Siamashka Date: Thu, 14 Dec 2023 02:10:01 +0200 Subject: [PATCH] expander.py: trace line numbers back to the original source file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Using the "#line" directives, it's possible to allow compilers to report line numbers from the original source file. At least GCC and Clang support this. It's useful for tracing the compilation error locations back to the original file. Example: #include #include int main() { lazy_segtree seg(arr); } == The current default behaviour: == $ ./expander.py example.cpp $ g++ combined.cpp combined.cpp: In function ‘int main()’: combined.cpp:263:3: error: ‘lazy_segtree’ was not declared in this scope; did you mean ‘atcoder::lazy_segtree’? == With the new '--origname' option: == $ ./expander.py --origname=example.cpp example.cpp $ g++ combined.cpp example.cpp: In function ‘int main()’: example.cpp:4:3: error: ‘lazy_segtree’ was not declared in this scope; did you mean ‘atcoder::lazy_segtree’? --- expander.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/expander.py b/expander.py index ff773da..e4b87f3 100755 --- a/expander.py +++ b/expander.py @@ -63,14 +63,18 @@ def expand_acl(self, acl_file_path: Path) -> List[str]: result.append(line) return result - def expand(self, source: str) -> str: + def expand(self, source: str, origname) -> str: self.included = set() result = [] # type: List[str] + linenum = 0 for line in source.splitlines(): + linenum += 1 m = self.atcoder_include.match(line) if m: acl_path = self.find_acl(m.group(1)) result.extend(self.expand_acl(acl_path)) + if origname: + result.append('#line ' + str(linenum + 1) + ' "' + origname + '"') continue result.append(line) @@ -88,6 +92,8 @@ def expand(self, source: str) -> str: parser.add_argument('-c', '--console', action='store_true', help='Print to Console') parser.add_argument('--lib', help='Path to Atcoder Library') + parser.add_argument('--origname', help='report line numbers from the original ' + + 'source file in GCC/Clang error messages') opts = parser.parse_args() lib_paths = [] @@ -99,7 +105,7 @@ def expand(self, source: str) -> str: lib_paths.append(Path.cwd()) expander = Expander(lib_paths) source = open(opts.source).read() - output = expander.expand(source) + output = expander.expand(source, opts.origname) if opts.console: print(output)