Add resolve_includes() function

This commit is contained in:
Jonathan Müller 2017-06-27 21:03:12 +02:00
commit abb2ba33d0
2 changed files with 32 additions and 16 deletions

View file

@ -9,6 +9,7 @@
#include <cppast/compile_config.hpp>
#include <cppast/cpp_file.hpp>
#include <cppast/cpp_preprocessor.hpp>
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<const cpp_file> 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<Range>(file_names),
[&](const std::string&) { return config; });
}
template <class FileParser>
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<const cpp_include_directive&>(entity);
parser.parse(include.full_path(), config);
}
}
}
} // namespace cppast
#endif // CPPAST_PARSER_HPP_INCLUDED

View file

@ -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<const cpp_include_directive&>(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 <future>
#include <condition_variable>
)";
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<libclang_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());
}