cppast/src/libclang/parse_error.hpp
2017-02-22 20:49:20 +01:00

71 lines
2.1 KiB
C++

// 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_PARSE_ERROR_HPP_INCLUDED
#define CPPAST_PARSE_ERROR_HPP_INCLUDED
#include <stdexcept>
#include <sstream>
#include <debug_assert.hpp>
#include <cppast/diagnostic.hpp>
#include "debug_helper.hpp"
namespace cppast
{
namespace detail
{
inline source_location make_location(const CXCursor& cur)
{
return source_location::make(get_display_name(cur).c_str());
}
// thrown on a parsing error
// not meant to escape to the user
class parse_error : public std::logic_error
{
public:
parse_error(source_location loc, std::string message)
: std::logic_error(std::move(message)), location_(std::move(loc))
{
}
parse_error(const CXCursor& cur, std::string message)
: parse_error(make_location(cur), std::move(message))
{
}
diagnostic get_diagnostic() const
{
return diagnostic{what(), location_, severity::error};
}
private:
source_location location_;
};
// DEBUG_ASSERT handler for parse errors
// throws a parse_error exception
struct parse_error_handler : debug_assert::set_level<1>, debug_assert::allow_exception
{
static void handle(const debug_assert::source_location&, const char*,
const CXCursor& cur, std::string message)
{
throw parse_error(cur, std::move(message));
}
};
template <typename... Args>
std::string format(Args&&... args)
{
std::ostringstream stream;
int dummy[] = {(stream << std::forward<Args>(args), 0)...};
(void)dummy;
return stream.str();
}
}
} // namespace cppast::detail
#endif // CPPAST_PARSE_ERROR_HPP_INCLUDED