diff options
| author | Richard Smith <richard-llvm@metafoo.co.uk> | 2012-06-13 01:07:41 +0000 |
|---|---|---|
| committer | Richard Smith <richard-llvm@metafoo.co.uk> | 2012-06-13 01:07:41 +0000 |
| commit | 25a80d424bb29b177efbc04b369d2b56793dd1da (patch) | |
| tree | c0930ef199ed0fc1efcf0f264a993e6dfd09f70f /clang/lib | |
| parent | a81618db4454645efb05224ef3cffb9ee92fdefd (diff) | |
| download | bcm5719-llvm-25a80d424bb29b177efbc04b369d2b56793dd1da.tar.gz bcm5719-llvm-25a80d424bb29b177efbc04b369d2b56793dd1da.zip | |
Add missing narrowing check: converting from a signed integral type to a wider
unsigned type is narrowing if the source is non-constant or negative.
llvm-svn: 158377
Diffstat (limited to 'clang/lib')
| -rw-r--r-- | clang/lib/Sema/SemaOverload.cpp | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp index 994471ab5c5..d723d45587f 100644 --- a/clang/lib/Sema/SemaOverload.cpp +++ b/clang/lib/Sema/SemaOverload.cpp @@ -389,11 +389,20 @@ StandardConversionSequence::getNarrowingKind(ASTContext &Ctx, const unsigned ToWidth = Ctx.getIntWidth(ToType); if (FromWidth > ToWidth || - (FromWidth == ToWidth && FromSigned != ToSigned)) { + (FromWidth == ToWidth && FromSigned != ToSigned) || + (FromSigned && !ToSigned)) { // Not all values of FromType can be represented in ToType. llvm::APSInt InitializerValue; const Expr *Initializer = IgnoreNarrowingConversion(Converted); - if (Initializer->isIntegerConstantExpr(InitializerValue, Ctx)) { + if (!Initializer->isIntegerConstantExpr(InitializerValue, Ctx)) { + // Such conversions on variables are always narrowing. + return NK_Variable_Narrowing; + } else if (FromWidth < ToWidth) { + // Negative -> unsigned is narrowing. Otherwise, more bits is never + // narrowing. + if (InitializerValue.isSigned() && InitializerValue.isNegative()) + return NK_Constant_Narrowing; + } else { ConstantValue = APValue(InitializerValue); // Add a bit to the InitializerValue so we don't have to worry about @@ -411,9 +420,6 @@ StandardConversionSequence::getNarrowingKind(ASTContext &Ctx, ConstantType = Initializer->getType(); return NK_Constant_Narrowing; } - } else { - // Variables are always narrowings. - return NK_Variable_Narrowing; } } return NK_Not_Narrowing; |

