Fix and test include directive parsing
This commit is contained in:
parent
dc5abeb785
commit
9c46c96820
7 changed files with 90 additions and 21 deletions
|
|
@ -96,5 +96,6 @@ target_include_directories(cppast PUBLIC ../include)
|
|||
target_link_libraries(cppast PUBLIC type_safe _cppast_tiny_process _cppast_libclang)
|
||||
target_compile_definitions(cppast PUBLIC
|
||||
CPPAST_LIBCLANG_SYSTEM_INCLUDE_DIR="${LIBCLANG_SYSTEM_INCLUDE_DIR}"
|
||||
CPPAST_CLANG_BINARY="${CLANG_BINARY}")
|
||||
CPPAST_CLANG_BINARY="${CLANG_BINARY}"
|
||||
CPPAST_CLANG_VERSION_STRING="${LLVM_VERSION}")
|
||||
set_target_properties(cppast PROPERTIES CXX_STANDARD 11)
|
||||
|
|
|
|||
|
|
@ -22,15 +22,39 @@ const std::string& detail::libclang_compile_config_access::clang_binary(
|
|||
return config.clang_binary_;
|
||||
}
|
||||
|
||||
int detail::libclang_compile_config_access::clang_version(const libclang_compile_config& config)
|
||||
{
|
||||
return config.clang_version_;
|
||||
}
|
||||
|
||||
const std::vector<std::string>& detail::libclang_compile_config_access::flags(
|
||||
const libclang_compile_config& config)
|
||||
{
|
||||
return config.get_flags();
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
int parse_number(const char*& str)
|
||||
{
|
||||
auto result = 0;
|
||||
for (; *str && *str != '.'; ++str)
|
||||
{
|
||||
result *= 10;
|
||||
result += int(*str - '0');
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
libclang_compile_config::libclang_compile_config() : compile_config({})
|
||||
{
|
||||
set_clang_binary(CPPAST_CLANG_BINARY);
|
||||
auto ptr = CPPAST_CLANG_VERSION_STRING;
|
||||
auto major = parse_number(ptr);
|
||||
auto minor = parse_number(ptr);
|
||||
auto patch = parse_number(ptr);
|
||||
set_clang_binary(CPPAST_CLANG_BINARY, major, minor, patch);
|
||||
|
||||
add_include_dir(CPPAST_LIBCLANG_SYSTEM_INCLUDE_DIR);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -35,14 +35,19 @@ namespace
|
|||
// -E: print preprocessor output
|
||||
// -CC: keep comments, even in macro
|
||||
// -dD: print macro definitions as well
|
||||
// -dI: print include directives as well
|
||||
auto flags = std::string("-E -CC -dD");
|
||||
if (detail::libclang_compile_config_access::clang_version(c) >= 40000)
|
||||
// -Xclang -dI: print include directives as well (clang >= 4.0.0)
|
||||
flags += " -Xclang -dI";
|
||||
// -fno-caret-diagnostics: don't show the source extract in diagnostics
|
||||
// -fno-show-column: don't show the column number
|
||||
// -fdiagnostics-format=msvc: use easier to parse MSVC format
|
||||
// -fdiagnostics-format msvc: use easier to parse MSVC format
|
||||
flags += " -fno-caret-diagnostics -fno-show-column -fdiagnostics-format=msvc";
|
||||
// -Wno-pragma-once-outside-header: hide wrong warning
|
||||
std::string cmd(detail::libclang_compile_config_access::clang_binary(c)
|
||||
+ " -E -CC -dD -dI -fno-caret-diagnostics -fno-show-column "
|
||||
"-fdiagnostics-format=msvc -Wno-pragma-once-outside-header ");
|
||||
flags += " -Wno-pragma-once-outside-header";
|
||||
|
||||
std::string cmd(detail::libclang_compile_config_access::clang_binary(c) + " "
|
||||
+ std::move(flags) + " ");
|
||||
|
||||
// add other flags
|
||||
for (auto& flag : detail::libclang_compile_config_access::flags(c))
|
||||
|
|
@ -61,10 +66,17 @@ namespace
|
|||
{
|
||||
// format: <filename>(<line>):
|
||||
// or: <filename>:
|
||||
auto fallback = ptr;
|
||||
std::string filename;
|
||||
while (*ptr && *ptr != ':' && *ptr != '(')
|
||||
filename.push_back(*ptr++);
|
||||
|
||||
if (filename == "error" || filename == "warning" || filename == "fatal error")
|
||||
{
|
||||
ptr = fallback;
|
||||
return {};
|
||||
}
|
||||
|
||||
type_safe::optional<unsigned> line;
|
||||
if (*ptr == '(')
|
||||
{
|
||||
|
|
@ -87,6 +99,7 @@ namespace
|
|||
severity parse_severity(const char*& ptr)
|
||||
{
|
||||
// format: <severity>:
|
||||
auto fallback = ptr;
|
||||
std::string sev;
|
||||
while (*ptr && *ptr != ':')
|
||||
sev.push_back(*ptr++);
|
||||
|
|
@ -99,7 +112,7 @@ namespace
|
|||
else if (sev == "fatal error")
|
||||
return severity::critical;
|
||||
else
|
||||
DEBUG_UNREACHABLE(detail::assert_handler{});
|
||||
ptr = fallback;
|
||||
return severity::error;
|
||||
}
|
||||
|
||||
|
|
@ -249,6 +262,12 @@ namespace
|
|||
return std::strncmp(p.ptr(), str, std::strlen(str)) == 0;
|
||||
}
|
||||
|
||||
void skip(position& p, const char* str)
|
||||
{
|
||||
DEBUG_ASSERT(starts_with(p, str), detail::assert_handler{});
|
||||
p.skip(std::strlen(str));
|
||||
}
|
||||
|
||||
detail::pp_doc_comment parse_c_doc_comment(position& p)
|
||||
{
|
||||
detail::pp_doc_comment result;
|
||||
|
|
@ -484,6 +503,8 @@ namespace
|
|||
if (!p.was_newl() || !starts_with(p, "#include"))
|
||||
return nullptr;
|
||||
p.skip(std::strlen("#include"));
|
||||
if (starts_with(p, "_next"))
|
||||
p.skip(std::strlen("_next"));
|
||||
skip_spaces(p);
|
||||
|
||||
auto include_kind = cpp_include_kind::system;
|
||||
|
|
@ -507,6 +528,7 @@ namespace
|
|||
filename += *p.ptr();
|
||||
DEBUG_ASSERT(starts_with(p, end_str), detail::assert_handler{}, "bad termination");
|
||||
p.skip();
|
||||
skip(p, " /* clang -E -dI */");
|
||||
DEBUG_ASSERT(starts_with(p, "\n"), detail::assert_handler{});
|
||||
// don't skip newline
|
||||
|
||||
|
|
|
|||
|
|
@ -20,7 +20,11 @@ bool diagnostic_logger::log(const char* source, const diagnostic& d) const
|
|||
|
||||
bool stderr_diagnostic_logger::do_log(const char* source, const diagnostic& d) const
|
||||
{
|
||||
std::fprintf(stderr, "[%s] [%s] %s %s\n", source, to_string(d.severity),
|
||||
d.location.to_string().c_str(), d.message.c_str());
|
||||
auto loc = d.location.to_string();
|
||||
if (loc.empty())
|
||||
std::fprintf(stderr, "[%s] [%s] %s\n", source, to_string(d.severity), d.message.c_str());
|
||||
else
|
||||
std::fprintf(stderr, "[%s] [%s] %s %s\n", source, to_string(d.severity),
|
||||
d.location.to_string().c_str(), d.message.c_str());
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue