diff options
author | Chad Rosier <mcrosier@apple.com> | 2012-02-03 02:54:37 +0000 |
---|---|---|
committer | Chad Rosier <mcrosier@apple.com> | 2012-02-03 02:54:37 +0000 |
commit | 96c755d13c58487ecd1a8d0cbbd017364c1725a0 (patch) | |
tree | 285338a75e0716f000464e0eb7701de9371a830c /clang/lib | |
parent | 9bb33f572f7609d469d3a505c9987b83eac5b78c (diff) | |
download | bcm5719-llvm-96c755d13c58487ecd1a8d0cbbd017364c1725a0.tar.gz bcm5719-llvm-96c755d13c58487ecd1a8d0cbbd017364c1725a0.zip |
C++ 5.2.10p2 has a note that mentions that, subject to all other restrictions,
a cast to the same type is allowed so long as it does not cast away constness.
Fix for PR11747. Patch by Aaron Ballman. Reviewed by Eli.
llvm-svn: 149664
Diffstat (limited to 'clang/lib')
-rw-r--r-- | clang/lib/Sema/SemaCast.cpp | 33 |
1 files changed, 21 insertions, 12 deletions
diff --git a/clang/lib/Sema/SemaCast.cpp b/clang/lib/Sema/SemaCast.cpp index 2420424bf2c..545305cfc42 100644 --- a/clang/lib/Sema/SemaCast.cpp +++ b/clang/lib/Sema/SemaCast.cpp @@ -1616,7 +1616,27 @@ static TryCastResult TryReinterpretCast(Sema &Self, ExprResult &SrcExpr, return TC_Failed; } - + + if (SrcType == DestType) { + // C++ 5.2.10p2 has a note that mentions that, subject to all other + // restrictions, a cast to the same type is allowed so long as it does not + // cast away constness. In C++98, the intent was not entirely clear here, + // since all other paragraphs explicitly forbid casts to the same type. + // C++11 clarifies this case with p2. + // + // The only allowed types are: integral, enumeration, pointer, or + // pointer-to-member types. We also won't restrict Obj-C pointers either. + Kind = CK_NoOp; + TryCastResult Result = TC_NotApplicable; + if (SrcType->isIntegralOrEnumerationType() || + SrcType->isAnyPointerType() || + SrcType->isMemberPointerType() || + SrcType->isBlockPointerType()) { + Result = TC_Success; + } + return Result; + } + bool destIsPtr = DestType->isAnyPointerType() || DestType->isBlockPointerType(); bool srcIsPtr = SrcType->isAnyPointerType() || @@ -1627,17 +1647,6 @@ static TryCastResult TryReinterpretCast(Sema &Self, ExprResult &SrcExpr, return TC_NotApplicable; } - if (SrcType == DestType) { - // C++ 5.2.10p2 has a note that mentions that, subject to all other - // restrictions, a cast to the same type is allowed. The intent is not - // entirely clear here, since all other paragraphs explicitly forbid casts - // to the same type. However, the behavior of compilers is pretty consistent - // on this point: allow same-type conversion if the involved types are - // pointers, disallow otherwise. - Kind = CK_NoOp; - return TC_Success; - } - if (DestType->isIntegralType(Self.Context)) { assert(srcIsPtr && "One type must be a pointer"); // C++ 5.2.10p4: A pointer can be explicitly converted to any integral |