Extract common main function of examples

This commit is contained in:
Jonathan Müller 2017-10-30 16:30:34 +01:00
commit 33d2523893
3 changed files with 75 additions and 54 deletions

View file

@ -0,0 +1,51 @@
// Copyright (C) 2017 Jonathan Müller <jonathanmueller.dev@gmail.com>
// This file is subject to the license terms in the LICENSE file
// found in the top-level directory of this distribution.
#ifndef CPPAST_EXAMPLE_PARSER_HPP_INCLUDED
#define CPPAST_EXAMPLE_PARSER_HPP_INCLUDED
#include <iostream>
#include <cppast/libclang_parser.hpp>
// reads the database directory from the command line argument
// parses all files in that directory
// and invokes the callback for each of them
template <typename Callback>
int example_main(int argc, char* argv[], const cppast::cpp_entity_index& index, Callback cb)
{
if (argc != 2)
{
std::cerr << "usage: " << argv[0] << " <compile-commands-json-dir>\n";
return 1;
}
else
{
cppast::libclang_compilation_database database(argv[1]); // the compilation database
// simple_file_parser allows parsing multiple files and stores the results for us
cppast::simple_file_parser<cppast::libclang_parser> parser(type_safe::ref(index));
try
{
cppast::parse_database(parser, database); // parse all files in the database
}
catch (cppast::libclang_error& ex)
{
std::cerr << "fatal libclang error: " << ex.what() << '\n';
return 1;
}
if (parser.error())
// a non-fatal parse error
// error has been logged to stderr
return 1;
for (auto& file : parser.files())
cb(file);
}
return 0;
}
#endif // CPPAST_EXAMPLE_PARSER_HPP_INCLUDED