From abb2ba33d08d6f0aa2c11a471cbf16462f7874f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20M=C3=BCller?= Date: Tue, 27 Jun 2017 21:03:12 +0200 Subject: [PATCH] Add resolve_includes() function --- include/cppast/parser.hpp | 21 ++++++++++++++++++++- test/integration.cpp | 27 ++++++++++++--------------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/include/cppast/parser.hpp b/include/cppast/parser.hpp index 5efa181..b1723c5 100644 --- a/include/cppast/parser.hpp +++ b/include/cppast/parser.hpp @@ -9,6 +9,7 @@ #include #include +#include namespace cppast { @@ -162,11 +163,15 @@ namespace cppast { } - void parse(std::string path, const config& c) + /// \effects Parses the given file using the given configuration. + /// \returns The parsed file or an empty optional, if a fatal error occurred. + type_safe::optional_ref parse(std::string path, const config& c) { auto file = parser_.parse(*idx_, std::move(path), c); + auto ptr = file.get(); if (file) files_.push_back(std::move(file)); + return type_safe::opt_ref(ptr); } /// \returns The result of [cppast::parser::error](). @@ -237,6 +242,20 @@ namespace cppast parse_files(parser, std::forward(file_names), [&](const std::string&) { return config; }); } + + template + void resolve_includes(FileParser& parser, const cpp_file& file, + typename FileParser::config config) + { + for (auto& entity : file) + { + if (entity.kind() == cpp_include_directive::kind()) + { + auto& include = static_cast(entity); + parser.parse(include.full_path(), config); + } + } + } } // namespace cppast #endif // CPPAST_PARSER_HPP_INCLUDED diff --git a/test/integration.cpp b/test/integration.cpp index e114931..6d25ee7 100644 --- a/test/integration.cpp +++ b/test/integration.cpp @@ -8,18 +8,6 @@ using namespace cppast; -void parse_included_files(const cpp_entity_index& idx, const cpp_file& file) -{ - for (auto& e : file) - { - if (e.kind() == cpp_entity_kind::include_directive_t) - { - auto path = static_cast(e).full_path(); - parse_file(idx, path.c_str()); - } - } -} - TEST_CASE("stdlib", "[!hide][integration]") { auto code = R"( @@ -111,8 +99,17 @@ TEST_CASE("stdlib", "[!hide][integration]") #include #include )"; + write_file("stdlib.cpp", code); - cpp_entity_index idx; - auto file = parse(idx, "stdlib.cpp", code); - parse_included_files(idx, *file); + cpp_entity_index idx; + simple_file_parser parser(type_safe::ref(idx), default_logger()); + + libclang_compile_config config; + config.set_flags(cpp_standard::cpp_latest); + auto file = parser.parse("stdlib.cpp", config); + REQUIRE(!parser.error()); + REQUIRE(file); + + resolve_includes(parser, file.value(), config); + REQUIRE(!parser.error()); }