From a0c2eece5b82a6ac701c3a4b46ef076aa33d0ae4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20M=C3=BCller?= Date: Thu, 16 Feb 2017 21:33:41 +0100 Subject: [PATCH] Add and use libclang_error exception class --- include/cppast/libclang_parser.hpp | 12 ++++++++++++ src/libclang/libclang_parser.cpp | 18 +++++++++++++++++- src/libclang/preprocessor.cpp | 4 +++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/include/cppast/libclang_parser.hpp b/include/cppast/libclang_parser.hpp index 78d8697..9f22621 100644 --- a/include/cppast/libclang_parser.hpp +++ b/include/cppast/libclang_parser.hpp @@ -5,6 +5,8 @@ #ifndef CPPAST_LIBCLANG_PARSER_HPP_INCLUDED #define CPPAST_LIBCLANG_PARSER_HPP_INCLUDED +#include + #include namespace cppast @@ -53,6 +55,16 @@ namespace cppast friend detail::libclang_compile_config_access; }; + /// The exception thrown when a fatal parse error occurs. + class libclang_error final : public std::runtime_error + { + public: + /// \effects Creates it with a message. + libclang_error(std::string msg) : std::runtime_error(std::move(msg)) + { + } + }; + /// A parser that uses libclang. class libclang_parser final : public parser { diff --git a/src/libclang/libclang_parser.cpp b/src/libclang/libclang_parser.cpp index 820822c..47012e9 100644 --- a/src/libclang/libclang_parser.cpp +++ b/src/libclang/libclang_parser.cpp @@ -134,7 +134,23 @@ namespace | CXTranslationUnit_KeepGoing, // flags &tu); if (error != CXError_Success) - DEBUG_UNREACHABLE(detail::assert_handler{}, "libclang error"); // TODO + { + switch (error) + { + case CXError_Success: + DEBUG_UNREACHABLE(detail::assert_handler{}); + break; + + case CXError_Failure: + throw libclang_error("clang_parseTranslationUnit: generic error"); + case CXError_Crashed: + throw libclang_error("clang_parseTranslationUnit: libclang crashed :("); + case CXError_InvalidArguments: + throw libclang_error("clang_parseTranslationUnit: you shouldn't see this message"); + case CXError_ASTReadError: + throw libclang_error("clang_parseTranslationUnit: AST deserialization error"); + } + } return tu; } diff --git a/src/libclang/preprocessor.cpp b/src/libclang/preprocessor.cpp index bf7ed07..6421bad 100644 --- a/src/libclang/preprocessor.cpp +++ b/src/libclang/preprocessor.cpp @@ -73,7 +73,9 @@ namespace auto exit_code = process.get_exit_status(); if (exit_code != 0) - DEBUG_UNREACHABLE(detail::assert_handler{}); // TODO: improve error handling + throw libclang_error("preprocessor: command '" + cmd + + "' exited with non-zero exit code (" + std::to_string(exit_code) + + ")"); return preprocessed; }