[PATCH] D156247: [Clang] Add a warning on uses of coroutine keywords

Ilya Biryukov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 27 08:17:39 PDT 2023


ilya-biryukov updated this revision to Diff 544783.
ilya-biryukov added a comment.
Herald added a project: clang-format.
Herald added reviewers: rymiel, HazardyKnusperkeks, owenpan, MyDeveloperDay.

- Expose -fno-coroutine and add tests

Current implementation exposes:

- `-fno-coroutines` to disable Coroutines in C++20 and beyond,
- `-fcoroutines` to enable Coroutines in prior standards.

TODO: decide if we want to allow `-fcoroutines` in C++17. If so, add tests for codegen. If not, add a warning.
TOOD: release notes.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D156247

Files:
  clang/include/clang/Driver/Options.td
  clang/lib/Basic/IdentifierTable.cpp
  clang/lib/Format/Format.cpp
  clang/lib/Frontend/InitPreprocessor.cpp
  clang/test/SemaCXX/coroutines.cpp
  clang/test/SemaCXX/cxx20-disabled-coroutines.cpp


Index: clang/test/SemaCXX/cxx20-disabled-coroutines.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/cxx20-disabled-coroutines.cpp
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++20 -fno-coroutines %s
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++23 -fno-coroutines %s
+// RUN: %clang_cc1 -fsyntax-only -verify -std=c++26 -fno-coroutines %s
+
+#if __cpp_impl_coroutine
+#else
+// expected-error at +1 {{coroutines disabled}}
+#error "coroutines disabled" 
+#endif
+
+int co_await;
+int co_return;
+int co_yield;
\ No newline at end of file
Index: clang/test/SemaCXX/coroutines.cpp
===================================================================
--- clang/test/SemaCXX/coroutines.cpp
+++ clang/test/SemaCXX/coroutines.cpp
@@ -2,7 +2,8 @@
 // found at http://wg21.link/coroutines.
 
 // RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify=expected,cxx20_23,cxx23    %s -fcxx-exceptions -fexceptions -Wunused-result
-// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify=expected,cxx14_20,cxx20_23 %s -fcxx-exceptions -fexceptions -Wunused-result
+// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify=expected,cxx14_20,cxx20,cxx20_23 %s -fcxx-exceptions -fexceptions -Wunused-result
+// RUN: %clang_cc1 -std=c++17 -fcoroutines -fsyntax-only -verify=expected,cxx14_20 %s -fcxx-exceptions -fexceptions -Wunused-result
 
 void no_coroutine_traits_bad_arg_await() {
   co_await a; // expected-error {{include <coroutine>}}
Index: clang/lib/Frontend/InitPreprocessor.cpp
===================================================================
--- clang/lib/Frontend/InitPreprocessor.cpp
+++ clang/lib/Frontend/InitPreprocessor.cpp
@@ -701,12 +701,14 @@
     Builder.defineMacro("__cpp_consteval", "202211L");
     Builder.defineMacro("__cpp_constexpr_dynamic_alloc", "201907L");
     Builder.defineMacro("__cpp_constinit", "201907L");
-    Builder.defineMacro("__cpp_impl_coroutine", "201902L");
+    // __cpp_impl_coroutine is below as it can be enabled with `-fcoroutines`.
     Builder.defineMacro("__cpp_designated_initializers", "201707L");
     Builder.defineMacro("__cpp_impl_three_way_comparison", "201907L");
     //Builder.defineMacro("__cpp_modules", "201907L");
     Builder.defineMacro("__cpp_using_enum", "201907L");
   }
+  if (LangOpts.Coroutines)
+    Builder.defineMacro("__cpp_impl_coroutine", "201902L");
   // C++23 features.
   if (LangOpts.CPlusPlus23) {
     Builder.defineMacro("__cpp_implicit_move", "202011L");
Index: clang/lib/Format/Format.cpp
===================================================================
--- clang/lib/Format/Format.cpp
+++ clang/lib/Format/Format.cpp
@@ -3803,6 +3803,8 @@
   // the sequence "<::" will be unconditionally treated as "[:".
   // Cf. Lexer::LexTokenInternal.
   LangOpts.Digraphs = LexingStd >= FormatStyle::LS_Cpp11;
+  // Do not miss coroutine keywords.
+  LangOpts.Coroutines =  LexingStd >= FormatStyle::LS_Cpp20;
 
   LangOpts.LineComment = 1;
   bool AlternativeOperators = Style.isCpp();
Index: clang/lib/Basic/IdentifierTable.cpp
===================================================================
--- clang/lib/Basic/IdentifierTable.cpp
+++ clang/lib/Basic/IdentifierTable.cpp
@@ -229,6 +229,8 @@
   if (LangOpts.MSVCCompat && (Flags & KEYNOMS18) &&
       !LangOpts.isCompatibleWithMSVC(LangOptions::MSVC2015))
     return KS_Disabled;
+  if (LangOpts.CPlusPlus20 && !LangOpts.Coroutines && (Flags & KEYCOROUTINES))
+    return KS_Disabled;
 
   KeywordStatus CurStatus = KS_Unknown;
 
Index: clang/include/clang/Driver/Options.td
===================================================================
--- clang/include/clang/Driver/Options.td
+++ clang/include/clang/Driver/Options.td
@@ -1283,8 +1283,8 @@
 // C++ Coroutines
 defm coroutines : BoolFOption<"coroutines",
   LangOpts<"Coroutines">, Default<cpp20.KeyPath>,
-  PosFlag<SetTrue, [CC1Option], "Enable support for the C++ Coroutines">,
-  NegFlag<SetFalse>>;
+  PosFlag<SetTrue, [CoreOption, CC1Option], "Enable support for the C++ Coroutines">,
+  NegFlag<SetFalse, [CoreOption, CC1Option], "Disable support for the C++ Coroutines">>;
 
 defm coro_aligned_allocation : BoolFOption<"coro-aligned-allocation",
   LangOpts<"CoroAlignedAllocation">, DefaultFalse,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156247.544783.patch
Type: text/x-patch
Size: 4265 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230727/7b090a07/attachment.bin>


More information about the cfe-commits mailing list