diff options
author | Douglas Gregor <dgregor@apple.com> | 2008-09-12 00:47:35 +0000 |
---|---|---|
committer | Douglas Gregor <dgregor@apple.com> | 2008-09-12 00:47:35 +0000 |
commit | aa1e21dcbdf8c34a9aa99c1006c79318932f6793 (patch) | |
tree | 275734a7b3a599bd0716f99e1e0d860de5349ebb /clang/lib/Sema | |
parent | baf6762e2600fcdb9e068451408d2a6a0cd782f7 (diff) | |
download | bcm5719-llvm-aa1e21dcbdf8c34a9aa99c1006c79318932f6793.tar.gz bcm5719-llvm-aa1e21dcbdf8c34a9aa99c1006c79318932f6793.zip |
Give string literals const element typesin C++, and cope with the deprecated C++ conversion from a string literal to a pointer-to-non-const-character
llvm-svn: 56137
Diffstat (limited to 'clang/lib/Sema')
-rw-r--r-- | clang/lib/Sema/Sema.h | 2 | ||||
-rw-r--r-- | clang/lib/Sema/SemaExpr.cpp | 16 | ||||
-rw-r--r-- | clang/lib/Sema/SemaExprCXX.cpp | 31 |
3 files changed, 49 insertions, 0 deletions
diff --git a/clang/lib/Sema/Sema.h b/clang/lib/Sema/Sema.h index f45e49ff018..e562547a8ee 100644 --- a/clang/lib/Sema/Sema.h +++ b/clang/lib/Sema/Sema.h @@ -890,6 +890,8 @@ private: // blcok pointer types. AssignConvertType CheckBlockPointerTypesForAssignment(QualType lhsType, QualType rhsType); + + bool IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType); /// the following "Check" methods will return a valid/converted QualType /// or a null QualType (indicating an error diagnostic was issued). diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index 017e19df6c1..f0765d538d6 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -290,6 +290,10 @@ Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { QualType StrTy = Context.CharTy; if (Literal.AnyWide) StrTy = Context.getWCharType(); if (Literal.Pascal) StrTy = Context.UnsignedCharTy; + + // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). + if (getLangOptions().CPlusPlus) + StrTy.addConst(); // 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 @@ -3066,6 +3070,18 @@ bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, DiagKind = diag::ext_typecheck_convert_pointer_void_func; break; case CompatiblePointerDiscardsQualifiers: + // If the qualifiers lost were because we were applying the + // (deprecated) C++ conversion from a string literal to a char* + // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: + // Ideally, this check would be performed in + // CheckPointerTypesForAssignment. However, that would require a + // bit of refactoring (so that the second argument is an + // expression, rather than a type), which should be done as part + // of a larger effort to fix CheckPointerTypesForAssignment for + // C++ semantics. + if (getLangOptions().CPlusPlus && + IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) + return false; DiagKind = diag::ext_typecheck_convert_discards_qualifiers; break; case IntToBlockPointer: diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp index a7e36c95fcd..c8a21f7d53f 100644 --- a/clang/lib/Sema/SemaExprCXX.cpp +++ b/clang/lib/Sema/SemaExprCXX.cpp @@ -202,3 +202,34 @@ bool Sema::CheckCXXBooleanCondition(Expr *&CondExpr) { Ty.getAsString(), CondExpr->getSourceRange()); return false; } + +/// Helper function to determine whether this is the (deprecated) C++ +/// conversion from a string literal to a pointer to non-const char or +/// non-const wchar_t (for narrow and wide string literals, +/// respectively). +bool +Sema::IsStringLiteralToNonConstPointerConversion(Expr *From, QualType ToType) { + // Look inside the implicit cast, if it exists. + if (ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(From)) + From = Cast->getSubExpr(); + + // A string literal (2.13.4) that is not a wide string literal can + // be converted to an rvalue of type "pointer to char"; a wide + // string literal can be converted to an rvalue of type "pointer + // to wchar_t" (C++ 4.2p2). + if (StringLiteral *StrLit = dyn_cast<StringLiteral>(From)) + if (const PointerType *ToPtrType = ToType->getAsPointerType()) + if (const BuiltinType *ToPointeeType + = ToPtrType->getPointeeType()->getAsBuiltinType()) { + // This conversion is considered only when there is an + // explicit appropriate pointer target type (C++ 4.2p2). + if (ToPtrType->getPointeeType().getCVRQualifiers() == 0 && + ((StrLit->isWide() && ToPointeeType->isWideCharType()) || + (!StrLit->isWide() && + (ToPointeeType->getKind() == BuiltinType::Char_U || + ToPointeeType->getKind() == BuiltinType::Char_S)))) + return true; + } + + return false; +} |