[Clang] Add `-funstable` flag to enable unstable and experimental features
authorEgor Zhdan <e_zhdan@apple.com>
Fri, 18 Feb 2022 21:04:49 +0000 (21:04 +0000)
committerEgor Zhdan <e_zhdan@apple.com>
Tue, 1 Mar 2022 12:35:20 +0000 (12:35 +0000)
This new flag enables `__has_feature(cxx_unstable)` that would replace libc++ macros for individual unstable/experimental features, e.g. `_LIBCPP_HAS_NO_INCOMPLETE_RANGES` or `_LIBCPP_HAS_NO_INCOMPLETE_FORMAT`.

This would make it easier and more convenient to opt-in into all libc++ unstable features at once.

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

clang/include/clang/Basic/Features.def
clang/include/clang/Basic/LangOptions.def
clang/include/clang/Driver/Options.td
clang/lib/Driver/ToolChains/Clang.cpp
clang/test/Driver/unstable-flag.cpp [new file with mode: 0644]
clang/test/Lexer/has_feature_cxx_unstable.cpp [new file with mode: 0644]

index 6ca0e64..522ae59 100644 (file)
@@ -173,6 +173,7 @@ FEATURE(cxx_thread_local,
 FEATURE(cxx_trailing_return, LangOpts.CPlusPlus11)
 FEATURE(cxx_unicode_literals, LangOpts.CPlusPlus11)
 FEATURE(cxx_unrestricted_unions, LangOpts.CPlusPlus11)
+FEATURE(cxx_unstable, LangOpts.Unstable)
 FEATURE(cxx_user_literals, LangOpts.CPlusPlus11)
 FEATURE(cxx_variadic_templates, LangOpts.CPlusPlus11)
 // C++14 features
index 6d98850..3745740 100644 (file)
@@ -150,6 +150,7 @@ LANGOPT(GNUAsm            , 1, 1, "GNU-style inline assembly")
 LANGOPT(Coroutines        , 1, 0, "C++20 coroutines")
 LANGOPT(DllExportInlines  , 1, 1, "dllexported classes dllexport inline methods")
 LANGOPT(RelaxedTemplateTemplateArgs, 1, 0, "C++17 relaxed matching of template template arguments")
+LANGOPT(Unstable          , 1, 0, "Enable unstable and experimental features")
 
 LANGOPT(DoubleSquareBracketAttributes, 1, 0, "'[[]]' attributes extension for all language standard modes")
 
index 60e90f3..24e2069 100644 (file)
@@ -1160,6 +1160,11 @@ defm coroutines_ts : BoolFOption<"coroutines-ts",
   PosFlag<SetTrue, [CC1Option], "Enable support for the C++ Coroutines TS">,
   NegFlag<SetFalse>>;
 
+defm unstable : BoolFOption<"unstable",
+  LangOpts<"Unstable">, DefaultFalse,
+  PosFlag<SetTrue, [CC1Option, CoreOption], "Enable unstable and experimental features">,
+  NegFlag<SetFalse>>;
+
 def fembed_offload_object_EQ : Joined<["-"], "fembed-offload-object=">,
   Group<f_Group>, Flags<[NoXarchOption, CC1Option]>,
   HelpText<"Embed Offloading device-side binary into host object file as a section.">,
index 341e108..14f33a0 100644 (file)
@@ -5764,6 +5764,13 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
     CmdArgs.push_back(A->getValue());
   }
 
+  if (Args.hasArg(options::OPT_funstable)) {
+    CmdArgs.push_back("-funstable");
+    if (!Args.hasArg(options::OPT_fno_coroutines_ts))
+      CmdArgs.push_back("-fcoroutines-ts");
+    CmdArgs.push_back("-fmodules-ts");
+  }
+
   if (Args.hasArg(options::OPT_fexperimental_new_constant_interpreter))
     CmdArgs.push_back("-fexperimental-new-constant-interpreter");
 
diff --git a/clang/test/Driver/unstable-flag.cpp b/clang/test/Driver/unstable-flag.cpp
new file mode 100644 (file)
index 0000000..7e241fa
--- /dev/null
@@ -0,0 +1,5 @@
+// RUN: %clang -funstable -### %s 2>&1 | FileCheck %s
+
+// CHECK: -funstable
+// CHECK: -fcoroutines-ts
+// CHECK: -fmodules-ts
diff --git a/clang/test/Lexer/has_feature_cxx_unstable.cpp b/clang/test/Lexer/has_feature_cxx_unstable.cpp
new file mode 100644 (file)
index 0000000..c4ce45a
--- /dev/null
@@ -0,0 +1,10 @@
+// RUN: %clang_cc1 -funstable -E %s -o - | FileCheck --check-prefix=CHECK-UNSTABLE %s
+// RUN: %clang_cc1 -E %s -o - | FileCheck --check-prefix=CHECK-NO-UNSTABLE %s
+
+#if __has_feature(cxx_unstable)
+int has_cxx_unstable();
+#else
+int has_no_cxx_unstable();
+#endif
+// CHECK-UNSTABLE: int has_cxx_unstable();
+// CHECK-NO-UNSTABLE: int has_no_cxx_unstable();