ADT: Fix that APSInt's string constructor claims it requires 5 bits to store a zero
authorRaphael Isemann <teemperor@gmail.com>
Wed, 10 Jun 2020 14:35:42 +0000 (16:35 +0200)
committerRaphael Isemann <teemperor@gmail.com>
Wed, 10 Jun 2020 14:36:33 +0000 (16:36 +0200)
commitf60e319419b848b5789d4713de38df381e4fe7b4
treeca750bd392e373f71b7d0d36b89760c4b64f1faa
parentde522035b9956e918a074b31ba3e2f6baf69706a
ADT: Fix that APSInt's string constructor claims it requires 5 bits to store a zero

Summary:

When constructing an APSInt from a string, the constructor doesn't correctly
truncate the bit width of the result if the passed in string was "0" (or any
alternative way to express 0 like "-0" or "000"). Instead of 1 (which is the
smallest allowed bit width) it returns an APSInt with a bit width of 5.

The reason is that the constructor checks that it never truncates the result to
the invalid bit width of 0, so when it calculates that storing a "0" doesn't
require any bits it just keeps the original overestimated bit width (which
happens to be 5).

This patch just sets the bit width of the result to 1 if the required bit width
is 0.

Reviewers: arphaman, dexonsmith

Reviewed By: dexonsmith

Subscribers: hiraditya, dexonsmith, JDevlieghere, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D81329
llvm/lib/Support/APSInt.cpp
llvm/unittests/ADT/APSIntTest.cpp