llvm.org GIT mirror llvm / 7413fef
Invert the MC -> Object dependency. Now that we have a lib/MC/MCAnalysis, the dependency was there just because of two helper classes. Move the two over to MC. This will allow IRObjectFile to parse inline assembly. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@212248 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 5 years ago
28 changed file(s) with 364 addition(s) and 411 deletion(s). Raw diff Collapse all Expand all
0 //===-- StringTableBuilder.h - String table building utility ------*- C++ -*-=//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_MC_STRINGTABLE_BUILDER_H
10 #define LLVM_MC_STRINGTABLE_BUILDER_H
11
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/StringMap.h"
14 #include
15
16 namespace llvm {
17
18 /// \brief Utility for building string tables with deduplicated suffixes.
19 class StringTableBuilder {
20 SmallString<256> StringTable;
21 StringMap StringIndexMap;
22
23 public:
24 /// \brief Add a string to the builder. Returns a StringRef to the internal
25 /// copy of s. Can only be used before the table is finalized.
26 StringRef add(StringRef s) {
27 assert(!isFinalized());
28 return StringIndexMap.GetOrCreateValue(s, 0).getKey();
29 }
30
31 /// \brief Analyze the strings and build the final table. No more strings can
32 /// be added after this point.
33 void finalize();
34
35 /// \brief Retrieve the string table data. Can only be used after the table
36 /// is finalized.
37 StringRef data() {
38 assert(isFinalized());
39 return StringTable;
40 }
41
42 /// \brief Get the offest of a string in the string table. Can only be used
43 /// after the table is finalized.
44 size_t getOffset(StringRef s) {
45 assert(isFinalized());
46 assert(StringIndexMap.count(s) && "String is not in table!");
47 return StringIndexMap[s];
48 }
49
50 private:
51 bool isFinalized() {
52 return !StringTable.empty();
53 }
54 };
55
56 } // end llvm namespace
57
58 #endif
0 #ifndef LLVM_MC_YAML_H
1 #define LLVM_MC_YAML_H
2
3 #include "llvm/Support/YAMLTraits.h"
4
5 namespace llvm {
6 namespace yaml {
7 /// \brief Specialized YAMLIO scalar type for representing a binary blob.
8 ///
9 /// A typical use case would be to represent the content of a section in a
10 /// binary file.
11 /// This class has custom YAMLIO traits for convenient reading and writing.
12 /// It renders as a string of hex digits in a YAML file.
13 /// For example, it might render as `DEADBEEFCAFEBABE` (YAML does not
14 /// require the quotation marks, so for simplicity when outputting they are
15 /// omitted).
16 /// When reading, any string whose content is an even number of hex digits
17 /// will be accepted.
18 /// For example, all of the following are acceptable:
19 /// `DEADBEEF`, `"DeADbEeF"`, `"\x44EADBEEF"` (Note: '\x44' == 'D')
20 ///
21 /// A significant advantage of using this class is that it never allocates
22 /// temporary strings or buffers for any of its functionality.
23 ///
24 /// Example:
25 ///
26 /// The YAML mapping:
27 /// \code
28 /// Foo: DEADBEEFCAFEBABE
29 /// \endcode
30 ///
31 /// Could be modeled in YAMLIO by the struct:
32 /// \code
33 /// struct FooHolder {
34 /// BinaryRef Foo;
35 /// };
36 /// namespace llvm {
37 /// namespace yaml {
38 /// template <>
39 /// struct MappingTraits {
40 /// static void mapping(IO &IO, FooHolder &FH) {
41 /// IO.mapRequired("Foo", FH.Foo);
42 /// }
43 /// };
44 /// } // end namespace yaml
45 /// } // end namespace llvm
46 /// \endcode
47 class BinaryRef {
48 friend bool operator==(const BinaryRef &LHS, const BinaryRef &RHS);
49 /// \brief Either raw binary data, or a string of hex bytes (must always
50 /// be an even number of characters).
51 ArrayRef Data;
52 /// \brief Discriminator between the two states of the `Data` member.
53 bool DataIsHexString;
54
55 public:
56 BinaryRef(ArrayRef Data) : Data(Data), DataIsHexString(false) {}
57 BinaryRef(StringRef Data)
58 : Data(reinterpret_cast(Data.data()), Data.size()),
59 DataIsHexString(true) {}
60 BinaryRef() : DataIsHexString(true) {}
61 /// \brief The number of bytes that are represented by this BinaryRef.
62 /// This is the number of bytes that writeAsBinary() will write.
63 ArrayRef::size_type binary_size() const {
64 if (DataIsHexString)
65 return Data.size() / 2;
66 return Data.size();
67 }
68 /// \brief Write the contents (regardless of whether it is binary or a
69 /// hex string) as binary to the given raw_ostream.
70 void writeAsBinary(raw_ostream &OS) const;
71 /// \brief Write the contents (regardless of whether it is binary or a
72 /// hex string) as hex to the given raw_ostream.
73 ///
74 /// For example, a possible output could be `DEADBEEFCAFEBABE`.
75 void writeAsHex(raw_ostream &OS) const;
76 };
77
78 inline bool operator==(const BinaryRef &LHS, const BinaryRef &RHS) {
79 // Special case for default constructed BinaryRef.
80 if (LHS.Data.empty() && RHS.Data.empty())
81 return true;
82
83 return LHS.DataIsHexString == RHS.DataIsHexString && LHS.Data == RHS.Data;
84 }
85
86 template <> struct ScalarTraits {
87 static void output(const BinaryRef &, void *, llvm::raw_ostream &);
88 static StringRef input(StringRef, void *, BinaryRef &);
89 static bool mustQuote(StringRef S) { return needsQuotes(S); }
90 };
91 }
92 }
93 #endif
1414 #define LLVM_OBJECT_COFFYAML_H
1515
1616 #include "llvm/ADT/Optional.h"
17 #include "llvm/Object/YAML.h"
17 #include "llvm/MC/YAML.h"
1818 #include "llvm/Support/COFF.h"
1919
2020 namespace llvm {
4848 struct Section {
4949 COFF::section Header;
5050 unsigned Alignment;
51 object::yaml::BinaryRef SectionData;
51 yaml::BinaryRef SectionData;
5252 std::vector Relocations;
5353 StringRef Name;
5454 Section();
1515 #ifndef LLVM_OBJECT_ELFYAML_H
1616 #define LLVM_OBJECT_ELFYAML_H
1717
18 #include "llvm/Object/YAML.h"
18 #include "llvm/MC/YAML.h"
1919 #include "llvm/Support/ELF.h"
2020
2121 namespace llvm {
8282 virtual ~Section();
8383 };
8484 struct RawContentSection : Section {
85 object::yaml::BinaryRef Content;
85 yaml::BinaryRef Content;
8686 llvm::yaml::Hex64 Size;
8787 RawContentSection() : Section(SectionKind::RawContent) {}
8888 static bool classof(const Section *S) {
+0
-59
include/llvm/Object/StringTableBuilder.h less more
None //===-- StringTableBuilder.h - String table building utility ------*- C++ -*-=//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef LLVM_OBJECT_STRINGTABLE_BUILDER_H
10 #define LLVM_OBJECT_STRINGTABLE_BUILDER_H
11
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/StringMap.h"
14 #include
15
16 namespace llvm {
17
18 /// \brief Utility for building string tables with deduplicated suffixes.
19 class StringTableBuilder {
20 SmallString<256> StringTable;
21 StringMap StringIndexMap;
22
23 public:
24 /// \brief Add a string to the builder. Returns a StringRef to the internal
25 /// copy of s. Can only be used before the table is finalized.
26 StringRef add(StringRef s) {
27 assert(!isFinalized());
28 return StringIndexMap.GetOrCreateValue(s, 0).getKey();
29 }
30
31 /// \brief Analyze the strings and build the final table. No more strings can
32 /// be added after this point.
33 void finalize();
34
35 /// \brief Retrieve the string table data. Can only be used after the table
36 /// is finalized.
37 StringRef data() {
38 assert(isFinalized());
39 return StringTable;
40 }
41
42 /// \brief Get the offest of a string in the string table. Can only be used
43 /// after the table is finalized.
44 size_t getOffset(StringRef s) {
45 assert(isFinalized());
46 assert(StringIndexMap.count(s) && "String is not in table!");
47 return StringIndexMap[s];
48 }
49
50 private:
51 bool isFinalized() {
52 return !StringTable.empty();
53 }
54 };
55
56 } // end llvm namespace
57
58 #endif
+0
-117
include/llvm/Object/YAML.h less more
None //===- YAML.h - YAMLIO utilities for object files ---------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares utility classes for handling the YAML representation of
10 // object files.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_OBJECT_YAML_H
15 #define LLVM_OBJECT_YAML_H
16
17 #include "llvm/Support/YAMLTraits.h"
18
19 namespace llvm {
20 namespace object {
21 namespace yaml {
22
23 /// \brief Specialized YAMLIO scalar type for representing a binary blob.
24 ///
25 /// A typical use case would be to represent the content of a section in a
26 /// binary file.
27 /// This class has custom YAMLIO traits for convenient reading and writing.
28 /// It renders as a string of hex digits in a YAML file.
29 /// For example, it might render as `DEADBEEFCAFEBABE` (YAML does not
30 /// require the quotation marks, so for simplicity when outputting they are
31 /// omitted).
32 /// When reading, any string whose content is an even number of hex digits
33 /// will be accepted.
34 /// For example, all of the following are acceptable:
35 /// `DEADBEEF`, `"DeADbEeF"`, `"\x44EADBEEF"` (Note: '\x44' == 'D')
36 ///
37 /// A significant advantage of using this class is that it never allocates
38 /// temporary strings or buffers for any of its functionality.
39 ///
40 /// Example:
41 ///
42 /// The YAML mapping:
43 /// \code
44 /// Foo: DEADBEEFCAFEBABE
45 /// \endcode
46 ///
47 /// Could be modeled in YAMLIO by the struct:
48 /// \code
49 /// struct FooHolder {
50 /// BinaryRef Foo;
51 /// };
52 /// namespace llvm {
53 /// namespace yaml {
54 /// template <>
55 /// struct MappingTraits {
56 /// static void mapping(IO &IO, FooHolder &FH) {
57 /// IO.mapRequired("Foo", FH.Foo);
58 /// }
59 /// };
60 /// } // end namespace yaml
61 /// } // end namespace llvm
62 /// \endcode
63 class BinaryRef {
64 friend bool operator==(const BinaryRef &LHS, const BinaryRef &RHS);
65 /// \brief Either raw binary data, or a string of hex bytes (must always
66 /// be an even number of characters).
67 ArrayRef Data;
68 /// \brief Discriminator between the two states of the `Data` member.
69 bool DataIsHexString;
70
71 public:
72 BinaryRef(ArrayRef Data) : Data(Data), DataIsHexString(false) {}
73 BinaryRef(StringRef Data)
74 : Data(reinterpret_cast(Data.data()), Data.size()),
75 DataIsHexString(true) {}
76 BinaryRef() : DataIsHexString(true) {}
77 /// \brief The number of bytes that are represented by this BinaryRef.
78 /// This is the number of bytes that writeAsBinary() will write.
79 ArrayRef::size_type binary_size() const {
80 if (DataIsHexString)
81 return Data.size() / 2;
82 return Data.size();
83 }
84 /// \brief Write the contents (regardless of whether it is binary or a
85 /// hex string) as binary to the given raw_ostream.
86 void writeAsBinary(raw_ostream &OS) const;
87 /// \brief Write the contents (regardless of whether it is binary or a
88 /// hex string) as hex to the given raw_ostream.
89 ///
90 /// For example, a possible output could be `DEADBEEFCAFEBABE`.
91 void writeAsHex(raw_ostream &OS) const;
92 };
93
94 inline bool operator==(const BinaryRef &LHS, const BinaryRef &RHS) {
95 // Special case for default constructed BinaryRef.
96 if (LHS.Data.empty() && RHS.Data.empty())
97 return true;
98
99 return LHS.DataIsHexString == RHS.DataIsHexString && LHS.Data == RHS.Data;
100 }
101
102 }
103 }
104
105 namespace yaml {
106 template <> struct ScalarTraits {
107 static void output(const object::yaml::BinaryRef &, void *,
108 llvm::raw_ostream &);
109 static StringRef input(StringRef, void *, object::yaml::BinaryRef &);
110 static bool mustQuote(StringRef S) { return needsQuotes(S); }
111 };
112 }
113
114 }
115
116 #endif
4242 MCValue.cpp
4343 MCWin64EH.cpp
4444 MachObjectWriter.cpp
45 StringTableBuilder.cpp
4546 SubtargetFeature.cpp
4647 WinCOFFObjectWriter.cpp
4748 WinCOFFStreamer.cpp
49 YAML.cpp
4850 )
4951
5052 add_subdirectory(MCAnalysis)
2727 #include "llvm/MC/MCObjectWriter.h"
2828 #include "llvm/MC/MCSectionELF.h"
2929 #include "llvm/MC/MCValue.h"
30 #include "llvm/Object/StringTableBuilder.h"
30 #include "llvm/MC/StringTableBuilder.h"
3131 #include "llvm/Support/Compression.h"
3232 #include "llvm/Support/Debug.h"
3333 #include "llvm/Support/Endian.h"
2121 type = Library
2222 name = MC
2323 parent = Libraries
24 required_libraries = Object Support
24 required_libraries = Support
1616 #include "llvm/MC/MCAnalysis/MCFunction.h"
1717 #include "llvm/MC/MCInstrInfo.h"
1818 #include "llvm/MC/MCRegisterInfo.h"
19 #include "llvm/Object/YAML.h"
19 #include "llvm/MC/YAML.h"
2020 #include "llvm/Support/Allocator.h"
2121 #include "llvm/Support/Casting.h"
2222 #include "llvm/Support/MathExtras.h"
101101 uint64_t Size;
102102
103103 std::vector Insts;
104 object::yaml::BinaryRef Data;
104 yaml::BinaryRef Data;
105105 };
106106
107107 struct BasicBlock {
0 //===-- StringTableBuilder.cpp - String table building utility ------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/MC/StringTableBuilder.h"
10 #include "llvm/ADT/SmallVector.h"
11
12 using namespace llvm;
13
14 static bool compareBySuffix(StringRef a, StringRef b) {
15 size_t sizeA = a.size();
16 size_t sizeB = b.size();
17 size_t len = std::min(sizeA, sizeB);
18 for (size_t i = 0; i < len; ++i) {
19 char ca = a[sizeA - i - 1];
20 char cb = b[sizeB - i - 1];
21 if (ca != cb)
22 return ca > cb;
23 }
24 return sizeA > sizeB;
25 }
26
27 void StringTableBuilder::finalize() {
28 SmallVector Strings;
29 for (auto i = StringIndexMap.begin(), e = StringIndexMap.end(); i != e; ++i)
30 Strings.push_back(i->getKey());
31
32 std::sort(Strings.begin(), Strings.end(), compareBySuffix);
33
34 // FIXME: Starting with a null byte is ELF specific. Generalize this so we
35 // can use the class with other object formats.
36 StringTable += '\x00';
37
38 StringRef Previous;
39 for (StringRef s : Strings) {
40 if (Previous.endswith(s)) {
41 StringIndexMap[s] = StringTable.size() - 1 - s.size();
42 continue;
43 }
44
45 StringIndexMap[s] = StringTable.size();
46 StringTable += s;
47 StringTable += '\x00';
48 Previous = s;
49 }
50 }
0 //===- YAML.cpp - YAMLIO utilities for object files -----------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines utility classes for handling the YAML representation of
10 // object files.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/MC/YAML.h"
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include
18
19 using namespace llvm;
20
21 void yaml::ScalarTraits::output(
22 const yaml::BinaryRef &Val, void *, llvm::raw_ostream &Out) {
23 Val.writeAsHex(Out);
24 }
25
26 StringRef yaml::ScalarTraits::input(StringRef Scalar, void *,
27 yaml::BinaryRef &Val) {
28 if (Scalar.size() % 2 != 0)
29 return "BinaryRef hex string must contain an even number of nybbles.";
30 // TODO: Can we improve YAMLIO to permit a more accurate diagnostic here?
31 // (e.g. a caret pointing to the offending character).
32 for (unsigned I = 0, N = Scalar.size(); I != N; ++I)
33 if (!isxdigit(Scalar[I]))
34 return "BinaryRef hex string must contain only hex digits.";
35 Val = yaml::BinaryRef(Scalar);
36 return StringRef();
37 }
38
39 void yaml::BinaryRef::writeAsBinary(raw_ostream &OS) const {
40 if (!DataIsHexString) {
41 OS.write((const char *)Data.data(), Data.size());
42 return;
43 }
44 for (unsigned I = 0, N = Data.size(); I != N; I += 2) {
45 uint8_t Byte;
46 StringRef((const char *)&Data[I], 2).getAsInteger(16, Byte);
47 OS.write(Byte);
48 }
49 }
50
51 void yaml::BinaryRef::writeAsHex(raw_ostream &OS) const {
52 if (binary_size() == 0)
53 return;
54 if (DataIsHexString) {
55 OS.write((const char *)Data.data(), Data.size());
56 return;
57 }
58 for (ArrayRef::iterator I = Data.begin(), E = Data.end(); I != E;
59 ++I) {
60 uint8_t Byte = *I;
61 OS << hexdigit(Byte >> 4);
62 OS << hexdigit(Byte & 0xf);
63 }
64 }
1111 MachOUniversal.cpp
1212 Object.cpp
1313 ObjectFile.cpp
14 StringTableBuilder.cpp
1514 SymbolicFile.cpp
16 YAML.cpp
1715 )
1818 type = Library
1919 name = Object
2020 parent = Libraries
21 required_libraries = BitReader Core Support
21 required_libraries = BitReader Core Support MC
+0
-51
lib/Object/StringTableBuilder.cpp less more
None //===-- StringTableBuilder.cpp - String table building utility ------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/ADT/SmallVector.h"
10 #include "llvm/Object/StringTableBuilder.h"
11
12 using namespace llvm;
13
14 static bool compareBySuffix(StringRef a, StringRef b) {
15 size_t sizeA = a.size();
16 size_t sizeB = b.size();
17 size_t len = std::min(sizeA, sizeB);
18 for (size_t i = 0; i < len; ++i) {
19 char ca = a[sizeA - i - 1];
20 char cb = b[sizeB - i - 1];
21 if (ca != cb)
22 return ca > cb;
23 }
24 return sizeA > sizeB;
25 }
26
27 void StringTableBuilder::finalize() {
28 SmallVector Strings;
29 for (auto i = StringIndexMap.begin(), e = StringIndexMap.end(); i != e; ++i)
30 Strings.push_back(i->getKey());
31
32 std::sort(Strings.begin(), Strings.end(), compareBySuffix);
33
34 // FIXME: Starting with a null byte is ELF specific. Generalize this so we
35 // can use the class with other object formats.
36 StringTable += '\x00';
37
38 StringRef Previous;
39 for (StringRef s : Strings) {
40 if (Previous.endswith(s)) {
41 StringIndexMap[s] = StringTable.size() - 1 - s.size();
42 continue;
43 }
44
45 StringIndexMap[s] = StringTable.size();
46 StringTable += s;
47 StringTable += '\x00';
48 Previous = s;
49 }
50 }
+0
-66
lib/Object/YAML.cpp less more
None //===- YAML.cpp - YAMLIO utilities for object files -----------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines utility classes for handling the YAML representation of
10 // object files.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Object/YAML.h"
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include
18
19 using namespace llvm;
20 using namespace object::yaml;
21
22 void yaml::ScalarTraits::output(
23 const object::yaml::BinaryRef &Val, void *, llvm::raw_ostream &Out) {
24 Val.writeAsHex(Out);
25 }
26
27 StringRef yaml::ScalarTraits::input(
28 StringRef Scalar, void *, object::yaml::BinaryRef &Val) {
29 if (Scalar.size() % 2 != 0)
30 return "BinaryRef hex string must contain an even number of nybbles.";
31 // TODO: Can we improve YAMLIO to permit a more accurate diagnostic here?
32 // (e.g. a caret pointing to the offending character).
33 for (unsigned I = 0, N = Scalar.size(); I != N; ++I)
34 if (!isxdigit(Scalar[I]))
35 return "BinaryRef hex string must contain only hex digits.";
36 Val = object::yaml::BinaryRef(Scalar);
37 return StringRef();
38 }
39
40 void BinaryRef::writeAsBinary(raw_ostream &OS) const {
41 if (!DataIsHexString) {
42 OS.write((const char *)Data.data(), Data.size());
43 return;
44 }
45 for (unsigned I = 0, N = Data.size(); I != N; I += 2) {
46 uint8_t Byte;
47 StringRef((const char *)&Data[I], 2).getAsInteger(16, Byte);
48 OS.write(Byte);
49 }
50 }
51
52 void BinaryRef::writeAsHex(raw_ostream &OS) const {
53 if (binary_size() == 0)
54 return;
55 if (DataIsHexString) {
56 OS.write((const char *)Data.data(), Data.size());
57 return;
58 }
59 for (ArrayRef::iterator I = Data.begin(), E = Data.end(); I != E;
60 ++I) {
61 uint8_t Byte = *I;
62 OS << hexdigit(Byte >> 4);
63 OS << hexdigit(Byte & 0xf);
64 }
65 }
6060
6161 ArrayRef sectionData;
6262 Obj.getSectionContents(Sect, sectionData);
63 Sec.SectionData = object::yaml::BinaryRef(sectionData);
63 Sec.SectionData = yaml::BinaryRef(sectionData);
6464
6565 std::vector Relocations;
6666 for (const auto &Reloc : Section.relocations()) {
267267 ErrorOr> ContentOrErr = Obj.getSectionContents(Shdr);
268268 if (std::error_code EC = ContentOrErr.getError())
269269 return EC;
270 S->Content = object::yaml::BinaryRef(ContentOrErr.get());
270 S->Content = yaml::BinaryRef(ContentOrErr.get());
271271 S->Size = S->Content.binary_size();
272272
273273 return S.release();
1313
1414 #include "yaml2obj.h"
1515 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/MC/StringTableBuilder.h"
1617 #include "llvm/Object/ELFObjectFile.h"
1718 #include "llvm/Object/ELFYAML.h"
18 #include "llvm/Object/StringTableBuilder.h"
1919 #include "llvm/Support/ELF.h"
2020 #include "llvm/Support/MemoryBuffer.h"
2121 #include "llvm/Support/YAMLTraits.h"
2020 add_subdirectory(LineEditor)
2121 add_subdirectory(Linker)
2222 add_subdirectory(MC)
23 add_subdirectory(Object)
2423 add_subdirectory(Option)
2524 add_subdirectory(Support)
2625 add_subdirectory(Transforms)
33
44 add_llvm_unittest(MCTests
55 MCAtomTest.cpp
6 StringTableBuilderTest.cpp
7 YAMLTest.cpp
68 )
0 //===----------- StringTableBuilderTest.cpp -------------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/MC/StringTableBuilder.h"
10 #include "gtest/gtest.h"
11 #include
12
13 using namespace llvm;
14
15 namespace {
16
17 TEST(StringTableBuilderTest, Basic) {
18 StringTableBuilder B;
19
20 B.add("foo");
21 B.add("bar");
22 B.add("foobar");
23
24 B.finalize();
25
26 std::string Expected;
27 Expected += '\x00';
28 Expected += "foobar";
29 Expected += '\x00';
30 Expected += "foo";
31 Expected += '\x00';
32
33 EXPECT_EQ(Expected, B.data());
34 EXPECT_EQ(1U, B.getOffset("foobar"));
35 EXPECT_EQ(4U, B.getOffset("bar"));
36 EXPECT_EQ(8U, B.getOffset("foo"));
37 }
38
39 }
0 //===- llvm/unittest/Object/YAMLTest.cpp - Tests for Object YAML ----------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/MC/YAML.h"
10 #include "llvm/Support/YAMLTraits.h"
11 #include "gtest/gtest.h"
12
13 using namespace llvm;
14
15 struct BinaryHolder {
16 yaml::BinaryRef Binary;
17 };
18
19 namespace llvm {
20 namespace yaml {
21 template <>
22 struct MappingTraits {
23 static void mapping(IO &IO, BinaryHolder &BH) {
24 IO.mapRequired("Binary", BH.Binary);
25 }
26 };
27 } // end namespace yaml
28 } // end namespace llvm
29
30 TEST(ObjectYAML, BinaryRef) {
31 BinaryHolder BH;
32 SmallVector Buf;
33 llvm::raw_svector_ostream OS(Buf);
34 yaml::Output YOut(OS);
35 YOut << BH;
36 EXPECT_NE(OS.str().find("''"), StringRef::npos);
37 }
99 LEVEL = ..
1010
1111 PARALLEL_DIRS = ADT Analysis Bitcode CodeGen DebugInfo ExecutionEngine IR \
12 LineEditor Linker MC Object Option Support Transforms
12 LineEditor Linker MC Option Support Transforms
1313
1414 include $(LEVEL)/Makefile.config
1515 include $(LLVM_SRC_ROOT)/unittests/Makefile.unittest
+0
-9
unittests/Object/CMakeLists.txt less more
None set(LLVM_LINK_COMPONENTS
1 Object
2 Support
3 )
4
5 add_llvm_unittest(ObjectTests
6 StringTableBuilderTest.cpp
7 YAMLTest.cpp
8 )
+0
-15
unittests/Object/Makefile less more
None ##===- unittests/Object/Makefile ---------------------------*- Makefile -*-===##
1 #
2 # The LLVM Compiler Infrastructure
3 #
4 # This file is distributed under the University of Illinois Open Source
5 # License. See LICENSE.TXT for details.
6 #
7 ##===----------------------------------------------------------------------===##
8
9 LEVEL = ../..
10 TESTNAME = Object
11 LINK_COMPONENTS := object
12
13 include $(LEVEL)/Makefile.config
14 include $(LLVM_SRC_ROOT)/unittests/Makefile.unittest
+0
-40
unittests/Object/StringTableBuilderTest.cpp less more
None //===----------- StringTableBuilderTest.cpp -------------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "gtest/gtest.h"
10 #include "llvm/Object/StringTableBuilder.h"
11 #include
12
13 using namespace llvm;
14
15 namespace {
16
17 TEST(StringTableBuilderTest, Basic) {
18 StringTableBuilder B;
19
20 B.add("foo");
21 B.add("bar");
22 B.add("foobar");
23
24 B.finalize();
25
26 std::string Expected;
27 Expected += '\x00';
28 Expected += "foobar";
29 Expected += '\x00';
30 Expected += "foo";
31 Expected += '\x00';
32
33 EXPECT_EQ(Expected, B.data());
34 EXPECT_EQ(1U, B.getOffset("foobar"));
35 EXPECT_EQ(4U, B.getOffset("bar"));
36 EXPECT_EQ(8U, B.getOffset("foo"));
37 }
38
39 }
+0
-38
unittests/Object/YAMLTest.cpp less more
None //===- llvm/unittest/Object/YAMLTest.cpp - Tests for Object YAML ----------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/Object/YAML.h"
10 #include "llvm/Support/YAMLTraits.h"
11 #include "gtest/gtest.h"
12
13 using namespace llvm;
14
15 struct BinaryHolder {
16 object::yaml::BinaryRef Binary;
17 };
18
19 namespace llvm {
20 namespace yaml {
21 template <>
22 struct MappingTraits {
23 static void mapping(IO &IO, BinaryHolder &BH) {
24 IO.mapRequired("Binary", BH.Binary);
25 }
26 };
27 } // end namespace yaml
28 } // end namespace llvm
29
30 TEST(ObjectYAML, BinaryRef) {
31 BinaryHolder BH;
32 SmallVector Buf;
33 llvm::raw_svector_ostream OS(Buf);
34 yaml::Output YOut(OS);
35 YOut << BH;
36 EXPECT_NE(OS.str().find("''"), StringRef::npos);
37 }