llvm.org GIT mirror llvm / 686dfe3
[Support] Make error banner optional in logAllUnhandledErrors In a lot of places an empty string was passed as the ErrorBanner to logAllUnhandledErrors. This patch makes that argument optional to simplify the call sites. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@346604 91177308-0d34-0410-b5e6-96231b3b80d8 Jonas Devlieghere 10 months ago
25 changed file(s) with 61 addition(s) and 61 deletion(s). Raw diff Collapse all Expand all
952952 /// will be printed before the first one is logged. A newline will be printed
953953 /// after each error.
954954 ///
955 /// This function is compatible with the helpers from Support/WithColor.h. You
956 /// can pass any of them as the OS. Please consider using them instead of
957 /// including 'error: ' in the ErrorBanner.
958 ///
955959 /// This is useful in the base level of your program to allow clean termination
956960 /// (allowing clean deallocation of resources, etc.), while reporting error
957961 /// information to the user.
958 void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner);
962 void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner = {});
959963
960964 /// Write all error messages (if any) in E to a string. The newline character
961965 /// is used to separate error messages.
215215 if (!LoadedObject) {
216216 std::string Buf;
217217 raw_string_ostream OS(Buf);
218 logAllUnhandledErrors(LoadedObject.takeError(), OS, "");
218 logAllUnhandledErrors(LoadedObject.takeError(), OS);
219219 OS.flush();
220220 report_fatal_error(Buf);
221221 }
6565 } else {
6666 HasError = true;
6767 raw_string_ostream ErrStream(ErrorStr);
68 logAllUnhandledErrors(ObjSectionToIDOrErr.takeError(), ErrStream, "");
68 logAllUnhandledErrors(ObjSectionToIDOrErr.takeError(), ErrStream);
6969 return nullptr;
7070 }
7171 }
254254 else {
255255 HasError = true;
256256 raw_string_ostream ErrStream(ErrorStr);
257 logAllUnhandledErrors(ObjSectionToIDOrErr.takeError(), ErrStream, "");
257 logAllUnhandledErrors(ObjSectionToIDOrErr.takeError(), ErrStream);
258258 return nullptr;
259259 }
260260 }
11291129 if (!SymTypeOrErr) {
11301130 std::string Buf;
11311131 raw_string_ostream OS(Buf);
1132 logAllUnhandledErrors(SymTypeOrErr.takeError(), OS, "");
1132 logAllUnhandledErrors(SymTypeOrErr.takeError(), OS);
11331133 OS.flush();
11341134 report_fatal_error(Buf);
11351135 }
11501150 if (!SectionOrErr) {
11511151 std::string Buf;
11521152 raw_string_ostream OS(Buf);
1153 logAllUnhandledErrors(SectionOrErr.takeError(), OS, "");
1153 logAllUnhandledErrors(SectionOrErr.takeError(), OS);
11541154 OS.flush();
11551155 report_fatal_error(Buf);
11561156 }
369369 else {
370370 HasError = true;
371371 raw_string_ostream ErrStream(ErrorStr);
372 logAllUnhandledErrors(ObjSectionToIDOrErr.takeError(), ErrStream, "");
372 logAllUnhandledErrors(ObjSectionToIDOrErr.takeError(), ErrStream);
373373 return nullptr;
374374 }
375375 }
2727 if (!SymTypeOrErr) {
2828 std::string Buf;
2929 raw_string_ostream OS(Buf);
30 logAllUnhandledErrors(SymTypeOrErr.takeError(), OS, "");
30 logAllUnhandledErrors(SymTypeOrErr.takeError(), OS);
3131 OS.flush();
3232 report_fatal_error(Buf);
3333 }
7272 bool LTOModule::isThinLTO() {
7373 Expected Result = getBitcodeLTOInfo(MBRef);
7474 if (!Result) {
75 logAllUnhandledErrors(Result.takeError(), errs(), "");
75 logAllUnhandledErrors(Result.takeError(), errs());
7676 return false;
7777 }
7878 return Result->IsThinLTO;
104104 if (!SecOrErr) {
105105 std::string Buf;
106106 raw_string_ostream OS(Buf);
107 logAllUnhandledErrors(SecOrErr.takeError(), OS, "");
107 logAllUnhandledErrors(SecOrErr.takeError(), OS);
108108 OS.flush();
109109 report_fatal_error(Buf);
110110 }
186186 if (!Ret) {
187187 std::string Buf;
188188 raw_string_ostream OS(Buf);
189 logAllUnhandledErrors(Ret.takeError(), OS, "");
189 logAllUnhandledErrors(Ret.takeError(), OS);
190190 OS.flush();
191191 report_fatal_error(Buf);
192192 }
198198 if (!Ret) {
199199 std::string Buf;
200200 raw_string_ostream OS(Buf);
201 logAllUnhandledErrors(Ret.takeError(), OS, "");
201 logAllUnhandledErrors(Ret.takeError(), OS);
202202 OS.flush();
203203 report_fatal_error(Buf);
204204 }
140140 std::string ErrMsg;
141141 {
142142 raw_string_ostream ErrStream(ErrMsg);
143 logAllUnhandledErrors(std::move(Err), ErrStream, "");
143 logAllUnhandledErrors(std::move(Err), ErrStream);
144144 }
145145 report_fatal_error(ErrMsg);
146146 }
510510 if (!ArOrErr) {
511511 std::string Buf;
512512 raw_string_ostream OS(Buf);
513 logAllUnhandledErrors(ArOrErr.takeError(), OS, "");
513 logAllUnhandledErrors(ArOrErr.takeError(), OS);
514514 OS.flush();
515515 errs() << Buf;
516516 exit(1);
3232 if (!ObjErr) {
3333 std::string Buf;
3434 raw_string_ostream OS(Buf);
35 logAllUnhandledErrors(ObjErr.takeError(), OS, "");
35 logAllUnhandledErrors(ObjErr.takeError(), OS);
3636 OS.flush();
3737 errs() << "error: " << Buf;
3838 return 1;
5151 static void error(Error Err) {
5252 if (!Err)
5353 return;
54 logAllUnhandledErrors(std::move(Err), WithColor::error(outs(), ""), "reading file: ");
54 logAllUnhandledErrors(std::move(Err), WithColor::error(outs()),
55 "reading file: ");
5556 outs().flush();
5657 exit(1);
5758 }
496497 if (auto E = isNotObjectErrorInvalidFileType(ChildOrErr.takeError())) {
497498 std::string Buf;
498499 raw_string_ostream OS(Buf);
499 logAllUnhandledErrors(std::move(E), OS, "");
500 logAllUnhandledErrors(std::move(E), OS);
500501 OS.flush();
501502 reportError(Arc->getFileName(), Buf);
502503 }
208208
209209 // This version of error() prints the archive name and member name, for example:
210210 // "libx.a(foo.o)" after the ToolName before the error message. It sets
211 // HadError but returns allowing the code to move on to other archive members.
211 // HadError but returns allowing the code to move on to other archive members.
212212 static void error(llvm::Error E, StringRef FileName, const Archive::Child &C,
213213 StringRef ArchitectureName = StringRef()) {
214214 HadError = true;
229229
230230 std::string Buf;
231231 raw_string_ostream OS(Buf);
232 logAllUnhandledErrors(std::move(E), OS, "");
232 logAllUnhandledErrors(std::move(E), OS);
233233 OS.flush();
234234 errs() << " " << Buf << "\n";
235235 }
237237 // This version of error() prints the file name and which architecture slice it
238238 // is from, for example: "foo.o (for architecture i386)" after the ToolName
239239 // before the error message. It sets HadError but returns allowing the code to
240 // move on to other architecture slices.
240 // move on to other architecture slices.
241241 static void error(llvm::Error E, StringRef FileName,
242242 StringRef ArchitectureName = StringRef()) {
243243 HadError = true;
248248
249249 std::string Buf;
250250 raw_string_ostream OS(Buf);
251 logAllUnhandledErrors(std::move(E), OS, "");
251 logAllUnhandledErrors(std::move(E), OS);
252252 OS.flush();
253253 errs() << " " << Buf << "\n";
254254 }
10281028 StringRef SectionName;
10291029 Obj.getSectionName(Ref, SectionName);
10301030 StringRef SegmentName = Obj.getSectionFinalSegmentName(Ref);
1031 if (Obj.is64Bit() &&
1032 Obj.getHeader64().filetype == MachO::MH_KEXT_BUNDLE &&
1031 if (Obj.is64Bit() && Obj.getHeader64().filetype == MachO::MH_KEXT_BUNDLE &&
10331032 SegmentName == "__TEXT_EXEC" && SectionName == "__text")
10341033 return 't';
10351034 if (SegmentName == "__TEXT" && SectionName == "__text")
16051604 uint64_t lc_main_offset = UINT64_MAX;
16061605 for (const auto &Command : MachO->load_commands()) {
16071606 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
1608 // We found a function starts segment, parse the addresses for
1607 // We found a function starts segment, parse the addresses for
16091608 // consumption.
16101609 MachO::linkedit_data_command LLC =
16111610 MachO->getLinkeditDataLoadCommand(Command);
6565 assert(E);
6666 std::string Buf;
6767 raw_string_ostream OS(Buf);
68 logAllUnhandledErrors(std::move(E), OS, "");
68 logAllUnhandledErrors(std::move(E), OS);
6969 OS.flush();
7070 WithColor::error(errs(), ToolName) << "'" << File << "': " << Buf;
7171 exit(1);
3030 return *EO;
3131 std::string Buf;
3232 raw_string_ostream OS(Buf);
33 logAllUnhandledErrors(EO.takeError(), OS, "");
33 logAllUnhandledErrors(EO.takeError(), OS);
3434 OS.flush();
3535 error(Buf);
3636 }
371371 assert(E);
372372 std::string Buf;
373373 raw_string_ostream OS(Buf);
374 logAllUnhandledErrors(std::move(E), OS, "");
374 logAllUnhandledErrors(std::move(E), OS);
375375 OS.flush();
376376 errs() << ToolName << ": '" << File << "': " << Buf;
377377 exit(1);
391391 errs() << " (for architecture " << ArchitectureName << ")";
392392 std::string Buf;
393393 raw_string_ostream OS(Buf);
394 logAllUnhandledErrors(std::move(E), OS, "");
394 logAllUnhandledErrors(std::move(E), OS);
395395 OS.flush();
396396 errs() << ": " << Buf;
397397 exit(1);
902902 if (!Name) {
903903 std::string Buf;
904904 llvm::raw_string_ostream OS(Buf);
905 logAllUnhandledErrors(Name.takeError(), OS, "");
905 logAllUnhandledErrors(Name.takeError(), OS);
906906 OS.flush();
907907 report_fatal_error(Buf);
908908 }
941941 if (!FunctionNameOrErr) {
942942 std::string Buf;
943943 llvm::raw_string_ostream OS(Buf);
944 logAllUnhandledErrors(FunctionNameOrErr.takeError(), OS, "");
944 logAllUnhandledErrors(FunctionNameOrErr.takeError(), OS);
945945 OS.flush();
946946 report_fatal_error(Buf);
947947 }
950950 if (!FunctionAddressOrErr) {
951951 std::string Buf;
952952 llvm::raw_string_ostream OS(Buf);
953 logAllUnhandledErrors(FunctionAddressOrErr.takeError(), OS, "");
953 logAllUnhandledErrors(FunctionAddressOrErr.takeError(), OS);
954954 OS.flush();
955955 report_fatal_error(Buf);
956956 }
966966 if (!Name) {
967967 std::string Buf;
968968 llvm::raw_string_ostream OS(Buf);
969 logAllUnhandledErrors(Name.takeError(), OS, "");
969 logAllUnhandledErrors(Name.takeError(), OS);
970970 OS.flush();
971971 report_fatal_error(Buf);
972972 }
975975 if (!AddressOrErr) {
976976 std::string Buf;
977977 llvm::raw_string_ostream OS(Buf);
978 logAllUnhandledErrors(AddressOrErr.takeError(), OS, "");
978 logAllUnhandledErrors(AddressOrErr.takeError(), OS);
979979 OS.flush();
980980 report_fatal_error(Buf);
981981 }
10241024 if (!FunctionNameOrErr) {
10251025 std::string Buf;
10261026 llvm::raw_string_ostream OS(Buf);
1027 logAllUnhandledErrors(FunctionNameOrErr.takeError(), OS, "");
1027 logAllUnhandledErrors(FunctionNameOrErr.takeError(), OS);
10281028 OS.flush();
10291029 report_fatal_error(Buf);
10301030 }
10331033 if (!FunctionAddressOrErr) {
10341034 std::string Buf;
10351035 llvm::raw_string_ostream OS(Buf);
1036 logAllUnhandledErrors(FunctionAddressOrErr.takeError(), OS, "");
1036 logAllUnhandledErrors(FunctionAddressOrErr.takeError(), OS);
10371037 OS.flush();
10381038 report_fatal_error(Buf);
10391039 }
3939 return *EO;
4040 std::string Buf;
4141 raw_string_ostream OS(Buf);
42 logAllUnhandledErrors(EO.takeError(), OS, "");
42 logAllUnhandledErrors(EO.takeError(), OS);
4343 OS.flush();
4444 reportError(Buf);
4545 }
308308 if (!MaybeObj) {
309309 std::string Buf;
310310 raw_string_ostream OS(Buf);
311 logAllUnhandledErrors(MaybeObj.takeError(), OS, "");
311 logAllUnhandledErrors(MaybeObj.takeError(), OS);
312312 OS.flush();
313313 ErrorAndExit("unable to create object file: '" + Buf + "'");
314314 }
437437 if (!MaybeObj) {
438438 std::string Buf;
439439 raw_string_ostream OS(Buf);
440 logAllUnhandledErrors(MaybeObj.takeError(), OS, "");
440 logAllUnhandledErrors(MaybeObj.takeError(), OS);
441441 OS.flush();
442442 ErrorAndExit("unable to create object file: '" + Buf + "'");
443443 }
709709 if (!MaybeObj) {
710710 std::string Buf;
711711 raw_string_ostream OS(Buf);
712 logAllUnhandledErrors(MaybeObj.takeError(), OS, "");
712 logAllUnhandledErrors(MaybeObj.takeError(), OS);
713713 OS.flush();
714714 ErrorAndExit("unable to create object file: '" + Buf + "'");
715715 }
139139
140140 std::string Buf;
141141 raw_string_ostream OS(Buf);
142 logAllUnhandledErrors(std::move(E), OS, "");
142 logAllUnhandledErrors(std::move(E), OS);
143143 OS.flush();
144144 errs() << " " << Buf << "\n";
145145 }
157157
158158 std::string Buf;
159159 raw_string_ostream OS(Buf);
160 logAllUnhandledErrors(std::move(E), OS, "");
160 logAllUnhandledErrors(std::move(E), OS);
161161 OS.flush();
162162 errs() << " " << Buf << "\n";
163163 }
736736 Twine("Failed loading input file '") + Filename + "'",
737737 std::make_error_code(std::errc::invalid_argument)),
738738 TraceOrErr.takeError());
739 logAllUnhandledErrors(TraceOrErr.takeError(), errs(), "");
739 logAllUnhandledErrors(TraceOrErr.takeError(), errs());
740740 continue;
741741 }
742742 auto &T = *TraceOrErr;
187187 if (!SymbolNameOrErr) {
188188 std::string Buf;
189189 raw_string_ostream OS(Buf);
190 logAllUnhandledErrors(SymbolNameOrErr.takeError(), OS, "");
190 logAllUnhandledErrors(SymbolNameOrErr.takeError(), OS);
191191 OS.flush();
192192 report_fatal_error(Buf);
193193 }
4949 Input = "";
5050 std::string ErrMsg;
5151 raw_string_ostream OS(ErrMsg);
52 logAllUnhandledErrors(std::move(Err), OS, "");
52 logAllUnhandledErrors(std::move(Err), OS);
5353 OS.flush();
5454 errs() << "Error reading file: " << Input << ": " << ErrMsg;
5555 errs().flush();
111111
112112 auto Channels = createPairedQueueChannels();
113113
114 auto ReportError =
115 [](Error Err) {
116 logAllUnhandledErrors(std::move(Err), llvm::errs(), "");
117 };
114 auto ReportError = [](Error Err) {
115 logAllUnhandledErrors(std::move(Err), llvm::errs());
116 };
118117
119118 // Copy the bytes out of the test object: the copy will be used to verify
120119 // that the original is correctly transmitted over RPC to the mock layer.
224223
225224 auto Channels = createPairedQueueChannels();
226225
227 auto ReportError =
228 [](Error Err) {
229 logAllUnhandledErrors(std::move(Err), llvm::errs(), "");
230 };
226 auto ReportError = [](Error Err) {
227 logAllUnhandledErrors(std::move(Err), llvm::errs());
228 };
231229
232230 RPCEndpoint ClientEP(*Channels.first, true);
233231 RemoteObjectClientLayer Client(ClientEP, ReportError);
488486
489487 auto Channels = createPairedQueueChannels();
490488
491 auto ReportError =
492 [](Error Err) {
493 logAllUnhandledErrors(std::move(Err), llvm::errs(), "");
494 };
489 auto ReportError = [](Error Err) {
490 logAllUnhandledErrors(std::move(Err), llvm::errs());
491 };
495492
496493 RPCEndpoint ClientEP(*Channels.first, true);
497494 RemoteObjectClientLayer Client(ClientEP, ReportError);
433433 TEST(Error, StringError) {
434434 std::string Msg;
435435 raw_string_ostream S(Msg);
436 logAllUnhandledErrors(make_error("foo" + Twine(42),
437 inconvertibleErrorCode()),
438 S, "");
436 logAllUnhandledErrors(
437 make_error("foo" + Twine(42), inconvertibleErrorCode()), S);
439438 EXPECT_EQ(S.str(), "foo42\n") << "Unexpected StringError log result";
440439
441440 auto EC =
450449 std::string Msg;
451450 raw_string_ostream S(Msg);
452451 logAllUnhandledErrors(createStringError(EC, "foo%s%d0x%" PRIx8, Bar, 1, 0xff),
453 S, "");
452 S);
454453 EXPECT_EQ(S.str(), "foobar10xff\n")
455454 << "Unexpected createStringError() log result";
456455
457456 S.flush();
458457 Msg.clear();
459 logAllUnhandledErrors(createStringError(EC, Bar), S, "");
458 logAllUnhandledErrors(createStringError(EC, Bar), S);
460459 EXPECT_EQ(S.str(), "bar\n")
461460 << "Unexpected createStringError() (overloaded) log result";
462461