[libcxx-commits] [PATCH] D143071: [libc++] Add abi_tag on __exception_guard to avoid ODR violations

Nikolas Klauser via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Wed Feb 1 07:59:11 PST 2023


philnik updated this revision to Diff 493951.
philnik added a comment.

Try to fix CI


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D143071/new/

https://reviews.llvm.org/D143071

Files:
  libcxx/include/__config
  libcxx/include/__utility/exception_guard.h
  libcxx/test/libcxx/utilities/exception_guard.odr.sh.cpp


Index: libcxx/test/libcxx/utilities/exception_guard.odr.sh.cpp
===================================================================
--- /dev/null
+++ libcxx/test/libcxx/utilities/exception_guard.odr.sh.cpp
@@ -0,0 +1,39 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// Make sure that __exception_guard is a different symbol when compiled with -fno-exceptions.
+
+// RUN: %{cxx} %s %{flags} %{compile_flags} -c -o %t.except.o -g -Wno-private-header
+// RUN: %{cxx} %s %{flags} %{compile_flags} -c -o %t.noexcept.o -g -fno-exceptions -Wno-private-header
+// RUN: %{cxx} %{flags} %{link_flags} -o %t.exe %t.except.o %t.noexcept.o
+// RUN: %{run}
+
+#include <__utility/exception_guard.h>
+#include <cassert>
+#include <string>
+#include <typeinfo>
+
+struct Rollback {
+  void operator()() {}
+};
+
+#if defined(__cpp_exceptions) && __cpp_exceptions >= 199711L
+
+const char* func();
+
+int main(int, char**) {
+  assert(std::string(typeid(std::__exception_guard<Rollback>).name()) != func());
+
+  return 0;
+}
+
+#else
+
+const char* func() { return typeid(std::__exception_guard<Rollback>).name(); }
+
+#endif
Index: libcxx/include/__utility/exception_guard.h
===================================================================
--- libcxx/include/__utility/exception_guard.h
+++ libcxx/include/__utility/exception_guard.h
@@ -89,7 +89,7 @@
 };
 #else  // _LIBCPP_NO_EXCEPTIONS
 template <class _Rollback>
-struct __exception_guard {
+struct _LIBCPP_EXCEPTION_ABI_TAG __exception_guard {
   __exception_guard() = delete;
   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_NODEBUG explicit __exception_guard(_Rollback) {}
 
Index: libcxx/include/__config
===================================================================
--- libcxx/include/__config
+++ libcxx/include/__config
@@ -442,6 +442,13 @@
 #    define _LIBCPP_NO_EXCEPTIONS
 #  endif
 
+// Use this tag to avoid ODR violations when combining -fno-exceptions and -fexceptions code.
+#  ifdef _LIBCPP_NO_EXCEPTIONS
+#    define _LIBCPP_EXCEPTION_ABI_TAG __attribute__((__abi_tag__("no_exceptions")))
+#  else
+#    define _LIBCPP_EXCEPTION_ABI_TAG
+#  endif
+
 #  define _LIBCPP_PREFERRED_ALIGNOF(_Tp) __alignof(_Tp)
 
 #  if defined(_LIBCPP_COMPILER_CLANG_BASED)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D143071.493951.patch
Type: text/x-patch
Size: 2568 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20230201/0938dba6/attachment.bin>


More information about the libcxx-commits mailing list