[cfe-commits] r82576 - in /cfe/trunk: include/clang/Basic/DiagnosticParseKinds.td lib/Parse/ParseDecl.cpp test/Sema/function.c

Douglas Gregor dgregor at apple.com
Tue Sep 22 14:41:41 PDT 2009


Author: dgregor
Date: Tue Sep 22 16:41:40 2009
New Revision: 82576

URL: http://llvm.org/viewvc/llvm-project?rev=82576&view=rev
Log:
In C++, a variadic function does not need an ellipsis prior to the comma. Parse it in both C and C++, but diagnose it as an error in C with a fix-it hint to add the comma.

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td
    cfe/trunk/lib/Parse/ParseDecl.cpp
    cfe/trunk/test/Sema/function.c

Modified: cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td?rev=82576&r1=82575&r2=82576&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticParseKinds.td Tue Sep 22 16:41:40 2009
@@ -136,6 +136,8 @@
 def err_argument_required_after_attribute : Error<
   "argument required after attribute">;
 def err_missing_param : Error<"expected parameter declarator">;
+def err_missing_comma_before_ellipsis : Error<
+  "C requires a comma prior to the ellipsis in a variadic function type">;
 def err_unexpected_typedef_ident : Error<
   "unexpected type name %0: expected identifier">;
 def err_expected_class_name : Error<"expected class name">;

Modified: cfe/trunk/lib/Parse/ParseDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParseDecl.cpp?rev=82576&r1=82575&r2=82576&view=diff

==============================================================================
--- cfe/trunk/lib/Parse/ParseDecl.cpp (original)
+++ cfe/trunk/lib/Parse/ParseDecl.cpp Tue Sep 22 16:41:40 2009
@@ -2448,6 +2448,7 @@
 ///       parameter-type-list: [C99 6.7.5]
 ///         parameter-list
 ///         parameter-list ',' '...'
+/// [C++]   parameter-list '...'
 ///
 ///       parameter-list: [C99 6.7.5]
 ///         parameter-declaration
@@ -2647,7 +2648,21 @@
     }
 
     // If the next token is a comma, consume it and keep reading arguments.
-    if (Tok.isNot(tok::comma)) break;
+    if (Tok.isNot(tok::comma)) {
+      if (Tok.is(tok::ellipsis)) {
+        IsVariadic = true;
+        EllipsisLoc = ConsumeToken();     // Consume the ellipsis.
+        
+        if (!getLang().CPlusPlus) {
+          // We have ellipsis without a preceding ',', which is ill-formed
+          // in C. Complain and provide the fix.
+          Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
+            << CodeModificationHint::CreateInsertion(EllipsisLoc, ", ");
+        }
+      }
+      
+      break;
+    }
 
     // Consume the comma.
     ConsumeToken();

Modified: cfe/trunk/test/Sema/function.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/function.c?rev=82576&r1=82575&r2=82576&view=diff

==============================================================================
--- cfe/trunk/test/Sema/function.c (original)
+++ cfe/trunk/test/Sema/function.c Tue Sep 22 16:41:40 2009
@@ -87,3 +87,5 @@
   P = P+1;  // no warning.
 }
 
+// missing ',' before '...'
+void t20(int i...) { } // expected-error {{requires a comma}}





More information about the cfe-commits mailing list