-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathexample_parser.hpp
56 lines (47 loc) · 1.53 KB
/
example_parser.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright (C) 2017-2023 Jonathan Müller and cppast contributors
// SPDX-License-Identifier: MIT
#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)
try
{
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;
}
catch (std::exception& ex)
{
std::cerr << ex.what() << '\n';
return 1;
}
#endif // CPPAST_EXAMPLE_PARSER_HPP_INCLUDED