Skip to content

Commit

Permalink
Add mutable_argv, closes hsutter#592
Browse files Browse the repository at this point in the history
See discussion in hsutter#592 comment thread
  • Loading branch information
hsutter authored and zaucy committed Dec 5, 2023
1 parent 347f374 commit 01645f2
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 23 deletions.
12 changes: 7 additions & 5 deletions include/cpp2util.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@
#include <string>
#include <string_view>
#include <vector>
#include <span>
#include <iostream>
#include <variant>
#include <any>
Expand Down Expand Up @@ -1490,16 +1491,17 @@ inline auto to_string(std::tuple<Ts...> const& t) -> std::string
//
struct args_t : std::vector<std::string_view>
{
args_t(int c, char const* const* v) : vector{static_cast<size_t>(c)}, argc{c}, argv{v} {}
args_t(int c, char** v) : vector{static_cast<size_t>(c)}, argc{c}, argv{v}, mutable_argv{v} {}

int argc = 0;
char const* const* argv = nullptr;
int argc = 0;
char const* const* argv = nullptr;
char** mutable_argv = nullptr;
};

inline auto make_args(int argc, char const* const* argv) -> args_t
inline auto make_args(int argc, char** argv) -> args_t
{
auto ret = args_t{argc, argv};
std::copy(argv, argv + argc, ret.data());
std::ranges::copy( std::span(argv, argc), ret.data() );
return ret;
}

Expand Down
6 changes: 5 additions & 1 deletion regression-tests/pure2-main-args.cpp2
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
main: (args) =
std::cout << "args.argc is (args.argc)$, and args.argv[0] is (args.argv[0])$\n";
std::cout
<< "args.argc is (args.argc)$\n"
<< "args.argv[0] is (args.argv[0])$\n"
<< "args.mutable_argv[0] is (args.mutable_argv[0])$\n"
;
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
args.argc is 1, and args.argv[0] is ./test.exe
args.argc is 1
args.argv[0] is ./test.exe
args.mutable_argv[0] is ./test.exe
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
args.argc is 1, and args.argv[0] is ./test.exe
args.argc is 1
args.argv[0] is ./test.exe
args.mutable_argv[0] is ./test.exe
4 changes: 2 additions & 2 deletions regression-tests/test-results/mixed-fixed-type-aliases.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ auto test(auto const& x) -> void;


#line 15 "mixed-fixed-type-aliases.cpp2"
[[nodiscard]] auto main(int const argc_, char const* const* const argv_) -> int;
[[nodiscard]] auto main(int const argc_, char** argv_) -> int;


//=== Cpp2 function definitions =================================================
Expand All @@ -35,7 +35,7 @@ auto test(auto const& x) -> void{
<< "\n";
}

[[nodiscard]] auto main(int const argc_, char const* const* const argv_) -> int{
[[nodiscard]] auto main(int const argc_, char** argv_) -> int{
auto args = cpp2::make_args(argc_, argv_);
#line 16 "mixed-fixed-type-aliases.cpp2"
my::u16 y {42};
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
args.argc is 1, and args.argv[0] is test.exe
args.argc is 1
args.argv[0] is test.exe
args.mutable_argv[0] is test.exe
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class quantity {
#line 5 "pure2-bugfix-for-discard-precedence.cpp2"
};

auto main(int const argc_, char const* const* const argv_) -> int;
auto main(int const argc_, char** argv_) -> int;


//=== Cpp2 function definitions =================================================
Expand All @@ -46,7 +46,7 @@ auto main(int const argc_, char const* const* const argv_) -> int;
[[nodiscard]] auto quantity::operator+(quantity const& that) -> quantity { return quantity(number + that.number); }

#line 7 "pure2-bugfix-for-discard-precedence.cpp2"
auto main(int const argc_, char const* const* const argv_) -> int{
auto main(int const argc_, char** argv_) -> int{
auto args = cpp2::make_args(argc_, argv_);
#line 8 "pure2-bugfix-for-discard-precedence.cpp2"
quantity x {1729};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
//=== Cpp2 type definitions and function declarations ===========================

#line 1 "pure2-initialization-safety-with-else-if.cpp2"
auto main(int const argc_, char const* const* const argv_) -> int;
auto main(int const argc_, char** argv_) -> int;


//=== Cpp2 function definitions =================================================

#line 1 "pure2-initialization-safety-with-else-if.cpp2"
auto main(int const argc_, char const* const* const argv_) -> int{
auto main(int const argc_, char** argv_) -> int{
auto args = cpp2::make_args(argc_, argv_);
#line 2 "pure2-initialization-safety-with-else-if.cpp2"
cpp2::deferred_init<int*> p;
Expand Down
9 changes: 6 additions & 3 deletions regression-tests/test-results/pure2-main-args.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@
//=== Cpp2 type definitions and function declarations ===========================

#line 1 "pure2-main-args.cpp2"
auto main(int const argc_, char const* const* const argv_) -> int;
auto main(int const argc_, char** argv_) -> int;


//=== Cpp2 function definitions =================================================

#line 1 "pure2-main-args.cpp2"
auto main(int const argc_, char const* const* const argv_) -> int {
auto main(int const argc_, char** argv_) -> int {
auto args = cpp2::make_args(argc_, argv_);
#line 2 "pure2-main-args.cpp2"
std::cout << "args.argc is " + cpp2::to_string(args.argc) + ", and args.argv[0] is " + cpp2::to_string(cpp2::assert_in_bounds(args.argv, 0)) + "\n"; }
std::cout
<< "args.argc is " + cpp2::to_string(args.argc) + "\n"
<< "args.argv[0] is " + cpp2::to_string(cpp2::assert_in_bounds(args.argv, 0)) + "\n"
<< "args.mutable_argv[0] is " + cpp2::to_string(cpp2::assert_in_bounds(args.mutable_argv, 0)) + "\n"; }

Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@


#line 2 "pure2-statement-scope-parameters.cpp2"
auto main(int const argc_, char const* const* const argv_) -> int;
auto main(int const argc_, char** argv_) -> int;


//=== Cpp2 function definitions =================================================


#line 2 "pure2-statement-scope-parameters.cpp2"
auto main(int const argc_, char const* const* const argv_) -> int{
auto main(int const argc_, char** argv_) -> int{
auto args = cpp2::make_args(argc_, argv_);
#line 3 "pure2-statement-scope-parameters.cpp2"
auto local_int {42};
Expand Down
2 changes: 1 addition & 1 deletion regression-tests/test-results/version
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

cppfront compiler v0.2.1 Build 8812:1816
cppfront compiler v0.2.1 Build 8814:1300
Copyright(c) Herb Sutter All rights reserved

SPDX-License-Identifier: CC-BY-NC-ND-4.0
Expand Down
2 changes: 1 addition & 1 deletion source/build.info
Original file line number Diff line number Diff line change
@@ -1 +1 @@
"8812:1816"
"8814:1300"
2 changes: 1 addition & 1 deletion source/cppfront.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4260,7 +4260,7 @@ class cppfront
)
{
printer.print_cpp2(
"(int const argc_, char const* const* const argv_)",
"(int const argc_, char** argv_)",
n.parameters->position()
);
current_functions.back().prolog.statements.push_back(
Expand Down

0 comments on commit 01645f2

Please sign in to comment.