From: Chris Lattner Date: Fri, 20 Jul 2007 18:48:28 +0000 (+0000) Subject: Fix a valgrind error noticed by Benoit Boissinot X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=942cfd37297528918616d06cd6e4e8bd6e4915a2;p=clang Fix a valgrind error noticed by Benoit Boissinot git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@40113 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/AST/ASTContext.cpp b/AST/ASTContext.cpp index 29057b491f..3e1f32ded8 100644 --- a/AST/ASTContext.cpp +++ b/AST/ASTContext.cpp @@ -565,7 +565,7 @@ QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray, // variable size array (for parameter types) at the end of them. FunctionTypeProto *FTP = (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) + - (NumArgs-1)*sizeof(QualType)); + NumArgs*sizeof(QualType)); new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic, Canonical); Types.push_back(FTP); diff --git a/AST/Type.cpp b/AST/Type.cpp index e6aa6c8ae2..4e415e4ab6 100644 --- a/AST/Type.cpp +++ b/AST/Type.cpp @@ -424,7 +424,7 @@ const char *BuiltinType::getName() const { } void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result, - QualType* ArgTys, + arg_type_iterator ArgTys, unsigned NumArgs, bool isVariadic) { ID.AddPointer(Result.getAsOpaquePtr()); for (unsigned i = 0; i != NumArgs; ++i) @@ -433,7 +433,7 @@ void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result, } void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) { - Profile(ID, getResultType(), ArgInfo, NumArgs, isVariadic()); + Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic()); } /// LookThroughTypedefs - Return the ultimate type this typedef corresponds to diff --git a/include/clang/AST/Type.h b/include/clang/AST/Type.h index 1c2d9b6f60..29472b4fe8 100644 --- a/include/clang/AST/Type.h +++ b/include/clang/AST/Type.h @@ -563,6 +563,8 @@ class FunctionTypeProto : public FunctionType, public llvm::FoldingSetNode { bool isVariadic, QualType Canonical) : FunctionType(FunctionProto, Result, isVariadic, Canonical), NumArgs(numArgs) { + // Fill in the trailing argument array. + QualType *ArgInfo = reinterpret_cast(this+1);; for (unsigned i = 0; i != numArgs; ++i) ArgInfo[i] = ArgArray[i]; } @@ -570,23 +572,23 @@ class FunctionTypeProto : public FunctionType, public llvm::FoldingSetNode { /// NumArgs - The number of arguments this function has, not counting '...'. unsigned NumArgs; - /// ArgInfo - This array holds the argument types. Note that this is actually - /// a variable-sized array, so it must be the last instance variable in the - /// class. - QualType ArgInfo[1]; + /// ArgInfo - There is an variable size array after the class in memory that + /// holds the argument types. friend class ASTContext; // ASTContext creates these. public: unsigned getNumArgs() const { return NumArgs; } QualType getArgType(unsigned i) const { assert(i < NumArgs && "Invalid argument number!"); - return ArgInfo[i]; + return arg_type_begin()[i]; } bool isVariadic() const { return getSubClassData(); } typedef const QualType *arg_type_iterator; - arg_type_iterator arg_type_begin() const { return ArgInfo; } - arg_type_iterator arg_type_end() const { return ArgInfo+NumArgs; } + arg_type_iterator arg_type_begin() const { + return reinterpret_cast(this+1); + } + arg_type_iterator arg_type_end() const { return arg_type_begin()+NumArgs; } virtual void getAsStringInternal(std::string &InnerString) const; @@ -597,7 +599,8 @@ public: void Profile(llvm::FoldingSetNodeID &ID); static void Profile(llvm::FoldingSetNodeID &ID, QualType Result, - QualType* ArgTys, unsigned NumArgs, bool isVariadic); + arg_type_iterator ArgTys, unsigned NumArgs, + bool isVariadic); };