From 2e19434e09b819105055ddc8e58f69db98ec8669 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Sat, 27 May 2017 15:24:25 +0200 Subject: Handle #line directives in C/C++ lexer This way the parser now reports logical rather than physical location in diagnostics. --- unit-tests/cc/lexer/driver.cxx | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) (limited to 'unit-tests/cc/lexer/driver.cxx') diff --git a/unit-tests/cc/lexer/driver.cxx b/unit-tests/cc/lexer/driver.cxx index db3f516..5803a88 100644 --- a/unit-tests/cc/lexer/driver.cxx +++ b/unit-tests/cc/lexer/driver.cxx @@ -16,38 +16,59 @@ namespace build2 { namespace cc { - // Usage: argv[0] [] + // Usage: argv[0] [-l] [] // int main (int argc, char* argv[]) { + bool loc (false); + const char* file (nullptr); + + for (int i (1); i != argc; ++i) + { + string a (argv[i]); + + if (a == "-l") + loc = true; + else + { + file = argv[i]; + break; + } + } + try { istream* is; - const char* in; // Reading from file is several times faster. // ifdstream ifs; - if (argc > 1) + if (file != nullptr) { - in = argv[1]; - ifs.open (in); + ifs.open (file); is = &ifs; } else { - in = "stdin"; + file = "stdin"; cin.exceptions (istream::failbit | istream::badbit); is = &cin; } - lexer l (*is, path (in)); + lexer l (*is, path (file)); // No use printing eos since we will either get it or loop forever. // for (token t; l.next (t) != token_type::eos; ) - cout << t << endl; + { + cout << t; + + if (loc) + cout << ' ' << t.file << ':' << t.line << ':' << t.column; + + cout << endl; + } } catch (const failed&) { -- cgit v1.1