diff options
author | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2012-04-11 02:11:16 +0000 |
---|---|---|
committer | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2012-04-11 02:11:16 +0000 |
commit | e722ed6f5464232e23be52f4976312ef526fae99 (patch) | |
tree | a0996067f02cadf86b040e792f2dc5cfb19ba4fc /tools/libclang | |
parent | 259052d8c819d101f6f627f960f56e582ecbcebc (diff) |
[libclang] If displayDiagnostics is set (when calling clang_createIndex), make sure to
output the errors that occurred even if we did not get an AST (e.g. because the
PCH failed to load).
Also honor displayDiagnostics in clang_indexSourceFile().
rdar://11203489
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@154472 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools/libclang')
-rw-r--r-- | tools/libclang/CIndex.cpp | 47 | ||||
-rw-r--r-- | tools/libclang/CIndexer.h | 5 | ||||
-rw-r--r-- | tools/libclang/Indexing.cpp | 8 |
3 files changed, 39 insertions, 21 deletions
diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp index eafb867eb2..df4c8eda66 100644 --- a/tools/libclang/CIndex.cpp +++ b/tools/libclang/CIndex.cpp @@ -51,6 +51,7 @@ using namespace clang; using namespace clang::cxcursor; using namespace clang::cxstring; using namespace clang::cxtu; +using namespace clang::cxindex; CXTranslationUnit cxtu::MakeCXTranslationUnit(CIndexer *CIdx, ASTUnit *TU) { if (!TU) @@ -2482,7 +2483,28 @@ clang_createTranslationUnitFromSourceFile(CXIndex CIdx, unsaved_files, num_unsaved_files, Options); } - + +void cxindex::printDiagsToStderr(ASTUnit *Unit) { + if (!Unit) + return; + + for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(), + DEnd = Unit->stored_diag_end(); + D != DEnd; ++D) { + CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOpts()); + CXString Msg = clang_formatDiagnostic(&Diag, + clang_defaultDiagnosticDisplayOptions()); + fprintf(stderr, "%s\n", clang_getCString(Msg)); + clang_disposeString(Msg); + } +#ifdef LLVM_ON_WIN32 + // On Windows, force a flush, since there may be multiple copies of + // stderr and stdout in the file system, all with different buffers + // but writing to the same device. + fflush(stderr); +#endif +} + struct ParseTranslationUnitInfo { CXIndex CIdx; const char *source_filename; @@ -2587,6 +2609,7 @@ static void clang_parseTranslationUnit_Impl(void *UserData) { } unsigned NumErrors = Diags->getClient()->getNumErrors(); + OwningPtr<ASTUnit> ErrUnit; OwningPtr<ASTUnit> Unit( ASTUnit::LoadFromCommandLine(Args->size() ? &(*Args)[0] : 0 /* vector::data() not portable */, @@ -2601,27 +2624,13 @@ static void clang_parseTranslationUnit_Impl(void *UserData) { PrecompilePreamble, TUKind, CacheCodeCompetionResults, - /*AllowPCHWithCompilerErrors=*/true)); + /*AllowPCHWithCompilerErrors=*/true, + &ErrUnit)); if (NumErrors != Diags->getClient()->getNumErrors()) { // Make sure to check that 'Unit' is non-NULL. - if (CXXIdx->getDisplayDiagnostics() && Unit.get()) { - for (ASTUnit::stored_diag_iterator D = Unit->stored_diag_begin(), - DEnd = Unit->stored_diag_end(); - D != DEnd; ++D) { - CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOpts()); - CXString Msg = clang_formatDiagnostic(&Diag, - clang_defaultDiagnosticDisplayOptions()); - fprintf(stderr, "%s\n", clang_getCString(Msg)); - clang_disposeString(Msg); - } -#ifdef LLVM_ON_WIN32 - // On Windows, force a flush, since there may be multiple copies of - // stderr and stdout in the file system, all with different buffers - // but writing to the same device. - fflush(stderr); -#endif - } + if (CXXIdx->getDisplayDiagnostics()) + printDiagsToStderr(Unit ? Unit.get() : ErrUnit.get()); } PTUI->result = MakeCXTranslationUnit(CXXIdx, Unit.take()); diff --git a/tools/libclang/CIndexer.h b/tools/libclang/CIndexer.h index 8a249f0ceb..1e5fb824bb 100644 --- a/tools/libclang/CIndexer.h +++ b/tools/libclang/CIndexer.h @@ -25,6 +25,7 @@ namespace llvm { } namespace clang { + class ASTUnit; class CIndexer { bool OnlyLocalDecls; @@ -94,6 +95,10 @@ public: /// \brief Print libclang's resource usage to standard error. void PrintLibclangResourceUsage(CXTranslationUnit TU); + + namespace cxindex { + void printDiagsToStderr(ASTUnit *Unit); + } } #endif diff --git a/tools/libclang/Indexing.cpp b/tools/libclang/Indexing.cpp index 6ece54857a..e660c4d6eb 100644 --- a/tools/libclang/Indexing.cpp +++ b/tools/libclang/Indexing.cpp @@ -388,7 +388,8 @@ static void clang_indexSourceFile_Impl(void *UserData) { } } - Unit = ASTUnit::LoadFromCompilerInvocationAction(CInvok.getPtr(), Diags, + DiagnosticErrorTrap DiagTrap(*Diags); + bool Success = ASTUnit::LoadFromCompilerInvocationAction(CInvok.getPtr(), Diags, IndexAction.get(), Unit, Persistent, @@ -397,7 +398,10 @@ static void clang_indexSourceFile_Impl(void *UserData) { /*CaptureDiagnostics=*/true, PrecompilePreamble, CacheCodeCompletionResults); - if (!Unit) + if (DiagTrap.hasErrorOccurred() && CXXIdx->getDisplayDiagnostics()) + printDiagsToStderr(Unit); + + if (!Success) return; if (out_TU) |