serene/include/serene/diagnostics.h

89 lines
2.4 KiB
C++

/* -*- C++ -*-
* Serene Programming Language
*
* Copyright (c) 2019-2021 Sameer Rahmani <lxsameer@gnu.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SERENE_DIAGNOSTICS_H
#define SERENE_DIAGNOSTICS_H
#include "serene/errors/constants.h"
#include "serene/reader/location.h"
#include "serene/source_mgr.h"
#include <llvm/ADT/StringRef.h>
#include <llvm/Support/raw_ostream.h>
#include <memory>
#include <mlir/IR/Diagnostics.h>
namespace serene {
class SereneContext;
class DiagnosticEngine;
class Diagnostic {
// TODO: Add support for llvm::SMFixIt
friend DiagnosticEngine;
enum Type {
Error,
// TODO: Add support for remarks and notes
Remark,
Note,
};
SereneContext &ctx;
reader::LocationRange loc;
std::string fn;
errors::ErrorVariant *err = nullptr;
Type type = Type::Error;
std::string message, lineContents;
std::string getPrefix(llvm::StringRef prefix = "");
public:
Diagnostic(SereneContext &ctx, reader::LocationRange loc,
errors::ErrorVariant *e, llvm::StringRef msg,
llvm::StringRef fn = "")
: ctx(ctx), loc(loc), fn(fn), err(e), message(msg){};
protected:
void print(llvm::raw_ostream &os, llvm::StringRef prefix = "");
void writeColorByType(llvm::raw_ostream &os, llvm::StringRef str);
};
class DiagnosticEngine {
SereneContext &ctx;
mlir::DiagnosticEngine &diagEngine;
Diagnostic toDiagnostic(reader::LocationRange loc, errors::ErrorVariant &e,
llvm::StringRef msg, llvm::StringRef fn = "");
void print(llvm::raw_ostream &os, Diagnostic &d);
public:
DiagnosticEngine(SereneContext &ctx);
void enqueueError(llvm::StringRef msg);
void emitSyntaxError(reader::LocationRange loc, errors::ErrorVariant &e,
llvm::StringRef msg = "");
};
std::unique_ptr<DiagnosticEngine> makeDiagnosticEngine(SereneContext &ctx);
} // namespace serene
#endif