Skip to content

Commit 237ccda

Browse files
authored
[Suggestion] Add a compile time flag to control the line error output format. (#188)
* Add flag to control error output format * Move print_colon_errors behind a cmdline flag.
1 parent f3de421 commit 237ccda

File tree

2 files changed

+34
-18
lines changed

2 files changed

+34
-18
lines changed

source/common.h

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,22 +112,7 @@ struct error
112112
: where{w}, msg{m}, internal{i}
113113
{ }
114114

115-
auto print(auto& o, std::string const& file) const -> void
116-
{
117-
o << file ;
118-
if (where.lineno > 0) {
119-
o << "("<< (where.lineno);
120-
if (where.colno >= 0) {
121-
o << "," << where.colno;
122-
}
123-
o << ")";
124-
}
125-
o << ":";
126-
if (internal) {
127-
o << " internal compiler";
128-
}
129-
o << " error: " << msg << "\n";
130-
}
115+
auto print(auto& o, std::string const& file) const -> void;
131116
};
132117

133118

source/cppfront.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,44 @@ static cmdline_processor::register_flag cmd_cpp1_filename(
115115
[](std::string const& name) { flag_cpp1_filename = name; }
116116
);
117117

118+
static auto flag_print_colon_errors = false;
119+
static cmdline_processor::register_flag cmd_print_colon_errors(
120+
2,
121+
"format-colon-errors",
122+
"Emit ':line:col:' format for error messages",
123+
[]{ flag_print_colon_errors = true; }
124+
);
118125

119126
struct text_with_pos{
120127
std::string text;
121128
source_position pos;
122129
text_with_pos(std::string const& t, source_position p) : text{t}, pos{p} { }
123130
};
124131

132+
// Defined out of line so we can use flag_print_colon_errors.
133+
auto error::print(auto& o, std::string const& file) const -> void {
134+
o << file ;
135+
if (where.lineno > 0) {
136+
if (flag_print_colon_errors) {
137+
o << ":"<< (where.lineno);
138+
if (where.colno >= 0) {
139+
o << ":" << where.colno;
140+
}
141+
}
142+
else {
143+
o << "("<< (where.lineno);
144+
if (where.colno >= 0) {
145+
o << "," << where.colno;
146+
}
147+
o << ")";
148+
}
149+
}
150+
o << ":";
151+
if (internal) {
152+
o << " internal compiler";
153+
}
154+
o << " error: " << msg << "\n";
155+
}
125156

126157
class positional_printer
127158
{
@@ -1856,7 +1887,7 @@ class cppfront
18561887
assert (i->expr_list);
18571888
if (!i->expr_list->expressions.empty()) {
18581889
local_args.text_chunks = print_to_text_chunks(*i->expr_list);
1859-
}
1890+
}
18601891

18611892
flush_args();
18621893
args.emplace(std::move(local_args));
@@ -1958,7 +1989,7 @@ class cppfront
19581989

19591990
if (i->expr_list) {
19601991
auto text = print_to_text_chunks(*i->expr_list);
1961-
for (auto&& e: text) {
1992+
for (auto&& e: text) {
19621993
suffix.push_back(e);
19631994
}
19641995
}

0 commit comments

Comments
 (0)