From 204961b7b1ee1c46bb17410b5ec5c15751747c71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20M=C3=BCller?= Date: Fri, 23 Jun 2017 13:01:08 +0200 Subject: [PATCH] Throw exception on multiple parse error --- include/cppast/cpp_entity_index.hpp | 13 ++++++++++--- src/cpp_entity_index.cpp | 9 +++++++-- src/libclang/enum_parser.cpp | 5 +++++ src/libclang/function_parser.cpp | 14 ++++++++++++++ src/libclang/parse_functions.cpp | 6 ++++++ 5 files changed, 42 insertions(+), 5 deletions(-) diff --git a/include/cppast/cpp_entity_index.hpp b/include/cppast/cpp_entity_index.hpp index abb3802..7e45df3 100644 --- a/include/cppast/cpp_entity_index.hpp +++ b/include/cppast/cpp_entity_index.hpp @@ -62,11 +62,18 @@ namespace cppast class cpp_entity_index { public: + /// Exception thrown on duplicate entity definition. + class duplicate_definition_error : public std::logic_error + { + public: + duplicate_definition_error(); + }; + /// \effects Registers a new [cppast::cpp_entity]() which is a definition. /// It will override any previously registered declarations of the same entity. - /// \requires If the entity has been registered before, it must be as declaration, - /// and the entity must live as long as the index lives. - /// \requires The entity must not be a namespace. + /// \throws duplicate_defintion_error if the entity has been registered as definition before. + /// \requires The entity must live as long as the index lives, + /// and it must not be a namespace. /// \notes This operation is thread safe. void register_definition(cpp_entity_id id, type_safe::object_ref entity) const; diff --git a/src/cpp_entity_index.cpp b/src/cpp_entity_index.cpp index f693685..da46bb2 100644 --- a/src/cpp_entity_index.cpp +++ b/src/cpp_entity_index.cpp @@ -10,6 +10,11 @@ using namespace cppast; +cpp_entity_index::duplicate_definition_error::duplicate_definition_error() +: std::logic_error("duplicate registration of entity definition") +{ +} + void cpp_entity_index::register_definition(cpp_entity_id id, type_safe::object_ref entity) const { @@ -21,8 +26,8 @@ void cpp_entity_index::register_definition(cpp_entity_id { // already in map, override declaration auto& value = result.first->second; - DEBUG_ASSERT(!value.is_definition, detail::precondition_error_handler{}, - "duplicate entity registration"); + if (value.is_definition) + throw duplicate_definition_error(); value.is_definition = true; value.entity = entity; } diff --git a/src/libclang/enum_parser.cpp b/src/libclang/enum_parser.cpp index 973d4a1..5409c99 100644 --- a/src/libclang/enum_parser.cpp +++ b/src/libclang/enum_parser.cpp @@ -96,6 +96,11 @@ std::unique_ptr detail::parse_cpp_enum(const detail::parse_context& { context.logger->log("libclang parser", ex.get_diagnostic()); } + catch (std::logic_error& ex) + { + context.logger->log("libclang parser", + diagnostic{ex.what(), make_location(child), severity::error}); + } }); if (clang_isCursorDefinition(cur)) return builder.finish(*context.idx, get_entity_id(cur), std::move(semantic_parent)); diff --git a/src/libclang/function_parser.cpp b/src/libclang/function_parser.cpp index 7217617..074823a 100644 --- a/src/libclang/function_parser.cpp +++ b/src/libclang/function_parser.cpp @@ -48,6 +48,12 @@ namespace { context.logger->log("libclang parser", ex.get_diagnostic()); } + catch (std::logic_error& ex) + { + context.logger->log("libclang parser", + diagnostic{ex.what(), detail::make_location(child), + severity::error}); + } }); } else @@ -66,6 +72,14 @@ namespace { context.logger->log("libclang parser", ex.get_diagnostic()); } + catch (std::logic_error& ex) + { + context.logger->log("libclang parser", + diagnostic{ex.what(), + detail::make_location( + clang_Cursor_getArgument(cur, unsigned(i))), + severity::error}); + } } } diff --git a/src/libclang/parse_functions.cpp b/src/libclang/parse_functions.cpp index 68e661e..bdbb68d 100644 --- a/src/libclang/parse_functions.cpp +++ b/src/libclang/parse_functions.cpp @@ -233,6 +233,12 @@ catch (parse_error& ex) context.logger->log("libclang parser", ex.get_diagnostic()); return nullptr; } +catch (std::logic_error& ex) +{ + context.logger->log("libclang parser", + diagnostic{ex.what(), detail::make_location(cur), severity::error}); + return nullptr; +} std::unique_ptr detail::parse_cpp_static_assert(const detail::parse_context& context, const CXCursor& cur)