Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ int main(int argc, char **argv)
const char *filename = nullptr;
bool use_istream = false;
bool fail_on_error = false;
bool linenrs = false;

// Settings..
simplecpp::DUI dui;
Expand Down Expand Up @@ -74,6 +75,10 @@ int main(int argc, char **argv)
fail_on_error = true;
found = true;
break;
case 'l':
linenrs = true;
found = true;
break;
}
if (!found) {
std::cout << "error: option '" << arg << "' is unknown." << std::endl;
Expand Down Expand Up @@ -107,6 +112,7 @@ int main(int argc, char **argv)
std::cout << " -is Use std::istream interface." << std::endl;
std::cout << " -e Output errors only." << std::endl;
std::cout << " -f Fail when errors were encountered (exitcode 1)." << std::endl;
std::cout << " -l Print lines numbers." << std::endl;
std::exit(0);
}

Expand Down Expand Up @@ -137,7 +143,7 @@ int main(int argc, char **argv)
// Output
if (!quiet) {
if (!error_only)
std::cout << outputTokens.stringify() << std::endl;
std::cout << outputTokens.stringify(linenrs) << std::endl;

for (const simplecpp::Output &output : outputList) {
std::cerr << output.location.file() << ':' << output.location.line << ": ";
Expand Down
15 changes: 12 additions & 3 deletions simplecpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -546,24 +546,33 @@ void simplecpp::TokenList::push_back(Token *tok)
backToken = tok;
}

void simplecpp::TokenList::dump() const
void simplecpp::TokenList::dump(bool linenrs) const
{
std::cout << stringify() << std::endl;
std::cout << stringify(linenrs) << std::endl;
}

std::string simplecpp::TokenList::stringify() const
std::string simplecpp::TokenList::stringify(bool linenrs) const
{
std::ostringstream ret;
Location loc(files);
bool filechg = true;
for (const Token *tok = cfront(); tok; tok = tok->next) {
if (tok->location.line < loc.line || tok->location.fileIndex != loc.fileIndex) {
ret << "\n#line " << tok->location.line << " \"" << tok->location.file() << "\"\n";
loc = tok->location;
filechg = true;
}

if (linenrs && filechg) {
ret << loc.line << ": ";
filechg = false;
}

while (tok->location.line > loc.line) {
ret << '\n';
loc.line++;
if (linenrs)
ret << loc.line << ": ";
}

if (sameline(tok->previous, tok))
Expand Down
4 changes: 2 additions & 2 deletions simplecpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ namespace simplecpp {
}
void push_back(Token *tok);

void dump() const;
std::string stringify() const;
void dump(bool linenrs = false) const;
std::string stringify(bool linenrs = false) const;

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