From 6ce2643e3ae7f91d0b21bc574fc321f7f015a466 Mon Sep 17 00:00:00 2001 From: Herb Sutter Date: Sat, 12 Aug 2023 18:42:04 -1000 Subject: [PATCH] Make output `-Wunused-parameter`-clean, and add real support for `_` unnamed parameters, closes #560 Emit `[[maybe_unused]]` on a `that` parameter if the class is empty. Add -Wunused-parameter to my GCC and Clang regression tests. Add `_` unnamed parameter support, and use ordinals instead of line/col numbers to keep the generated names stable. As an example from one of the updated regression tests that had a helper function to just accept (but not use) five generic parameters, that function is now just: `call: (_, _, _, _, _) = { }` --- regression-tests/mixed-forwarding.cpp2 | 4 ++-- .../mixed-parameter-passing-with-forward.cpp2 | 6 ++--- regression-tests/mixed-parameter-passing.cpp2 | 6 ++--- ...-postfix-expression-custom-formatting.cpp2 | 2 +- .../pure2-bugfix-for-discard-precedence.cpp2 | 4 ++-- ...bugfix-for-memberwise-base-assignment.cpp2 | 2 +- regression-tests/pure2-types-basics.cpp2 | 2 +- regression-tests/pure2-types-inheritance.cpp2 | 2 +- ...pure2-ufcs-member-access-and-chaining.cpp2 | 4 ++-- .../clang-12/pure2-types-basics.cpp.execution | 2 +- .../clang-12/run-tests-clang-12.sh | 2 +- .../gcc-13/pure2-types-basics.cpp.execution | 2 +- .../test-results/gcc-13/run-tests-gcc-13.sh | 2 +- .../test-results/mixed-forwarding.cpp | 8 +++---- .../mixed-parameter-passing-with-forward.cpp | 12 +++++----- .../test-results/mixed-parameter-passing.cpp | 12 +++++----- ...d-postfix-expression-custom-formatting.cpp | 4 ++-- .../pure2-types-basics.cpp.execution | 2 +- .../pure2-bugfix-for-discard-precedence.cpp | 6 ++--- ...-bugfix-for-memberwise-base-assignment.cpp | 24 +++++++++---------- .../test-results/pure2-types-basics.cpp | 2 +- .../test-results/pure2-types-inheritance.cpp | 4 ++-- .../pure2-ufcs-member-access-and-chaining.cpp | 8 +++---- regression-tests/test-results/version | 2 +- source/build.info | 2 +- source/cppfront.cpp | 18 +++++++++++++- source/parse.h | 5 ++++ 27 files changed, 85 insertions(+), 64 deletions(-) diff --git a/regression-tests/mixed-forwarding.cpp2 b/regression-tests/mixed-forwarding.cpp2 index 81efe7d270..4415753266 100644 --- a/regression-tests/mixed-forwarding.cpp2 +++ b/regression-tests/mixed-forwarding.cpp2 @@ -8,9 +8,9 @@ struct X { X(X && that) : i{that.i} { std::cout << "move X " << i << "\n"; } }; -copy_from: (copy x:_) = { } +copy_from: (copy _) = { } -use: (x:_) = {} +use: (_) = {} // invoking each of these with an rvalue std::pair argument ... apply_implicit_forward: (forward t: std::pair) = { diff --git a/regression-tests/mixed-parameter-passing-with-forward.cpp2 b/regression-tests/mixed-parameter-passing-with-forward.cpp2 index 8ccca8b86b..dea5be5d65 100644 --- a/regression-tests/mixed-parameter-passing-with-forward.cpp2 +++ b/regression-tests/mixed-parameter-passing-with-forward.cpp2 @@ -3,12 +3,12 @@ #include #include -copy_from: (copy x:_) = { } +copy_from: (copy _) = { } parameter_styles: ( - in a: std::string, // "in" is default + in _: std::string, // "in" is default copy b: std::string, - inout c: std::string, + inout _: std::string, move d: std::string, forward e: std::string ) diff --git a/regression-tests/mixed-parameter-passing.cpp2 b/regression-tests/mixed-parameter-passing.cpp2 index 0d0fa4ea54..1ebc595a83 100644 --- a/regression-tests/mixed-parameter-passing.cpp2 +++ b/regression-tests/mixed-parameter-passing.cpp2 @@ -3,12 +3,12 @@ #include #include -copy_from: (copy x:_) = { } +copy_from: (copy _) = { } parameter_styles: ( - in a: std::string, // "in" is default + in _: std::string, // "in" is default copy b: std::string, - inout c: std::string, + inout _: std::string, move d: std::string ) = { diff --git a/regression-tests/mixed-postfix-expression-custom-formatting.cpp2 b/regression-tests/mixed-postfix-expression-custom-formatting.cpp2 index 307a37be0c..209c73d7c8 100644 --- a/regression-tests/mixed-postfix-expression-custom-formatting.cpp2 +++ b/regression-tests/mixed-postfix-expression-custom-formatting.cpp2 @@ -1,5 +1,5 @@ -call: (v:_, w:_, x:_, y:_, z:_) = { } +call: (_, _, _, _, _) = { } test: (a:_) -> std::string = { return call( a, diff --git a/regression-tests/pure2-bugfix-for-discard-precedence.cpp2 b/regression-tests/pure2-bugfix-for-discard-precedence.cpp2 index 2a30653a49..40264e3163 100644 --- a/regression-tests/pure2-bugfix-for-discard-precedence.cpp2 +++ b/regression-tests/pure2-bugfix-for-discard-precedence.cpp2 @@ -1,8 +1,8 @@ quantity: type = { number: i32; - operator=: (out this, _: std::in_place_t, x: i32) = { + operator=: (out this, i: std::in_place_t, x: i32) = { number = x; - _ = _; + _ = i; } operator+=: (inout this, that) -> forward quantity = { number += that.number; diff --git a/regression-tests/pure2-bugfix-for-memberwise-base-assignment.cpp2 b/regression-tests/pure2-bugfix-for-memberwise-base-assignment.cpp2 index c27d68b51b..9b95fae2ce 100644 --- a/regression-tests/pure2-bugfix-for-memberwise-base-assignment.cpp2 +++ b/regression-tests/pure2-bugfix-for-memberwise-base-assignment.cpp2 @@ -1,7 +1,7 @@ Base: type = { operator=: (out this) = { } operator=: (out this, that) = std::cout << "(out this, that)\n"; - operator=: (implicit out this, x) = std::cout << "(implicit out this, x)\n"; + operator=: (implicit out this, _) = std::cout << "(implicit out this, _)\n"; } Derived: type = { diff --git a/regression-tests/pure2-types-basics.cpp2 b/regression-tests/pure2-types-basics.cpp2 index b9416943d8..ce18c76fea 100644 --- a/regression-tests/pure2-types-basics.cpp2 +++ b/regression-tests/pure2-types-basics.cpp2 @@ -19,7 +19,7 @@ myclass : type = { operator=: (out this, x: int, s: std::string) = { this.data = 77; - this.more = s + " plugh"; + this.more = s + std::to_string(x) + " plugh"; std::cout << "myclass: from int and string\n"; print(); } diff --git a/regression-tests/pure2-types-inheritance.cpp2 b/regression-tests/pure2-types-inheritance.cpp2 index 4dfde55c42..eabf56dfdb 100644 --- a/regression-tests/pure2-types-inheritance.cpp2 +++ b/regression-tests/pure2-types-inheritance.cpp2 @@ -5,7 +5,7 @@ Human: @interface type = { N: namespace = { Machine: @polymorphic_base type = { - operator=: (out this, id: std::string) = {} + operator=: (out this, _: std::string) = {} work: (virtual this); } } diff --git a/regression-tests/pure2-ufcs-member-access-and-chaining.cpp2 b/regression-tests/pure2-ufcs-member-access-and-chaining.cpp2 index 68ed760c2b..83ee65e4b4 100644 --- a/regression-tests/pure2-ufcs-member-access-and-chaining.cpp2 +++ b/regression-tests/pure2-ufcs-member-access-and-chaining.cpp2 @@ -21,7 +21,7 @@ main: () -> int = { 42.no_return(); } -no_return: (x: int) = { } +no_return: (_) = { } ufcs: (i:int) -> int = { return i+2; @@ -37,5 +37,5 @@ get_i: (r:_) -> int = { } // And a test for non-local UFCS, which shouldn't do a [&] capture -f: (x)->int = 0; +f: (_)->int = 0; y: int = 0.f(); diff --git a/regression-tests/test-results/clang-12/pure2-types-basics.cpp.execution b/regression-tests/test-results/clang-12/pure2-types-basics.cpp.execution index be62df51a9..96b31fc422 100644 --- a/regression-tests/test-results/clang-12/pure2-types-basics.cpp.execution +++ b/regression-tests/test-results/clang-12/pure2-types-basics.cpp.execution @@ -11,7 +11,7 @@ myclass: explicit from string myclass: default data: 504, more: 3.141590 myclass: from int and string - data: 77, more: hair plugh + data: 77, more: hair1 plugh x's state before assignments: data: 1, more: 504 myclass: implicit from int data: 84, more: 504 diff --git a/regression-tests/test-results/clang-12/run-tests-clang-12.sh b/regression-tests/test-results/clang-12/run-tests-clang-12.sh index c11e70a8bc..d179a23909 100644 --- a/regression-tests/test-results/clang-12/run-tests-clang-12.sh +++ b/regression-tests/test-results/clang-12/run-tests-clang-12.sh @@ -10,7 +10,7 @@ for f in *.cpp do let count=count+1 printf "[%s] Starting clang++-12 %s\n" "$count" "$f" - clang++-12 -I../../../include -std=c++20 -pthread -o test.exe $f > $f.output 2>&1 + clang++-12 -I../../../include -std=c++20 -pthread -Wunused-parameter -o test.exe $f > $f.output 2>&1 rm -f $f if test -f "test.exe"; then let exe_count=exe_count+1 diff --git a/regression-tests/test-results/gcc-13/pure2-types-basics.cpp.execution b/regression-tests/test-results/gcc-13/pure2-types-basics.cpp.execution index be62df51a9..96b31fc422 100644 --- a/regression-tests/test-results/gcc-13/pure2-types-basics.cpp.execution +++ b/regression-tests/test-results/gcc-13/pure2-types-basics.cpp.execution @@ -11,7 +11,7 @@ myclass: explicit from string myclass: default data: 504, more: 3.141590 myclass: from int and string - data: 77, more: hair plugh + data: 77, more: hair1 plugh x's state before assignments: data: 1, more: 504 myclass: implicit from int data: 84, more: 504 diff --git a/regression-tests/test-results/gcc-13/run-tests-gcc-13.sh b/regression-tests/test-results/gcc-13/run-tests-gcc-13.sh index 70cbafde7d..c2ec29294a 100644 --- a/regression-tests/test-results/gcc-13/run-tests-gcc-13.sh +++ b/regression-tests/test-results/gcc-13/run-tests-gcc-13.sh @@ -10,7 +10,7 @@ for f in *.cpp do let count=count+1 printf "[%s] Starting gcc 13 %s\n" "$count" "$f" - g++ -I../../../include -std=c++20 -pthread -o test.exe $f > $f.output 2>&1 + g++ -I../../../include -std=c++20 -pthread -Wunused-parameter -o test.exe $f > $f.output 2>&1 rm -f $f if test -f "test.exe"; then let exe_count=exe_count+1 diff --git a/regression-tests/test-results/mixed-forwarding.cpp b/regression-tests/test-results/mixed-forwarding.cpp index f07a354e1c..5777940a22 100644 --- a/regression-tests/test-results/mixed-forwarding.cpp +++ b/regression-tests/test-results/mixed-forwarding.cpp @@ -20,9 +20,9 @@ struct X { }; #line 11 "mixed-forwarding.cpp2" -auto copy_from(auto x) -> void; +auto copy_from([[maybe_unused]] auto param1) -> void; -auto use(auto const& x) -> void; +auto use([[maybe_unused]] auto const& param1) -> void; // invoking each of these with an rvalue std::pair argument ... auto apply_implicit_forward(auto&& t) -> void @@ -46,9 +46,9 @@ CPP2_REQUIRES (std::is_same_v>) #line 11 "mixed-forwarding.cpp2" -auto copy_from(auto x) -> void{} +auto copy_from([[maybe_unused]] auto param1) -> void{} -auto use(auto const& x) -> void{} +auto use([[maybe_unused]] auto const& param1) -> void{} #line 16 "mixed-forwarding.cpp2" auto apply_implicit_forward(auto&& t) -> void diff --git a/regression-tests/test-results/mixed-parameter-passing-with-forward.cpp b/regression-tests/test-results/mixed-parameter-passing-with-forward.cpp index b3930a43ed..a6bc80b167 100644 --- a/regression-tests/test-results/mixed-parameter-passing-with-forward.cpp +++ b/regression-tests/test-results/mixed-parameter-passing-with-forward.cpp @@ -15,12 +15,12 @@ #include #line 6 "mixed-parameter-passing-with-forward.cpp2" -auto copy_from(auto x) -> void; +auto copy_from([[maybe_unused]] auto param1) -> void; auto parameter_styles( - cpp2::in a, // "in" is default + [[maybe_unused]] cpp2::in param1, // "in" is default std::string b, - std::string& c, + [[maybe_unused]] std::string& param3, std::string&& d, auto&& e ) -> void @@ -36,12 +36,12 @@ CPP2_REQUIRES (std::is_same_v) #line 6 "mixed-parameter-passing-with-forward.cpp2" -auto copy_from(auto x) -> void{} +auto copy_from([[maybe_unused]] auto param1) -> void{} auto parameter_styles( - cpp2::in a, + [[maybe_unused]] cpp2::in param1, std::string b, - std::string& c, + [[maybe_unused]] std::string& param3, std::string&& d, auto&& e ) -> void diff --git a/regression-tests/test-results/mixed-parameter-passing.cpp b/regression-tests/test-results/mixed-parameter-passing.cpp index 7a566ccc27..537d9bd64f 100644 --- a/regression-tests/test-results/mixed-parameter-passing.cpp +++ b/regression-tests/test-results/mixed-parameter-passing.cpp @@ -15,12 +15,12 @@ #include #line 6 "mixed-parameter-passing.cpp2" -auto copy_from(auto x) -> void; +auto copy_from([[maybe_unused]] auto param1) -> void; auto parameter_styles( - cpp2::in a, // "in" is default + [[maybe_unused]] cpp2::in param1, // "in" is default std::string b, - std::string& c, + [[maybe_unused]] std::string& param3, std::string&& d ) -> void; @@ -32,12 +32,12 @@ auto parameter_styles( #line 6 "mixed-parameter-passing.cpp2" -auto copy_from(auto x) -> void{} +auto copy_from([[maybe_unused]] auto param1) -> void{} auto parameter_styles( - cpp2::in a, + [[maybe_unused]] cpp2::in param1, std::string b, - std::string& c, + [[maybe_unused]] std::string& param3, std::string&& d ) -> void { diff --git a/regression-tests/test-results/mixed-postfix-expression-custom-formatting.cpp b/regression-tests/test-results/mixed-postfix-expression-custom-formatting.cpp index 85c910fda9..81738a3b5f 100644 --- a/regression-tests/test-results/mixed-postfix-expression-custom-formatting.cpp +++ b/regression-tests/test-results/mixed-postfix-expression-custom-formatting.cpp @@ -11,7 +11,7 @@ #line 2 "mixed-postfix-expression-custom-formatting.cpp2" -auto call(auto const& v, auto const& w, auto const& x, auto const& y, auto const& z) -> void; +auto call([[maybe_unused]] auto const& param1, [[maybe_unused]] auto const& param2, [[maybe_unused]] auto const& param3, [[maybe_unused]] auto const& param4, [[maybe_unused]] auto const& param5) -> void; [[nodiscard]] auto test(auto const& a) -> std::string; @@ -24,7 +24,7 @@ auto call(auto const& v, auto const& w, auto const& x, auto const& y, auto const #line 2 "mixed-postfix-expression-custom-formatting.cpp2" -auto call(auto const& v, auto const& w, auto const& x, auto const& y, auto const& z) -> void{} +auto call([[maybe_unused]] auto const& param1, [[maybe_unused]] auto const& param2, [[maybe_unused]] auto const& param3, [[maybe_unused]] auto const& param4, [[maybe_unused]] auto const& param5) -> void{} [[nodiscard]] auto test(auto const& a) -> std::string{ return call(a, diff --git a/regression-tests/test-results/msvc-2022/pure2-types-basics.cpp.execution b/regression-tests/test-results/msvc-2022/pure2-types-basics.cpp.execution index be62df51a9..96b31fc422 100644 --- a/regression-tests/test-results/msvc-2022/pure2-types-basics.cpp.execution +++ b/regression-tests/test-results/msvc-2022/pure2-types-basics.cpp.execution @@ -11,7 +11,7 @@ myclass: explicit from string myclass: default data: 504, more: 3.141590 myclass: from int and string - data: 77, more: hair plugh + data: 77, more: hair1 plugh x's state before assignments: data: 1, more: 504 myclass: implicit from int data: 84, more: 504 diff --git a/regression-tests/test-results/pure2-bugfix-for-discard-precedence.cpp b/regression-tests/test-results/pure2-bugfix-for-discard-precedence.cpp index b695966734..0266e0c427 100644 --- a/regression-tests/test-results/pure2-bugfix-for-discard-precedence.cpp +++ b/regression-tests/test-results/pure2-bugfix-for-discard-precedence.cpp @@ -15,7 +15,7 @@ class quantity; #line 1 "pure2-bugfix-for-discard-precedence.cpp2" class quantity { private: cpp2::i32 number; - public: explicit quantity(cpp2::in _, cpp2::in x); + public: explicit quantity(cpp2::in i, cpp2::in x); #line 7 "pure2-bugfix-for-discard-precedence.cpp2" @@ -35,12 +35,12 @@ auto main() -> int; #line 3 "pure2-bugfix-for-discard-precedence.cpp2" - quantity::quantity(cpp2::in _, cpp2::in x) + quantity::quantity(cpp2::in i, cpp2::in x) : number{ x } #line 3 "pure2-bugfix-for-discard-precedence.cpp2" { - static_cast(_); + static_cast(i); } auto quantity::operator+=(quantity const& that) -> quantity&{ number += that.number; diff --git a/regression-tests/test-results/pure2-bugfix-for-memberwise-base-assignment.cpp b/regression-tests/test-results/pure2-bugfix-for-memberwise-base-assignment.cpp index 187cfa4a09..ec18893786 100644 --- a/regression-tests/test-results/pure2-bugfix-for-memberwise-base-assignment.cpp +++ b/regression-tests/test-results/pure2-bugfix-for-memberwise-base-assignment.cpp @@ -19,19 +19,19 @@ class Derived; #line 1 "pure2-bugfix-for-memberwise-base-assignment.cpp2" class Base { public: explicit Base(); - public: Base(Base const& that); + public: Base([[maybe_unused]] Base const& that); #line 3 "pure2-bugfix-for-memberwise-base-assignment.cpp2" - public: auto operator=(Base const& that) -> Base& ; + public: auto operator=([[maybe_unused]] Base const& that) -> Base& ; #line 3 "pure2-bugfix-for-memberwise-base-assignment.cpp2" - public: Base(Base&& that) noexcept; + public: Base([[maybe_unused]] Base&& that) noexcept; #line 3 "pure2-bugfix-for-memberwise-base-assignment.cpp2" - public: auto operator=(Base&& that) noexcept -> Base& ; - public: Base(auto const& x); + public: auto operator=([[maybe_unused]] Base&& that) noexcept -> Base& ; + public: Base([[maybe_unused]] auto const& param2); #line 4 "pure2-bugfix-for-memberwise-base-assignment.cpp2" - public: auto operator=(auto const& x) -> Base& ; + public: auto operator=([[maybe_unused]] auto const& param2) -> Base& ; }; class Derived: public Base { @@ -49,22 +49,22 @@ auto main() -> int; #line 2 "pure2-bugfix-for-memberwise-base-assignment.cpp2" Base::Base(){} - Base::Base (Base const& that) { std::cout << "(out this, that)\n"; } + Base::Base ([[maybe_unused]] Base const& that) { std::cout << "(out this, that)\n"; } #line 3 "pure2-bugfix-for-memberwise-base-assignment.cpp2" - auto Base::operator=(Base const& that) -> Base& { std::cout << "(out this, that)\n"; + auto Base::operator=([[maybe_unused]] Base const& that) -> Base& { std::cout << "(out this, that)\n"; return *this; #line 3 "pure2-bugfix-for-memberwise-base-assignment.cpp2" } #line 3 "pure2-bugfix-for-memberwise-base-assignment.cpp2" - Base::Base (Base&& that) noexcept { std::cout << "(out this, that)\n"; } + Base::Base ([[maybe_unused]] Base&& that) noexcept { std::cout << "(out this, that)\n"; } #line 3 "pure2-bugfix-for-memberwise-base-assignment.cpp2" - auto Base::operator=(Base&& that) noexcept -> Base& { std::cout << "(out this, that)\n"; + auto Base::operator=([[maybe_unused]] Base&& that) noexcept -> Base& { std::cout << "(out this, that)\n"; return *this; #line 3 "pure2-bugfix-for-memberwise-base-assignment.cpp2" } - Base::Base(auto const& x) { std::cout << "(implicit out this, x)\n"; } + Base::Base([[maybe_unused]] auto const& param2) { std::cout << "(implicit out this, _)\n"; } #line 4 "pure2-bugfix-for-memberwise-base-assignment.cpp2" - auto Base::operator=(auto const& x) -> Base& { std::cout << "(implicit out this, x)\n"; + auto Base::operator=([[maybe_unused]] auto const& param2) -> Base& { std::cout << "(implicit out this, _)\n"; return *this; #line 4 "pure2-bugfix-for-memberwise-base-assignment.cpp2" } diff --git a/regression-tests/test-results/pure2-types-basics.cpp b/regression-tests/test-results/pure2-types-basics.cpp index 3429feb0f9..eeca3e2f64 100644 --- a/regression-tests/test-results/pure2-types-basics.cpp +++ b/regression-tests/test-results/pure2-types-basics.cpp @@ -138,7 +138,7 @@ namespace N { myclass::myclass(cpp2::in x, cpp2::in s) : data{ 77 } - , more{ s + " plugh" } + , more{ s + std::to_string(x) + " plugh" } #line 20 "pure2-types-basics.cpp2" { diff --git a/regression-tests/test-results/pure2-types-inheritance.cpp b/regression-tests/test-results/pure2-types-inheritance.cpp index 23f6be8943..77765240a4 100644 --- a/regression-tests/test-results/pure2-types-inheritance.cpp +++ b/regression-tests/test-results/pure2-types-inheritance.cpp @@ -39,7 +39,7 @@ public: virtual ~Human() noexcept; namespace N { template class Machine { - public: explicit Machine(cpp2::in id); + public: explicit Machine([[maybe_unused]] cpp2::in param2); public: virtual auto work() const -> void = 0; public: virtual ~Machine() noexcept; @@ -97,7 +97,7 @@ auto main() -> int; #line 6 "pure2-types-inheritance.cpp2" namespace N { - template Machine::Machine(cpp2::in id){} + template Machine::Machine([[maybe_unused]] cpp2::in param2){} template Machine::~Machine() noexcept{} diff --git a/regression-tests/test-results/pure2-ufcs-member-access-and-chaining.cpp b/regression-tests/test-results/pure2-ufcs-member-access-and-chaining.cpp index 1bd8f78692..86a875ca38 100644 --- a/regression-tests/test-results/pure2-ufcs-member-access-and-chaining.cpp +++ b/regression-tests/test-results/pure2-ufcs-member-access-and-chaining.cpp @@ -15,7 +15,7 @@ #line 24 "pure2-ufcs-member-access-and-chaining.cpp2" -auto no_return(cpp2::in x) -> void; +auto no_return([[maybe_unused]] auto const& param1) -> void; [[nodiscard]] auto ufcs(cpp2::in i) -> int; struct fun__ret { int i; }; @@ -32,7 +32,7 @@ auto no_return(cpp2::in x) -> void; #line 39 "pure2-ufcs-member-access-and-chaining.cpp2" // And a test for non-local UFCS, which shouldn't do a [&] capture -[[nodiscard]] auto f(auto const& x) -> int; +[[nodiscard]] auto f([[maybe_unused]] auto const& param1) -> int; extern int y; //=== Cpp2 function definitions ================================================= @@ -61,7 +61,7 @@ extern int y; CPP2_UFCS_0(no_return, 42); } -auto no_return(cpp2::in x) -> void{} +auto no_return([[maybe_unused]] auto const& param1) -> void{} [[nodiscard]] auto ufcs(cpp2::in i) -> int{ return i + 2; @@ -79,6 +79,6 @@ auto no_return(cpp2::in x) -> void{} } #line 40 "pure2-ufcs-member-access-and-chaining.cpp2" -[[nodiscard]] auto f(auto const& x) -> int { return 0; } +[[nodiscard]] auto f([[maybe_unused]] auto const& param1) -> int { return 0; } int y {CPP2_UFCS_0_NONLOCAL(f, 0)}; diff --git a/regression-tests/test-results/version b/regression-tests/test-results/version index 3c92818d40..eea4cf89b7 100644 --- a/regression-tests/test-results/version +++ b/regression-tests/test-results/version @@ -1,5 +1,5 @@ -cppfront compiler v0.2.1 Build 8812:1658 +cppfront compiler v0.2.1 Build 8812:1816 Copyright(c) Herb Sutter All rights reserved SPDX-License-Identifier: CC-BY-NC-ND-4.0 diff --git a/source/build.info b/source/build.info index ff608259a9..9e125a11eb 100644 --- a/source/build.info +++ b/source/build.info @@ -1 +1 @@ -"8812:1658" \ No newline at end of file +"8812:1816" \ No newline at end of file diff --git a/source/cppfront.cpp b/source/cppfront.cpp index f61a692a7e..8ce4aca99e 100644 --- a/source/cppfront.cpp +++ b/source/cppfront.cpp @@ -3842,6 +3842,7 @@ class cppfront if (n.mod == parameter_declaration_node::modifier::implicit) { + assert(!current_functions.empty()); if ( n.pass != passing_style::out || !current_functions.back().decl->has_name("operator=") @@ -3893,8 +3894,16 @@ class cppfront auto type_name = get_enclosing_type_name(); assert(type_name); + // If we're in an empty type that has no member object, mark 'that' as + // [[maybe_unused]] to silence Cpp1 compiler warnings + assert(!current_functions.empty()); + auto maybe_unused = std::string{}; + if (current_functions.back().decl->get_parent()->get_type_scope_declarations(declaration_node::objects).empty()) { + maybe_unused = "[[maybe_unused]] "; + } + printer.print_cpp2( - print_to_string( *type_name ) + pass + " that", + maybe_unused + print_to_string( *type_name ) + pass + " that", n.position() ); return; @@ -3978,6 +3987,12 @@ class cppfront auto identifier = print_to_string( *n.declaration->identifier ); // First any prefix + + if (identifier == "_") { + printer.print_cpp2( "[[maybe_unused]] ", n.position() ); + identifier = "param" + std::to_string(n.ordinal); + } + if ( !is_returns && !type_id.is_wildcard() @@ -4055,6 +4070,7 @@ class cppfront printer.preempt_position_pop(); // Then any suffix + if ( !is_returns && !type_id.is_wildcard() diff --git a/source/parse.h b/source/parse.h index 112c102c79..c4779e5bd0 100644 --- a/source/parse.h +++ b/source/parse.h @@ -1859,6 +1859,7 @@ struct parameter_declaration_node { source_position pos = {}; passing_style pass = passing_style::in; + int ordinal = 1; enum class modifier { none=0, implicit, virtual_, override_, final_ }; modifier mod = modifier::none; @@ -6184,8 +6185,12 @@ class parser auto param = std::make_unique(); + auto count = 1; while ((param = parameter_declaration(is_returns, is_named, is_template, is_statement)) != nullptr) { + param->ordinal = count; + ++count; + if ( std::ssize(n->parameters) > 1 && n->parameters.back()->has_name("that")