diff options
author | Patrik Hägglund <patrik.h.hagglund@ericsson.com> | 2012-11-28 12:13:12 +0000 |
---|---|---|
committer | Patrik Hägglund <patrik.h.hagglund@ericsson.com> | 2012-11-28 12:13:12 +0000 |
commit | 37d5be777c81393c54cae8b26241ebaca2ed9e27 (patch) | |
tree | f280bb536f3573d0285188efb29aaeedc1bf1cf0 /lib/VMCore | |
parent | 1416edc30adbd53b2be863f7f3de56de4a4c9d0a (diff) | |
download | external_llvm-37d5be777c81393c54cae8b26241ebaca2ed9e27.zip external_llvm-37d5be777c81393c54cae8b26241ebaca2ed9e27.tar.gz external_llvm-37d5be777c81393c54cae8b26241ebaca2ed9e27.tar.bz2 |
Add error handling in getInt.
Accordingly, update a testcase with a broken datalayout string.
Also, we never parse negative numbers, because '-' is used as a
separator. Therefore, use unsigned as result type.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@168785 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/VMCore')
-rw-r--r-- | lib/VMCore/DataLayout.cpp | 74 |
1 files changed, 36 insertions, 38 deletions
diff --git a/lib/VMCore/DataLayout.cpp b/lib/VMCore/DataLayout.cpp index e21868b..540f4de 100644 --- a/lib/VMCore/DataLayout.cpp +++ b/lib/VMCore/DataLayout.cpp @@ -152,13 +152,6 @@ DataLayout::InvalidPointerElem = PointerAlignElem::get(~0U, 0U, 0U, 0U); // DataLayout Class Implementation //===----------------------------------------------------------------------===// -/// getInt - Get an integer ignoring errors. -static int getInt(StringRef R) { - int Result = 0; - R.getAsInteger(10, Result); - return Result; -} - void DataLayout::init(StringRef Desc) { initializeDataLayoutPass(*PassRegistry::getPassRegistry()); @@ -186,6 +179,16 @@ void DataLayout::init(StringRef Desc) { (void)errMsg; } +/// Get an unsinged integer, including error checks. +static unsigned getInt(StringRef R) { + if (R.empty()) + return 0; + unsigned Result; + bool error = R.getAsInteger(10, Result); + assert(!error && "not a number, or does not fit in an unsigned int"); + return Result; +} + std::string DataLayout::parseSpecifier(StringRef Desc) { while (!Desc.empty()) { @@ -207,31 +210,31 @@ std::string DataLayout::parseSpecifier(StringRef Desc) { LittleEndian = true; break; case 'p': { - int AddrSpace = 0; + unsigned AddrSpace = 0; if (Specifier.size() > 1) { AddrSpace = getInt(Specifier.substr(1)); - if (AddrSpace < 0 || AddrSpace > (1 << 24)) - return "Invalid address space, must be a positive 24bit integer"; + if (AddrSpace > (1 << 24)) + return "Invalid address space, must be a 24bit integer"; } Split = Token.split(':'); - int PointerMemSizeBits = getInt(Split.first); - if (PointerMemSizeBits < 0 || PointerMemSizeBits % 8 != 0) - return "invalid pointer size, must be a positive 8-bit multiple"; + unsigned PointerMemSizeBits = getInt(Split.first); + if (PointerMemSizeBits % 8 != 0) + return "invalid pointer size, must be an 8-bit multiple"; // Pointer ABI alignment. Split = Split.second.split(':'); - int PointerABIAlignBits = getInt(Split.first); - if (PointerABIAlignBits < 0 || PointerABIAlignBits % 8 != 0) { + unsigned PointerABIAlignBits = getInt(Split.first); + if (PointerABIAlignBits % 8 != 0) { return "invalid pointer ABI alignment, " - "must be a positive 8-bit multiple"; + "must be an 8-bit multiple"; } // Pointer preferred alignment. Split = Split.second.split(':'); - int PointerPrefAlignBits = getInt(Split.first); - if (PointerPrefAlignBits < 0 || PointerPrefAlignBits % 8 != 0) { + unsigned PointerPrefAlignBits = getInt(Split.first); + if (PointerPrefAlignBits % 8 != 0) { return "invalid pointer preferred alignment, " - "must be a positive 8-bit multiple"; + "must be an 8-bit multiple"; } if (PointerPrefAlignBits == 0) @@ -255,26 +258,22 @@ std::string DataLayout::parseSpecifier(StringRef Desc) { case 'a': AlignType = AGGREGATE_ALIGN; break; case 's': AlignType = STACK_ALIGN; break; } - int Size = getInt(Specifier.substr(1)); - if (Size < 0) { - return std::string("invalid ") + field + "-size field, " - "must be positive"; - } + unsigned Size = getInt(Specifier.substr(1)); Split = Token.split(':'); - int ABIAlignBits = getInt(Split.first); - if (ABIAlignBits < 0 || ABIAlignBits % 8 != 0) { + unsigned ABIAlignBits = getInt(Split.first); + if (ABIAlignBits % 8 != 0) { return std::string("invalid ") + field +"-abi-alignment field, " - "must be a positive 8-bit multiple"; + "must be an 8-bit multiple"; } unsigned ABIAlign = ABIAlignBits / 8; Split = Split.second.split(':'); - int PrefAlignBits = getInt(Split.first); - if (PrefAlignBits < 0 || PrefAlignBits % 8 != 0) { + unsigned PrefAlignBits = getInt(Split.first); + if (PrefAlignBits % 8 != 0) { return std::string("invalid ") + field +"-preferred-alignment field, " - "must be a positive 8-bit multiple"; + "must be an 8-bit multiple"; } unsigned PrefAlign = PrefAlignBits / 8; if (PrefAlign == 0) @@ -286,23 +285,22 @@ std::string DataLayout::parseSpecifier(StringRef Desc) { case 'n': // Native integer types. Specifier = Specifier.substr(1); do { - int Width = getInt(Specifier); - if (Width <= 0) { + unsigned Width = getInt(Specifier); + if (Width == 0) { return std::string("invalid native integer size \'") + - Specifier.str() + "\', must be a positive integer."; + Specifier.str() + "\', must be a non-zero integer."; } - if (Width != 0) - LegalIntWidths.push_back(Width); + LegalIntWidths.push_back(Width); Split = Token.split(':'); Specifier = Split.first; Token = Split.second; } while (!Specifier.empty() || !Token.empty()); break; case 'S': { // Stack natural alignment. - int StackNaturalAlignBits = getInt(Specifier.substr(1)); - if (StackNaturalAlignBits < 0 || StackNaturalAlignBits % 8 != 0) { + unsigned StackNaturalAlignBits = getInt(Specifier.substr(1)); + if (StackNaturalAlignBits % 8 != 0) { return "invalid natural stack alignment (S-field), " - "must be a positive 8-bit multiple"; + "must be an 8-bit multiple"; } StackNaturalAlign = StackNaturalAlignBits / 8; break; |