| ... | ... | @@ -13,6 +13,7 @@ |
| 13 | 13 | * 3. Prevent C++ from infecting the rest of the project. |
| 14 | 14 | */ |
| 15 | 15 | #include "zig_clang.h" |
| 16 | #include "list.hpp" |
| 16 | 17 | |
| 17 | 18 | #if __GNUC__ >= 8 |
| 18 | 19 | #pragma GCC diagnostic push |
| ... | ... | @@ -1947,35 +1948,28 @@ ZigClangASTUnit *ZigClangLoadFromCommandLine(const char **args_begin, const char |
| 1947 | 1948 | bool allow_pch_with_compiler_errors = false; |
| 1948 | 1949 | bool single_file_parse = false; |
| 1949 | 1950 | bool for_serialization = false; |
| 1950 | | std::unique_ptr<clang::ASTUnit> *err_unit = new std::unique_ptr<clang::ASTUnit>(); |
| 1951 | std::unique_ptr<clang::ASTUnit> err_unit; |
| 1951 | 1952 | clang::ASTUnit *ast_unit = clang::ASTUnit::LoadFromCommandLine( |
| 1952 | 1953 | args_begin, args_end, |
| 1953 | 1954 | pch_container_ops, diags, resources_path, |
| 1954 | 1955 | only_local_decls, clang::CaptureDiagsKind::All, clang::None, true, 0, clang::TU_Complete, |
| 1955 | 1956 | false, false, allow_pch_with_compiler_errors, clang::SkipFunctionBodiesScope::None, |
| 1956 | | single_file_parse, user_files_are_volatile, for_serialization, clang::None, err_unit, |
| 1957 | single_file_parse, user_files_are_volatile, for_serialization, clang::None, &err_unit, |
| 1957 | 1958 | nullptr); |
| 1958 | 1959 | |
| 1960 | *errors_len = 0; |
| 1961 | |
| 1959 | 1962 | // Early failures in LoadFromCommandLine may return with ErrUnit unset. |
| 1960 | 1963 | if (!ast_unit && !err_unit) { |
| 1961 | 1964 | return nullptr; |
| 1962 | 1965 | } |
| 1963 | 1966 | |
| 1964 | | if (diags->getClient()->getNumErrors() > 0) { |
| 1965 | | if (ast_unit) { |
| 1966 | | *err_unit = std::unique_ptr<clang::ASTUnit>(ast_unit); |
| 1967 | | } |
| 1967 | if (diags->hasErrorOccurred()) { |
| 1968 | clang::ASTUnit *unit = ast_unit ? ast_unit : err_unit.get(); |
| 1969 | ZigList<Stage2ErrorMsg> errors = {}; |
| 1968 | 1970 | |
| 1969 | | size_t cap = 4; |
| 1970 | | *errors_len = 0; |
| 1971 | | *errors_ptr = reinterpret_cast<Stage2ErrorMsg*>(malloc(cap * sizeof(Stage2ErrorMsg))); |
| 1972 | | if (*errors_ptr == nullptr) { |
| 1973 | | return nullptr; |
| 1974 | | } |
| 1975 | | |
| 1976 | | for (clang::ASTUnit::stored_diag_iterator it = (*err_unit)->stored_diag_begin(), |
| 1977 | | it_end = (*err_unit)->stored_diag_end(); |
| 1978 | | it != it_end; ++it) |
| 1971 | for (clang::ASTUnit::stored_diag_iterator it = unit->stored_diag_begin(), |
| 1972 | it_end = unit->stored_diag_end(); it != it_end; ++it) |
| 1979 | 1973 | { |
| 1980 | 1974 | switch (it->getLevel()) { |
| 1981 | 1975 | case clang::DiagnosticsEngine::Ignored: |
| ... | ... | @@ -1987,21 +1981,10 @@ ZigClangASTUnit *ZigClangLoadFromCommandLine(const char **args_begin, const char |
| 1987 | 1981 | case clang::DiagnosticsEngine::Fatal: |
| 1988 | 1982 | break; |
| 1989 | 1983 | } |
| 1984 | |
| 1990 | 1985 | llvm::StringRef msg_str_ref = it->getMessage(); |
| 1991 | | if (*errors_len >= cap) { |
| 1992 | | cap *= 2; |
| 1993 | | Stage2ErrorMsg *new_errors = reinterpret_cast<Stage2ErrorMsg *>( |
| 1994 | | realloc(*errors_ptr, cap * sizeof(Stage2ErrorMsg))); |
| 1995 | | if (new_errors == nullptr) { |
| 1996 | | free(*errors_ptr); |
| 1997 | | *errors_ptr = nullptr; |
| 1998 | | *errors_len = 0; |
| 1999 | | return nullptr; |
| 2000 | | } |
| 2001 | | *errors_ptr = new_errors; |
| 2002 | | } |
| 2003 | | Stage2ErrorMsg *msg = *errors_ptr + *errors_len; |
| 2004 | | *errors_len += 1; |
| 1986 | |
| 1987 | Stage2ErrorMsg *msg = errors.add_one(); |
| 2005 | 1988 | msg->msg_ptr = (const char *)msg_str_ref.bytes_begin(); |
| 2006 | 1989 | msg->msg_len = msg_str_ref.size(); |
| 2007 | 1990 | |
| ... | ... | @@ -2027,10 +2010,8 @@ ZigClangASTUnit *ZigClangLoadFromCommandLine(const char **args_begin, const char |
| 2027 | 2010 | } |
| 2028 | 2011 | } |
| 2029 | 2012 | |
| 2030 | | if (*errors_len == 0) { |
| 2031 | | free(*errors_ptr); |
| 2032 | | *errors_ptr = nullptr; |
| 2033 | | } |
| 2013 | *errors_ptr = errors.items; |
| 2014 | *errors_len = errors.length; |
| 2034 | 2015 | |
| 2035 | 2016 | return nullptr; |
| 2036 | 2017 | } |