llvm.org GIT mirror llvm / 7809543
[Support] Make ErrorAsOutParameter take an Error* rather than an Error&. This allows ErrorAsOutParameter to work better with "optional" errors. For example, consider a function where for certain input values it is known that the function can't fail. This can now be written as: Result foo(Arg X, Error *Err) { ErrorAsOutParameter EAO(Err); if (<Error Condition>) { if (Err) *Err = <report error>; else llvm_unreachable("Unexpected failure!"); } } Rather than having to construct an ErrorAsOutParameter under every conditional where Err is known to be non-null. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@276430 91177308-0d34-0410-b5e6-96231b3b80d8 Lang Hames 4 years ago
7 changed file(s) with 22 addition(s) and 15 deletion(s). Raw diff Collapse all Expand all
687687
688688 private:
689689 OrcRemoteTargetClient(ChannelT &Channel, Error &Err) : Channel(Channel) {
690 ErrorAsOutParameter EAO(Err);
690 ErrorAsOutParameter EAO(&Err);
691691 if (auto RIOrErr = callST(Channel)) {
692692 std::tie(RemoteTargetTriple, RemotePointerSize, RemotePageSize,
693693 RemoteTrampolineSize, RemoteIndirectStubSize) = *RIOrErr;
130130 // iteration. And if there is an error break out of the loop.
131131 child_iterator &operator++() { // Preincrement
132132 assert(E && "Can't increment iterator with no Error attached");
133 ErrorAsOutParameter ErrAsOutParam(E);
133134 if (auto ChildOrErr = C.getNext())
134135 C = *ChildOrErr;
135136 else {
136 ErrorAsOutParameter ErrAsOutParam(*E);
137137 C = C.getParent()->child_end().C;
138138 *E = ChildOrErr.takeError();
139139 E = nullptr;
570570 /// RAII:
571571 ///
572572 /// Result foo(Error &Err) {
573 /// ErrorAsOutParameter ErrAsOutParam(Err); // 'Checked' flag set
573 /// ErrorAsOutParameter ErrAsOutParam(&Err); // 'Checked' flag set
574574 /// //
575575 /// // <- 'Checked' flag auto-cleared when ErrAsOutParam is destructed.
576576 /// }
577 ///
578 /// ErrorAsOutParameter takes an Error* rather than Error& so that it can be
579 /// used with optional Errors (Error pointers that are allowed to be null). If
580 /// ErrorAsOutParameter took an Error reference, an instance would have to be
581 /// created inside every condition that verified that Error was non-null. By
582 /// taking an Error pointer we can just create one instance at the top of the
583 /// function.
577584 class ErrorAsOutParameter {
578585 public:
579 ErrorAsOutParameter(Error &Err) : Err(Err) {
586 ErrorAsOutParameter(Error *Err) : Err(Err) {
580587 // Raise the checked bit if Err is success.
581 (void)!!Err;
588 if (Err)
589 (void)!!*Err;
582590 }
583591 ~ErrorAsOutParameter() {
584592 // Clear the checked bit.
585 if (!Err)
586 Err = Error::success();
593 if (Err && !*Err)
594 *Err = Error::success();
587595 }
588596
589597 private:
590 Error &Err;
598 Error *Err;
591599 };
592600
593601 /// Tagged union holding either a T or a Error.
107107 : Parent(Parent) {
108108 if (!Start)
109109 return;
110 ErrorAsOutParameter ErrAsOutParam(Err);
110111
111112 uint64_t Size = sizeof(ArchiveMemberHeader);
112113 Data = StringRef(Start, Size);
113114 if (!isThinMember()) {
114115 Expected MemberSize = getRawSize();
115116 if (!MemberSize) {
116 if (Err) {
117 ErrorAsOutParameter ErrAsOutParam(*Err);
117 if (Err)
118118 *Err = MemberSize.takeError();
119 }
120119 return;
121120 }
122121 Size += MemberSize.get();
298297
299298 Archive::Archive(MemoryBufferRef Source, Error &Err)
300299 : Binary(Binary::ID_Archive, Source) {
301 ErrorAsOutParameter ErrAsOutParam(Err);
300 ErrorAsOutParameter ErrAsOutParam(&Err);
302301 StringRef Buffer = Data.getBuffer();
303302 // Check for sufficient magic.
304303 if (Buffer.startswith(ThinMagic)) {
266266 DataInCodeLoadCmd(nullptr), LinkOptHintsLoadCmd(nullptr),
267267 DyldInfoLoadCmd(nullptr), UuidLoadCmd(nullptr),
268268 HasPageZeroSegment(false) {
269 ErrorAsOutParameter ErrAsOutParam(Err);
269 ErrorAsOutParameter ErrAsOutParam(&Err);
270270 uint64_t BigSize;
271271 if (is64Bit()) {
272272 parseHeader(this, Header64, Err);
113113 MachOUniversalBinary::MachOUniversalBinary(MemoryBufferRef Source, Error &Err)
114114 : Binary(Binary::ID_MachOUniversalBinary, Source), Magic(0),
115115 NumberOfObjects(0) {
116 ErrorAsOutParameter ErrAsOutParam(Err);
116 ErrorAsOutParameter ErrAsOutParam(&Err);
117117 if (Data.getBufferSize() < sizeof(MachO::fat_header)) {
118118 Err = make_error("File too small to be a Mach-O "
119119 "universal file",
108108
109109 // ErrorAsOutParameter tester.
110110 void errAsOutParamHelper(Error &Err) {
111 ErrorAsOutParameter ErrAsOutParam(Err);
111 ErrorAsOutParameter ErrAsOutParam(&Err);
112112 // Verify that checked flag is raised - assignment should not crash.
113113 Err = Error::success();
114114 // Raise the checked bit manually - caller should still have to test the