[cfe-commits] r42917 - in /cfe/trunk: Sema/SemaChecking.cpp include/clang/Basic/DiagnosticKinds.def test/Sema/format-strings.c test/Sema/varargs.c

Anders Carlsson andersca at mac.com
Fri Oct 12 10:48:42 PDT 2007


Author: andersca
Date: Fri Oct 12 12:48:41 2007
New Revision: 42917

URL: http://llvm.org/viewvc/llvm-project?rev=42917&view=rev
Log:
Add some more diagnostics for va_start, fix tests so they pass with these new diags.


Added:
    cfe/trunk/test/Sema/varargs.c
Modified:
    cfe/trunk/Sema/SemaChecking.cpp
    cfe/trunk/include/clang/Basic/DiagnosticKinds.def
    cfe/trunk/test/Sema/format-strings.c

Modified: cfe/trunk/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/SemaChecking.cpp?rev=42917&r1=42916&r2=42917&view=diff

==============================================================================
--- cfe/trunk/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/Sema/SemaChecking.cpp Fri Oct 12 12:48:41 2007
@@ -43,6 +43,37 @@
     assert(NumArgsInCall == 1 &&
            "Wrong number of arguments to builtin CFStringMakeConstantString");
     return CheckBuiltinCFStringArgument(Args[0]);
+  } else if (FnInfo->getBuiltinID() == Builtin::BI__builtin_va_start) {
+    if (NumArgsInCall > 2) {
+      Diag(Args[2]->getLocStart(), 
+           diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
+           SourceRange(Args[2]->getLocStart(),
+                       Args[NumArgsInCall - 1]->getLocEnd()));
+      return true;
+    }
+    
+    FunctionTypeProto* proto = 
+      cast<FunctionTypeProto>(CurFunctionDecl->getType());      
+    if (!proto->isVariadic()) {
+      Diag(Fn->getLocStart(),
+           diag::err_va_start_used_in_non_variadic_function);
+      return true;
+    }
+      
+    bool SecondArgIsLastNamedArgument = false;
+    if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Args[1])) {
+      if (ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) {
+        ParmVarDecl *LastNamedArg = 
+          CurFunctionDecl->getParamDecl(CurFunctionDecl->getNumParams() - 1);
+              
+        if (PV == LastNamedArg)
+          SecondArgIsLastNamedArgument = true;
+      }
+    }
+      
+    if (!SecondArgIsLastNamedArgument)
+      Diag(Args[1]->getLocStart(), 
+           diag::warn_second_parameter_of_va_start_not_last_named_argument);
   }
   
   // Search the KnownFunctionIDs for the identifier.

Modified: cfe/trunk/include/clang/Basic/DiagnosticKinds.def
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticKinds.def?rev=42917&r1=42916&r2=42917&view=diff

==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticKinds.def (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticKinds.def Fri Oct 12 12:48:41 2007
@@ -847,6 +847,10 @@
     "multiple default labels in one switch")
 DIAG(warn_empty_if_body, WARNING,
     "if statement has empty body")
+DIAG(err_va_start_used_in_non_variadic_function, ERROR,
+    "'va_start' used in function with fixed args")
+DIAG(warn_second_parameter_of_va_start_not_last_named_argument, WARNING,
+    "second parameter of 'va_start' not last named argument")
 
 DIAG(warn_return_missing_expr, WARNING,
      "non-void function '%0' should return a value")

Modified: cfe/trunk/test/Sema/format-strings.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/format-strings.c?rev=42917&r1=42916&r2=42917&view=diff

==============================================================================
--- cfe/trunk/test/Sema/format-strings.c (original)
+++ cfe/trunk/test/Sema/format-strings.c Fri Oct 12 12:48:41 2007
@@ -45,7 +45,7 @@
   printf("%\0d",1); // expected-warning {{string contains '\0'}}
 }
 
-void check_empty_format_string(char* buf)
+void check_empty_format_string(char* buf, ...)
 {
   va_list ap;
   va_start(ap,buf);
@@ -53,9 +53,8 @@
   sprintf(buf,""); // expected-warning {{format string is empty}}
 }
 
-void check_wide_string()
+void check_wide_string(char* b, ...)
 {
-  char *b;
   va_list ap;
   va_start(ap,b);
 

Added: cfe/trunk/test/Sema/varargs.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/varargs.c?rev=42917&view=auto

==============================================================================
--- cfe/trunk/test/Sema/varargs.c (added)
+++ cfe/trunk/test/Sema/varargs.c Fri Oct 12 12:48:41 2007
@@ -0,0 +1,18 @@
+// RUN: clang -fsyntax-only -verify %s
+
+void f1(int a)
+{
+    __builtin_va_list ap;
+    
+    __builtin_va_start(ap, a, a); // expected-error {{too many arguments to function}}
+    __builtin_va_start(ap, a); // expected-error {{'va_start' used in function with fixed args}}
+}
+
+void f2(int a, int b, ...)
+{
+    __builtin_va_list ap;
+    
+    __builtin_va_start(ap, 10); // expected-warning {{second parameter of 'va_start' not last named argument}}
+    __builtin_va_start(ap, a); // expected-warning {{second parameter of 'va_start' not last named argument}}
+    __builtin_va_start(ap, b);
+}





More information about the cfe-commits mailing list