diff options
author | David Neto <dneto@google.com> | 2015-10-09 15:48:09 -0400 |
---|---|---|
committer | David Neto <dneto@google.com> | 2015-10-26 12:55:33 -0400 |
commit | ac508b0d80087582f9c32b54e467f539d5ab04c8 (patch) | |
tree | a48e9e97e8592f747c340ae0cb5d2edac5fe2d5f /source/diagnostic.h | |
parent | cc936dc61321abe51e285bc1bf21d8b017ebd8ff (diff) |
DiagnosticStream can convert to a stored error code
Use this to shorten error return code in the assembler.
For example, change this:
if (error = something()) {
diagnostic() << " Bad integer literal " << value;
return SPV_ERROR_INVALID_VALUE;
}
to this:
if (error = something())
return diagnostic() << " Bad integer literal " << value;
Also shorten code due to the fact that binaryEncodeU32 and
binaryCodeU64 can't fail (short of failure to expand a std::vector).
Diffstat (limited to 'source/diagnostic.h')
-rw-r--r-- | source/diagnostic.h | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/source/diagnostic.h b/source/diagnostic.h index 4e6d3789..27be491e 100644 --- a/source/diagnostic.h +++ b/source/diagnostic.h @@ -31,6 +31,7 @@ #include <iostream> #include <sstream> +#include <utility> class diagnostic_helper { public: @@ -51,20 +52,26 @@ class diagnostic_helper { spv_diagnostic *pDiagnostic; }; -// On destruction of the diagnostic stream, a diagnostic message will be -// written to pDiagnostic containing all of the data written to the stream. +// A DiagnosticStream remembers the current position of the input and an error +// code, and captures diagnostic messages via the left-shift operator. +// Captured messages are emitted during the destructor. // TODO(awoloszyn): This is very similar to diagnostic_helper, and hides // the data more easily. Replace diagnostic_helper elsewhere // eventually. class DiagnosticStream { public: - DiagnosticStream(spv_position position, spv_diagnostic *pDiagnostic) - : position_(position), pDiagnostic_(pDiagnostic) {} + DiagnosticStream(spv_position position, spv_diagnostic *pDiagnostic, + spv_result_t error) + : position_(position), pDiagnostic_(pDiagnostic), error_(error) {} - DiagnosticStream(DiagnosticStream &&other) : position_(other.position_) { - stream_.str(other.stream_.str()); - other.stream_.str(""); - pDiagnostic_ = other.pDiagnostic_; + DiagnosticStream(DiagnosticStream &&other) + : stream_(other.stream_.str()), + position_(other.position_), + pDiagnostic_(other.pDiagnostic_), + error_(other.error_) { + // The other object's destructor will emit the text in its stream_ + // member if its pDiagnostic_ member is non-null. Prevent that, + // since emitting that text is now the responsibility of *this. other.pDiagnostic_ = nullptr; } @@ -77,10 +84,14 @@ class DiagnosticStream { return *this; } + // Conversion operator to spv_result, returning the error code. + operator spv_result_t() { return error_; } + private: std::stringstream stream_; spv_position position_; spv_diagnostic *pDiagnostic_; + spv_result_t error_; }; #define DIAGNOSTIC \ |