Skip to content

Improves main function #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
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
41 changes: 38 additions & 3 deletions include/cpp2util.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@
#include <cstddef>
#include <utility>
#include <cstdio>
#include <vector>
#include <string_view>
#include <span>

#if defined(CPP2_USE_SOURCE_LOCATION)
#include <source_location>
Expand Down Expand Up @@ -297,7 +300,6 @@ constexpr auto contract_group::set_handler(handler h) -> handler {
return old;
}


// Null pointer deref checking
//
auto assert_not_null(auto&& p CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT) -> auto&&
Expand Down Expand Up @@ -326,7 +328,6 @@ auto assert_in_bounds(auto&& x, auto&& arg CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAU
return std::forward<decltype(x)>(x) [ std::forward<decltype(arg)>(arg) ];
}


//-----------------------------------------------------------------------
//
// Arena objects for std::allocators
Expand Down Expand Up @@ -825,6 +826,41 @@ inline auto to_string(std::tuple<Ts...> const& t) -> std::string
}
}

// Helper to convert the arguments of main to safer type.
inline auto convert_main_args(int argc, char** argv) -> std::vector<std::string_view>
{
std::vector<std::string_view> args;
for (int i = 0; i < argc; ++i) {
args.push_back(argv[i]);
}
args.shrink_to_fit();
return args;
}

#define CPP2_MAIN_VOID_NO_ARGS \
int main() { \
cpp2__main(); \
return 0; \
}

#define CPP2_MAIN_INT_NO_ARGS \
int main() { \
return cpp2__main(); \
}

#define CPP2_MAIN_VOID_ARGS \
int main(int argc, char** argv) { \
auto args = cpp2::convert_main_args(argc, argv); \
cpp2__main(std::span{args}); \
return 0; \
}

#define CPP2_MAIN_INT_ARGS \
int main(int argc, char** argv) { \
auto args = cpp2::convert_main_args(argc, argv); \
return cpp2__main(std::span{args}); \
}


//-----------------------------------------------------------------------
//
Expand Down Expand Up @@ -881,7 +917,6 @@ inline auto fopen( const char* filename, const char* mode ) {

}


using cpp2::cpp2_new;


Expand Down
2 changes: 1 addition & 1 deletion regression-tests/mixed-bounds-check.cpp2
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

#include <vector>

main: () -> int = {
main: () = {
v : std::vector = (1, 2, 3, 4, 5);
std::cout << v[5] << "\n";
}
2 changes: 1 addition & 1 deletion regression-tests/mixed-bounds-safety-with-assert-2.cpp2
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

main: () -> int = {
main: () = {
v: std::vector<int> = (1, 2, 3, 4, 5);
add_42_to_subrange(v, 1, 3);

Expand Down
2 changes: 1 addition & 1 deletion regression-tests/mixed-bounds-safety-with-assert.cpp2
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

main: () -> int = {
main: () = {
v: std::vector<int> = (1, 2, 3, 4, 5);
print_subrange(v, 1, 13);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <algorithm>
#include <vector>

main: () -> int = {
main: () = {
vec: std::vector<std::string>
= ("hello", "2022");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <algorithm>
#include <iostream>

main: () -> int = {
main: () = {
vec: std::vector<std::string>
= ("hello", "2022");
view: std::span = vec;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <algorithm>
#include <iostream>

main: () -> int = {
main: () = {
vec: std::vector<std::string>
= ("hello", "2022");
view: std::span = vec;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <algorithm>
#include <iostream>

main: () -> int = {
main: () = {
vec: std::vector<std::string>
= ("hello", "2022");
view: std::span = vec;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <algorithm>
#include <iostream>

main: () -> int = {
main: () = {
vec: std::vector<std::string>
= ("hello", "2023");
view: std::span = vec;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <algorithm>
#include <iostream>

main: () -> int = {
main: () = {
vec: std::vector<std::string>
= ("hello", "2022");
view: std::span = vec;
Expand Down
2 changes: 1 addition & 1 deletion regression-tests/mixed-initialization-safety-1.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fill: (
x = value.substr(0, count);
}

main: () -> int =
main: () =
{
x: std::string; // note: uninitialized!

Expand Down
2 changes: 1 addition & 1 deletion regression-tests/mixed-initialization-safety-2.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fill: (
x = value.substr(0, count);
}

main: () -> int =
main: () =
{
x: std::string; // note: uninitialized!

Expand Down
2 changes: 1 addition & 1 deletion regression-tests/mixed-initialization-safety-3.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <vector>
#include <ctime>

main: () -> int = {
main: () = {
x: std::string; // note: uninitialized!

if flip_a_coin() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

main: () -> int
main: ()
= {
v: std::vector<int> = (1, 2, 3, 4, 5);
counter := 42;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <vector>
#include <string>

main: () -> int = {
main: () = {
cpp2::Null.set_handler(call_my_framework&);
try_pointer_stuff();
std::cout << "done\n";
Expand Down
2 changes: 1 addition & 1 deletion regression-tests/mixed-lifetime-safety-pointer-init-1.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <cstdlib>
#include <iostream>

main: () -> int = {
main: () = {
x: int = 42;
p: *int;

Expand Down
2 changes: 1 addition & 1 deletion regression-tests/mixed-lifetime-safety-pointer-init-2.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <cstdlib>
#include <iostream>

main: () -> int = {
main: () = {
x: int = 42;
p: *int;

Expand Down
2 changes: 1 addition & 1 deletion regression-tests/mixed-lifetime-safety-pointer-init-3.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <cstdlib>
#include <iostream>

main: () -> int = {
main: () = {
x: int = 42;
p: *int;

Expand Down
2 changes: 1 addition & 1 deletion regression-tests/mixed-lifetime-safety-pointer-init-4.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <cstdlib>
#include <iostream>

main: () -> int = {
main: () = {
x: int = 42;
y: int = 43;
p: *int;
Expand Down
2 changes: 1 addition & 1 deletion regression-tests/mixed-parameter-passing-with-forward.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ parameter_styles: (

}

main: () -> int = { }
main: () = { }
2 changes: 1 addition & 1 deletion regression-tests/mixed-parameter-passing.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ parameter_styles: (

}

main: () -> int = { }
main: () = { }
2 changes: 1 addition & 1 deletion regression-tests/mixed-postexpression-with-capture.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <algorithm>
#include <iostream>

main: () -> int = {
main: () = {
insert_at( 0, 42 );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ test: (a:_) -> std::string = {
);
}

main: () -> int = { }
main: () = { }
2 changes: 1 addition & 1 deletion regression-tests/mixed-string-interpolation.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

struct custom_struct_with_no_stringize_customization { } custom;

main: () -> int = {
main: () = {
a := 2;
b: std::optional<int> = ();
std::cout << "a = (a)$, b = (b)$\n";
Expand Down
2 changes: 1 addition & 1 deletion regression-tests/mixed-type-safety-1.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ print: ( msg: std::string, b: bool ) =

//--- examples -------------------------

main: () -> int =
main: () =
{
print( "1.1 is int? ", 1.1 is int );
print( "1 is int? ", 1 is int );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

main: () -> int
main: ()
= {
words: std::vector<std::string> = ( "decorated", "hello", "world" );

Expand Down
2 changes: 1 addition & 1 deletion regression-tests/pure2-bounds-safety-span.cpp2
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

main: () -> int
main: ()
= {
words: std::vector<std::string> = ( "decorated", "hello", "world" );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ vals: () -> (i : int) = {
return;
}

main: () -> int = {
main: () = {
v := vals();
v.i;
}
2 changes: 1 addition & 1 deletion regression-tests/pure2-hello.cpp2
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

main: () -> int = {
main: () = {
std::cout << "Hello " << name() << "\n";
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
main: () -> int = {
main: () = {
v: std::variant<int, double> = 42.0;
a: std::any = "xyzzy" as std::string;
o: std::optional<int> = ();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
main: () -> int = {
main: () = {
print_an_int("syzygy");
print_an_int(1);
print_an_int(1.1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

main: () -> int = {
main: () = {
v: std::variant<int, std::string> = "xyzzy" as std::string;
a: std::any = "xyzzy" as std::string;
o: std::optional<std::string> = "xyzzy" as std::string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

main: () -> int = {
main: () = {
p: std::unique_ptr<int> = ();
i: std::vector<int>::iterator = ();
v: std::variant<std::monostate, int, std::string> = ();
Expand All @@ -23,4 +23,4 @@ test_generic: ( x: _ ) = {
is _ = " no match";
}
<< "\n";
}
}
4 changes: 4 additions & 0 deletions regression-tests/pure2-int-main-arg.cpp2
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
main: (move args :std::span<std::string_view>) -> int = {
for args do: (arg: _) = std::cout << arg << '\n';
return 0;
}
2 changes: 1 addition & 1 deletion regression-tests/pure2-intro-example-hello-2022.cpp2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
main: () -> int = {
main: () = {
vec: std::vector<std::string>
= ("hello", "2022");
view: std::span = vec;
Expand Down
2 changes: 1 addition & 1 deletion regression-tests/pure2-intro-example-three-loops.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ decorate_and_print: (inout thing:_) = {
print(thing);
}

main: () -> int = {
main: () = {
words: std::vector<std::string> =
( "hello", "big", "world" );
view: std::span<std::string> = words;
Expand Down
2 changes: 1 addition & 1 deletion regression-tests/pure2-lifetime-safety-pointer-init-1.cpp2
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

main: () -> int = {
main: () = {
x: int = 42;
p: *int;

Expand Down
2 changes: 1 addition & 1 deletion regression-tests/pure2-lifetime-safety-reject-null.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <vector>
#include <string>

main: () -> int
main: ()
= {
words: std::vector<std::string> = ( "decorated", "hello", "world" );

Expand Down
2 changes: 1 addition & 1 deletion regression-tests/pure2-stdio-with-raii.cpp2
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

// "A better C than C" ... ?
//
main: () -> int = {
main: () = {
s: std::string = "Freddy";
myfile := cpp2::fopen("xyzzy", "w");
myfile.fprintf( "Hello %s with UFCS!", s.c_str() );
Expand Down
2 changes: 1 addition & 1 deletion regression-tests/pure2-stdio.cpp2
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

// "A better C than C" ... ?
//
main: () -> int = {
main: () = {
s: std::string = "Fred";
myfile := fopen("xyzzy", "w");
myfile.fprintf( "Hello %s with UFCS!", s.c_str() );
Expand Down
Loading