LLVM: include/llvm/Support/Error.h Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
13#ifndef LLVM_SUPPORT_ERROR_H
14#define LLVM_SUPPORT_ERROR_H
15
18#include "llvm/Config/abi-breaking.h"
25#include
26#include
27#include
28#include
29#include
30#include
31#include
32#include
33#include <system_error>
34#include <type_traits>
35#include
36#include
37
38namespace llvm {
39
41
42
43
45public:
47
48
50
51
52 virtual std::string message() const {
53 std::string Msg;
56 return Msg;
57 }
58
59
60
61
62
64
65
66 static const void *classID() { return &ID; }
67
68
70
71
72
73 virtual bool isA(const void *const ClassID) const {
74 return ClassID == classID();
75 }
76
77
78 template bool isA() const {
79 return isA(ErrorInfoT::classID());
80 }
81
82private:
83 virtual void anchor();
84
85 static char ID;
86};
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
160
161
162
164
165
166 template <typename... HandlerTs>
168
169 template
171
172
173
174 template friend class Expected;
175
176
178
179protected:
180
182 setPtr(nullptr);
183 setChecked(false);
184 }
185
186public:
187
189
190
192
193
194
195
197 setChecked(true);
198 *this = std::move(Other);
199 }
200
201
202
203 Error(std::unique_ptr Payload) {
204 setPtr(Payload.release());
205 setChecked(false);
206 }
207
208
210
211
212
213
214
216
217 assertIsChecked();
218 setPtr(Other.getPtr());
219
220
221 setChecked(false);
222
223
224 Other.setPtr(nullptr);
225 Other.setChecked(true);
226
227 return *this;
228 }
229
230
231
233 assertIsChecked();
235 }
236
237
238
239
240 explicit operator bool() {
241 setChecked(getPtr() == nullptr);
242 return getPtr() != nullptr;
243 }
244
245
246 template bool isA() const {
247 return getPtr() && getPtr()->isA(ErrT::classID());
248 }
249
250
251
254 return nullptr;
255 return getPtr()->dynamicClassID();
256 }
257
258private:
259#if LLVM_ENABLE_ABI_BREAKING_CHECKS
260
261
262
263
264
265 [[noreturn]] LLVM_ABI void fatalUncheckedError() const;
266#endif
267
268 void assertIsChecked() {
269#if LLVM_ENABLE_ABI_BREAKING_CHECKS
271 fatalUncheckedError();
272#endif
273 }
274
275 ErrorInfoBase *getPtr() const {
276#if LLVM_ENABLE_ABI_BREAKING_CHECKS
277 return reinterpret_cast<ErrorInfoBase*>(
278 reinterpret_cast<uintptr_t>(Payload) &
279 ~static_cast<uintptr_t>(0x1));
280#else
281 return Payload;
282#endif
283 }
284
285 void setPtr(ErrorInfoBase *EI) {
286#if LLVM_ENABLE_ABI_BREAKING_CHECKS
287 Payload = reinterpret_cast<ErrorInfoBase*>(
288 (reinterpret_cast<uintptr_t>(EI) &
289 ~static_cast<uintptr_t>(0x1)) |
290 (reinterpret_cast<uintptr_t>(Payload) & 0x1));
291#else
292 Payload = EI;
293#endif
294 }
295
296 bool getChecked() const {
297#if LLVM_ENABLE_ABI_BREAKING_CHECKS
298 return (reinterpret_cast<uintptr_t>(Payload) & 0x1) == 0;
299#else
300 return true;
301#endif
302 }
303
304 void setChecked(bool V) {
305#if LLVM_ENABLE_ABI_BREAKING_CHECKS
306 Payload = reinterpret_cast<ErrorInfoBase*>(
307 (reinterpret_cast<uintptr_t>(Payload) &
308 ~static_cast<uintptr_t>(0x1)) |
309 (V ? 0 : 1));
310#endif
311 }
312
313 std::unique_ptr takePayload() {
314 std::unique_ptr Tmp(getPtr());
315 setPtr(nullptr);
316 setChecked(true);
317 return Tmp;
318 }
319
322 P->log(OS);
323 else
324 OS << "success";
325 return OS;
326 }
327
329};
330
331
332
333
335
337
338
339
340template <typename ErrT, typename... ArgTs> Error make_error(ArgTs &&... Args) {
341 return Error(std::make_unique(std::forward(Args)...));
342}
343
344
345
346
347
348
349
350
351
352
353template <typename ThisErrT, typename ParentErrT = ErrorInfoBase>
355public:
356 using ParentErrT::ParentErrT;
357
358 static const void *classID() { return &ThisErrT::ID; }
359
360 const void *dynamicClassID() const override { return &ThisErrT::ID; }
361
362 bool isA(const void *const ClassID) const override {
363 return ClassID == classID() || ParentErrT::isA(ClassID);
364 }
365};
366
367
368
370
371
372 template <typename... HandlerTs>
374
375
376 template
378
379
381
382public:
384 OS << "Multiple errors:\n";
385 for (const auto &ErrPayload : Payloads) {
386 ErrPayload->log(OS);
387 OS << "\n";
388 }
389 }
390
391 std::error_code convertToErrorCode() const override;
392
393
395
396private:
397 ErrorList(std::unique_ptr Payload1,
398 std::unique_ptr Payload2) {
399 assert(!Payload1->isA() && !Payload2->isA() &&
400 "ErrorList constructor payloads should be singleton errors");
401 Payloads.push_back(std::move(Payload1));
402 Payloads.push_back(std::move(Payload2));
403 }
404
405
408
410 if (!E1)
411 return E2;
412 if (!E2)
413 return E1;
415 auto &E1List = static_cast<ErrorList &>(*E1.getPtr());
417 auto E2Payload = E2.takePayload();
418 auto &E2List = static_cast<ErrorList &>(*E2Payload);
419 for (auto &Payload : E2List.Payloads)
420 E1List.Payloads.push_back(std::move(Payload));
421 } else {
422 E1List.Payloads.push_back(E2.takePayload());
423 }
424
425 return E1;
426 }
427 if (E2.isA()) {
428 auto &E2List = static_cast<ErrorList &>(*E2.getPtr());
429 E2List.Payloads.insert(E2List.Payloads.begin(), E1.takePayload());
430 return E2;
431 }
432 return Error(std::unique_ptr(
433 new ErrorList(E1.takePayload(), E2.takePayload())));
434 }
435
436 std::vector<std::unique_ptr> Payloads;
437};
438
439
440
441
443 return ErrorList::join(std::move(E1), std::move(E2));
444}
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485template class [[nodiscard]] Expected {
487 template friend class Expected;
488
489 static constexpr bool isRef = std::is_reference_v;
490
491 using wrap = std::reference_wrapper<std::remove_reference_t>;
492
493 using error_type = std::unique_ptr;
494
495public:
498
499private:
500 using reference = std::remove_reference_t &;
501 using const_reference = const std::remove_reference_t &;
502 using pointer = std::remove_reference_t *;
503 using const_pointer = const std::remove_reference_t *;
504
505public:
506
508 : HasError(true)
509#if LLVM_ENABLE_ABI_BREAKING_CHECKS
510
512#endif
513 {
514 assert(Err && "Cannot create Expected from Error success value.");
515 new (getErrorStorage()) error_type(Err.takePayload());
516 }
517
518
519
520
522
523
524
525 template
527 std::enable_if_t<std::is_convertible_v<OtherT, T>> * = nullptr)
528 : HasError(false)
529#if LLVM_ENABLE_ABI_BREAKING_CHECKS
530
531 ,
533#endif
534 {
535 new (getStorage()) storage_type(std::forward(Val));
536 }
537
538
540
541
542
543 template
545 std::enable_if_t<std::is_convertible_v<OtherT, T>> * = nullptr) {
546 moveConstruct(std::move(Other));
547 }
548
549
550
551 template
554 std::enable_if_t<!std::is_convertible_v<OtherT, T>> * = nullptr) {
555 moveConstruct(std::move(Other));
556 }
557
558
560 moveAssign(std::move(Other));
561 return *this;
562 }
563
564
566 assertIsChecked();
567 if (!HasError)
568 getStorage()->~storage_type();
569 else
570 getErrorStorage()->~error_type();
571 }
572
573
574 explicit operator bool() {
575#if LLVM_ENABLE_ABI_BREAKING_CHECKS
577#endif
578 return !HasError;
579 }
580
581
583 assertIsChecked();
584 return *getStorage();
585 }
586
587
588 const_reference get() const {
589 assertIsChecked();
591 }
592
593
594 template
597 std::enable_if_t<std::is_assignable_v<OtherT &, T &&>> * = nullptr) && {
598 if (*this)
601 }
602
603
604 template bool errorIsA() const {
605 return HasError && (*getErrorStorage())->template isA();
606 }
607
608
609
610
611
613#if LLVM_ENABLE_ABI_BREAKING_CHECKS
615#endif
616 return HasError ? Error(std::move(*getErrorStorage())) : Error::success();
617 }
618
619
621 assertIsChecked();
622 return toPointer(getStorage());
623 }
624
625
627 assertIsChecked();
628 return toPointer(getStorage());
629 }
630
631
633 assertIsChecked();
634 return *getStorage();
635 }
636
637
639 assertIsChecked();
640 return *getStorage();
641 }
642
643private:
644 template
645 static bool compareThisIfSameType(const T1 &a, const T1 &b) {
646 return &a == &b;
647 }
648
649 template <class T1, class T2>
650 static bool compareThisIfSameType(const T1 &, const T2 &) {
651 return false;
652 }
653
654 template void moveConstruct(Expected &&Other) {
655 HasError = Other.HasError;
656#if LLVM_ENABLE_ABI_BREAKING_CHECKS
658 Other.Unchecked = false;
659#endif
660
661 if (!HasError)
662 new (getStorage()) storage_type(std::move(*Other.getStorage()));
663 else
664 new (getErrorStorage()) error_type(std::move(*Other.getErrorStorage()));
665 }
666
667 template void moveAssign(Expected &&Other) {
668 assertIsChecked();
669
670 if (compareThisIfSameType(*this, Other))
671 return;
672
673 this->~Expected();
674 new (this) Expected(std::move(Other));
675 }
676
677 pointer toPointer(pointer Val) { return Val; }
678
679 const_pointer toPointer(const_pointer Val) const { return Val; }
680
681 pointer toPointer(wrap *Val) { return &Val->get(); }
682
683 const_pointer toPointer(const wrap *Val) const { return &Val->get(); }
684
685 storage_type *getStorage() {
686 assert(!HasError && "Cannot get value when an error exists!");
687 return &TStorage;
688 }
689
690 const storage_type *getStorage() const {
691 assert(!HasError && "Cannot get value when an error exists!");
692 return &TStorage;
693 }
694
695 error_type *getErrorStorage() {
696 assert(HasError && "Cannot get error when a value exists!");
697 return &ErrorStorage;
698 }
699
700 const error_type *getErrorStorage() const {
701 assert(HasError && "Cannot get error when a value exists!");
702 return &ErrorStorage;
703 }
704
705
706 void setUnchecked() {
707#if LLVM_ENABLE_ABI_BREAKING_CHECKS
709#endif
710 }
711
712#if LLVM_ENABLE_ABI_BREAKING_CHECKS
714 dbgs() << "Expected must be checked before access or destruction.\n";
715 if (HasError) {
716 dbgs() << "Unchecked Expected contained error:\n";
717 (*getErrorStorage())->log(dbgs());
718 } else {
719 dbgs() << "Expected value was in success state. (Note: Expected "
720 "values in success mode must still be checked prior to being "
721 "destroyed).\n";
722 }
723 abort();
724 }
725#endif
726
727 void assertIsChecked() const {
728#if LLVM_ENABLE_ABI_BREAKING_CHECKS
730 fatalUncheckedExpected();
731#endif
732 }
733
734 union {
737 };
738 bool HasError : 1;
739#if LLVM_ENABLE_ABI_BREAKING_CHECKS
741#endif
742};
743
744
745
747 bool gen_crash_diag = true);
748
749
750
752
753
755
756
757
758
759
760
761
762
763
764
765
766
767
768
770 if (Err) {
771 if (!Msg)
772 Msg = "Failure value returned from cantFail wrapped call";
773#ifndef NDEBUG
774 std::string Str;
776 OS << Msg << "\n" << Err;
777 Msg = Str.c_str();
778#endif
780 }
781}
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796template
798 if (ValOrErr)
799 return std::move(*ValOrErr);
800 else {
801 if (!Msg)
802 Msg = "Failure value returned from cantFail wrapped call";
803#ifndef NDEBUG
804 std::string Str;
806 auto E = ValOrErr.takeError();
807 OS << Msg << "\n" << E;
808 Msg = Str.c_str();
809#endif
811 }
812}
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827template
829 if (ValOrErr)
830 return *ValOrErr;
831 else {
832 if (!Msg)
833 Msg = "Failure value returned from cantFail wrapped call";
834#ifndef NDEBUG
835 std::string Str;
837 auto E = ValOrErr.takeError();
838 OS << Msg << "\n" << E;
839 Msg = Str.c_str();
840#endif
842 }
843}
844
845
846
847template
850 decltype(&std::remove_reference_t::operator())> {};
851
852
854public:
856 return E.template isA();
857 }
858
859 template
860 static Error apply(HandlerT &&H, std::unique_ptr E) {
862 return H(static_cast<ErrT &>(*E));
863 }
864};
865
866
868public:
870 return E.template isA();
871 }
872
873 template
874 static Error apply(HandlerT &&H, std::unique_ptr E) {
876 H(static_cast<ErrT &>(*E));
878 }
879};
880
881
882template
884public:
886 return E.template isA();
887 }
888
889 template
890 static Error apply(HandlerT &&H, std::unique_ptr E) {
892 std::unique_ptr SubE(static_cast<ErrT *>(E.release()));
893 return H(std::move(SubE));
894 }
895};
896
897
898template
900public:
902 return E.template isA();
903 }
904
905 template
906 static Error apply(HandlerT &&H, std::unique_ptr E) {
908 std::unique_ptr SubE(static_cast<ErrT *>(E.release()));
909 H(std::move(SubE));
911 }
912};
913
914
915template <typename C, typename RetT, typename ErrT>
918
919
920template <typename C, typename RetT, typename ErrT>
923
924
925template <typename C, typename RetT, typename ErrT>
928
929
930template <typename C, typename RetT, typename ErrT>
933
934
935
936template <typename C, typename RetT, typename ErrT>
939
940
941
942template <typename C, typename RetT, typename ErrT>
945
947 return Error(std::move(Payload));
948}
949
950template <typename HandlerT, typename... HandlerTs>
952 HandlerT &&Handler, HandlerTs &&... Handlers) {
955 std::move(Payload));
957 std::forward(Handlers)...);
958}
959
960
961
962
963
964
965
966template <typename... HandlerTs>
968 if ()
970
971 std::unique_ptr Payload = E.takePayload();
972
976 for (auto &P : List.Payloads)
977 R = ErrorList::join(
978 std::move(R),
979 handleErrorImpl(std::move(P), std::forward(Hs)...));
980 return R;
981 }
982
983 return handleErrorImpl(std::move(Payload), std::forward(Hs)...);
984}
985
986
987
988
989template <typename... HandlerTs>
993
994
995
999
1000
1001
1004 if (!Payload)
1005 return;
1006
1007 if (Payload->isA<ErrorList>()) {
1009 for (const auto &P : List.Payloads)
1011 return;
1012 }
1013
1014 return H(*Payload);
1015}
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041template <typename T, typename RecoveryFtor, typename... HandlerTs>
1043 HandlerTs &&... Handlers) {
1044 if (ValOrErr)
1045 return ValOrErr;
1046
1047 if (auto Err = handleErrors(ValOrErr.takeError(),
1048 std::forward(Handlers)...))
1049 return std::move(Err);
1050
1051 return RecoveryPath();
1052}
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1066 Twine ErrorBanner = {});
1067
1068
1069
1071
1072
1073
1075
1076
1077
1078
1079
1080
1081
1082
1086
1087
1088
1089
1090
1091
1092
1093
1095 if (E)
1096 return std::move(*E);
1098 return std::nullopt;
1099}
1100
1102 if (E)
1103 return std::move(*E);
1105 return std::nullopt;
1106}
1107
1108
1109
1110
1111
1112
1114 bool IsError = static_cast<bool>(Err);
1115 if (IsError)
1117 return IsError;
1118}
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1145public:
1146
1148
1149 if (Err)
1150 (void)!!*Err;
1151 }
1152
1156
1158
1159 if (Err && !*Err)
1161 }
1162
1163private:
1165};
1166
1167
1168
1169
1170template
1172public:
1174 : ValOrErr(ValOrErr) {
1175 if (ValOrErr)
1176 (void)!!*ValOrErr;
1177 }
1178
1180 if (ValOrErr)
1181 ValOrErr->setUnchecked();
1182 }
1183
1184private:
1186};
1187
1188
1189
1190
1191
1192
1195
1196 void anchor() override;
1197
1198public:
1202
1203
1205
1206protected:
1209
1211};
1212
1213
1214
1215
1216
1217
1218
1220
1221
1223
1224
1225
1226
1227
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1241 return std::error_code(errno, std::generic_category());
1242}
1243
1244
1246 if (auto EC = EO.getError())
1248 return std::move(*EO);
1249}
1250
1251
1253 if (auto Err = E.takeError())
1255 return std::move(*E);
1256}
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1283public:
1285
1286 StringError(std::string &&S, std::error_code EC, bool PrintMsgOnly);
1287
1289
1291
1294
1295 const std::string &getMessage() const { return Msg; }
1296
1297private:
1298 std::string Msg;
1299 std::error_code EC;
1300 const bool PrintMsgOnly = false;
1301};
1302
1303
1304template <typename... Ts>
1306 const Ts &... Vals) {
1307 std::string Buffer;
1310}
1311
1313
1317
1321
1322
1326
1327template <typename... Ts>
1331
1332template <typename... Ts>
1334 const Ts &... Vals) {
1335 return createStringError(std::make_error_code(EC), Fmt, Vals...);
1336}
1337
1338
1339
1340
1341
1343
1346
1347public:
1349 assert(Err && "Trying to log after takeError().");
1350 OS << "'" << FileName << "': ";
1351 if (Line)
1352 OS << "line " << *Line << ": ";
1353 Err->log(OS);
1354 }
1355
1357 std::string Msg;
1359 Err->log(OS);
1360 return Msg;
1361 }
1362
1364
1366
1367 std::error_code convertToErrorCode() const override;
1368
1369
1371
1372private:
1373 FileError(const Twine &F, std::optional<size_t> LineNum,
1374 std::unique_ptr E) {
1375 assert(E && "Cannot create FileError from Error success value.");
1376 FileName = F.str();
1377 Err = std::move(E);
1378 Line = std::move(LineNum);
1379 }
1380
1381 static Error build(const Twine &F, std::optional<size_t> Line, Error E) {
1382 std::unique_ptr Payload;
1384 [&](std::unique_ptr EIB) -> Error {
1385 Payload = std::move(EIB);
1387 });
1388 return Error(
1389 std::unique_ptr(new FileError(F, Line, std::move(Payload))));
1390 }
1391
1392 std::string FileName;
1393 std::optional<size_t> Line;
1394 std::unique_ptr Err;
1395};
1396
1397
1398
1400 return FileError::build(F, std::optional<size_t>(), std::move(E));
1401}
1402
1403
1404
1406 return FileError::build(F, std::optional<size_t>(Line), std::move(E));
1407}
1408
1409
1410
1414
1415
1416
1420
1421
1422
1424 const Twine &S) {
1427}
1428
1429
1430
1431template <typename... Ts>
1433 char const *Fmt, const Ts &...Vals) {
1436}
1437
1439
1440
1441
1442
1443
1445public:
1446
1447 ExitOnError(std::string Banner = "", int DefaultErrorExitCode = 1)
1448 : Banner(std::move(Banner)),
1449 GetExitCode([=](const Error &) { return DefaultErrorExitCode; }) {}
1450
1451
1452 void setBanner(std::string Banner) { this->Banner = std::move(Banner); }
1453
1454
1456 this->GetExitCode = std::move(GetExitCode);
1457 }
1458
1459
1461
1462
1463
1465 checkError(E.takeError());
1466 return std::move(*E);
1467 }
1468
1469
1470
1472 checkError(E.takeError());
1473 return *E;
1474 }
1475
1476private:
1477 void checkError(Error Err) const {
1478 if (Err) {
1479 int ExitCode = GetExitCode(Err);
1481 exit(ExitCode);
1482 }
1483 }
1484
1485 std::string Banner;
1486 std::function<int(const Error &)> GetExitCode;
1487};
1488
1489
1493
1494
1496 return Error(std::unique_ptr(
1498}
1499
1500}
1501
1502#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_UNLIKELY(EXPR)
#define LLVM_ATTRIBUTE_NOINLINE
LLVM_ATTRIBUTE_NOINLINE - On compilers where we have a directive to do so, mark a method "not for inl...
Provides ErrorOr smart pointer.
static LLVMTargetMachineRef wrap(const TargetMachine *P)
static const char * getPtr(const MachOObjectFile &O, size_t Offset, size_t MachOFilesetEntryOffset=0)
std::error_code EC
Definition Error.h:1210
LLVM_ABI friend Error errorCodeToError(std::error_code)
Helper for converting an std::error_code to a Error.
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
Definition Error.h:1200
ECError(std::error_code EC)
Definition Error.h:1208
void setErrorCode(std::error_code EC)
Definition Error.h:1199
void log(raw_ostream &OS) const override
Print an error message to an output stream.
Definition Error.h:1201
static char ID
Definition Error.h:1204
ErrorAsOutParameter(Error *Err)
Definition Error.h:1147
ErrorAsOutParameter(Error &Err)
Definition Error.h:1153
~ErrorAsOutParameter()
Definition Error.h:1157
static bool appliesTo(const ErrorInfoBase &E)
Definition Error.h:855
static Error apply(HandlerT &&H, std::unique_ptr< ErrorInfoBase > E)
Definition Error.h:860
static Error apply(HandlerT &&H, std::unique_ptr< ErrorInfoBase > E)
Definition Error.h:890
static bool appliesTo(const ErrorInfoBase &E)
Definition Error.h:885
static bool appliesTo(const ErrorInfoBase &E)
Definition Error.h:869
static Error apply(HandlerT &&H, std::unique_ptr< ErrorInfoBase > E)
Definition Error.h:874
static bool appliesTo(const ErrorInfoBase &E)
Definition Error.h:901
static Error apply(HandlerT &&H, std::unique_ptr< ErrorInfoBase > E)
Definition Error.h:906
Helper for testing applicability of, and applying, handlers for ErrorInfo types.
Definition Error.h:850
Base class for error info classes.
Definition Error.h:44
virtual ~ErrorInfoBase()=default
virtual std::string message() const
Return the error message as a string.
Definition Error.h:52
static const void * classID()
Definition Error.h:66
bool isA() const
Definition Error.h:78
virtual const void * dynamicClassID() const =0
virtual bool isA(const void *const ClassID) const
Definition Error.h:73
virtual std::error_code convertToErrorCode() const =0
Convert this error to a std::error_code.
virtual void log(raw_ostream &OS) const =0
Print an error message to an output stream.
Base class for user error types.
Definition Error.h:354
bool isA(const void *const ClassID) const override
Definition Error.h:362
const void * dynamicClassID() const override
Definition Error.h:360
static const void * classID()
Definition Error.h:358
Special ErrorInfo subclass representing a list of ErrorInfos.
Definition Error.h:369
friend Error handleErrors(Error E, HandlerTs &&... Handlers)
Pass the ErrorInfo(s) contained in E to their respective handlers.
Definition Error.h:967
void log(raw_ostream &OS) const override
Print an error message to an output stream.
Definition Error.h:383
static char ID
Definition Error.h:394
friend Error joinErrors(Error, Error)
Concatenate errors.
Definition Error.h:442
friend void visitErrors(const Error &E, HandlerT H)
Visit all the ErrorInfo(s) contained in E by passing them to the respective handler,...
Definition Error.h:1002
Represents either an error or a value T.
Subclass of Error for the sole purpose of identifying the success path in the type system.
Definition Error.h:334
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Error(Error &&Other)
Move-construct an error value.
Definition Error.h:196
friend Error handleErrors(Error E, HandlerTs &&... Handlers)
Pass the ErrorInfo(s) contained in E to their respective handlers.
Definition Error.h:967
~Error()
Destroy a Error.
Definition Error.h:232
const void * dynamicClassID() const
Returns the dynamic class id of this error, or null if this is a success value.
Definition Error.h:252
friend raw_ostream & operator<<(raw_ostream &OS, const Error &E)
Definition Error.h:320
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Error(std::unique_ptr< ErrorInfoBase > Payload)
Create an error value.
Definition Error.h:203
Error & operator=(const Error &Other)=delete
Error()
Create a success value. Prefer using 'Error::success()' for readability.
Definition Error.h:181
friend class Expected
Definition Error.h:174
Error(const Error &Other)=delete
friend void visitErrors(const Error &E, HandlerT H)
Visit all the ErrorInfo(s) contained in E by passing them to the respective handler,...
Definition Error.h:1002
Error & operator=(Error &&Other)
Move-assign an error value.
Definition Error.h:215
friend class ErrorList
Definition Error.h:163
bool isA() const
Check whether one error is a subclass of another.
Definition Error.h:246
void setBanner(std::string Banner)
Set the banner string for any errors caught by operator().
Definition Error.h:1452
ExitOnError(std::string Banner="", int DefaultErrorExitCode=1)
Create an error on exit helper.
Definition Error.h:1447
T operator()(Expected< T > &&E) const
Check E.
Definition Error.h:1464
T & operator()(Expected< T & > &&E) const
Check E.
Definition Error.h:1471
void operator()(Error Err) const
Check Err. If it's in a failure state log the error(s) and exit.
Definition Error.h:1460
void setExitCodeMapper(std::function< int(const Error &)> GetExitCode)
Set the exit-code mapper function.
Definition Error.h:1455
~ExpectedAsOutParameter()
Definition Error.h:1179
ExpectedAsOutParameter(Expected< T > *ValOrErr)
Definition Error.h:1173
Tagged union holding either a T or a Error.
Definition Error.h:485
const_reference operator*() const
Returns a const reference to the stored T value.
Definition Error.h:638
Expected(Expected< OtherT > &&Other, std::enable_if_t<!std::is_convertible_v< OtherT, T > > *=nullptr)
Move construct an Expected value from an Expected, where OtherT isn't convertible to T.
Definition Error.h:552
Error moveInto(OtherT &Value, std::enable_if_t< std::is_assignable_v< OtherT &, T && > > *=nullptr) &&
Returns takeError() after moving the held T (if any) into V.
Definition Error.h:595
pointer operator->()
Returns a pointer to the stored T value.
Definition Error.h:620
reference operator*()
Returns a reference to the stored T value.
Definition Error.h:632
Expected(OtherT &&Val, std::enable_if_t< std::is_convertible_v< OtherT, T > > *=nullptr)
Create an Expected success value from the given OtherT value, which must be convertible to T.
Definition Error.h:526
~Expected()
Destroy an Expected.
Definition Error.h:565
const_reference get() const
Returns a const reference to the stored T value.
Definition Error.h:588
bool errorIsA() const
Check that this Expected is an error of type ErrT.
Definition Error.h:604
Expected(ErrorSuccess)=delete
Forbid to convert from Error::success() implicitly, this avoids having Expected foo() { return Err...
friend class Expected
Definition Error.h:487
Expected(Error &&Err)
Create an Expected error value from the given Error.
Definition Error.h:507
Error takeError()
Definition Error.h:612
friend class ExpectedAsOutParameter
Definition Error.h:486
const_pointer operator->() const
Returns a const pointer to the stored T value.
Definition Error.h:626
Expected & operator=(Expected &&Other)
Move-assign from another Expected.
Definition Error.h:559
reference get()
Returns a reference to the stored T value.
Definition Error.h:582
storage_type TStorage
Definition Error.h:735
std::conditional_t< isRef, wrap, std::unique_ptr< InFlightAlloc > > storage_type
Definition Error.h:496
error_type ErrorStorage
Definition Error.h:736
Expected(Expected &&Other)
Move construct an Expected value.
Definition Error.h:539
std::unique_ptr< InFlightAlloc > value_type
Definition Error.h:497
Expected(Expected< OtherT > &&Other, std::enable_if_t< std::is_convertible_v< OtherT, T > > *=nullptr)
Move construct an Expected value from an Expected, where OtherT must be convertible to T.
Definition Error.h:544
Error takeError()
Definition Error.h:1365
static char ID
Definition Error.h:1370
std::string messageWithoutFileInfo() const
Definition Error.h:1356
StringRef getFileName() const
Definition Error.h:1363
void log(raw_ostream &OS) const override
Print an error message to an output stream.
Definition Error.h:1348
friend Error createFileError(const Twine &, Error)
Concatenate a source file path and/or name with an Error.
Definition Error.h:1399
void log(raw_ostream &OS) const override
Print an error message to an output stream.
StringError(std::string &&S, std::error_code EC, bool PrintMsgOnly)
const std::string & getMessage() const
Definition Error.h:1295
std::error_code convertToErrorCode() const override
Convert this error to a std::error_code.
static char ID
Definition Error.h:1284
StringRef - Represent a constant reference to a string, i.e.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
LLVM_ABI std::string str() const
Return the twine contents as a std::string.
LLVM Value Representation.
This class implements an extremely fast bulk output stream that can only output to a stream.
A raw_ostream that writes to an std::string.
struct LLVMOpaqueError * LLVMErrorRef
Opaque reference to an error instance.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ C
The default llvm calling convention, compatible with C.
This is an optimization pass for GlobalISel generic memory operations.
bool errorToBool(Error Err)
Helper for converting an Error to a bool.
Definition Error.h:1113
LLVM_ABI void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner={})
Log all errors (if any) in E to OS.
void visitErrors(const Error &E, HandlerT H)
Visit all the ErrorInfo(s) contained in E by passing them to the respective handler,...
Definition Error.h:1002
Error createFileError(const Twine &F, Error E)
Concatenate a source file path and/or name with an Error.
Definition Error.h:1399
std::optional< T > expectedToStdOptional(Expected< T > &&E)
Definition Error.h:1101
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
Definition Error.h:990
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Error handleErrors(Error E, HandlerTs &&... Hs)
Pass the ErrorInfo(s) contained in E to their respective handlers.
Definition Error.h:967
ErrorOr< T > expectedToErrorOr(Expected< T > &&E)
Convert an Expected to an ErrorOr.
Definition Error.h:1252
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1305
LLVM_ABI void reportFatalInternalError(Error Err)
Report a fatal error that indicates a bug in LLVM.
std::optional< T > expectedToOptional(Expected< T > &&E)
Convert an Expected to an Optional without doing anything.
Definition Error.h:1094
LLVM_ABI std::string toStringWithoutConsuming(const Error &E)
Like toString(), but does not consume the error.
Error joinErrors(Error E1, Error E2)
Concatenate errors.
Definition Error.h:442
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Expected< T > handleExpected(Expected< T > ValOrErr, RecoveryFtor &&RecoveryPath, HandlerTs &&... Handlers)
Handle any errors (if present) in an Expected, then try a recovery path.
Definition Error.h:1042
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Error handleErrorImpl(std::unique_ptr< ErrorInfoBase > Payload)
Definition Error.h:946
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition Error.h:769
std::string join(IteratorT Begin, IteratorT End, StringRef Separator)
Joins the strings in the range [Begin, End), adding Separator between the elements.
Attribute unwrap(LLVMAttributeRef Attr)
Expected< T > errorOrToExpected(ErrorOr< T > &&EO)
Convert an ErrorOr to an Expected.
Definition Error.h:1245
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
LLVMAttributeRef wrap(Attribute Attr)
LLVM_ABI Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
std::error_code errnoAsErrorCode()
Helper to get errno as an std::error_code.
Definition Error.h:1240
LogicalResult success(bool IsSuccess=true)
Utility function to generate a LogicalResult.
LLVM_ABI std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
void consumeError(Error Err)
Consume a Error without doing anything.
Definition Error.h:1083
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Implement std::hash so that hash_code can be used in STL containers.