Add basic compilation database parsing

This commit is contained in:
Jonathan Müller 2017-06-06 17:23:48 +02:00
commit d14965b24e
4 changed files with 283 additions and 29 deletions

View file

@ -25,6 +25,50 @@ namespace cppast
};
} // namespace detail
/// 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 compilation database.
///
/// This represents a `compile_commands.json` file,
/// which stores all the commands needed to compile a set of files.
/// It can be generated by CMake using the `CMAKE_EXPORT_COMPILE_COMMANDS` option.
class libclang_compilation_database
{
public:
/// \effects Creates it giving the directory where the `compile_commands.json` file is located.
/// \throws `libclang_error` if the database could not be loaded or found.
libclang_compilation_database(const std::string& build_directory);
libclang_compilation_database(libclang_compilation_database&& other)
: database_(other.database_)
{
other.database_ = nullptr;
}
~libclang_compilation_database();
libclang_compilation_database& operator=(libclang_compilation_database&& other)
{
libclang_compilation_database tmp(std::move(other));
std::swap(tmp.database_, database_);
return *this;
}
private:
using database = void*;
database database_;
friend libclang_compile_config;
};
/// Compilation config for the [cppast::libclang_parser]().
class libclang_compile_config final : public compile_config
{
@ -36,6 +80,20 @@ namespace cppast
/// It will also define `__cppast__` with the value `"libclang"` as well as `__cppast_major__` and `__cppast_minor__`.
libclang_compile_config();
/// Creates the configuration stored in the database.
///
/// \effects It will use the options found in the database for the specified file.
/// This does not necessarily need to match the file that is going to be parsed,
/// but it should.
/// It will also add the default configuration options.
/// \notes Header files are not included in the compilation database,
/// you need to pass in the file name of the corresponding source file,
/// if you want to parse one.
/// \notes It will only consider options you could also set by the other functions.
/// \notes The file key will include the specified directory in the JSON, if it is not a full path.
libclang_compile_config(const libclang_compilation_database& database,
const std::string& file);
/// \effects Sets the path to the location of the `clang++` binary and the version of that binary.
/// \notes It will be used for preprocessing.
void set_clang_binary(std::string binary, int major, int minor, int patch)
@ -64,16 +122,6 @@ 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
{