Fix a diagnoses-valid in C++20 with variadic macros
authorAaron Ballman <aaron@aaronballman.com>
Sat, 9 Oct 2021 12:20:20 +0000 (08:20 -0400)
committerAaron Ballman <aaron@aaronballman.com>
Sat, 9 Oct 2021 12:20:20 +0000 (08:20 -0400)
C++20 and later allow you to pass no argument for the ... parameter in
a variadic macro, whereas earlier language modes and C disallow it.

We no longer diagnose in C++20 and later modes. This fixes PR51609.

clang/include/clang/Basic/DiagnosticLexKinds.td
clang/lib/Lex/PPMacroExpansion.cpp
clang/test/Preprocessor/empty_va_arg.cpp [new file with mode: 0644]

index 763b7b0..7fbf19e 100644 (file)
@@ -412,6 +412,10 @@ def ext_embedded_directive : Extension<
 def ext_missing_varargs_arg : Extension<
   "must specify at least one argument for '...' parameter of variadic macro">,
   InGroup<GNUZeroVariadicMacroArguments>;
+def warn_cxx17_compat_missing_varargs_arg : Warning<
+  "passing no argument for the '...' parameter of a variadic macro is "
+  "incompatible with C++ standards before C++20">,
+  InGroup<CXXPre20Compat>, DefaultIgnore;
 def ext_empty_fnmacro_arg : Extension<
   "empty macro arguments are a C99 feature">, InGroup<C99>;
 def warn_cxx98_compat_empty_fnmacro_arg : Warning<
index 136f61a..7b5232d 100644 (file)
@@ -988,7 +988,11 @@ MacroArgs *Preprocessor::ReadMacroCallArgumentList(Token &MacroName,
       // If the macro contains the comma pasting extension, the diagnostic
       // is suppressed; we know we'll get another diagnostic later.
       if (!MI->hasCommaPasting()) {
-        Diag(Tok, diag::ext_missing_varargs_arg);
+        // C++20 allows this construct, but standards before C++20 and all C
+        // standards do not allow the construct (we allow it as an extension).
+        Diag(Tok, getLangOpts().CPlusPlus20
+                      ? diag::warn_cxx17_compat_missing_varargs_arg
+                      : diag::ext_missing_varargs_arg);
         Diag(MI->getDefinitionLoc(), diag::note_macro_here)
           << MacroName.getIdentifierInfo();
       }
diff --git a/clang/test/Preprocessor/empty_va_arg.cpp b/clang/test/Preprocessor/empty_va_arg.cpp
new file mode 100644 (file)
index 0000000..2ee431f
--- /dev/null
@@ -0,0 +1,12 @@
+// RUN: %clang_cc1 -Eonly -std=c++17 -pedantic -verify %s
+// RUN: %clang_cc1 -Eonly -std=c17 -pedantic -verify -x c %s
+// RUN: %clang_cc1 -Eonly -std=c++20 -pedantic -Wpre-c++20-compat -verify=compat %s
+
+#define FOO(x, ...) // expected-note {{macro 'FOO' defined here}} \
+                    // compat-note {{macro 'FOO' defined here}}
+
+int main() {
+  FOO(42) // expected-warning {{must specify at least one argument for '...' parameter of variadic macro}} \
+          // compat-warning {{passing no argument for the '...' parameter of a variadic macro is incompatible with C++ standards before C++20}}
+}
+