#ifndef TOOLS_GN_ERR_H_
#define TOOLS_GN_ERR_H_
#include <memory>
#include <string>
#include <vector>
#include "gn/label.h"
#include "gn/location.h"
#include "gn/token.h"
class ParseNode;
class Value;
class Err {
private:
struct ErrInfo {
ErrInfo(const Location& loc,
const std::string& msg,
const std::string& help)
: location(loc), message(msg), help_text(help) {}
Location location;
Label toolchain_label;
std::vector<LocationRange> ranges;
std::string message;
std::string help_text;
std::vector<Err> sub_errs;
};
public:
using RangeList = std::vector<LocationRange>;
Err() = default;
Err(const Location& location,
const std::string& msg,
const std::string& help = std::string());
Err(const LocationRange& range,
const std::string& msg,
const std::string& help = std::string());
Err(const Token& token,
const std::string& msg,
const std::string& help_text = std::string());
Err(const ParseNode* node,
const std::string& msg,
const std::string& help_text = std::string());
Err(const Value& value,
const std::string& msg,
const std::string& help_text = std::string());
Err(const Err& other);
Err(Err&& other) = default;
Err& operator=(const Err&);
Err& operator=(Err&&) = default;
bool has_error() const { return !!info_; }
const Location& location() const { return info_->location; }
const std::string& message() const { return info_->message; }
const std::string& help_text() const { return info_->help_text; }
void AppendRange(const LocationRange& range) {
info_->ranges.push_back(range);
}
const RangeList& ranges() const { return info_->ranges; }
void set_toolchain_label(const Label& toolchain_label) {
info_->toolchain_label = toolchain_label;
}
void AppendSubErr(const Err& err);
void PrintToStdout() const;
void PrintNonfatalToStdout() const;
private:
void InternalPrintToStdout(bool is_sub_err, bool is_fatal) const;
std::unique_ptr<ErrInfo> info_;
};
#endif