llvm.org GIT mirror llvm / b48816b
Clean up of libObject/Archive interfaces and change the last three uses of ErrorOr<> changing them to Expected<> to allow them to pass through llvm Errors. No functional change. This commit by itself will break the next lld builds.  I’ll be committing the matching change for lld immediately next. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@277656 91177308-0d34-0410-b5e6-96231b3b80d8 Kevin Enderby 3 years ago
4 changed file(s) with 87 addition(s) and 50 deletion(s). Raw diff Collapse all Expand all
1818 #include "llvm/ADT/iterator_range.h"
1919 #include "llvm/Object/Binary.h"
2020 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/ErrorOr.h"
21 #include "llvm/Support/Error.h"
2222 #include "llvm/Support/FileSystem.h"
2323 #include "llvm/Support/MemoryBuffer.h"
2424
9999 Expected getNext() const;
100100
101101 Expected getName() const;
102 ErrorOr getFullName() const;
102 Expected getFullName() const;
103103 Expected getRawName() const { return Header.getRawName(); }
104104 Expected getLastModified() const {
105105 return Header.getLastModified();
117117 /// \return the size in the archive header for this member.
118118 Expected getRawSize() const;
119119
120 ErrorOr getBuffer() const;
120 Expected getBuffer() const;
121121 uint64_t getChildOffset() const;
122122
123123 Expected getMemoryBufferRef() const;
178178 , SymbolIndex(symi)
179179 , StringIndex(stri) {}
180180 StringRef getName() const;
181 ErrorOr getMember() const;
181 Expected getMember() const;
182182 Symbol getNext() const;
183183 };
184184
388388 return Parent->IsThin && Name != "/" && Name != "//";
389389 }
390390
391 ErrorOr Archive::Child::getFullName() const {
391 Expected Archive::Child::getFullName() const {
392392 Expected isThin = isThinMember();
393393 if (!isThin)
394 return errorToErrorCode(isThin.takeError());
394 return isThin.takeError();
395395 assert(isThin.get());
396396 Expected NameOrErr = getName();
397397 if (!NameOrErr)
398 return errorToErrorCode(NameOrErr.takeError());
398 return NameOrErr.takeError();
399399 StringRef Name = *NameOrErr;
400400 if (sys::path::is_absolute(Name))
401401 return Name;
406406 return StringRef(FullName);
407407 }
408408
409 ErrorOr Archive::Child::getBuffer() const {
409 Expected Archive::Child::getBuffer() const {
410410 Expected isThinOrErr = isThinMember();
411411 if (!isThinOrErr)
412 return errorToErrorCode(isThinOrErr.takeError());
412 return isThinOrErr.takeError();
413413 bool isThin = isThinOrErr.get();
414414 if (!isThin) {
415415 Expected Size = getSize();
416416 if (!Size)
417 return errorToErrorCode(Size.takeError());
417 return Size.takeError();
418418 return StringRef(Data.data() + StartOfFile, Size.get());
419419 }
420 ErrorOr FullNameOrEr = getFullName();
421 if (std::error_code EC = FullNameOrEr.getError())
422 return EC;
423 const std::string &FullName = *FullNameOrEr;
420 Expected FullNameOrErr = getFullName();
421 if (!FullNameOrErr)
422 return FullNameOrErr.takeError();
423 const std::string &FullName = *FullNameOrErr;
424424 ErrorOr> Buf = MemoryBuffer::getFile(FullName);
425425 if (std::error_code EC = Buf.getError())
426 return EC;
426 return errorCodeToError(EC);
427427 Parent->ThinBuffers.push_back(std::move(*Buf));
428428 return Parent->ThinBuffers.back()->getBuffer();
429429 }
484484 if (!NameOrErr)
485485 return NameOrErr.takeError();
486486 StringRef Name = NameOrErr.get();
487 ErrorOr Buf = getBuffer();
488 if (std::error_code EC = Buf.getError())
489 return errorCodeToError(EC);
487 Expected Buf = getBuffer();
488 if (!Buf)
489 return Buf.takeError();
490490 return MemoryBufferRef(*Buf, Name);
491491 }
492492
589589 Format = K_BSD;
590590 else // Name == "__.SYMDEF_64"
591591 Format = K_DARWIN64;
592 // We know that the symbol table is not an external file, so we just assert
593 // there is no error.
594 SymbolTable = *C->getBuffer();
592 // We know that the symbol table is not an external file, but we still must
593 // check any Expected<> return value.
594 Expected BufOrErr = C->getBuffer();
595 if (!BufOrErr) {
596 Err = BufOrErr.takeError();
597 return;
598 }
599 SymbolTable = BufOrErr.get();
595600 if (Increment())
596601 return;
597602 setFirstRegular(*C);
610615 }
611616 Name = NameOrErr.get();
612617 if (Name == "__.SYMDEF SORTED" || Name == "__.SYMDEF") {
613 // We know that the symbol table is not an external file, so we just
614 // assert there is no error.
615 SymbolTable = *C->getBuffer();
618 // We know that the symbol table is not an external file, but we still
619 // must check any Expected<> return value.
620 Expected BufOrErr = C->getBuffer();
621 if (!BufOrErr) {
622 Err = BufOrErr.takeError();
623 return;
624 }
625 SymbolTable = BufOrErr.get();
616626 if (Increment())
617627 return;
618628 }
619629 else if (Name == "__.SYMDEF_64 SORTED" || Name == "__.SYMDEF_64") {
620630 Format = K_DARWIN64;
621 // We know that the symbol table is not an external file, so we just
622 // assert there is no error.
623 SymbolTable = *C->getBuffer();
631 // We know that the symbol table is not an external file, but we still
632 // must check any Expected<> return value.
633 Expected BufOrErr = C->getBuffer();
634 if (!BufOrErr) {
635 Err = BufOrErr.takeError();
636 return;
637 }
638 SymbolTable = BufOrErr.get();
624639 if (Increment())
625640 return;
626641 }
635650
636651 bool has64SymTable = false;
637652 if (Name == "/" || Name == "/SYM64/") {
638 // We know that the symbol table is not an external file, so we just assert
639 // there is no error.
640 SymbolTable = *C->getBuffer();
653 // We know that the symbol table is not an external file, but we still
654 // must check any Expected<> return value.
655 Expected BufOrErr = C->getBuffer();
656 if (!BufOrErr) {
657 Err = BufOrErr.takeError();
658 return;
659 }
660 SymbolTable = BufOrErr.get();
641661 if (Name == "/SYM64/")
642662 has64SymTable = true;
643663
657677
658678 if (Name == "//") {
659679 Format = has64SymTable ? K_MIPS64 : K_GNU;
660 // The string table is never an external member, so we just assert on the
661 // ErrorOr.
662 StringTable = *C->getBuffer();
680 // The string table is never an external member, but we still
681 // must check any Expected<> return value.
682 Expected BufOrErr = C->getBuffer();
683 if (!BufOrErr) {
684 Err = BufOrErr.takeError();
685 return;
686 }
687 StringTable = BufOrErr.get();
663688 if (Increment())
664689 return;
665690 setFirstRegular(*C);
680705 }
681706
682707 Format = K_COFF;
683 // We know that the symbol table is not an external file, so we just assert
684 // there is no error.
685 SymbolTable = *C->getBuffer();
708 // We know that the symbol table is not an external file, but we still
709 // must check any Expected<> return value.
710 Expected BufOrErr = C->getBuffer();
711 if (!BufOrErr) {
712 Err = BufOrErr.takeError();
713 return;
714 }
715 SymbolTable = BufOrErr.get();
686716
687717 if (Increment())
688718 return;
701731 Name = NameOrErr.get();
702732
703733 if (Name == "//") {
704 // The string table is never an external member, so we just assert on the
705 // ErrorOr.
706 StringTable = *C->getBuffer();
734 // The string table is never an external member, but we still
735 // must check any Expected<> return value.
736 Expected BufOrErr = C->getBuffer();
737 if (!BufOrErr) {
738 Err = BufOrErr.takeError();
739 return;
740 }
741 StringTable = BufOrErr.get();
707742 if (Increment())
708743 return;
709744 }
737772 return Parent->getSymbolTable().begin() + StringIndex;
738773 }
739774
740 ErrorOr Archive::Symbol::getMember() const {
775 Expected Archive::Symbol::getMember() const {
741776 const char *Buf = Parent->getSymbolTable().begin();
742777 const char *Offsets = Buf;
743778 if (Parent->kind() == K_MIPS64 || Parent->kind() == K_DARWIN64)
772807
773808 uint32_t SymbolCount = read32le(Buf);
774809 if (SymbolIndex >= SymbolCount)
775 return object_error::parse_failed;
810 return errorCodeToError(object_error::parse_failed);
776811
777812 // Skip SymbolCount to get to the indices table.
778813 const char *Indices = Buf + 4;
784819 --OffsetIndex;
785820
786821 if (OffsetIndex >= MemberCount)
787 return object_error::parse_failed;
822 return errorCodeToError(object_error::parse_failed);
788823
789824 Offset = read32le(Offsets + OffsetIndex * 4);
790825 }
793828 Error Err;
794829 Child C(Parent, Loc, &Err);
795830 if (Err)
796 return errorToErrorCode(std::move(Err));
831 return std::move(Err);
797832 return C;
798833 }
799834
924959 if (auto MemberOrErr = bs->getMember())
925960 return Child(*MemberOrErr);
926961 else
927 return errorCodeToError(MemberOrErr.getError());
962 return MemberOrErr.takeError();
928963 }
929964 }
930965 return Optional();
317317 if (Verbose)
318318 outs() << "Printing " << Name << "\n";
319319
320 ErrorOr DataOrErr = C.getBuffer();
321 failIfError(DataOrErr.getError());
320 Expected DataOrErr = C.getBuffer();
321 failIfError(DataOrErr.takeError());
322322 StringRef Data = *DataOrErr;
323323 outs().write(Data.data(), Data.size());
324324 }
375375 raw_fd_ostream file(FD, false);
376376
377377 // Get the data and its length
378 StringRef Data = *C.getBuffer();
378 Expected BufOrErr = C.getBuffer();
379 failIfError(BufOrErr.takeError());
380 StringRef Data = BufOrErr.get();
379381
380382 // Write the data.
381383 file.write(Data.data(), Data.size());
10961096 if (I != E) {
10971097 outs() << "Archive map\n";
10981098 for (; I != E; ++I) {
1099 ErrorOr C = I->getMember();
1100 if (error(C.getError()))
1101 return;
1099 Expected C = I->getMember();
1100 if (!C)
1101 error(C.takeError(), Filename);
11021102 Expected FileNameOrErr = C->getName();
11031103 if (!FileNameOrErr) {
11041104 error(FileNameOrErr.takeError(), Filename);