Skip to content

Commit 7d6622e

Browse files
committed
expander.py: trace line numbers back to the original source file
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 <iostream> #include <atcoder/lazysegtree> int main() { lazy_segtree<S, op, e, F, mapping, composition, id> 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’?
1 parent d8ca7f2 commit 7d6622e

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

expander.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,18 @@ def expand_acl(self, acl_file_path: Path) -> List[str]:
6363
result.append(line)
6464
return result
6565

66-
def expand(self, source: str) -> str:
66+
def expand(self, source: str, origname) -> str:
6767
self.included = set()
6868
result = [] # type: List[str]
69+
linenum = 0
6970
for line in source.splitlines():
71+
linenum += 1
7072
m = self.atcoder_include.match(line)
7173
if m:
7274
acl_path = self.find_acl(m.group(1))
7375
result.extend(self.expand_acl(acl_path))
76+
if origname:
77+
result.append('#line ' + str(linenum + 1) + ' "' + origname + '"')
7478
continue
7579

7680
result.append(line)
@@ -88,6 +92,8 @@ def expand(self, source: str) -> str:
8892
parser.add_argument('-c', '--console',
8993
action='store_true', help='Print to Console')
9094
parser.add_argument('--lib', help='Path to Atcoder Library')
95+
parser.add_argument('--origname', help='report line numbers from the original ' +
96+
'source file in GCC/Clang error messages')
9197
opts = parser.parse_args()
9298

9399
lib_paths = []
@@ -99,7 +105,7 @@ def expand(self, source: str) -> str:
99105
lib_paths.append(Path.cwd())
100106
expander = Expander(lib_paths)
101107
source = open(opts.source).read()
102-
output = expander.expand(source)
108+
output = expander.expand(source, opts.origname)
103109

104110
if opts.console:
105111
print(output)

0 commit comments

Comments
 (0)