Add fast preprocessing mode

This commit is contained in:
Jonathan Müller 2018-02-01 18:27:33 +01:00
commit 8c426ac115
7 changed files with 442 additions and 244 deletions

View file

@ -25,6 +25,8 @@ namespace cppast
static const std::vector<std::string>& flags(const libclang_compile_config& config);
static bool write_preprocessed(const libclang_compile_config& config);
static bool fast_preprocessing(const libclang_compile_config& config);
};
void for_each_file(const libclang_compilation_database& database, void* user_data,
@ -128,6 +130,18 @@ namespace cppast
write_preprocessed_ = b;
}
/// \effects Sets whether or not the fast preprocessor is enabled.
/// Default value is `false`.
/// \notes The fast preprocessor gets a list of all macros that are defined in the translation unit,
/// then preprocesses it without resolving includes but manually defining the list of macros to ensure correctness.
/// Later stages will use the includes again.
/// This hack breaks if you define the same macro multiple times in the file being parsed (headers don't matter)
/// or you rely on the order of macro directives.
void fast_preprocessing(bool b) noexcept
{
fast_preprocessing_ = b;
}
private:
void do_set_flags(cpp_standard standard, compile_flags flags) override;
@ -144,7 +158,8 @@ namespace cppast
std::string clang_binary_;
int clang_version_;
bool write_preprocessed_;
bool write_preprocessed_ : 1;
bool fast_preprocessing_ : 1;
friend detail::libclang_compile_config_access;
};

View file

@ -221,17 +221,20 @@ namespace cppast
/// Parses all files included by `file`.
/// \effects For each [cppast::cpp_include_directive]() in file it will parse the included file.
template <class FileParser>
void resolve_includes(FileParser& parser, const cpp_file& file,
typename FileParser::config config)
std::size_t resolve_includes(FileParser& parser, const cpp_file& file,
typename FileParser::config config)
{
auto count = 0u;
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);
++count;
}
}
return count;
}
} // namespace cppast