11d8db493f86761df9470254a2ad572fc6abf1bf6Jordy Rose//===--- APSIntType.cpp - Simple record of the type of APSInts ------------===//
21d8db493f86761df9470254a2ad572fc6abf1bf6Jordy Rose//
31d8db493f86761df9470254a2ad572fc6abf1bf6Jordy Rose//                     The LLVM Compiler Infrastructure
41d8db493f86761df9470254a2ad572fc6abf1bf6Jordy Rose//
51d8db493f86761df9470254a2ad572fc6abf1bf6Jordy Rose// This file is distributed under the University of Illinois Open Source
61d8db493f86761df9470254a2ad572fc6abf1bf6Jordy Rose// License. See LICENSE.TXT for details.
71d8db493f86761df9470254a2ad572fc6abf1bf6Jordy Rose//
81d8db493f86761df9470254a2ad572fc6abf1bf6Jordy Rose//===----------------------------------------------------------------------===//
91d8db493f86761df9470254a2ad572fc6abf1bf6Jordy Rose
101d8db493f86761df9470254a2ad572fc6abf1bf6Jordy Rose#include "clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h"
111d8db493f86761df9470254a2ad572fc6abf1bf6Jordy Rose
121d8db493f86761df9470254a2ad572fc6abf1bf6Jordy Roseusing namespace clang;
131d8db493f86761df9470254a2ad572fc6abf1bf6Jordy Roseusing namespace ento;
141d8db493f86761df9470254a2ad572fc6abf1bf6Jordy Rose
151d8db493f86761df9470254a2ad572fc6abf1bf6Jordy RoseAPSIntType::RangeTestResultKind
164708b3dde86b06f40927ae9cf30a2de83949a8f2Jordan RoseAPSIntType::testInRange(const llvm::APSInt &Value,
174708b3dde86b06f40927ae9cf30a2de83949a8f2Jordan Rose                        bool AllowSignConversions) const {
184708b3dde86b06f40927ae9cf30a2de83949a8f2Jordan Rose
191d8db493f86761df9470254a2ad572fc6abf1bf6Jordy Rose  // Negative numbers cannot be losslessly converted to unsigned type.
204708b3dde86b06f40927ae9cf30a2de83949a8f2Jordan Rose  if (IsUnsigned && !AllowSignConversions &&
214708b3dde86b06f40927ae9cf30a2de83949a8f2Jordan Rose      Value.isSigned() && Value.isNegative())
221d8db493f86761df9470254a2ad572fc6abf1bf6Jordy Rose    return RTR_Below;
231d8db493f86761df9470254a2ad572fc6abf1bf6Jordy Rose
241d8db493f86761df9470254a2ad572fc6abf1bf6Jordy Rose  unsigned MinBits;
254708b3dde86b06f40927ae9cf30a2de83949a8f2Jordan Rose  if (AllowSignConversions) {
264708b3dde86b06f40927ae9cf30a2de83949a8f2Jordan Rose    if (Value.isSigned() && !IsUnsigned)
274708b3dde86b06f40927ae9cf30a2de83949a8f2Jordan Rose      MinBits = Value.getMinSignedBits();
284708b3dde86b06f40927ae9cf30a2de83949a8f2Jordan Rose    else
294708b3dde86b06f40927ae9cf30a2de83949a8f2Jordan Rose      MinBits = Value.getActiveBits();
304708b3dde86b06f40927ae9cf30a2de83949a8f2Jordan Rose
314708b3dde86b06f40927ae9cf30a2de83949a8f2Jordan Rose  } else {
324708b3dde86b06f40927ae9cf30a2de83949a8f2Jordan Rose    // Signed integers can be converted to signed integers of the same width
334708b3dde86b06f40927ae9cf30a2de83949a8f2Jordan Rose    // or (if positive) unsigned integers with one fewer bit.
344708b3dde86b06f40927ae9cf30a2de83949a8f2Jordan Rose    // Unsigned integers can be converted to unsigned integers of the same width
354708b3dde86b06f40927ae9cf30a2de83949a8f2Jordan Rose    // or signed integers with one more bit.
364708b3dde86b06f40927ae9cf30a2de83949a8f2Jordan Rose    if (Value.isSigned())
374708b3dde86b06f40927ae9cf30a2de83949a8f2Jordan Rose      MinBits = Value.getMinSignedBits() - IsUnsigned;
384708b3dde86b06f40927ae9cf30a2de83949a8f2Jordan Rose    else
394708b3dde86b06f40927ae9cf30a2de83949a8f2Jordan Rose      MinBits = Value.getActiveBits() + !IsUnsigned;
404708b3dde86b06f40927ae9cf30a2de83949a8f2Jordan Rose  }
411d8db493f86761df9470254a2ad572fc6abf1bf6Jordy Rose
421d8db493f86761df9470254a2ad572fc6abf1bf6Jordy Rose  if (MinBits <= BitWidth)
431d8db493f86761df9470254a2ad572fc6abf1bf6Jordy Rose    return RTR_Within;
441d8db493f86761df9470254a2ad572fc6abf1bf6Jordy Rose
451d8db493f86761df9470254a2ad572fc6abf1bf6Jordy Rose  if (Value.isSigned() && Value.isNegative())
461d8db493f86761df9470254a2ad572fc6abf1bf6Jordy Rose    return RTR_Below;
471d8db493f86761df9470254a2ad572fc6abf1bf6Jordy Rose  else
481d8db493f86761df9470254a2ad572fc6abf1bf6Jordy Rose    return RTR_Above;
491d8db493f86761df9470254a2ad572fc6abf1bf6Jordy Rose}
50