From ca5623b43d971226102810c544e099230e2cdd1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20M=C3=BCller?= Date: Mon, 3 Jul 2017 09:02:31 +0200 Subject: [PATCH] Add file name to error output --- include/cppast/diagnostic.hpp | 6 ++++++ src/libclang/enum_parser.cpp | 5 +++-- src/libclang/function_parser.cpp | 22 ++++++++++++++-------- src/libclang/libclang_parser.cpp | 2 +- src/libclang/parse_error.hpp | 14 +++++++++++++- src/libclang/parse_functions.cpp | 5 +++-- 6 files changed, 40 insertions(+), 14 deletions(-) diff --git a/include/cppast/diagnostic.hpp b/include/cppast/diagnostic.hpp index 46b66e2..9ddedfb 100644 --- a/include/cppast/diagnostic.hpp +++ b/include/cppast/diagnostic.hpp @@ -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 diff --git a/src/libclang/enum_parser.cpp b/src/libclang/enum_parser.cpp index a49096d..e1a6201 100644 --- a/src/libclang/enum_parser.cpp +++ b/src/libclang/enum_parser.cpp @@ -99,13 +99,14 @@ std::unique_ptr 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)) diff --git a/src/libclang/function_parser.cpp b/src/libclang/function_parser.cpp index 568a6fd..5042536 100644 --- a/src/libclang/function_parser.cpp +++ b/src/libclang/function_parser.cpp @@ -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}); } } } diff --git a/src/libclang/libclang_parser.cpp b/src/libclang/libclang_parser.cpp index e85d7e2..cc56359 100644 --- a/src/libclang/libclang_parser.cpp +++ b/src/libclang/libclang_parser.cpp @@ -545,7 +545,7 @@ std::unique_ptr 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; } diff --git a/src/libclang/parse_error.hpp b/src/libclang/parse_error.hpp index a0e0707..4f63766 100644 --- a/src/libclang/parse_error.hpp +++ b/src/libclang/parse_error.hpp @@ -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}; } diff --git a/src/libclang/parse_functions.cpp b/src/libclang/parse_functions.cpp index 22d3cf0..1a93792 100644 --- a/src/libclang/parse_functions.cpp +++ b/src/libclang/parse_functions.cpp @@ -241,14 +241,15 @@ std::unique_ptr 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; }