Add file name to error output

This commit is contained in:
Jonathan Müller 2017-07-03 09:02:31 +02:00
commit ca5623b43d
6 changed files with 40 additions and 14 deletions

View file

@ -47,6 +47,12 @@ namespace cppast
return {type_safe::nullopt, type_safe::nullopt, type_safe::nullopt, type_safe::nullopt};
}
/// \returns A source location where entity and file name is available.
static source_location make_entity(std::string entity, std::string file)
{
return {std::move(entity), std::move(file), 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

View file

@ -99,13 +99,14 @@ std::unique_ptr<cpp_entity> detail::parse_cpp_enum(const detail::parse_context&
catch (parse_error& ex)
{
context.error = true;
context.logger->log("libclang parser", ex.get_diagnostic());
context.logger->log("libclang parser", ex.get_diagnostic(context.file));
}
catch (std::logic_error& ex)
{
context.error = true;
auto location = make_location(child);
context.logger->log("libclang parser",
diagnostic{ex.what(), make_location(child), severity::error});
diagnostic{ex.what(), location, severity::error});
}
});
if (clang_isCursorDefinition(cur))

View file

@ -51,13 +51,14 @@ namespace
catch (detail::parse_error& ex)
{
context.error = true;
context.logger->log("libclang parser", ex.get_diagnostic());
context.logger->log("libclang parser", ex.get_diagnostic(context.file));
}
catch (std::logic_error& ex)
{
context.error = true;
context.logger->log("libclang parser",
diagnostic{ex.what(), detail::make_location(child),
diagnostic{ex.what(),
detail::make_location(context.file, child),
severity::error});
}
});
@ -76,15 +77,20 @@ namespace
}
catch (detail::parse_error& ex)
{
context.logger->log("libclang parser", ex.get_diagnostic());
context.error = true;
context.logger->log("libclang parser", ex.get_diagnostic(context.file));
}
catch (std::logic_error& ex)
{
context.logger->log("libclang parser",
diagnostic{ex.what(),
detail::make_location(
clang_Cursor_getArgument(cur, unsigned(i))),
severity::error});
context.error = true;
context.logger
->log("libclang parser",
diagnostic{ex.what(),
detail::make_location(context.file,
clang_Cursor_getArgument(cur,
unsigned(
i))),
severity::error});
}
}
}

View file

@ -545,7 +545,7 @@ std::unique_ptr<cpp_file> libclang_parser::do_parse(const cpp_entity_index& idx,
}
catch (detail::parse_error& ex)
{
logger().log("libclang parser", ex.get_diagnostic());
logger().log("libclang parser", ex.get_diagnostic(path));
set_error();
return nullptr;
}

View file

@ -27,6 +27,12 @@ namespace cppast
return source_location::make_file(cxstring(file).c_str(), line);
}
inline source_location make_location(const CXFile& file, const CXCursor& cur)
{
return source_location::make_entity(get_display_name(cur).c_str(),
cxstring(clang_getFileName(file)).c_str());
}
inline source_location make_location(const CXType& type)
{
return source_location::make_entity(cxstring(clang_getTypeSpelling(type)).c_str());
@ -52,8 +58,14 @@ namespace cppast
{
}
diagnostic get_diagnostic() const
diagnostic get_diagnostic(const CXFile& file)
{
return get_diagnostic(cxstring(clang_getFileName(file)).c_str());
}
diagnostic get_diagnostic(std::string file)
{
location_.file = std::move(file);
return diagnostic{what(), location_, severity::error};
}

View file

@ -241,14 +241,15 @@ std::unique_ptr<cpp_entity> detail::parse_entity(const detail::parse_context& co
catch (parse_error& ex)
{
context.error = true;
context.logger->log("libclang parser", ex.get_diagnostic());
context.logger->log("libclang parser", ex.get_diagnostic(context.file));
return nullptr;
}
catch (std::logic_error& ex)
{
context.error = true;
context.logger->log("libclang parser",
diagnostic{ex.what(), detail::make_location(cur), severity::error});
diagnostic{ex.what(), detail::make_location(context.file, cur),
severity::error});
return nullptr;
}