Fix PR1992 by computing the right type for string literals, which
is an array type not a pointer type. This requires updating some
diags that change and updating the code generator to handle the
proper form of strings.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@46941 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/Sema/SemaExpr.cpp b/Sema/SemaExpr.cpp
index 8764a5e..0d48ad7 100644
--- a/Sema/SemaExpr.cpp
+++ b/Sema/SemaExpr.cpp
@@ -42,23 +42,27 @@
llvm::SmallVector<SourceLocation, 4> StringTokLocs;
for (unsigned i = 0; i != NumStringToks; ++i)
StringTokLocs.push_back(StringToks[i].getLocation());
-
- // FIXME: handle wchar_t
- QualType t;
-
- if (Literal.Pascal)
- t = Context.getPointerType(Context.UnsignedCharTy);
- else
- t = Context.getPointerType(Context.CharTy);
-
+
+ // Verify that pascal strings aren't too large.
if (Literal.Pascal && Literal.GetStringLength() > 256)
return Diag(StringToks[0].getLocation(), diag::err_pascal_string_too_long,
SourceRange(StringToks[0].getLocation(),
StringToks[NumStringToks-1].getLocation()));
+ QualType StrTy = Context.CharTy;
+ // FIXME: handle wchar_t
+ if (Literal.Pascal) StrTy = Context.UnsignedCharTy;
+
+ // Get an array type for the string, according to C99 6.4.5. This includes
+ // the nul terminator character as well as the string length for pascal
+ // strings.
+ StrTy = Context.getConstantArrayType(StrTy,
+ llvm::APInt(32, Literal.GetStringLength()+1),
+ ArrayType::Normal, 0);
+
// Pass &StringTokLocs[0], StringTokLocs.size() to factory!
return new StringLiteral(Literal.GetString(), Literal.GetStringLength(),
- Literal.AnyWide, t,
+ Literal.AnyWide, StrTy,
StringToks[0].getLocation(),
StringToks[NumStringToks-1].getLocation());
}