[PATCH] D113115: Add diagnostic groups for attribute extensions

Aaron Ballman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 3 08:42:08 PDT 2021


aaron.ballman created this revision.
aaron.ballman added reviewers: rsmith, rjmccall, jyknight, erichkeane.
aaron.ballman requested review of this revision.
Herald added a project: clang.

Some users have a need to control attribute extension diagnostics independent of other extension diagnostics. Consider something like use of [[nodiscard]] within C++11:

  #if __has_cpp_attribute(nodiscard)
  [[nodiscard]] 
  #endif
  int f();

If compiled with -Wc++17-extensions enabled, this will produce `warning: use of the 'nodiscard' attribute is a C++17 extension`. This diagnostic is correct -- using `[[nodiscard]]` in C++11 mode is a C++17 extension. And the behavior of `__has_cpp_attribute(nodiscard)` is also correct -- we support `[[nodiscard]]` in C++11 mode as a conforming extension. But this makes use of `-Werror` or -pedantic-errors` builds more onerous.

This patch adds diagnostic groups for attribute extensions so that users can selectively disable attribute extension diagnostics. I believe this is preferable to requiring users to specify additional flags because it means `-Wc++17-extensions` continues to be the way we enable all C++17-related extension diagnostics. It would be quite easy for someone to use that flag thinking they're protected from some portability issues without realizing it skipped attribute extensions if we went the other way.

This addresses PR33518.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D113115

Files:
  clang/include/clang/Basic/DiagnosticGroups.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/test/SemaCXX/attr-extension-diags.cpp


Index: clang/test/SemaCXX/attr-extension-diags.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/attr-extension-diags.cpp
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -std=c++11 -verify=ext -fsyntax-only -Wfuture-attribute-extensions %s
+// RUN: %clang_cc1 -std=c++11 -verify -fsyntax-only -Wno-future-attribute-extensions %s
+// RUN: %clang_cc1 -std=c++11 -verify -fsyntax-only -Wno-c++14-attribute-extensions -Wno-c++17-attribute-extensions -Wno-c++20-attribute-extensions %s
+
+// expected-no-diagnostics
+
+[[deprecated]] int func1(); // ext-warning {{use of the 'deprecated' attribute is a C++14 extension}}
+[[deprecated("msg")]] int func2(); // ext-warning {{use of the 'deprecated' attribute is a C++14 extension}}
+
+[[nodiscard]] int func3(); // ext-warning {{use of the 'nodiscard' attribute is a C++17 extension}}
+[[nodiscard("msg")]] int func4(); // ext-warning {{use of the 'nodiscard' attribute is a C++20 extension}}
+
+void func5() {
+  if (true) [[likely]]; // ext-warning {{use of the 'likely' attribute is a C++20 extension}}  
+}
Index: clang/include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -8617,11 +8617,11 @@
   InGroup<DiagGroup<"unused-volatile-lvalue">>;
 
 def ext_cxx14_attr : Extension<
-  "use of the %0 attribute is a C++14 extension">, InGroup<CXX14>;
+  "use of the %0 attribute is a C++14 extension">, InGroup<CXX14Attrs>;
 def ext_cxx17_attr : Extension<
-  "use of the %0 attribute is a C++17 extension">, InGroup<CXX17>;
+  "use of the %0 attribute is a C++17 extension">, InGroup<CXX17Attrs>;
 def ext_cxx20_attr : Extension<
-  "use of the %0 attribute is a C++20 extension">, InGroup<CXX20>;
+  "use of the %0 attribute is a C++20 extension">, InGroup<CXX20Attrs>;
 
 def warn_unused_comparison : Warning<
   "%select{equality|inequality|relational|three-way}0 comparison result unused">,
Index: clang/include/clang/Basic/DiagnosticGroups.td
===================================================================
--- clang/include/clang/Basic/DiagnosticGroups.td
+++ clang/include/clang/Basic/DiagnosticGroups.td
@@ -1040,6 +1040,13 @@
 def NonGCC : DiagGroup<"non-gcc",
     [SignCompare, Conversion, LiteralRange]>;
 
+def CXX14Attrs : DiagGroup<"c++14-attribute-extensions">;
+def CXX17Attrs : DiagGroup<"c++17-attribute-extensions">;
+def CXX20Attrs : DiagGroup<"c++20-attribute-extensions">;
+def FutureAttrs : DiagGroup<"future-attribute-extensions", [CXX14Attrs,
+                                                            CXX17Attrs,
+                                                            CXX20Attrs]>;
+
 // A warning group for warnings about using C++11 features as extensions in
 // earlier C++ versions.
 def CXX11 : DiagGroup<"c++11-extensions", [CXX11ExtraSemi, CXX11InlineNamespace,
@@ -1047,15 +1054,15 @@
 
 // A warning group for warnings about using C++14 features as extensions in
 // earlier C++ versions.
-def CXX14 : DiagGroup<"c++14-extensions", [CXX14BinaryLiteral]>;
+def CXX14 : DiagGroup<"c++14-extensions", [CXX14BinaryLiteral, CXX14Attrs]>;
 
 // A warning group for warnings about using C++17 features as extensions in
 // earlier C++ versions.
-def CXX17 : DiagGroup<"c++17-extensions">;
+def CXX17 : DiagGroup<"c++17-extensions", [CXX17Attrs]>;
 
 // A warning group for warnings about using C++20 features as extensions in
 // earlier C++ versions.
-def CXX20 : DiagGroup<"c++20-extensions", [CXX20Designator]>;
+def CXX20 : DiagGroup<"c++20-extensions", [CXX20Designator, CXX20Attrs]>;
 
 // A warning group for warnings about using C++2b features as extensions in
 // earlier C++ versions.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D113115.384457.patch
Type: text/x-patch
Size: 3795 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20211103/58af5e6f/attachment-0001.bin>


More information about the cfe-commits mailing list