| 1 | // Copyright (C) Microsoft Corporation. All rights reserved. |
| 2 | // |
| 3 | // Defines macros similar to those in <wil/result_macros.h> for use with kernel mode components. |
| 4 | // Also defines result macros for use with <unexpected.h> for either kernel or user mode. |
| 5 | #pragma once |
| 6 | |
| 7 | /// Static information for an error site. |
| 8 | struct ErrorSite |
| 9 | { |
| 10 | PCSTR file; |
| 11 | PCSTR function; |
| 12 | int line; |
| 13 | PCSTR messageFormat; |
| 14 | }; |
| 15 | |
| 16 | // No logging for Linux at the moment. |
| 17 | #define ON_FAILURE_WITH_SOURCE(str, status) |
| 18 | #define ON_FAILURE_WITH_SOURCE_MSG(str, status, messageFormat, ...) |
| 19 | |
| 20 | #define RETURN_ERROR_IF_UNEXPECTED(expected) \ |
| 21 | do \ |
| 22 | { \ |
| 23 | auto __localError = (expected).OptionalError(); \ |
| 24 | if (__localError) \ |
| 25 | { \ |
| 26 | ON_FAILURE_WITH_SOURCE(#expected, *__localError); \ |
| 27 | return *__localError; \ |
| 28 | } \ |
| 29 | } while (0) |
| 30 | |
| 31 | #define RETURN_ERROR_IF_UNEXPECTED_MSG(expected, messageFormat, ...) \ |
| 32 | do \ |
| 33 | { \ |
| 34 | auto __localError = (expected).OptionalError(); \ |
| 35 | if (__localError) \ |
| 36 | { \ |
| 37 | ON_FAILURE_WITH_SOURCE_MSG(#expected, *__localError, messageFormat, __VA_ARGS__); \ |
| 38 | return *__localError; \ |
| 39 | } \ |
| 40 | } while (0) |
| 41 | |
| 42 | #define RETURN_IF_UNEXPECTED(expected) \ |
| 43 | do \ |
| 44 | { \ |
| 45 | auto __localError = (expected).OptionalError(); \ |
| 46 | if (__localError) \ |
| 47 | { \ |
| 48 | ON_FAILURE_WITH_SOURCE(#expected, *__localError); \ |
| 49 | return ::util::Unexpected{*__localError}; \ |
| 50 | } \ |
| 51 | } while (0) |
| 52 | |
| 53 | #define RETURN_IF_UNEXPECTED_MSG(expected, messageFormat, ...) \ |
| 54 | do \ |
| 55 | { \ |
| 56 | auto __localError = (expected).OptionalError(); \ |
| 57 | if (__localError) \ |
| 58 | { \ |
| 59 | ON_FAILURE_WITH_SOURCE_MSG(#expected, *__localError, messageFormat, __VA_ARGS__); \ |
| 60 | return ::util::Unexpected{*__localError}; \ |
| 61 | } \ |
| 62 | } while (0) |
| 63 | |
| 64 | #define RETURN_UNEXPECTED_IF_NTSTATUS_FAILED(status) \ |
| 65 | do \ |
| 66 | { \ |
| 67 | NTSTATUS __localStatus = (status); \ |
| 68 | if (!NT_SUCCESS(__localStatus)) \ |
| 69 | { \ |
| 70 | ON_FAILURE_WITH_SOURCE(#status, __localStatus); \ |
| 71 | return ::util::Unexpected{__localStatus}; \ |
| 72 | } \ |
| 73 | } while (0) |
| 74 | |
| 75 | #define RETURN_UNEXPECTED_IF_NTSTATUS_FAILED_MSG(status, message, ...) \ |
| 76 | do \ |
| 77 | { \ |
| 78 | NTSTATUS __localStatus = (status); \ |
| 79 | if (!NT_SUCCESS(__localStatus)) \ |
| 80 | { \ |
| 81 | ON_FAILURE_WITH_SOURCE_MSG(#status, __localStatus, message, __VA_ARGS__); \ |
| 82 | return ::util::Unexpected{__localStatus}; \ |
| 83 | } \ |
| 84 | } while (0) |