llvm.org GIT mirror llvm / 5263d0a
Move some code out of line. No functionality change. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@185901 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 6 years ago
2 changed file(s) with 76 addition(s) and 65 deletion(s). Raw diff Collapse all Expand all
1313 #ifndef LLVM_OBJECT_ARCHIVE_H
1414 #define LLVM_OBJECT_ARCHIVE_H
1515
16 #include "llvm/ADT/SmallString.h"
1716 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/Twine.h"
1917 #include "llvm/Object/Binary.h"
20 #include "llvm/Support/DataTypes.h"
2118 #include "llvm/Support/ErrorHandling.h"
2219 #include "llvm/Support/MemoryBuffer.h"
2320
3229 char Size[10]; ///< Size of data, not including header or padding.
3330 char Terminator[2];
3431
35 ///! Get the name without looking up long names.
36 llvm::StringRef getName() const {
37 char EndCond;
38 if (Name[0] == '/' || Name[0] == '#')
39 EndCond = ' ';
40 else
41 EndCond = '/';
42 llvm::StringRef::size_type end =
43 llvm::StringRef(Name, sizeof(Name)).find(EndCond);
44 if (end == llvm::StringRef::npos)
45 end = sizeof(Name);
46 assert(end <= sizeof(Name) && end > 0);
47 // Don't include the EndCond if there is one.
48 return llvm::StringRef(Name, end);
49 }
32 /// Get the name without looking up long names.
33 llvm::StringRef getName() const;
5034
51 uint64_t getSize() const {
52 uint64_t ret;
53 if (llvm::StringRef(Size, sizeof(Size)).rtrim(" ").getAsInteger(10, ret))
54 llvm_unreachable("Size is not an integer.");
55 return ret;
56 }
35 uint64_t getSize() const;
5736 };
5837
5938 static const ArchiveMemberHeader *ToHeader(const char *base) {
7150 uint16_t StartOfFile;
7251
7352 public:
74 Child(const Archive *p, StringRef d) : Parent(p), Data(d) {
75 if (!p || d.empty())
76 return;
77 // Setup StartOfFile and PaddingBytes.
78 StartOfFile = sizeof(ArchiveMemberHeader);
79 // Don't include attached name.
80 StringRef Name = ToHeader(Data.data())->getName();
81 if (Name.startswith("#1/")) {
82 uint64_t NameSize;
83 if (Name.substr(3).rtrim(" ").getAsInteger(10, NameSize))
84 llvm_unreachable("Long name length is not an integer");
85 StartOfFile += NameSize;
86 }
87 }
53 Child(const Archive *p, StringRef d);
8854
8955 bool operator ==(const Child &other) const {
9056 return (Parent == other.Parent) && (Data.begin() == other.Data.begin());
9460 return Data.begin() < other.Data.begin();
9561 }
9662
97 Child getNext() const {
98 size_t SpaceToSkip = Data.size();
99 // If it's odd, add 1 to make it even.
100 if (SpaceToSkip & 1)
101 ++SpaceToSkip;
102
103 const char *NextLoc = Data.data() + SpaceToSkip;
104
105 // Check to see if this is past the end of the archive.
106 if (NextLoc >= Parent->Data->getBufferEnd())
107 return Child(Parent, StringRef(0, 0));
108
109 size_t NextSize =
110 sizeof(ArchiveMemberHeader) + ToHeader(NextLoc)->getSize();
111
112 return Child(Parent, StringRef(NextLoc, NextSize));
113 }
63 Child getNext() const;
11464
11565 error_code getName(StringRef &Result) const;
11666 StringRef getRawName() const { return ToHeader(Data.data())->getName(); }
12676 }
12777
12878 error_code getMemoryBuffer(OwningPtr &Result,
129 bool FullPath = false) const {
130 StringRef Name;
131 if (error_code ec = getName(Name))
132 return ec;
133 SmallString<128> Path;
134 Result.reset(MemoryBuffer::getMemBuffer(
135 getBuffer(), FullPath ? (Twine(Parent->getFileName()) + "(" + Name +
136 ")").toStringRef(Path) : Name, false));
137 return error_code::success();
138 }
79 bool FullPath = false) const;
13980
14081 error_code getAsBinary(OwningPtr &Result) const;
14182 };
1212
1313 #include "llvm/Object/Archive.h"
1414 #include "llvm/ADT/APInt.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/Twine.h"
1517 #include "llvm/Support/Endian.h"
1618 #include "llvm/Support/MemoryBuffer.h"
1719
3537 }
3638
3739 void Archive::anchor() { }
40
41 StringRef ArchiveMemberHeader::getName() const {
42 char EndCond;
43 if (Name[0] == '/' || Name[0] == '#')
44 EndCond = ' ';
45 else
46 EndCond = '/';
47 llvm::StringRef::size_type end =
48 llvm::StringRef(Name, sizeof(Name)).find(EndCond);
49 if (end == llvm::StringRef::npos)
50 end = sizeof(Name);
51 assert(end <= sizeof(Name) && end > 0);
52 // Don't include the EndCond if there is one.
53 return llvm::StringRef(Name, end);
54 }
55
56 uint64_t ArchiveMemberHeader::getSize() const {
57 uint64_t ret;
58 if (llvm::StringRef(Size, sizeof(Size)).rtrim(" ").getAsInteger(10, ret))
59 llvm_unreachable("Size is not an integer.");
60 return ret;
61 }
62
63 Archive::Child::Child(const Archive *p, StringRef d) : Parent(p), Data(d) {
64 if (!p || d.empty())
65 return;
66 // Setup StartOfFile and PaddingBytes.
67 StartOfFile = sizeof(ArchiveMemberHeader);
68 // Don't include attached name.
69 StringRef Name = ToHeader(Data.data())->getName();
70 if (Name.startswith("#1/")) {
71 uint64_t NameSize;
72 if (Name.substr(3).rtrim(" ").getAsInteger(10, NameSize))
73 llvm_unreachable("Long name length is not an integer");
74 StartOfFile += NameSize;
75 }
76 }
77
78 Archive::Child Archive::Child::getNext() const {
79 size_t SpaceToSkip = Data.size();
80 // If it's odd, add 1 to make it even.
81 if (SpaceToSkip & 1)
82 ++SpaceToSkip;
83
84 const char *NextLoc = Data.data() + SpaceToSkip;
85
86 // Check to see if this is past the end of the archive.
87 if (NextLoc >= Parent->Data->getBufferEnd())
88 return Child(Parent, StringRef(0, 0));
89
90 size_t NextSize = sizeof(ArchiveMemberHeader) + ToHeader(NextLoc)->getSize();
91
92 return Child(Parent, StringRef(NextLoc, NextSize));
93 }
3894
3995 error_code Archive::Child::getName(StringRef &Result) const {
4096 StringRef name = getRawName();
86142 else
87143 Result = name;
88144 return object_error::success;
145 }
146
147 error_code Archive::Child::getMemoryBuffer(OwningPtr &Result,
148 bool FullPath) const {
149 StringRef Name;
150 if (error_code ec = getName(Name))
151 return ec;
152 SmallString<128> Path;
153 Result.reset(MemoryBuffer::getMemBuffer(
154 getBuffer(), FullPath ? (Twine(Parent->getFileName()) + "(" + Name + ")")
155 .toStringRef(Path)
156 : Name,
157 false));
158 return error_code::success();
89159 }
90160
91161 error_code Archive::Child::getAsBinary(OwningPtr &Result) const {