2021-02-17 20:13:34 +00:00
|
|
|
// Copyright 2021 The Tint Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#ifndef SRC_DEBUG_H_
|
|
|
|
#define SRC_DEBUG_H_
|
|
|
|
|
2021-02-18 16:33:38 +00:00
|
|
|
#include <utility>
|
2021-02-17 20:13:34 +00:00
|
|
|
|
|
|
|
#include "src/diagnostic/diagnostic.h"
|
|
|
|
#include "src/diagnostic/formatter.h"
|
|
|
|
#include "src/diagnostic/printer.h"
|
|
|
|
|
|
|
|
namespace tint {
|
|
|
|
|
|
|
|
/// Function type used for registering an internal compiler error reporter
|
|
|
|
using InternalCompilerErrorReporter = void(const diag::List&);
|
|
|
|
|
|
|
|
/// Sets the global error reporter to be called in case of internal compiler
|
|
|
|
/// errors.
|
|
|
|
/// @param reporter the error reporter
|
|
|
|
void SetInternalCompilerErrorReporter(InternalCompilerErrorReporter* reporter);
|
|
|
|
|
2021-02-18 16:33:38 +00:00
|
|
|
/// InternalCompilerError is a helper for reporting internal compiler errors.
|
|
|
|
/// Construct the InternalCompilerError with the source location of the ICE
|
|
|
|
/// fault and append any error details with the `<<` operator.
|
|
|
|
/// When the InternalCompilerError is destructed, the concatenated error message
|
|
|
|
/// is appended to the diagnostics list with the severity of
|
|
|
|
/// tint::diag::Severity::InternalCompilerError, and if a
|
|
|
|
/// InternalCompilerErrorReporter is set, then it is called with the diagnostic
|
|
|
|
/// list.
|
|
|
|
class InternalCompilerError {
|
|
|
|
public:
|
|
|
|
/// Constructor
|
|
|
|
/// @param file the file containing the ICE
|
|
|
|
/// @param line the line containing the ICE
|
|
|
|
/// @param diagnostics the list of diagnostics to append the ICE message to
|
|
|
|
InternalCompilerError(const char* file, size_t line, diag::List& diagnostics);
|
|
|
|
|
|
|
|
/// Destructor.
|
|
|
|
/// Adds the internal compiler error message to the diagnostics list, and then
|
|
|
|
/// calls the InternalCompilerErrorReporter if one is set.
|
|
|
|
~InternalCompilerError();
|
|
|
|
|
|
|
|
/// Appends `arg` to the ICE message.
|
|
|
|
/// @param arg the argument to append to the ICE message
|
|
|
|
/// @returns this object so calls can be chained
|
|
|
|
template <typename T>
|
|
|
|
InternalCompilerError& operator<<(T&& arg) {
|
|
|
|
msg_ << std::forward<T>(arg);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
char const* const file_;
|
|
|
|
size_t const line_;
|
|
|
|
diag::List& diagnostics_;
|
|
|
|
std::stringstream msg_;
|
|
|
|
};
|
2021-02-17 20:13:34 +00:00
|
|
|
|
|
|
|
} // namespace tint
|
|
|
|
|
|
|
|
/// TINT_ICE() is a macro for appending an internal compiler error message
|
|
|
|
/// to the diagnostics list `diagnostics`, and calling the
|
|
|
|
/// InternalCompilerErrorReporter with the full diagnostic list if a reporter is
|
|
|
|
/// set.
|
|
|
|
/// The ICE message contains the callsite's file and line.
|
2021-02-18 16:33:38 +00:00
|
|
|
/// Use the `<<` operator to append an error message to the ICE.
|
|
|
|
#define TINT_ICE(diagnostics) \
|
|
|
|
tint::InternalCompilerError(__FILE__, __LINE__, diagnostics)
|
2021-02-17 20:13:34 +00:00
|
|
|
|
|
|
|
/// TINT_UNREACHABLE() is a macro for appending a "TINT_UNREACHABLE"
|
|
|
|
/// internal compiler error message to the diagnostics list `diagnostics`, and
|
|
|
|
/// calling the InternalCompilerErrorReporter with the full diagnostic list if a
|
|
|
|
/// reporter is set.
|
|
|
|
/// The ICE message contains the callsite's file and line.
|
2021-02-18 16:33:38 +00:00
|
|
|
/// Use the `<<` operator to append an error message to the ICE.
|
|
|
|
#define TINT_UNREACHABLE(diagnostics) \
|
|
|
|
TINT_ICE(diagnostics) << "TINT_UNREACHABLE "
|
2021-02-17 20:13:34 +00:00
|
|
|
|
2021-03-09 13:52:18 +00:00
|
|
|
/// TINT_ASSERT() is a macro for checking the expression is true, triggering a
|
|
|
|
/// TINT_ICE if it is not.
|
|
|
|
/// The ICE message contains the callsite's file and line.
|
|
|
|
/// @warning: Unlike TINT_ICE() and TINT_UNREACHABLE(), TINT_ASSERT() does not
|
|
|
|
/// append a message to an existing tint::diag::List. As such, TINT_ASSERT()
|
|
|
|
/// may silently fail in builds where SetInternalCompilerErrorReporter() is not
|
|
|
|
/// called. Only use in places where there's no sensible place to put proper
|
|
|
|
/// error handling.
|
|
|
|
#define TINT_ASSERT(condition) \
|
|
|
|
do { \
|
|
|
|
if (!(condition)) { \
|
|
|
|
tint::diag::List diagnostics; \
|
|
|
|
TINT_ICE(diagnostics) << "TINT_ASSERT(" << #condition << ")"; \
|
|
|
|
} \
|
|
|
|
} while (false)
|
|
|
|
|
2021-02-17 20:13:34 +00:00
|
|
|
#endif // SRC_DEBUG_H_
|