[cfe-commits] r142465 - in /cfe/trunk: include/clang/Sema/Sema.h lib/Sema/SemaChecking.cpp lib/Sema/SemaExpr.cpp

Peter Collingbourne peter at pcc.me.uk
Tue Oct 18 17:16:45 PDT 2011


Author: pcc
Date: Tue Oct 18 19:16:45 2011
New Revision: 142465

URL: http://llvm.org/viewvc/llvm-project?rev=142465&view=rev
Log:
Move static array parameter checks to SemaExpr, per Doug's request

Modified:
    cfe/trunk/include/clang/Sema/Sema.h
    cfe/trunk/lib/Sema/SemaChecking.cpp
    cfe/trunk/lib/Sema/SemaExpr.cpp

Modified: cfe/trunk/include/clang/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=142465&r1=142464&r2=142465&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/Sema.h (original)
+++ cfe/trunk/include/clang/Sema/Sema.h Tue Oct 18 19:16:45 2011
@@ -2475,6 +2475,9 @@
                                Expr **Args, unsigned NumArgs,
                                SourceLocation RParenLoc,
                                bool ExecConfig = false);
+  void CheckStaticArrayArgument(SourceLocation CallLoc,
+                                ParmVarDecl *Param,
+                                const Expr *ArgExpr);
 
   /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments.
   /// This provides the location of the left/right parens and a list of comma
@@ -6112,10 +6115,6 @@
                              const Expr * const *ExprArgs,
                              SourceLocation CallSiteLoc);
 
-  void CheckStaticArrayArguments(const FunctionDecl *FDecl,
-                                 const Expr * const *ExprArgs,
-                                 SourceLocation CallSiteLoc);
-
   void CheckPrintfScanfArguments(const CallExpr *TheCall, bool HasVAListArg,
                                  unsigned format_idx, unsigned firstDataArg,
                                  bool isPrintf);

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=142465&r1=142464&r2=142465&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Tue Oct 18 19:16:45 2011
@@ -360,9 +360,6 @@
                           TheCall->getCallee()->getLocStart());
   }
 
-  CheckStaticArrayArguments(FDecl, TheCall->getArgs(),
-                            TheCall->getCallee()->getLocStart());
-
   // Builtin handling
   int CMF = -1;
   switch (FDecl->getBuiltinID()) {
@@ -1365,64 +1362,6 @@
   }
 }
 
-static void DiagnoseCalleeStaticArrayParam(Sema &S, ParmVarDecl *PVD) {
-  TypeLoc TL = PVD->getTypeSourceInfo()->getTypeLoc();
-  if (ArrayTypeLoc *ATL = dyn_cast<ArrayTypeLoc>(&TL))
-    S.Diag(PVD->getLocation(), diag::note_callee_static_array)
-      << ATL->getLocalSourceRange();
-}
-
-/// CheckStaticArrayArguments - Check that each argument corresponding to a
-/// static array parameter is non-null, and that if it is formed by
-/// array-to-pointer decay, the underlying array is sufficiently large.
-///
-/// C99 6.7.5.3p7: If the keyword static also appears within the [ and ] of the
-/// array type derivation, then for each call to the function, the value of the
-/// corresponding actual argument shall provide access to the first element of
-/// an array with at least as many elements as specified by the size expression.
-void
-Sema::CheckStaticArrayArguments(const FunctionDecl *FDecl,
-                                const Expr * const *ExprArgs,
-                                SourceLocation CallSiteLoc) {
-  // Static array parameters are not supported in C++.
-  if (getLangOptions().CPlusPlus)
-    return;
-
-  for (FunctionDecl::param_const_iterator i = FDecl->param_begin(),
-       e = FDecl->param_end(); i != e; ++i, ++ExprArgs) {
-    const Expr *ArgExpr = *ExprArgs;
-    QualType OrigTy = (*i)->getOriginalType();
-
-    const ArrayType *AT = Context.getAsArrayType(OrigTy);
-    if (!AT || AT->getSizeModifier() != ArrayType::Static)
-      continue;
-
-    if (ArgExpr->isNullPointerConstant(Context,
-                                       Expr::NPC_NeverValueDependent)) {
-      Diag(CallSiteLoc, diag::warn_null_arg) << ArgExpr->getSourceRange();
-      DiagnoseCalleeStaticArrayParam(*this, *i);
-      continue;
-    }
-
-    const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT);
-    if (!CAT)
-      continue;
-
-    const ConstantArrayType *ArgCAT =
-      Context.getAsConstantArrayType(ArgExpr->IgnoreParenImpCasts()->getType());
-    if (!ArgCAT)
-      continue;
-
-    if (ArgCAT->getSize().ult(CAT->getSize())) {
-      Diag(CallSiteLoc, diag::warn_static_array_too_small)
-        << ArgExpr->getSourceRange()
-        << (unsigned) ArgCAT->getSize().getZExtValue()
-        << (unsigned) CAT->getSize().getZExtValue();
-      DiagnoseCalleeStaticArrayParam(*this, *i);
-    }
-  }
-}
-
 /// CheckPrintfScanfArguments - Check calls to printf and scanf (and similar
 /// functions) for correct use of format strings.
 void

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=142465&r1=142464&r2=142465&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Tue Oct 18 19:16:45 2011
@@ -3424,6 +3424,7 @@
     QualType ProtoArgType = Proto->getArgType(i);
 
     Expr *Arg;
+    ParmVarDecl *Param;
     if (ArgIx < NumArgs) {
       Arg = Args[ArgIx++];
 
@@ -3434,7 +3435,7 @@
         return true;
 
       // Pass the argument
-      ParmVarDecl *Param = 0;
+      Param = 0;
       if (FDecl && i < FDecl->getNumParams())
         Param = FDecl->getParamDecl(i);
 
@@ -3456,7 +3457,7 @@
 
       Arg = ArgE.takeAs<Expr>();
     } else {
-      ParmVarDecl *Param = FDecl->getParamDecl(i);
+      Param = FDecl->getParamDecl(i);
 
       ExprResult ArgExpr =
         BuildCXXDefaultArgExpr(CallLoc, FDecl, Param);
@@ -3471,6 +3472,9 @@
     // with its own checking, such as a BinaryOperator.
     CheckArrayAccess(Arg);
 
+    // Check for violations of C99 static array rules (C99 6.7.5.3p7).
+    CheckStaticArrayArgument(CallLoc, Param, Arg);
+
     AllArgs.push_back(Arg);
   }
 
@@ -3508,6 +3512,60 @@
   return Invalid;
 }
 
+static void DiagnoseCalleeStaticArrayParam(Sema &S, ParmVarDecl *PVD) {
+  TypeLoc TL = PVD->getTypeSourceInfo()->getTypeLoc();
+  if (ArrayTypeLoc *ATL = dyn_cast<ArrayTypeLoc>(&TL))
+    S.Diag(PVD->getLocation(), diag::note_callee_static_array)
+      << ATL->getLocalSourceRange();
+}
+
+/// CheckStaticArrayArgument - If the given argument corresponds to a static
+/// array parameter, check that it is non-null, and that if it is formed by
+/// array-to-pointer decay, the underlying array is sufficiently large.
+///
+/// C99 6.7.5.3p7: If the keyword static also appears within the [ and ] of the
+/// array type derivation, then for each call to the function, the value of the
+/// corresponding actual argument shall provide access to the first element of
+/// an array with at least as many elements as specified by the size expression.
+void
+Sema::CheckStaticArrayArgument(SourceLocation CallLoc,
+                               ParmVarDecl *Param,
+                               const Expr *ArgExpr) {
+  // Static array parameters are not supported in C++.
+  if (!Param || getLangOptions().CPlusPlus)
+    return;
+
+  QualType OrigTy = Param->getOriginalType();
+
+  const ArrayType *AT = Context.getAsArrayType(OrigTy);
+  if (!AT || AT->getSizeModifier() != ArrayType::Static)
+    return;
+
+  if (ArgExpr->isNullPointerConstant(Context,
+                                     Expr::NPC_NeverValueDependent)) {
+    Diag(CallLoc, diag::warn_null_arg) << ArgExpr->getSourceRange();
+    DiagnoseCalleeStaticArrayParam(*this, Param);
+    return;
+  }
+
+  const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT);
+  if (!CAT)
+    return;
+
+  const ConstantArrayType *ArgCAT =
+    Context.getAsConstantArrayType(ArgExpr->IgnoreParenImpCasts()->getType());
+  if (!ArgCAT)
+    return;
+
+  if (ArgCAT->getSize().ult(CAT->getSize())) {
+    Diag(CallLoc, diag::warn_static_array_too_small)
+      << ArgExpr->getSourceRange()
+      << (unsigned) ArgCAT->getSize().getZExtValue()
+      << (unsigned) CAT->getSize().getZExtValue();
+    DiagnoseCalleeStaticArrayParam(*this, Param);
+  }
+}
+
 /// Given a function expression of unknown-any type, try to rebuild it
 /// to have a function type.
 static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *fn);





More information about the cfe-commits mailing list