[PATCH] D16503: [MSVC Compat] Warn when suppressing a trailing comma in macro args

Ehsan Akhgari via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 22 19:47:14 PST 2016


ehsan created this revision.
ehsan added a reviewer: thakis.
ehsan added a subscriber: cfe-commits.

MSVC suppresses a trailing comma if no arguments are passed in place of
a variadic argument[1].  This patch adds a -Wmicrosoft warning when
accepting such code.

[1] https://msdn.microsoft.com/en-us/library/ms177415.aspx

http://reviews.llvm.org/D16503

Files:
  include/clang/Basic/DiagnosticGroups.td
  include/clang/Basic/DiagnosticLexKinds.td
  lib/Lex/TokenLexer.cpp
  test/Preprocessor/microsoft-ext.c
  test/Preprocessor/suppressed-comma-msextension.cpp

Index: test/Preprocessor/suppressed-comma-msextension.cpp
===================================================================
--- /dev/null
+++ test/Preprocessor/suppressed-comma-msextension.cpp
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -verify -fms-compatibility -Wmicrosoft %s
+// RUN: %clang_cc1 -P -E -fms-compatibility %s | FileCheck %s
+
+void function(int a);
+#define macro(a, b) function(b - a)
+#define COMMA_ELIDER(...) \
+  macro(x, __VA_ARGS__); \
+  function(x, __VA_ARGS__);
+// expected-warning at -2 {{suppressing trailing comma when not specifying any macro variadic arguments is a Microsoft extension}}
+// expected-warning at -2 {{suppressing trailing comma when not specifying any macro variadic arguments is a Microsoft extension}}
+void func() {
+int x;
+COMMA_ELIDER()
+// CHECK: function( - x); function(x);
+}
Index: test/Preprocessor/microsoft-ext.c
===================================================================
--- test/Preprocessor/microsoft-ext.c
+++ test/Preprocessor/microsoft-ext.c
@@ -34,12 +34,3 @@
 
 MAKE_FUNC(MAK, ER, int a, _COMMA, int b);
 // CHECK: void func(int a , int b) {}
-
-#define macro(a, b) (a - b)
-void function(int a);
-#define COMMA_ELIDER(...) \
-  macro(x, __VA_ARGS__); \
-  function(x, __VA_ARGS__);
-COMMA_ELIDER();
-// CHECK: (x - );
-// CHECK: function(x);
Index: lib/Lex/TokenLexer.cpp
===================================================================
--- lib/Lex/TokenLexer.cpp
+++ lib/Lex/TokenLexer.cpp
@@ -150,6 +150,9 @@
   // Issue an extension diagnostic for the paste operator.
   if (HasPasteOperator)
     PP.Diag(ResultToks.back().getLocation(), diag::ext_paste_comma);
+  else
+    // Issue an extension diagnostic for the suppressed trailing comma.
+    PP.Diag(ResultToks.back().getLocation(), diag::ext_elided_macro_argument_microsoft);
 
   // Remove the comma.
   ResultToks.pop_back();
Index: include/clang/Basic/DiagnosticLexKinds.td
===================================================================
--- include/clang/Basic/DiagnosticLexKinds.td
+++ include/clang/Basic/DiagnosticLexKinds.td
@@ -65,6 +65,10 @@
 def ext_ctrl_z_eof_microsoft : Extension<
   "treating Ctrl-Z as end-of-file is a Microsoft extension">,
   InGroup<MicrosoftEndOfFile>;
+def ext_elided_macro_argument_microsoft: Extension<
+  "suppressing trailing comma when not specifying any macro variadic "
+  "arguments is a Microsoft extension">,
+  InGroup<MicrosoftElidedMacroArgument>;
 
 def ext_token_used : Extension<"extension used">,
   InGroup<DiagGroup<"language-extension-token">>;
Index: include/clang/Basic/DiagnosticGroups.td
===================================================================
--- include/clang/Basic/DiagnosticGroups.td
+++ include/clang/Basic/DiagnosticGroups.td
@@ -769,6 +769,7 @@
 def MicrosoftAnonTag : DiagGroup<"microsoft-anon-tag">;
 def MicrosoftCommentPaste : DiagGroup<"microsoft-comment-paste">;
 def MicrosoftEndOfFile : DiagGroup<"microsoft-end-of-file">;
+def MicrosoftElidedMacroArgument : DiagGroup<"microsoft-elided-macro-argument">;
 // Aliases.
 def : DiagGroup<"msvc-include", [MicrosoftInclude]>;
                 // -Wmsvc-include = -Wmicrosoft-include
@@ -783,7 +784,7 @@
      MicrosoftRedeclareStatic, MicrosoftEnumForwardReference, MicrosoftGoto,
      MicrosoftFlexibleArray, MicrosoftExtraQualification, MicrosoftCast,
      MicrosoftConstInit, MicrosoftVoidPseudoDtor, MicrosoftAnonTag,
-     MicrosoftCommentPaste, MicrosoftEndOfFile]>;
+     MicrosoftCommentPaste, MicrosoftEndOfFile, MicrosoftElidedMacroArgument]>;
 
 def ObjCNonUnifiedException : DiagGroup<"objc-nonunified-exceptions">;
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D16503.45786.patch
Type: text/x-patch
Size: 3606 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160123/85c1b9fd/attachment.bin>


More information about the cfe-commits mailing list