From: Rafael Espindola Date: Thu, 29 Nov 2012 16:09:03 +0000 (+0000) Subject: Merge function types in C. X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=8778c28022933cada30bcb408b4ce83675e29e03;p=platform%2Fupstream%2Fllvm.git Merge function types in C. Among other differences, GCC accepts typedef int IA[]; typedef int A10[10]; static A10 *f(void); static IA *f(void); void g(void) { (void)sizeof(*f()); } but clang used to reject it with: invalid application of 'sizeof' to an incomplete type 'IA' (aka 'int []') The intention of c99's 6.2.7 seems to be that we should use the composite type and accept as gcc does. Doing the type merging required some extra fixes: * Use the type from the function type in initializations, even if an parameter is available. * Fix the merging of the noreturn attribute in function types. * Make CodeGen handle the fact that an parameter type can be different from the corresponding type in the function type. llvm-svn: 168895 --- diff --git a/clang/include/clang/Sema/Initialization.h b/clang/include/clang/Sema/Initialization.h index 0b0af0c..80dbd38 100644 --- a/clang/include/clang/Sema/Initialization.h +++ b/clang/include/clang/Sema/Initialization.h @@ -172,17 +172,25 @@ public: static InitializedEntity InitializeVariable(VarDecl *Var) { return InitializedEntity(Var); } - + /// \brief Create the initialization entity for a parameter. static InitializedEntity InitializeParameter(ASTContext &Context, ParmVarDecl *Parm) { + return InitializeParameter(Context, Parm, + Parm->getType().getUnqualifiedType()); + } + + /// \brief Create the initialization entity for a parameter, but use + /// another type. + static InitializedEntity InitializeParameter(ASTContext &Context, + ParmVarDecl *Parm, + QualType Type) { bool Consumed = (Context.getLangOpts().ObjCAutoRefCount && Parm->hasAttr()); InitializedEntity Entity; Entity.Kind = EK_Parameter; - Entity.Type = Context.getVariableArrayDecayedType( - Parm->getType().getUnqualifiedType()); + Entity.Type = Context.getVariableArrayDecayedType(Type); Entity.Parent = 0; Entity.Parameter = (static_cast(Consumed) | reinterpret_cast(Parm)); diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp index 7fee560..26c2941 100644 --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -6486,14 +6486,14 @@ QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs, if (lbaseInfo.getProducesResult() != rbaseInfo.getProducesResult()) return QualType(); - // functypes which return are preferred over those that do not. - if (lbaseInfo.getNoReturn() && !rbaseInfo.getNoReturn()) - allLTypes = false; - else if (!lbaseInfo.getNoReturn() && rbaseInfo.getNoReturn()) - allRTypes = false; // FIXME: some uses, e.g. conditional exprs, really want this to be 'both'. bool NoReturn = lbaseInfo.getNoReturn() || rbaseInfo.getNoReturn(); + if (lbaseInfo.getNoReturn() != NoReturn) + allLTypes = false; + if (rbaseInfo.getNoReturn() != NoReturn) + allRTypes = false; + FunctionType::ExtInfo einfo = lbaseInfo.withNoReturn(NoReturn); if (lproto && rproto) { // two C99 style function prototypes diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp index 54ccb86..3b39d2e 100644 --- a/clang/lib/CodeGen/CGCall.cpp +++ b/clang/lib/CodeGen/CGCall.cpp @@ -1232,7 +1232,15 @@ void CodeGenFunction::EmitFunctionProlog(const CGFunctionInfo &FI, if (isPromoted) V = emitArgumentDemotion(*this, Arg, V); - + + // Because of merging of function types from multiple decls it is + // possible for the type of an argument to not match the corresponding + // type in the function type. Since we are codegening the callee + // in here, add a cast to the argument type. + llvm::Type *LTy = ConvertType(Arg->getType()); + if (V->getType() != LTy) + V = Builder.CreateBitCast(V, LTy); + EmitParmDecl(*Arg, V, ArgNo); break; } diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index eb75329..001a6ac 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -2402,6 +2402,12 @@ bool Sema::MergeCompatibleFunctionDecls(FunctionDecl *New, FunctionDecl *Old, if (getLangOpts().CPlusPlus) return MergeCXXFunctionDecl(New, Old, S); + // Merge the function types so the we get the composite types for the return + // and argument types. + QualType Merged = Context.mergeTypes(Old->getType(), New->getType()); + if (!Merged.isNull()) + New->setType(Merged); + return false; } diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index dd1bc0b..f6defda 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -3730,10 +3730,10 @@ bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, (!Param || !Param->hasAttr())) Arg = stripARCUnbridgedCast(Arg); - InitializedEntity Entity = - Param? InitializedEntity::InitializeParameter(Context, Param) - : InitializedEntity::InitializeParameter(Context, ProtoArgType, - Proto->isArgConsumed(i)); + InitializedEntity Entity = Param ? + InitializedEntity::InitializeParameter(Context, Param, ProtoArgType) + : InitializedEntity::InitializeParameter(Context, ProtoArgType, + Proto->isArgConsumed(i)); ExprResult ArgE = PerformCopyInitialization(Entity, SourceLocation(), Owned(Arg), diff --git a/clang/test/Sema/merge-decls.c b/clang/test/Sema/merge-decls.c index da3e245..29707d2 100644 --- a/clang/test/Sema/merge-decls.c +++ b/clang/test/Sema/merge-decls.c @@ -48,3 +48,46 @@ void test1_g(void) } (void)sizeof(*test1_f()); } + +typedef int test2_IA[]; +typedef int test2_A10[10]; + +static test2_A10 *test2_f(void); +static test2_IA *test2_f(void); + +void test2_g(void) +{ + (void)sizeof(*test2_f()); +} + +int (*test3_f())[10]; +int (*test3_f())[]; +int test3_k = sizeof(*test3_f()); + +void test4_f(int); +void test4_f(a) + char a; +{ + int v[sizeof(a) == 1 ? 1 : -1]; +} + +int test5_f(int (*)[10]); +int test5_f(int (*x)[]) { + return sizeof(*x); // expected-error {{invalid application of 'sizeof' to an incomplete type 'int []'}} +} + +void test6_f(int (*a)[11]); +void test6_f(a) + int (*a)[]; +{} +void test6_g() { + int arr[10]; + test6_f(&arr); // expected-warning {{incompatible pointer types passing 'int (*)[10]' to parameter of type 'int (*)[11]}} +} + +void test7_f(int (*)[10]); +void test7_f(int (*)[]); // expected-note {{passing argument to parameter here}} +void test7_g() { + int x[5]; + test7_f(&x); // expected-warning {{incompatible pointer types passing 'int (*)[5]' to parameter of type 'int (*)[10]}} +}