================
@@ -24,20 +24,34 @@
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerHelpers.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
+#include <algorithm>
using namespace clang;
using namespace ento;
using namespace taint;
namespace {
-QualType getSufficientTypeForOverflowOp(CheckerContext &C, const QualType &T) {
- // Calling a builtin with a non-integer type result produces compiler error.
- assert(T->isIntegerType());
+/// \return an integer type that is large enough for the binary operation on
the
+/// operands of \p Arg1Ty and \p Arg2Ty, respectively.
+QualType getSufficientTypeForOverflowOp(CheckerContext &C,
+ BinaryOperator::Opcode Op,
+ QualType Arg1Ty, QualType Arg2Ty) {
+ assert(Arg1Ty->isIntegerType() && Arg2Ty->isIntegerType());
ASTContext &ACtx = C.getASTContext();
- unsigned BitWidth = ACtx.getIntWidth(T);
- return ACtx.getBitIntType(T->isUnsignedIntegerType(), BitWidth * 2);
+ unsigned BitWidth =
+ std::max(ACtx.getIntWidth(Arg1Ty), ACtx.getIntWidth(Arg2Ty));
+
+ // A signed type with doubled bit width may not be enough for their
+ // multiplication result only when both operands are unsigned. In other
+ // words, we use a signed type if either operand is signed. Additionally,
+ // subtraction always needs a signed result. (Subtracting a negative
+ // operand falls into the prior case, so it is still fine with a signed
+ // result type. Excluding the case of 1-bit signed integer tho.)
----------------
ziqingluo-90 wrote:
yeah, that's why we are fine here. 1-bit signed integer is the just a
theoretical possibility.
https://github.com/llvm/llvm-project/pull/214553
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits