Hans Wennborg | 83e6e1e | 2014-04-30 16:25:02 +0000 | [diff] [blame] | 1 | //===-- StringTableBuilder.cpp - String table building utility ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Rafael Espindola | 97de474 | 2014-07-03 02:01:39 +0000 | [diff] [blame] | 10 | #include "llvm/MC/StringTableBuilder.h" |
Hans Wennborg | 83e6e1e | 2014-04-30 16:25:02 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/SmallVector.h" |
Hans Wennborg | 83e6e1e | 2014-04-30 16:25:02 +0000 | [diff] [blame] | 12 | |
| 13 | using namespace llvm; |
| 14 | |
Hans Wennborg | 83e6e1e | 2014-04-30 16:25:02 +0000 | [diff] [blame] | 15 | void StringTableBuilder::finalize() { |
| 16 | SmallVector<StringRef, 8> Strings; |
| 17 | for (auto i = StringIndexMap.begin(), e = StringIndexMap.end(); i != e; ++i) |
| 18 | Strings.push_back(i->getKey()); |
| 19 | |
Benjamin Kramer | ce246a1 | 2014-09-24 13:19:28 +0000 | [diff] [blame^] | 20 | // Sort the vector so a string is sorted above its suffixes. |
| 21 | std::sort(Strings.begin(), Strings.end(), [](StringRef A, StringRef B) { |
| 22 | typedef std::reverse_iterator<StringRef::iterator> Reverse; |
| 23 | return !std::lexicographical_compare(Reverse(A.end()), Reverse(A.begin()), |
| 24 | Reverse(B.end()), Reverse(B.begin())); |
| 25 | }); |
Hans Wennborg | 83e6e1e | 2014-04-30 16:25:02 +0000 | [diff] [blame] | 26 | |
| 27 | // FIXME: Starting with a null byte is ELF specific. Generalize this so we |
| 28 | // can use the class with other object formats. |
| 29 | StringTable += '\x00'; |
| 30 | |
| 31 | StringRef Previous; |
| 32 | for (StringRef s : Strings) { |
| 33 | if (Previous.endswith(s)) { |
| 34 | StringIndexMap[s] = StringTable.size() - 1 - s.size(); |
| 35 | continue; |
| 36 | } |
| 37 | |
| 38 | StringIndexMap[s] = StringTable.size(); |
| 39 | StringTable += s; |
| 40 | StringTable += '\x00'; |
| 41 | Previous = s; |
| 42 | } |
| 43 | } |