Index: include/clang/AST/Expr.h =================================================================== --- include/clang/AST/Expr.h +++ include/clang/AST/Expr.h @@ -1535,6 +1535,14 @@ static int mapCharByteWidth(TargetInfo const &target,StringKind k); + template + static CharType *allocateAndCopyString(const ASTContext &C, StringRef Str, + unsigned Len) { + CharType *AStrData = new (C) CharType[Len]; + std::memcpy(AStrData,Str.data(),Len*sizeof(CharType)); + return AStrData; + } + public: /// This is the "fully general" constructor that allows representation of /// strings formed from multiple concatenated tokens. Index: lib/AST/Expr.cpp =================================================================== --- lib/AST/Expr.cpp +++ lib/AST/Expr.cpp @@ -998,21 +998,15 @@ switch(CharByteWidth) { case 1: { - char *AStrData = new (C) char[Length]; - std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData)); - StrData.asChar = AStrData; + StrData.asChar = allocateAndCopyString(C, Str, Length); break; } case 2: { - uint16_t *AStrData = new (C) uint16_t[Length]; - std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData)); - StrData.asUInt16 = AStrData; + StrData.asUInt16 = allocateAndCopyString(C, Str, Length); break; } case 4: { - uint32_t *AStrData = new (C) uint32_t[Length]; - std::memcpy(AStrData,Str.data(),Length*sizeof(*AStrData)); - StrData.asUInt32 = AStrData; + StrData.asUInt32 = allocateAndCopyString(C, Str, Length); break; } default: