clang API Documentation
00001 //===--- APSIntType.cpp - Simple record of the type of APSInts ------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 00010 #include "clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h" 00011 00012 using namespace clang; 00013 using namespace ento; 00014 00015 APSIntType::RangeTestResultKind 00016 APSIntType::testInRange(const llvm::APSInt &Value, 00017 bool AllowSignConversions) const { 00018 00019 // Negative numbers cannot be losslessly converted to unsigned type. 00020 if (IsUnsigned && !AllowSignConversions && 00021 Value.isSigned() && Value.isNegative()) 00022 return RTR_Below; 00023 00024 unsigned MinBits; 00025 if (AllowSignConversions) { 00026 if (Value.isSigned() && !IsUnsigned) 00027 MinBits = Value.getMinSignedBits(); 00028 else 00029 MinBits = Value.getActiveBits(); 00030 00031 } else { 00032 // Signed integers can be converted to signed integers of the same width 00033 // or (if positive) unsigned integers with one fewer bit. 00034 // Unsigned integers can be converted to unsigned integers of the same width 00035 // or signed integers with one more bit. 00036 if (Value.isSigned()) 00037 MinBits = Value.getMinSignedBits() - IsUnsigned; 00038 else 00039 MinBits = Value.getActiveBits() + !IsUnsigned; 00040 } 00041 00042 if (MinBits <= BitWidth) 00043 return RTR_Within; 00044 00045 if (Value.isSigned() && Value.isNegative()) 00046 return RTR_Below; 00047 else 00048 return RTR_Above; 00049 }