Add missing missing file check

This commit is contained in:
Jonathan Müller 2018-02-18 14:26:52 +01:00
commit 2ced972b9f
2 changed files with 37 additions and 10 deletions

View file

@ -393,13 +393,15 @@ namespace
}; };
clang_preprocess_result clang_preprocess_impl(const libclang_compile_config& c, clang_preprocess_result clang_preprocess_impl(const libclang_compile_config& c,
const diagnostic_logger& logger,
const std::string& full_path, const std::string& full_path,
const char* macro_path) const char* macro_path)
{ {
clang_preprocess_result result; clang_preprocess_result result;
std::string diagnostic; std::string diagnostic;
auto diagnostic_handler = [&](const char* str, std::size_t n) { auto expect_bad_exit_code = false;
auto diagnostic_handler = [&](const char* str, std::size_t n) {
diagnostic.reserve(diagnostic.size() + n); diagnostic.reserve(diagnostic.size() + n);
for (auto end = str + n; str != end; ++str) for (auto end = str + n; str != end; ++str)
if (*str == '\r') if (*str == '\r')
@ -407,10 +409,19 @@ namespace
else if (*str == '\n') else if (*str == '\n')
{ {
// handle current diagnostic // handle current diagnostic
auto file = parse_missing_file(full_path, diagnostic); if (macro_path)
if (file) {
// save for clang without -dI flag // hide diagnostics
result.included_files.push_back(file.value());
auto file = parse_missing_file(full_path, diagnostic);
if (file)
// save for clang without -dI flag
result.included_files.push_back(file.value());
expect_bad_exit_code = true;
}
else
log_diagnostic(logger, diagnostic);
diagnostic.clear(); diagnostic.clear();
} }
@ -430,7 +441,12 @@ namespace
}, },
diagnostic_handler); diagnostic_handler);
// wait for process end // wait for process end
process.get_exit_status(); auto exit_code = process.get_exit_status();
DEBUG_ASSERT(diagnostic.empty(), detail::assert_handler{});
if (exit_code != 0 && !expect_bad_exit_code)
throw libclang_error("preprocessor: command '" + cmd
+ "' exited with non-zero exit code (" + std::to_string(exit_code)
+ ")");
return result; return result;
} }
@ -438,6 +454,10 @@ namespace
clang_preprocess_result clang_preprocess(const libclang_compile_config& c, clang_preprocess_result clang_preprocess(const libclang_compile_config& c,
const char* full_path, const diagnostic_logger& logger) const char* full_path, const diagnostic_logger& logger)
{ {
if (!std::ifstream(full_path))
throw libclang_error("preprocessor: file '" + std::string(full_path)
+ "' doesn't exist");
// if we're fast preprocessing we only preprocess the main file, not includes // if we're fast preprocessing we only preprocess the main file, not includes
// this is done by disabling all include search paths when doing the preprocessing // this is done by disabling all include search paths when doing the preprocessing
// to allow macros a separate preprocessing with the -dM flag is done that extracts all macros // to allow macros a separate preprocessing with the -dM flag is done that extracts all macros
@ -449,7 +469,7 @@ namespace
clang_preprocess_result result; clang_preprocess_result result;
try try
{ {
result = clang_preprocess_impl(c, full_path, result = clang_preprocess_impl(c, logger, full_path,
fast_preprocessing ? macro_file.c_str() : nullptr); fast_preprocessing ? macro_file.c_str() : nullptr);
} }
catch (...) catch (...)

View file

@ -77,9 +77,16 @@ struct foo {};
config.set_flags(cpp_standard::cpp_latest); config.set_flags(cpp_standard::cpp_latest);
config.fast_preprocessing(true); config.fast_preprocessing(true);
auto result = detail::preprocess(config, file_name, default_logger().get()); try
REQUIRE(result.macros.size() == 1u); {
REQUIRE(result.macros[0].macro->name() == "INCLUDE_GUARD"); auto result = detail::preprocess(config, file_name, default_logger().get());
REQUIRE(result.macros.size() == 1u);
REQUIRE(result.macros[0].macro->name() == "INCLUDE_GUARD");
}
catch (libclang_error& ex)
{
FAIL(ex.what());
}
} }
TEST_CASE("preprocessor line numbers") TEST_CASE("preprocessor line numbers")