From 5549976193e34417d4474a5f4a514268ef6666c7 Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Tue, 17 Jun 2008 02:43:46 +0000 Subject: This patch is motivated by numerous strict-aliasing warnings when compiling clang as a Release build. The big change is that all AST nodes (subclasses of Stmt) whose children are Expr* store their children as Stmt* or arrays of Stmt*. This is to remove strict-aliasing warnings when using StmtIterator. None of the interfaces of any of the classes have changed (except those with arg_iterators, see below), as the accessor methods introduce the needed casts (via cast<>). While this extra casting may seem cumbersome, it actually adds some important sanity checks throughout the codebase, as clients using StmtIterator can potentially overwrite children that are expected to be Expr* with Stmt* (that aren't Expr*). The casts provide extra sanity checks that are operational in debug builds to catch invariant violations such as these. For classes that have arg_iterators (e.g., CallExpr), the definition of arg_iterator has been replaced. Instead of it being Expr**, it is an actual class (called ExprIterator) that wraps a Stmt**, and provides the necessary operators for iteration. The nice thing about this class is that it also uses cast<> to type-checking, which introduces extra sanity checks throughout the codebase that are useful for debugging. A few of the CodeGen functions that use arg_iterator (especially from OverloadExpr) have been modified to take begin and end iterators instead of a base Expr** and the number of arguments. This matches more with the abstraction of iteration. This still needs to be cleaned up a little bit, as clients expect that ExprIterator is a RandomAccessIterator (which we may or may not wish to allow for efficiency of representation). This is a fairly large patch. It passes the tests (except CodeGen/bitfield.c, which was already broken) on both a Debug and Release build, but it should obviously be reviewed. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@52378 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGExpr.cpp | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) (limited to 'lib/CodeGen/CGExpr.cpp') diff --git a/lib/CodeGen/CGExpr.cpp b/lib/CodeGen/CGExpr.cpp index 05a8972a40..5856531f43 100644 --- a/lib/CodeGen/CGExpr.cpp +++ b/lib/CodeGen/CGExpr.cpp @@ -650,13 +650,15 @@ RValue CodeGenFunction::EmitCallExpr(const CallExpr *E) { llvm::Value *Callee = EmitScalarExpr(E->getCallee()); return EmitCallExpr(Callee, E->getCallee()->getType(), - E->arg_begin(), E->getNumArgs()); + E->arg_begin(), E->arg_end()); } -RValue CodeGenFunction::EmitCallExpr(Expr *FnExpr, Expr *const *Args, - unsigned NumArgs) { +RValue CodeGenFunction::EmitCallExpr(Expr *FnExpr, + CallExpr::const_arg_iterator ArgBeg, + CallExpr::const_arg_iterator ArgEnd) { + llvm::Value *Callee = EmitScalarExpr(FnExpr); - return EmitCallExpr(Callee, FnExpr->getType(), Args, NumArgs); + return EmitCallExpr(Callee, FnExpr->getType(), ArgBeg, ArgEnd); } LValue CodeGenFunction::EmitCallExprLValue(const CallExpr *E) { @@ -702,7 +704,9 @@ LValue CodeGenFunction::EmitObjCIvarRefLValue(const ObjCIvarRefExpr *E) { } RValue CodeGenFunction::EmitCallExpr(llvm::Value *Callee, QualType FnType, - Expr *const *ArgExprs, unsigned NumArgs) { + CallExpr::const_arg_iterator ArgBeg, + CallExpr::const_arg_iterator ArgEnd) { + // The callee type will always be a pointer to function type, get the function // type. FnType = cast(FnType.getCanonicalType())->getPointeeType(); @@ -718,20 +722,20 @@ RValue CodeGenFunction::EmitCallExpr(llvm::Value *Callee, QualType FnType, // FIXME: set the stret attribute on the argument. } - for (unsigned i = 0, e = NumArgs; i != e; ++i) { - QualType ArgTy = ArgExprs[i]->getType(); + for (CallExpr::const_arg_iterator I = ArgBeg; I != ArgEnd; ++I) { + QualType ArgTy = I->getType(); if (!hasAggregateLLVMType(ArgTy)) { // Scalar argument is passed by-value. - Args.push_back(EmitScalarExpr(ArgExprs[i])); + Args.push_back(EmitScalarExpr(*I)); } else if (ArgTy->isAnyComplexType()) { // Make a temporary alloca to pass the argument. llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy)); - EmitComplexExprIntoAddr(ArgExprs[i], DestMem, false); + EmitComplexExprIntoAddr(*I, DestMem, false); Args.push_back(DestMem); } else { llvm::Value *DestMem = CreateTempAlloca(ConvertType(ArgTy)); - EmitAggExpr(ArgExprs[i], DestMem, false); + EmitAggExpr(*I, DestMem, false); Args.push_back(DestMem); } } @@ -744,8 +748,10 @@ RValue CodeGenFunction::EmitCallExpr(llvm::Value *Callee, QualType FnType, ParamAttrList.push_back( llvm::ParamAttrsWithIndex::get(1, llvm::ParamAttr::StructRet)); unsigned increment = hasAggregateLLVMType(ResultType) ? 2 : 1; - for (unsigned i = 0; i < NumArgs; i++) { - QualType ParamType = ArgExprs[i]->getType(); + + unsigned i = 0; + for (CallExpr::const_arg_iterator I = ArgBeg; I != ArgEnd; ++I, ++i) { + QualType ParamType = I->getType(); unsigned ParamAttrs = 0; if (ParamType->isRecordType()) ParamAttrs |= llvm::ParamAttr::ByVal; -- cgit v1.2.3