[cfe-commits] r45234 - in /cfe/trunk/Sema: Sema.h SemaChecking.cpp SemaExpr.cpp

Chris Lattner sabre at nondot.org
Wed Dec 19 15:59:05 PST 2007


Author: lattner
Date: Wed Dec 19 17:59:04 2007
New Revision: 45234

URL: http://llvm.org/viewvc/llvm-project?rev=45234&view=rev
Log:
refactor some code that handles sema of direct function calls.

Modified:
    cfe/trunk/Sema/Sema.h
    cfe/trunk/Sema/SemaChecking.cpp
    cfe/trunk/Sema/SemaExpr.cpp

Modified: cfe/trunk/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/Sema.h?rev=45234&r1=45233&r2=45234&view=diff

==============================================================================
--- cfe/trunk/Sema/Sema.h (original)
+++ cfe/trunk/Sema/Sema.h Wed Dec 19 17:59:04 2007
@@ -714,13 +714,11 @@
   // Extra semantic analysis beyond the C type system
   private:
   
-  bool CheckFunctionCall(Expr *Fn,
-                         SourceLocation LParenLoc, SourceLocation RParenLoc,
+  bool CheckFunctionCall(Expr *Fn, SourceLocation RParenLoc,
                          FunctionDecl *FDecl,
                          Expr** Args, unsigned NumArgsInCall);
 
-  void CheckPrintfArguments(Expr *Fn,
-                            SourceLocation LParenLoc, SourceLocation RParenLoc,
+  void CheckPrintfArguments(Expr *Fn, SourceLocation RParenLoc,
                             bool HasVAListArg, FunctionDecl *FDecl,
                             unsigned format_idx, Expr** Args,
                             unsigned NumArgsInCall);
@@ -730,6 +728,7 @@
 
   
   bool CheckBuiltinCFStringArgument(Expr* Arg);
+  bool SemaBuiltinVAStart(Expr *Fn, Expr** Args, unsigned NumArgs);
   
   void CheckFloatComparison(SourceLocation loc, Expr* lex, Expr* rex);
 };

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

==============================================================================
--- cfe/trunk/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/Sema/SemaChecking.cpp Wed Dec 19 17:59:04 2007
@@ -32,52 +32,20 @@
 /// CheckFunctionCall - Check a direct function call for various correctness
 /// and safety properties not strictly enforced by the C type system.
 bool
-Sema::CheckFunctionCall(Expr *Fn,
-                        SourceLocation LParenLoc, SourceLocation RParenLoc,
+Sema::CheckFunctionCall(Expr *Fn, SourceLocation RParenLoc,
                         FunctionDecl *FDecl,
                         Expr** Args, unsigned NumArgsInCall) {
                         
   // Get the IdentifierInfo* for the called function.
   IdentifierInfo *FnInfo = FDecl->getIdentifier();
   
-  if (FnInfo->getBuiltinID() == 
-      Builtin::BI__builtin___CFStringMakeConstantString) {
+  switch (FnInfo->getBuiltinID()) {
+  case Builtin::BI__builtin___CFStringMakeConstantString:
     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 = CurFunctionDecl ? 
-      cast<FunctionTypeProto>(CurFunctionDecl->getType()) : 
-      cast<FunctionTypeProto>(ObjcGetTypeForMethodDefinition(CurMethodDecl));
-    if (!proto->isVariadic()) {
-      Diag(Fn->getLocStart(),
-           diag::err_va_start_used_in_non_variadic_function);
-      return true;
-    }
-    // FIXME: This isn't correct for methods (results in bogus warning).
-    bool SecondArgIsLastNamedArgument = false;
-    if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Args[1])) {
-      if (ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) {
-        ParmVarDecl *LastNamedArg = CurFunctionDecl ?
-          CurFunctionDecl->getParamDecl(CurFunctionDecl->getNumParams() - 1) :
-          CurMethodDecl->getParamDecl(CurMethodDecl->getNumParams() - 1);
-        
-        if (PV == LastNamedArg)
-          SecondArgIsLastNamedArgument = true;
-      }
-    }
-      
-    if (!SecondArgIsLastNamedArgument)
-      Diag(Args[1]->getLocStart(), 
-           diag::warn_second_parameter_of_va_start_not_last_named_argument);
+  case Builtin::BI__builtin_va_start:
+    return SemaBuiltinVAStart(Fn, Args, NumArgsInCall);
   }
   
   // Search the KnownFunctionIDs for the identifier.
@@ -93,20 +61,20 @@
     bool HasVAListArg = false;
     
     switch (i) {
-      default: assert(false && "No format string argument index.");
-      case id_printf:    format_idx = 0; break;
-      case id_fprintf:   format_idx = 1; break;
-      case id_sprintf:   format_idx = 1; break;
-      case id_snprintf:  format_idx = 2; break;
-      case id_asprintf:  format_idx = 1; break;
-      case id_vsnprintf: format_idx = 2; HasVAListArg = true; break;
-      case id_vasprintf: format_idx = 1; HasVAListArg = true; break;
-      case id_vfprintf:  format_idx = 1; HasVAListArg = true; break;
-      case id_vsprintf:  format_idx = 1; HasVAListArg = true; break;
-      case id_vprintf:   format_idx = 0; HasVAListArg = true; break;
+    default: assert(false && "No format string argument index.");
+    case id_printf:    format_idx = 0; break;
+    case id_fprintf:   format_idx = 1; break;
+    case id_sprintf:   format_idx = 1; break;
+    case id_snprintf:  format_idx = 2; break;
+    case id_asprintf:  format_idx = 1; break;
+    case id_vsnprintf: format_idx = 2; HasVAListArg = true; break;
+    case id_vasprintf: format_idx = 1; HasVAListArg = true; break;
+    case id_vfprintf:  format_idx = 1; HasVAListArg = true; break;
+    case id_vsprintf:  format_idx = 1; HasVAListArg = true; break;
+    case id_vprintf:   format_idx = 0; HasVAListArg = true; break;
     }
     
-    CheckPrintfArguments(Fn, LParenLoc, RParenLoc, HasVAListArg,
+    CheckPrintfArguments(Fn, RParenLoc, HasVAListArg,
 			 FDecl, format_idx, Args, NumArgsInCall);       
   }
   
@@ -157,6 +125,49 @@
   return false;
 }
 
+bool Sema::SemaBuiltinVAStart(Expr *Fn, Expr** Args, unsigned NumArgs) {
+  if (NumArgs > 2) {
+    Diag(Args[2]->getLocStart(), 
+         diag::err_typecheck_call_too_many_args, Fn->getSourceRange(),
+         SourceRange(Args[2]->getLocStart(), Args[NumArgs-1]->getLocEnd()));
+    return true;
+  }
+  
+  FunctionTypeProto *Proto;
+  if (CurFunctionDecl)
+    Proto = cast<FunctionTypeProto>(CurFunctionDecl->getType());
+  else
+    Proto =
+      cast<FunctionTypeProto>(ObjcGetTypeForMethodDefinition(CurMethodDecl));
+  
+  if (!Proto->isVariadic()) {
+    Diag(Fn->getLocStart(), diag::err_va_start_used_in_non_variadic_function);
+    return true;
+  }
+  
+  // Verify that the second argument to the builtin is the last argument of the
+  // current function or method.
+  bool SecondArgIsLastNamedArgument = false;
+  if (DeclRefExpr *DR = dyn_cast<DeclRefExpr>(Args[1])) {
+    if (ParmVarDecl *PV = dyn_cast<ParmVarDecl>(DR->getDecl())) {
+      // FIXME: This isn't correct for methods (results in bogus warning).
+      // Get the last formal in the current function.
+      ParmVarDecl *LastArg;
+      if (CurFunctionDecl)
+        LastArg = *(CurFunctionDecl->param_end()-1);
+      else
+        LastArg = *(CurMethodDecl->param_end()-1);
+      SecondArgIsLastNamedArgument = PV == LastArg;
+    }
+  }
+  
+  if (!SecondArgIsLastNamedArgument)
+    Diag(Args[1]->getLocStart(), 
+         diag::warn_second_parameter_of_va_start_not_last_named_argument);
+  return false;
+}  
+
+
 /// CheckPrintfArguments - Check calls to printf (and similar functions) for
 /// correct use of format strings.  
 ///
@@ -204,8 +215,7 @@
 ///
 /// For now, we ONLY do (1), (3), (5), (6), (7), and (8).
 void
-Sema::CheckPrintfArguments(Expr *Fn, 
-                           SourceLocation LParenLoc, SourceLocation RParenLoc,
+Sema::CheckPrintfArguments(Expr *Fn, SourceLocation RParenLoc,
                            bool HasVAListArg, FunctionDecl *FDecl,
                            unsigned format_idx, Expr** Args, 
                            unsigned NumArgsInCall) {

Modified: cfe/trunk/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/Sema/SemaExpr.cpp?rev=45234&r1=45233&r2=45234&view=diff

==============================================================================
--- cfe/trunk/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/Sema/SemaExpr.cpp Wed Dec 19 17:59:04 2007
@@ -680,8 +680,7 @@
   if (ImplicitCastExpr *IcExpr = dyn_cast<ImplicitCastExpr>(Fn))
     if (DeclRefExpr *DRExpr = dyn_cast<DeclRefExpr>(IcExpr->getSubExpr()))
       if (FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRExpr->getDecl()))
-        if (CheckFunctionCall(Fn, LParenLoc, RParenLoc, FDecl, Args,
-                              NumArgsInCall))
+        if (CheckFunctionCall(Fn, RParenLoc, FDecl, Args, NumArgsInCall))
           return true;
 
   return new CallExpr(Fn, Args, NumArgsInCall, resultType, RParenLoc);





More information about the cfe-commits mailing list