[libcxx-commits] [libcxx] 515afe8 - [libc++] Change workaround for init_priority((100)) outside of system headers

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Mon Dec 13 11:37:59 PST 2021


Author: Louis Dionne
Date: 2021-12-13T14:37:54-05:00
New Revision: 515afe8b13a659a7a979280ed87891e74cc7915c

URL: https://github.com/llvm/llvm-project/commit/515afe8b13a659a7a979280ed87891e74cc7915c
DIFF: https://github.com/llvm/llvm-project/commit/515afe8b13a659a7a979280ed87891e74cc7915c.diff

LOG: [libc++] Change workaround for init_priority((100)) outside of system headers

We had previously been using a different workaround for pretending that
we were inside a system header, however it had some undesirable effects
on dependency parsing for build systems, as explained in [1].

This patch changes the workaround to use `#pragma GCC system_header`,
which shouldn't suffer from the same issue. Unfortunately, it is a lot
more verbose. The issue is that `#pragma GCC system_header` is ignored
when we are inside a source file, so we have to create a header just for
the sake of using it. IMO this seems like an artificial restriction
without much benefit, but investigating that is a different story.
For now, this should at least solve build system problems at the
cost of some readability.

[1]: https://reviews.llvm.org/D95972#3178968

Differential Revision: https://reviews.llvm.org/D115334

Added: 
    libcxx/src/chrono_system_time_init.h
    libcxx/src/experimental/memory_resource_init_helper.h
    libcxx/src/iostream_init.h

Modified: 
    libcxx/src/chrono.cpp
    libcxx/src/experimental/memory_resource.cpp
    libcxx/src/iostream.cpp

Removed: 
    


################################################################################
diff  --git a/libcxx/src/chrono.cpp b/libcxx/src/chrono.cpp
index 8ff3faf9df6a9..5aa7af75894b8 100644
--- a/libcxx/src/chrono.cpp
+++ b/libcxx/src/chrono.cpp
@@ -80,9 +80,9 @@ class GetSystemTimeInit {
   GetSystemTimeAsFileTimePtr fp;
 };
 
-# 83 "chrono.cpp" 1 3
-GetSystemTimeInit GetSystemTimeAsFileTimeFunc _LIBCPP_INIT_PRIORITY_MAX;
-# 85 "chrono.cpp" 2
+// Pretend we're inside a system header so the compiler doesn't flag the use of the init_priority
+// attribute with a value that's reserved for the implementation (we're the implementation).
+#include "chrono_system_time_init.h"
 } // namespace
 
 #endif

diff  --git a/libcxx/src/chrono_system_time_init.h b/libcxx/src/chrono_system_time_init.h
new file mode 100644
index 0000000000000..3c5a0c33a56aa
--- /dev/null
+++ b/libcxx/src/chrono_system_time_init.h
@@ -0,0 +1,2 @@
+#pragma GCC system_header
+GetSystemTimeInit GetSystemTimeAsFileTimeFunc _LIBCPP_INIT_PRIORITY_MAX;
\ No newline at end of file

diff  --git a/libcxx/src/experimental/memory_resource.cpp b/libcxx/src/experimental/memory_resource.cpp
index be2fb47fad166..29db8b3dee6de 100644
--- a/libcxx/src/experimental/memory_resource.cpp
+++ b/libcxx/src/experimental/memory_resource.cpp
@@ -76,9 +76,9 @@ union ResourceInitHelper {
   ~ResourceInitHelper() {}
 };
 
-# 79 "memory_resource.cpp" 1 3
-_LIBCPP_SAFE_STATIC ResourceInitHelper res_init _LIBCPP_INIT_PRIORITY_MAX;
-# 81 "memory_resource.cpp" 2
+// Pretend we're inside a system header so the compiler doesn't flag the use of the init_priority
+// attribute with a value that's reserved for the implementation (we're the implementation).
+#include "memory_resource_init_helper.h"
 
 } // end namespace
 

diff  --git a/libcxx/src/experimental/memory_resource_init_helper.h b/libcxx/src/experimental/memory_resource_init_helper.h
new file mode 100644
index 0000000000000..2e1cae5ecc604
--- /dev/null
+++ b/libcxx/src/experimental/memory_resource_init_helper.h
@@ -0,0 +1,2 @@
+#pragma GCC system_header
+_LIBCPP_SAFE_STATIC ResourceInitHelper res_init _LIBCPP_INIT_PRIORITY_MAX;
\ No newline at end of file

diff  --git a/libcxx/src/iostream.cpp b/libcxx/src/iostream.cpp
index 6070449621a07..d87bf7af84ba8 100644
--- a/libcxx/src/iostream.cpp
+++ b/libcxx/src/iostream.cpp
@@ -85,12 +85,9 @@ __asm__("?wclog@" _LIBCPP_ABI_NAMESPACE_STR "@std@@3V?$basic_ostream at _WU?$char_t
 ;
 #endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
 
-// Hacky way to make the compiler believe that we're inside a system header so
-// it doesn't flag the use of the init_priority attribute with a value that's
-// reserved for the implementation (we're the implementation).
-# 80 "iostream.cpp" 1 3
-_LIBCPP_HIDDEN ios_base::Init __start_std_streams _LIBCPP_INIT_PRIORITY_MAX;
-# 82 "iostream.cpp" 2
+// Pretend we're inside a system header so the compiler doesn't flag the use of the init_priority
+// attribute with a value that's reserved for the implementation (we're the implementation).
+#include "iostream_init.h"
 
 // On Windows the TLS storage for locales needs to be initialized before we create
 // the standard streams, otherwise it may not be alive during program termination

diff  --git a/libcxx/src/iostream_init.h b/libcxx/src/iostream_init.h
new file mode 100644
index 0000000000000..b0a60f42a67c2
--- /dev/null
+++ b/libcxx/src/iostream_init.h
@@ -0,0 +1,2 @@
+#pragma GCC system_header
+_LIBCPP_HIDDEN ios_base::Init __start_std_streams _LIBCPP_INIT_PRIORITY_MAX;
\ No newline at end of file


        


More information about the libcxx-commits mailing list