llvm.org GIT mirror llvm / 0659928
Convert the Archive API to use ErrorOr. Now that we have c++11, even things like ErrorOr<std::unique_ptr<...>> are easy to use. No intended functionality change. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211033 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 5 years ago
8 changed file(s) with 95 addition(s) and 100 deletion(s). Raw diff Collapse all Expand all
7171
7272 Child getNext() const;
7373
74 std::error_code getName(StringRef &Result) const;
74 ErrorOr getName() const;
7575 StringRef getRawName() const { return getHeader()->getName(); }
7676 sys::TimeValue getLastModified() const {
7777 return getHeader()->getLastModified();
8888 return StringRef(Data.data() + StartOfFile, getSize());
8989 }
9090
91 std::error_code getMemoryBuffer(std::unique_ptr &Result,
92 bool FullPath = false) const;
93
94 std::error_code getAsBinary(std::unique_ptr &Result,
95 LLVMContext *Context = nullptr) const;
91 ErrorOr>
92 getMemoryBuffer(bool FullPath = false) const;
93
94 ErrorOr>
95 getAsBinary(LLVMContext *Context = nullptr) const;
9696 };
9797
9898 class child_iterator {
136136 : Parent(p)
137137 , SymbolIndex(symi)
138138 , StringIndex(stri) {}
139 std::error_code getName(StringRef &Result) const;
140 std::error_code getMember(child_iterator &Result) const;
139 StringRef getName() const;
140 ErrorOr getMember() const;
141141 Symbol getNext() const;
142142 };
143143
304304 // Look for our symbols in each Archive
305305 object::Archive::child_iterator ChildIt = A->findSym(Name);
306306 if (ChildIt != A->child_end()) {
307 std::unique_ptr ChildBin;
308307 // FIXME: Support nested archives?
309 if (!ChildIt->getAsBinary(ChildBin) && ChildBin->isObject()) {
308 ErrorOr> ChildBinOrErr =
309 ChildIt->getAsBinary();
310 if (ChildBinOrErr.getError())
311 continue;
312 std::unique_ptr ChildBin = std::move(ChildBinOrErr.get());
313 if (ChildBin->isObject()) {
310314 std::unique_ptr OF(
311315 static_cast(ChildBin.release()));
312316 // This causes the object file to be loaded.
114114 return Child(Parent, NextLoc);
115115 }
116116
117 std::error_code Archive::Child::getName(StringRef &Result) const {
117 ErrorOr Archive::Child::getName() const {
118118 StringRef name = getRawName();
119119 // Check if it's a special name.
120120 if (name[0] == '/') {
121 if (name.size() == 1) { // Linker member.
122 Result = name;
123 return object_error::success;
124 }
125 if (name.size() == 2 && name[1] == '/') { // String table.
126 Result = name;
127 return object_error::success;
128 }
121 if (name.size() == 1) // Linker member.
122 return name;
123 if (name.size() == 2 && name[1] == '/') // String table.
124 return name;
129125 // It's a long name.
130126 // Get the offset.
131127 std::size_t offset;
146142 // GNU long file names end with a /.
147143 if (Parent->kind() == K_GNU) {
148144 StringRef::size_type End = StringRef(addr).find('/');
149 Result = StringRef(addr, End);
150 } else {
151 Result = addr;
145 return StringRef(addr, End);
152146 }
153 return object_error::success;
147 return StringRef(addr);
154148 } else if (name.startswith("#1/")) {
155149 uint64_t name_size;
156150 if (name.substr(3).rtrim(" ").getAsInteger(10, name_size))
157151 llvm_unreachable("Long name length is not an ingeter");
158 Result = Data.substr(sizeof(ArchiveMemberHeader), name_size)
152 return Data.substr(sizeof(ArchiveMemberHeader), name_size)
159153 .rtrim(StringRef("\0", 1));
160 return object_error::success;
161154 }
162155 // It's a simple name.
163156 if (name[name.size() - 1] == '/')
164 Result = name.substr(0, name.size() - 1);
165 else
166 Result = name;
167 return object_error::success;
168 }
169
170 std::error_code
171 Archive::Child::getMemoryBuffer(std::unique_ptr &Result,
172 bool FullPath) const {
173 StringRef Name;
174 if (std::error_code ec = getName(Name))
175 return ec;
157 return name.substr(0, name.size() - 1);
158 return name;
159 }
160
161 ErrorOr>
162 Archive::Child::getMemoryBuffer(bool FullPath) const {
163 ErrorOr NameOrErr = getName();
164 if (std::error_code EC = NameOrErr.getError())
165 return EC;
166 StringRef Name = NameOrErr.get();
176167 SmallString<128> Path;
177 Result.reset(MemoryBuffer::getMemBuffer(
178 getBuffer(), FullPath ? (Twine(Parent->getFileName()) + "(" + Name + ")")
179 .toStringRef(Path)
180 : Name,
168 std::unique_ptr Ret(MemoryBuffer::getMemBuffer(
169 getBuffer(),
170 FullPath
171 ? (Twine(Parent->getFileName()) + "(" + Name + ")").toStringRef(Path)
172 : Name,
181173 false));
182 return std::error_code();
183 }
184
185 std::error_code Archive::Child::getAsBinary(std::unique_ptr &Result,
186 LLVMContext *Context) const {
174 return std::move(Ret);
175 }
176
177 ErrorOr>
178 Archive::Child::getAsBinary(LLVMContext *Context) const {
187179 std::unique_ptr ret;
188 std::unique_ptr Buff;
189 if (std::error_code ec = getMemoryBuffer(Buff))
190 return ec;
191 ErrorOr BinaryOrErr = createBinary(Buff.release(), Context);
192 if (std::error_code EC = BinaryOrErr.getError())
180 ErrorOr> BuffOrErr = getMemoryBuffer();
181 if (std::error_code EC = BuffOrErr.getError())
193182 return EC;
194 Result.reset(BinaryOrErr.get());
195 return object_error::success;
183 return createBinary(BuffOrErr.get().release(), Context);
196184 }
197185
198186 ErrorOr Archive::create(MemoryBuffer *Source) {
255243 if (Name.startswith("#1/")) {
256244 Format = K_BSD;
257245 // We know this is BSD, so getName will work since there is no string table.
258 ec = i->getName(Name);
246 ErrorOr NameOrErr = i->getName();
247 ec = NameOrErr.getError();
259248 if (ec)
260249 return;
250 Name = NameOrErr.get();
261251 if (Name == "__.SYMDEF SORTED") {
262252 SymbolTable = i;
263253 ++i;
335325 return Child(this, nullptr);
336326 }
337327
338 std::error_code Archive::Symbol::getName(StringRef &Result) const {
339 Result = StringRef(Parent->SymbolTable->getBuffer().begin() + StringIndex);
340 return object_error::success;
341 }
342
343 std::error_code Archive::Symbol::getMember(child_iterator &Result) const {
328 StringRef Archive::Symbol::getName() const {
329 return Parent->SymbolTable->getBuffer().begin() + StringIndex;
330 }
331
332 ErrorOr Archive::Symbol::getMember() const {
344333 const char *Buf = Parent->SymbolTable->getBuffer().begin();
345334 const char *Offsets = Buf + 4;
346335 uint32_t Offset = 0;
380369 }
381370
382371 const char *Loc = Parent->getData().begin() + Offset;
383 Result = Child(Parent, Loc);
384
385 return object_error::success;
372 child_iterator Iter(Child(Parent, Loc));
373 return Iter;
386374 }
387375
388376 Archive::Symbol Archive::Symbol::getNext() const {
440428 Archive::child_iterator Archive::findSym(StringRef name) const {
441429 Archive::symbol_iterator bs = symbol_begin();
442430 Archive::symbol_iterator es = symbol_end();
443 Archive::child_iterator result;
444
445 StringRef symname;
431
446432 for (; bs != es; ++bs) {
447 if (bs->getName(symname))
433 StringRef SymName = bs->getName();
434 if (SymName == name) {
435 ErrorOr ResultOrErr = bs->getMember();
436 // FIXME: Should we really eat the error?
437 if (ResultOrErr.getError())
448438 return child_end();
449 if (symname == name) {
450 if (bs->getMember(result))
451 return child_end();
452 return result;
439 return ResultOrErr.get();
453440 }
454441 }
455442 return child_end();
368368 for (object::Archive::child_iterator I = OldArchive->child_begin(),
369369 E = OldArchive->child_end();
370370 I != E; ++I) {
371 StringRef Name;
372 failIfError(I->getName(Name));
371 ErrorOr NameOrErr = I->getName();
372 failIfError(NameOrErr.getError());
373 StringRef Name = NameOrErr.get();
373374
374375 if (!Members.empty() &&
375376 std::find(Members.begin(), Members.end(), Name) == Members.end())
543544 E = OldArchive->child_end();
544545 I != E; ++I) {
545546 int Pos = Ret.size();
546 StringRef Name;
547 failIfError(I->getName(Name));
547 ErrorOr NameOrErr = I->getName();
548 failIfError(NameOrErr.getError());
549 StringRef Name = NameOrErr.get();
548550 if (Name == PosName) {
549551 assert(AddAfter || AddBefore);
550552 if (AddBefore)
782784
783785 } else {
784786 object::Archive::child_iterator OldMember = Member.getOld();
785 failIfError(OldMember->getMemoryBuffer(MemberBuffer));
787 ErrorOr> MemberBufferOrErr =
788 OldMember->getMemoryBuffer();
789 failIfError(MemberBufferOrErr.getError());
790 MemberBuffer = std::move(MemberBufferOrErr.get());
786791 }
787792 MemberBuffers[I] = MemberBuffer.release();
788793 }
732732 if (I != E) {
733733 outs() << "Archive map\n";
734734 for (; I != E; ++I) {
735 Archive::child_iterator C;
736 StringRef SymName;
737 StringRef FileName;
738 if (error(I->getMember(C)))
735 ErrorOr C = I->getMember();
736 if (error(C.getError()))
739737 return;
740 if (error(I->getName(SymName)))
738 ErrorOr FileNameOrErr = C.get()->getName();
739 if (error(FileNameOrErr.getError()))
741740 return;
742 if (error(C->getName(FileName)))
743 return;
744 outs() << SymName << " in " << FileName << "\n";
741 StringRef SymName = I->getName();
742 outs() << SymName << " in " << FileNameOrErr.get() << "\n";
745743 }
746744 outs() << "\n";
747745 }
749747
750748 for (Archive::child_iterator I = A->child_begin(), E = A->child_end();
751749 I != E; ++I) {
752 std::unique_ptr Child;
753 if (I->getAsBinary(Child, &Context))
750 ErrorOr> ChildOrErr = I->getAsBinary(&Context);
751 if (ChildOrErr.getError())
754752 continue;
755 if (SymbolicFile *O = dyn_cast(Child.get())) {
753 if (SymbolicFile *O = dyn_cast(&*ChildOrErr.get())) {
756754 outs() << O->getFileName() << ":\n";
757755 dumpSymbolNamesFromObject(O);
758756 }
772770 else if (!I->getAsArchive(A)) {
773771 for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end();
774772 AI != AE; ++AI) {
775 std::unique_ptr Child;
776 if (AI->getAsBinary(Child, &Context))
773 ErrorOr> ChildOrErr =
774 AI->getAsBinary(&Context);
775 if (ChildOrErr.getError())
777776 continue;
778 if (SymbolicFile *O = dyn_cast(Child.get())) {
777 if (SymbolicFile *O = dyn_cast(&*ChildOrErr.get())) {
779778 outs() << A->getFileName() << ":";
780779 outs() << O->getFileName() << ":\n";
781780 dumpSymbolNamesFromObject(O);
849849 static void DumpArchive(const Archive *a) {
850850 for (Archive::child_iterator i = a->child_begin(), e = a->child_end(); i != e;
851851 ++i) {
852 std::unique_ptr child;
853 if (std::error_code EC = i->getAsBinary(child)) {
852 ErrorOr> ChildOrErr = i->getAsBinary();
853 if (std::error_code EC = ChildOrErr.getError()) {
854854 // Ignore non-object files.
855855 if (EC != object_error::invalid_file_type)
856856 errs() << ToolName << ": '" << a->getFileName() << "': " << EC.message()
857857 << ".\n";
858858 continue;
859859 }
860 if (ObjectFile *o = dyn_cast(child.get()))
860 if (ObjectFile *o = dyn_cast(&*ChildOrErr.get()))
861861 DumpObject(o);
862862 else
863863 errs() << ToolName << ": '" << a->getFileName() << "': "
241241 for (Archive::child_iterator ArcI = Arc->child_begin(),
242242 ArcE = Arc->child_end();
243243 ArcI != ArcE; ++ArcI) {
244 std::unique_ptr child;
245 if (std::error_code EC = ArcI->getAsBinary(child)) {
244 ErrorOr> ChildOrErr = ArcI->getAsBinary();
245 if (std::error_code EC = ChildOrErr.getError()) {
246246 // Ignore non-object files.
247247 if (EC != object_error::invalid_file_type)
248248 reportError(Arc->getFileName(), EC.message());
249249 continue;
250250 }
251251
252 if (ObjectFile *Obj = dyn_cast(child.get()))
252 if (ObjectFile *Obj = dyn_cast(&*ChildOrErr.get()))
253253 dumpObject(Obj);
254254 else
255255 reportError(Arc->getFileName(), readobj_error::unrecognized_file_format);
244244 // This is an archive. Iterate over each member and display its sizes.
245245 for (object::Archive::child_iterator i = a->child_begin(),
246246 e = a->child_end(); i != e; ++i) {
247 std::unique_ptr child;
248 if (std::error_code ec = i->getAsBinary(child)) {
249 errs() << ToolName << ": " << file << ": " << ec.message() << ".\n";
247 ErrorOr> ChildOrErr = i->getAsBinary();
248 if (std::error_code EC = ChildOrErr.getError()) {
249 errs() << ToolName << ": " << file << ": " << EC.message() << ".\n";
250250 continue;
251251 }
252 if (ObjectFile *o = dyn_cast(child.get())) {
252 if (ObjectFile *o = dyn_cast(&*ChildOrErr.get())) {
253253 if (OutputFormat == sysv)
254254 outs() << o->getFileName() << " (ex " << a->getFileName()
255255 << "):\n";