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

@ -95,7 +95,8 @@ struct libclang_parser::impl
}
};
libclang_parser::libclang_parser() : pimpl_(new impl)
libclang_parser::libclang_parser(type_safe::object_ref<const diagnostic_logger> logger)
: parser(logger), pimpl_(new impl)
{
}
@ -164,7 +165,7 @@ std::unique_ptr<cpp_file> libclang_parser::do_parse(const cpp_entity_index& idx,
auto& config = static_cast<const libclang_compile_config&>(c);
// preprocess + parse
auto preprocessed = detail::preprocess(config, path.c_str());
auto preprocessed = detail::preprocess(config, path.c_str(), logger());
auto tu = get_cxunit(pimpl_->index, config, path.c_str(), preprocessed.source);
// convert entity hierachies

View file

@ -7,7 +7,6 @@
#include <algorithm>
#include <cstddef>
#include <cstring>
#include <cstdio>
// treat the tiny-process-library as header only
#include <process.hpp>
@ -23,6 +22,7 @@
#include <type_safe/reference.hpp>
#include <cppast/cpp_entity_kind.hpp>
#include <cppast/diagnostic.hpp>
using namespace cppast;
namespace ts = type_safe;
@ -54,9 +54,10 @@ namespace
}
// gets the full preprocessor output
std::string get_full_preprocess_output(const libclang_compile_config& c, const char* full_path)
std::string get_full_preprocess_output(const libclang_compile_config& c, const char* full_path,
const diagnostic_logger& logger)
{
std::string preprocessed;
std::string preprocessed, diagnostics;
auto cmd = get_command(c, full_path);
Process process(cmd, "",
@ -66,16 +67,20 @@ namespace
if (*str != '\r')
preprocessed.push_back(*str);
},
[&](const char* str, std::size_t n) {
std::fprintf(stderr, "%.*s\n", static_cast<int>(n),
str); // TODO: log error properly
});
[&](const char* str, std::size_t n) { diagnostics.append(str, n); });
auto exit_code = process.get_exit_status();
if (exit_code != 0)
{
if (!diagnostics.empty())
logger.log("preprocessor",
diagnostic{std::move(diagnostics), {}, severity::critical});
throw libclang_error("preprocessor: command '" + cmd
+ "' exited with non-zero exit code (" + std::to_string(exit_code)
+ ")");
}
else if (!diagnostics.empty())
logger.log("preprocessor", diagnostic{std::move(diagnostics), {}, severity::error});
return preprocessed;
}
@ -369,11 +374,11 @@ namespace
}
detail::preprocessor_output detail::preprocess(const libclang_compile_config& config,
const char* path)
const char* path, const diagnostic_logger& logger)
{
detail::preprocessor_output result;
auto output = get_full_preprocess_output(config, path);
auto output = get_full_preprocess_output(config, path, logger);
position p(ts::ref(result.source), output.c_str());
std::size_t file_depth = 0u;

View file

@ -24,7 +24,8 @@ namespace cppast
std::vector<pp_entity> entities;
};
preprocessor_output preprocess(const libclang_compile_config& config, const char* path);
preprocessor_output preprocess(const libclang_compile_config& config, const char* path,
const diagnostic_logger& logger);
}
} // namespace cppast::detail

18
src/parser.cpp Normal file
View file

@ -0,0 +1,18 @@
// 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.
#include <cppast/parser.hpp>
#include <cstdio>
#include <mutex>
#include <cppast/diagnostic.hpp>
using namespace cppast;
bool stderr_diagnostic_logger::do_log(const char* source, const diagnostic& d) const
{
std::fprintf(stderr, "[%s] %s %s", source, d.location.to_string().c_str(), d.message.c_str());
return true;
}