Skip to content

Commit 1a09674

Browse files
committed
Turn null and subscript checks on by default
And add ability to opt out using `-null-checks-`/`-n-` and `-subscript-checks-`/`-s-`
1 parent 9e568b7 commit 1a09674

File tree

3 files changed

+55
-33
lines changed

3 files changed

+55
-33
lines changed

regression-tests/test-results/run-tests.bat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ copy ..\*.cpp2 .
77
set count=0
88
for %%f in (mixed-*.cpp2) do (
99
echo Starting cppfront.exe %%f
10-
C:\GitHub\cppfront\x64\Debug\cppfront.exe -n -s %%f > %%f.output 2>&1
10+
C:\GitHub\cppfront\x64\Debug\cppfront.exe %%f > %%f.output 2>&1
1111
del %%f
1212
set /a count+=1
1313
)
1414
for %%f in (pure2-*.cpp2) do (
1515
echo Starting cppfront.exe %%f -p
16-
C:\GitHub\cppfront\x64\Debug\cppfront.exe -n -s -p %%f > %%f.output 2>&1
16+
C:\GitHub\cppfront\x64\Debug\cppfront.exe -p %%f > %%f.output 2>&1
1717
del %%f
1818
set /a count+=1
1919
)

source/common.h

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,10 @@ class cmdline_processor
288288
callback0 handler0;
289289
callback1 handler1;
290290
std::string synonym;
291+
bool opt_out;
291292

292-
flag(int g, std::string_view n, std::string_view d, callback0 h0, callback1 h1, std::string_view s)
293-
: group{g}, name{n}, description{d}, handler0{h0}, handler1{h1}, synonym{s}
293+
flag(int g, std::string_view n, std::string_view d, callback0 h0, callback1 h1, std::string_view s, bool o)
294+
: group{g}, name{n}, description{d}, handler0{h0}, handler1{h1}, synonym{s}, opt_out{o}
294295
{ }
295296
};
296297
std::vector<flag> flags;
@@ -341,6 +342,10 @@ class cmdline_processor
341342

342343
for (auto& flag : flags) {
343344
auto length_to_match = std::max(flag.unique_prefix, as<int>(arg->text.length())-1);
345+
if (flag.opt_out && arg->text.ends_with("-")) {
346+
length_to_match = std::max(flag.unique_prefix, as<int>(arg->text.length())-2);
347+
}
348+
344349
// Allow a switch to start with either - or /
345350
if (arg->text.starts_with("-" + flag.name.substr(0, length_to_match)) ||
346351
arg->text.starts_with("/" + flag.name.substr(0, length_to_match)) ||
@@ -355,30 +360,28 @@ class cmdline_processor
355360
flag.handler0();
356361
}
357362

358-
// Else this is a switch that takes the next arg as its value, so pass that
363+
// Else
359364
else {
360-
if (arg+1 == args.end()) {
361-
print("Missing argument to option " + arg->text + " (try -help)\n");
362-
help_requested = true;
365+
// If this is a switch that could be suffixed with "-" to opt out
366+
if (flag.opt_out) {
367+
flag.handler1( arg->text.ends_with("-") ? "-" : "" );
368+
}
369+
// Else this is a switch that takes the next arg as its value, so pass that
370+
else {
371+
if (arg+1 == args.end()) {
372+
print("Missing argument to option " + arg->text + " (try -help)\n");
373+
help_requested = true;
374+
}
375+
arg->pos = processed;
376+
++arg; // move to next argument, which is the argument to this switch
377+
flag.handler1(arg->text);
363378
}
364-
arg->pos = processed;
365-
++arg; // move to next argument, which is the argument to this switch
366-
flag.handler1(arg->text);
367379
}
368380

369381
arg->pos = processed;
370382
break;
371383
}
372384
}
373-
374-
// For now comment this out to try leaving unmatched switches alone, so that
375-
// Unix absolute filenames work... and in case an absolute filename collides
376-
// with a legit switch name, also added "--" above... let's see how this works
377-
//if (arg->pos != processed && (arg->text.starts_with("-") || arg->text.starts_with("/"))) {
378-
// arg->pos = processed;
379-
// print("Unknown option: " + arg->text + " (try -help)\n");
380-
// help_requested = true;
381-
//}
382385
}
383386

384387
std::erase_if( args, [=](auto& arg){ return arg.pos == processed; } );
@@ -408,6 +411,9 @@ class cmdline_processor
408411
n += flag.name.substr(flag.unique_prefix);
409412
n += "]";
410413
}
414+
if (flag.opt_out) {
415+
n += "[-]";
416+
}
411417
if (!flag.synonym.empty()) {
412418
n += ", -" + flag.synonym;
413419
}
@@ -417,14 +423,16 @@ class cmdline_processor
417423
}
418424
}
419425

420-
auto add_flag(int group, std::string_view name, std::string_view description, callback0 handler0, callback1 handler1, std::string_view synonym) {
421-
flags.emplace_back( group, name, description, handler0, handler1, synonym );
422-
if (max_flag_length < std::ssize(name)) {
423-
max_flag_length = std::ssize(name);
426+
auto add_flag(int group, std::string_view name, std::string_view description, callback0 handler0, callback1 handler1, std::string_view synonym, bool opt_out) {
427+
flags.emplace_back( group, name, description, handler0, handler1, synonym, opt_out );
428+
auto length = std::ssize(name);
429+
if (opt_out) { length += 3; } // space to print "[-]"
430+
if (max_flag_length < length) {
431+
max_flag_length = length;
424432
}
425433
}
426434
struct register_flag {
427-
register_flag(int group, std::string_view name, std::string_view description, callback0 handler0, callback1 handler1 = nullptr, std::string_view synonym = {});
435+
register_flag(int group, std::string_view name, std::string_view description, callback0 handler0, callback1 handler1 = nullptr, std::string_view synonym = {}, bool opt_out = false);
428436
};
429437

430438
auto set_args(int argc, char* argv[]) -> void {
@@ -457,8 +465,16 @@ class cmdline_processor
457465

458466
} cmdline;
459467

460-
cmdline_processor::register_flag::register_flag(int group, std::string_view name, std::string_view description, callback0 handler0, callback1 handler1, std::string_view synonym) {
461-
cmdline.add_flag( group, name, description, handler0, handler1, synonym );
468+
cmdline_processor::register_flag::register_flag(
469+
int group,
470+
std::string_view name,
471+
std::string_view description,
472+
callback0 handler0,
473+
callback1 handler1,
474+
std::string_view synonym,
475+
bool opt_out
476+
) {
477+
cmdline.add_flag( group, name, description, handler0, handler1, synonym, opt_out );
462478
}
463479

464480
static cmdline_processor::register_flag cmd_help (

source/cppfront.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,26 @@ static cmdline_processor::register_flag cmd_cpp2_only(
7676
[]{ flag_cpp2_only = true; }
7777
);
7878

79-
static auto flag_safe_null_pointers = false;
79+
static auto flag_safe_null_pointers = true;
8080
static cmdline_processor::register_flag cmd_safe_null_pointers(
8181
2,
8282
"null-checks",
83-
"Enable null safety contract checks",
84-
[]{ flag_safe_null_pointers = true; }
83+
"Null safety checks (on by default, - to disable)",
84+
nullptr,
85+
[](std::string const& opt){ flag_safe_null_pointers = opt.empty(); },
86+
{},
87+
true
8588
);
8689

87-
static auto flag_safe_subscripts = false;
90+
static auto flag_safe_subscripts = true;
8891
static cmdline_processor::register_flag cmd_safe_subscripts(
8992
2,
9093
"subscript-checks",
91-
"Enable subscript bounds safety contract checks",
92-
[]{ flag_safe_subscripts = true; }
94+
"Subscript safety checks (on by default, - to disable)",
95+
nullptr,
96+
[](std::string const& opt){ flag_safe_subscripts = opt.empty(); },
97+
{},
98+
true
9399
);
94100

95101
static auto flag_use_source_location = false;

0 commit comments

Comments
 (0)