https://github.com/fpetrogalli updated https://github.com/llvm/llvm-project/pull/83584
>From 2b4d67bf59d609321701540a15f48eda04688652 Mon Sep 17 00:00:00 2001 From: Vinayak Dev <104419489+vinayakd...@users.noreply.github.com> Date: Fri, 1 Mar 2024 21:10:46 +0530 Subject: [PATCH 1/3] [Clang][Sema]: Allow copy constructor side effects (#81127) Copy constructors can have initialization with side effects, and thus clang should not emit a warning when -Wunused-variable is used in this context. Currently however, a warning is emitted. Now, compilation happens without warnings. Fixes #79518 --- clang/docs/ReleaseNotes.rst | 4 +++ clang/lib/Sema/SemaDecl.cpp | 3 +- clang/test/SemaCXX/warn-unused-variables.cpp | 33 ++++++++++++++++++-- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 1286263700963c..cfe0ac6a5dca61 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -218,6 +218,10 @@ Bug Fixes in This Version for logical operators in C23. Fixes (`#64356 <https://github.com/llvm/llvm-project/issues/64356>`_). +- Clang no longer produces a false-positive `-Wunused-variable` warning + for variables created through copy initialization having side-effects in C++17 and later. + Fixes (`#79518 <https://github.com/llvm/llvm-project/issues/79518>`_). + Bug Fixes to Compiler Builtins ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 9fdd8eb236d1ee..6289cf75e17413 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -2044,7 +2044,8 @@ static bool ShouldDiagnoseUnusedDecl(const LangOptions &LangOpts, return false; if (Init) { - const auto *Construct = dyn_cast<CXXConstructExpr>(Init); + const auto *Construct = + dyn_cast<CXXConstructExpr>(Init->IgnoreImpCasts()); if (Construct && !Construct->isElidable()) { const CXXConstructorDecl *CD = Construct->getConstructor(); if (!CD->isTrivial() && !RD->hasAttr<WarnUnusedAttr>() && diff --git a/clang/test/SemaCXX/warn-unused-variables.cpp b/clang/test/SemaCXX/warn-unused-variables.cpp index b649c7d8089355..29e8d08d37d8c6 100644 --- a/clang/test/SemaCXX/warn-unused-variables.cpp +++ b/clang/test/SemaCXX/warn-unused-variables.cpp @@ -1,5 +1,7 @@ // RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -verify %s -// RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -verify -std=gnu++11 %s +// RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -verify=expected,cxx98-14 -std=gnu++11 %s +// RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -verify=expected,cxx98-14 -std=gnu++14 %s +// RUN: %clang_cc1 -fsyntax-only -Wunused-variable -Wunused-label -Wno-c++1y-extensions -verify -std=gnu++17 %s template<typename T> void f() { T t; t = 17; @@ -183,7 +185,8 @@ void foo(int size) { NonTriviallyDestructible array[2]; // no warning NonTriviallyDestructible nestedArray[2][2]; // no warning - Foo fooScalar = 1; // expected-warning {{unused variable 'fooScalar'}} + // Copy initialzation gives warning before C++17 + Foo fooScalar = 1; // cxx98-14-warning {{unused variable 'fooScalar'}} Foo fooArray[] = {1,2}; // expected-warning {{unused variable 'fooArray'}} Foo fooNested[2][2] = { {1,2}, {3,4} }; // expected-warning {{unused variable 'fooNested'}} } @@ -297,3 +300,29 @@ void RAIIWrapperTest() { } } // namespace gh54489 + +// Ensure that -Wunused-variable does not emit warning +// on copy constructors with side effects (C++17 and later) +#if __cplusplus >= 201703L +namespace gh79518 { + +struct S { + S(int); +}; + +// With an initializer list +struct A { + int x; + A(int x) : x(x) {} +}; + +void foo() { + S s(0); // no warning + S s2 = 0; // no warning + S s3{0}; // no warning + + A a = 1; // no warning +} + +} // namespace gh79518 +#endif >From 9cd3e17192833e2cbafed55b649307ead32dcc3e Mon Sep 17 00:00:00 2001 From: Francesco Petrogalli <francesco.petroga...@apple.com> Date: Fri, 1 Mar 2024 16:23:57 +0100 Subject: [PATCH 2/3] [clang][Builtins] Parse clang extended vectors types. Clang extended vector types are mangled as follows: ext_vector_type_<lanes>_<scalar type> This is used to defetmine the builtins signature for builtins that use parmeters defined as typedef <scalar type> ext_vector_type_<lanes>_<scalar type> __attribute__((ext_vector_type(<lanes>))) For example: typedef double ext_vector_type_4_double __attribute__((ext_vector_type(4))) --- .../target-builtins-prototype-parser.td | 20 +++++++++++++++++++ clang/utils/TableGen/ClangBuiltinsEmitter.cpp | 10 ++++++++++ 2 files changed, 30 insertions(+) create mode 100644 clang/test/TableGen/target-builtins-prototype-parser.td diff --git a/clang/test/TableGen/target-builtins-prototype-parser.td b/clang/test/TableGen/target-builtins-prototype-parser.td new file mode 100644 index 00000000000000..681a607da7e115 --- /dev/null +++ b/clang/test/TableGen/target-builtins-prototype-parser.td @@ -0,0 +1,20 @@ +// RUN: clang-tblgen -I %p/../../../clang/include/ %s --gen-clang-builtins | FileCheck %s +// RUN: not clang-tblgen -I %p/../../../clang/include/ %s --gen-clang-builtins -DERROR_EXPECTED_LANES 2>&1 | FileCheck %s --check-prefix=ERROR_EXPECTED_LANES + +include "clang/Basic/BuiltinsBase.td" + +def : Builtin { + let Prototype = "ext_vector_type_8_int(double, ext_vector_type_4_bool)"; + let Spellings = ["__builtin_test_use_clang_extended_vectors"]; +} + +// CHECK: BUILTIN(__builtin_test_use_clang_extended_vectors, "E8idE4b", "") + +#ifdef ERROR_EXPECTED_LANES +def : Builtin { +// ERROR_EXPECTED_LANES: :[[# @LINE + 1]]:7: error: Expected number of lanes + let Prototype = "ext_vector_type__int(double, ext_vector_type_4_bool)"; + let Spellings = ["__builtin_test_use_clang_extended_vectors"]; +} +#endif + diff --git a/clang/utils/TableGen/ClangBuiltinsEmitter.cpp b/clang/utils/TableGen/ClangBuiltinsEmitter.cpp index 48f55b8af97e4e..774f703390a05e 100644 --- a/clang/utils/TableGen/ClangBuiltinsEmitter.cpp +++ b/clang/utils/TableGen/ClangBuiltinsEmitter.cpp @@ -85,6 +85,16 @@ class PrototypeParser { if (Substitution.empty()) PrintFatalError(Loc, "Not a template"); ParseType(Substitution); + } else if (T.consume_front("ext_vector_type_")) { + // Clang extended vector types are mangled as follows: + // + // ext_vector_type_<lanes>_<scalar type> + unsigned long long Lanes; + if (llvm::consumeUnsignedInteger(T, 10, Lanes)) + PrintFatalError(Loc, "Expected number of lanes "); + Type += "E" + std::to_string(Lanes); + T.consume_front("_"); + ParseType(T); } else { auto ReturnTypeVal = StringSwitch<std::string>(T) .Case("__builtin_va_list_ref", "A") >From e7ac017c9092a99828401d2209061b02570c7dbb Mon Sep 17 00:00:00 2001 From: Francesco Petrogalli <francesco.petroga...@apple.com> Date: Fri, 1 Mar 2024 16:41:04 +0100 Subject: [PATCH 3/3] Move the check just before the prototype. NFC. --- clang/test/TableGen/target-builtins-prototype-parser.td | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/test/TableGen/target-builtins-prototype-parser.td b/clang/test/TableGen/target-builtins-prototype-parser.td index 681a607da7e115..97de30d91a9491 100644 --- a/clang/test/TableGen/target-builtins-prototype-parser.td +++ b/clang/test/TableGen/target-builtins-prototype-parser.td @@ -4,11 +4,11 @@ include "clang/Basic/BuiltinsBase.td" def : Builtin { +// CHECK: BUILTIN(__builtin_test_use_clang_extended_vectors, "E8idE4b", "") let Prototype = "ext_vector_type_8_int(double, ext_vector_type_4_bool)"; let Spellings = ["__builtin_test_use_clang_extended_vectors"]; } -// CHECK: BUILTIN(__builtin_test_use_clang_extended_vectors, "E8idE4b", "") #ifdef ERROR_EXPECTED_LANES def : Builtin { _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits