Skip to content

Commit 7f24374

Browse files
committed
fixed #462 - added CLI option -l to print line numbers
1 parent ad24c6e commit 7f24374

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

main.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ int main(int argc, char **argv)
1818
const char *filename = nullptr;
1919
bool use_istream = false;
2020
bool fail_on_error = false;
21+
bool linenrs = false;
2122

2223
// Settings..
2324
simplecpp::DUI dui;
@@ -74,6 +75,10 @@ int main(int argc, char **argv)
7475
fail_on_error = true;
7576
found = true;
7677
break;
78+
case 'l':
79+
linenrs = true;
80+
found = true;
81+
break;
7782
}
7883
if (!found) {
7984
std::cout << "error: option '" << arg << "' is unknown." << std::endl;
@@ -107,6 +112,7 @@ int main(int argc, char **argv)
107112
std::cout << " -is Use std::istream interface." << std::endl;
108113
std::cout << " -e Output errors only." << std::endl;
109114
std::cout << " -f Fail when errors were encountered (exitcode 1)." << std::endl;
115+
std::cout << " -l Print lines numbers." << std::endl;
110116
std::exit(0);
111117
}
112118

@@ -137,7 +143,7 @@ int main(int argc, char **argv)
137143
// Output
138144
if (!quiet) {
139145
if (!error_only)
140-
std::cout << outputTokens.stringify() << std::endl;
146+
std::cout << outputTokens.stringify(linenrs) << std::endl;
141147

142148
for (const simplecpp::Output &output : outputList) {
143149
std::cerr << output.location.file() << ':' << output.location.line << ": ";

simplecpp.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -546,24 +546,33 @@ void simplecpp::TokenList::push_back(Token *tok)
546546
backToken = tok;
547547
}
548548

549-
void simplecpp::TokenList::dump() const
549+
void simplecpp::TokenList::dump(bool linenrs) const
550550
{
551-
std::cout << stringify() << std::endl;
551+
std::cout << stringify(linenrs) << std::endl;
552552
}
553553

554-
std::string simplecpp::TokenList::stringify() const
554+
std::string simplecpp::TokenList::stringify(bool linenrs) const
555555
{
556556
std::ostringstream ret;
557557
Location loc(files);
558+
bool filechg = true;
558559
for (const Token *tok = cfront(); tok; tok = tok->next) {
559560
if (tok->location.line < loc.line || tok->location.fileIndex != loc.fileIndex) {
560561
ret << "\n#line " << tok->location.line << " \"" << tok->location.file() << "\"\n";
561562
loc = tok->location;
563+
filechg = true;
564+
}
565+
566+
if (linenrs && filechg) {
567+
ret << loc.line << ": ";
568+
filechg = false;
562569
}
563570

564571
while (tok->location.line > loc.line) {
565572
ret << '\n';
566573
loc.line++;
574+
if (linenrs)
575+
ret << loc.line << ": ";
567576
}
568577

569578
if (sameline(tok->previous, tok))

simplecpp.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ namespace simplecpp {
288288
}
289289
void push_back(Token *tok);
290290

291-
void dump() const;
292-
std::string stringify() const;
291+
void dump(bool linenrs = false) const;
292+
std::string stringify(bool linenrs = false) const;
293293

294294
void readfile(Stream &stream, const std::string &filename=std::string(), OutputList *outputList = nullptr);
295295
void constFold();

0 commit comments

Comments
 (0)