Add diagnostic_logger

This commit is contained in:
Jonathan Müller 2017-02-16 22:08:56 +01:00
commit 108fd1b2ee
8 changed files with 165 additions and 15 deletions

View file

@ -0,0 +1,80 @@
// Copyright (C) 2017 Jonathan Müller <jonathanmueller.dev@gmail.com>
// This file is subject to the license terms in the LICENSE file
// found in the top-level directory of this distribution.
#ifndef CPPAST_DIAGNOSTIC_HPP_INCLUDED
#define CPPAST_DIAGNOSTIC_HPP_INCLUDED
#include <string>
#include <type_safe/optional.hpp>
namespace cppast
{
/// Describes a physical source location attached to a [cppast::diagnostic]().
/// \notes All information might be unavailable.
struct source_location
{
type_safe::optional<std::string> entity;
type_safe::optional<std::string> file;
type_safe::optional<unsigned> line;
/// \returns A source location where all information is available.
static source_location make(std::string entity, std::string file, unsigned line)
{
return {std::move(entity), std::move(file), line};
}
/// \returns A source location where only file and line information is available.
static source_location make(std::string file, unsigned line)
{
return {type_safe::nullopt, std::move(file), line};
}
/// \returns A source location where only the entity name is available.
static source_location make(std::string entity)
{
return {std::move(entity), type_safe::nullopt, type_safe::nullopt};
}
/// \returns A possible string representation of the source location.
/// \notes It will include a separator, but no trailing whitespace.
std::string to_string() const
{
std::string result;
if (file)
{
result += file.value() + ":";
if (line)
result += line.value() + ":";
if (entity)
result += " (" + entity.value() + "):";
}
else if (entity)
result += entity.value() + ":";
return result;
}
};
/// The severity of a [cppast::diagnostic]().
enum class severity
{
warning, //< A warning that doesn't impact AST generation.
error, //< A non-critical error that does impact AST generation but not critically.
critical, //< A critical error where AST generation isn't possible.
/// \notes This will usually result in an exception being thrown after the diagnostic has been displayed.
};
/// A diagnostic.
///
/// It represents an error message from a [cppast::parser]().
struct diagnostic
{
std::string message;
source_location location;
cppast::severity severity;
};
} // namespace cppast
#endif // CPPAST_DIAGNOSTIC_HPP_INCLUDED

View file

@ -69,7 +69,7 @@ namespace cppast
class libclang_parser final : public parser
{
public:
libclang_parser();
explicit libclang_parser(type_safe::object_ref<const diagnostic_logger> logger);
~libclang_parser() noexcept override;
private:

View file

@ -11,6 +11,39 @@
namespace cppast
{
class cpp_entity_index;
class diagnostic;
/// Base class for a [cppast::diagnostic]() logger.
///
/// Its task is controlling how diagnostic are being displayed.
class diagnostic_logger
{
public:
diagnostic_logger() noexcept = default;
diagnostic_logger(const diagnostic_logger&) = delete;
diagnostic_logger& operator=(const diagnostic_logger&) = delete;
virtual ~diagnostic_logger() noexcept = default;
/// \effects Logs the diagnostic by invoking the `do_log()` member function.
/// \returns Whether or not the diagnostic was logged.
/// \notes `source` points to a string literal that gives additional context to what generates the message.
bool log(const char* source, const diagnostic& d) const
{
return do_log(source, d);
}
private:
virtual bool do_log(const char* source, const diagnostic& d) const = 0;
};
/// A [cppast::diagnostic_logger]() that logs to `stderr`.
///
/// It prints all diagnostics in an implementation-defined format.
class stderr_diagnostic_logger final : public diagnostic_logger
{
private:
bool do_log(const char* source, const diagnostic& d) const override;
};
/// Base class for a parser.
///
@ -33,13 +66,24 @@ namespace cppast
}
protected:
parser() = default;
/// \effects Creates it giving it a reference to the logger it uses.
explicit parser(type_safe::object_ref<const diagnostic_logger> logger) : logger_(logger)
{
}
/// \returns A reference to the logger used.
const diagnostic_logger& logger() const noexcept
{
return *logger_;
}
private:
/// \effects Parses the given file.
/// \returns The [cppast::cpp_file]() object describing it.
virtual std::unique_ptr<cpp_file> do_parse(const cpp_entity_index& idx, std::string path,
const compile_config& config) const = 0;
type_safe::object_ref<const diagnostic_logger> logger_;
};
} // namespace cppast