From: Chris Lattner Date: Wed, 18 Feb 2009 06:48:40 +0000 (+0000) Subject: fix the ownership issues and location tracking in X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=39c28bbbf235533e9ae7d06fb9b13371dfcc542d;p=clang fix the ownership issues and location tracking in Sema::ParseObjCStringLiteral. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@64900 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaExprObjC.cpp b/lib/Sema/SemaExprObjC.cpp index 886a3ca1dc..cbcfa056a2 100644 --- a/lib/Sema/SemaExprObjC.cpp +++ b/lib/Sema/SemaExprObjC.cpp @@ -15,45 +15,51 @@ #include "clang/AST/ASTContext.h" #include "clang/AST/DeclObjC.h" #include "clang/AST/ExprObjC.h" +#include "llvm/ADT/SmallString.h" using namespace clang; Sema::ExprResult Sema::ParseObjCStringLiteral(SourceLocation *AtLocs, - ExprTy **Strings, + ExprTy **strings, unsigned NumStrings) { + StringLiteral **Strings = reinterpret_cast(strings); + // Most ObjC strings are formed out of a single piece. However, we *can* // have strings formed out of multiple @ strings with multiple pptokens in // each one, e.g. @"foo" "bar" @"baz" "qux" which need to be turned into one // StringLiteral for ObjCStringLiteral to hold onto. - StringLiteral *S = static_cast(Strings[0]); + StringLiteral *S = Strings[0]; // If we have a multi-part string, merge it all together. if (NumStrings != 1) { // Concatenate objc strings. - StringLiteral* ES = static_cast(Strings[NumStrings-1]); - SourceLocation EndLoc = ES->getSourceRange().getEnd(); - unsigned Length = 0; - for (unsigned i = 0; i < NumStrings; i++) - Length += static_cast(Strings[i])->getByteLength(); + llvm::SmallString<128> StrBuf; + llvm::SmallVector StrLocs; - // FIXME: This should not be allocated by SEMA! - char *strBuf = new char[Length]; - char *p = strBuf; for (unsigned i = 0; i != NumStrings; ++i) { - S = static_cast(Strings[i]); + S = Strings[i]; + + // ObjC strings can't be wide. if (S->isWide()) { Diag(S->getLocStart(), diag::err_cfstring_literal_not_string_constant) << S->getSourceRange(); return true; } - memcpy(p, S->getStrData(), S->getByteLength()); - p += S->getByteLength(); + // Get the string data. + StrBuf.append(S->getStrData(), S->getStrData()+S->getByteLength()); + + // Get the locations of the string tokens. + StrLocs.append(S->tokloc_begin(), S->tokloc_end()); + + // Free the temporary string. S->Destroy(Context); } - // FIXME: PASS LOCATIONS PROPERLY. - S = StringLiteral::Create(Context, strBuf, Length, false, + + // Create the aggregate string with the appropriate content and location + // information. + S = StringLiteral::Create(Context, &StrBuf[0], StrBuf.size(), false, Context.getPointerType(Context.CharTy), - AtLocs[0]); + &StrLocs[0], StrLocs.size()); } // Verify that this composite string is acceptable for ObjC strings.