From: Douglas Gregor Date: Thu, 18 Feb 2010 20:11:31 +0000 (+0000) Subject: Resurrect the displayDiagnostics parameter to clang_createIndex(), and X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b2710713377f3b900a92ab69c4cf091f560a5cfb;p=clang Resurrect the displayDiagnostics parameter to clang_createIndex(), and display captured diagnostics when we can't return an invalid CXTranslationUnit. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@96606 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang-c/Index.h b/include/clang-c/Index.h index 63b2eae868..eeb76a3429 100644 --- a/include/clang-c/Index.h +++ b/include/clang-c/Index.h @@ -146,8 +146,8 @@ CINDEX_LINKAGE void clang_disposeString(CXString string); * * Here is an example: * - * // excludeDeclsFromPCH = 1 - * Idx = clang_createIndex(1); + * // excludeDeclsFromPCH = 1, displayDiagnostics=1 + * Idx = clang_createIndex(1, 1); * * // IndexTest.pch was produced with the following command: * // "clang -x c IndexTest.h -emit-ast -o IndexTest.pch" @@ -171,7 +171,8 @@ CINDEX_LINKAGE void clang_disposeString(CXString string); * -include-pch) allows 'excludeDeclsFromPCH' to remove redundant callbacks * (which gives the indexer the same performance benefit as the compiler). */ -CINDEX_LINKAGE CXIndex clang_createIndex(int excludeDeclarationsFromPCH); +CINDEX_LINKAGE CXIndex clang_createIndex(int excludeDeclarationsFromPCH, + int displayDiagnostics); /** * \brief Destroy the given index. diff --git a/test/Index/cindex-on-invalid.m b/test/Index/cindex-on-invalid.m index 0f6bdffe91..651c40a335 100644 --- a/test/Index/cindex-on-invalid.m +++ b/test/Index/cindex-on-invalid.m @@ -1,6 +1,5 @@ // RUN: not c-index-test -test-load-source local %s > %t 2> %t.err // RUN: FileCheck %s < %t.err -// XFAIL: * // CHECK: error: expected identifier or '(' // CHECK: Unable to load translation unit! diff --git a/tools/CIndex/CIndex.cpp b/tools/CIndex/CIndex.cpp index 8bb6a0a7c5..128b3b6e99 100644 --- a/tools/CIndex/CIndex.cpp +++ b/tools/CIndex/CIndex.cpp @@ -903,10 +903,13 @@ bool CursorVisitor::VisitAttributes(Decl *D) { } extern "C" { -CXIndex clang_createIndex(int excludeDeclarationsFromPCH) { +CXIndex clang_createIndex(int excludeDeclarationsFromPCH, + int displayDiagnostics) { CIndexer *CIdxr = new CIndexer(); if (excludeDeclarationsFromPCH) CIdxr->setOnlyLocalDecls(); + if (displayDiagnostics) + CIdxr->setDisplayDiagnostics(); return CIdxr; } @@ -993,8 +996,18 @@ clang_createTranslationUnitFromSourceFile(CXIndex CIdx, // FIXME: Until we have broader testing, just drop the entire AST if we // encountered an error. - if (NumErrors != Diags->getNumErrors()) + if (NumErrors != Diags->getNumErrors()) { + if (CXXIdx->getDisplayDiagnostics()) { + for (ASTUnit::diag_iterator D = Unit->diag_begin(), + DEnd = Unit->diag_end(); + D != DEnd; ++D) { + CXStoredDiagnostic Diag(*D, Unit->getASTContext().getLangOptions()); + clang_displayDiagnostic(&Diag, stderr, + clang_defaultDiagnosticDisplayOptions()); + } + } return 0; + } return Unit.take(); } @@ -1085,18 +1098,35 @@ clang_createTranslationUnitFromSourceFile(CXIndex CIdx, RemappedFiles.data(), RemappedFiles.size(), /*CaptureDiagnostics=*/true); - if (ATU) - ATU->unlinkTemporaryFile(); - - // FIXME: Currently we don't report diagnostics on invalid ASTs. if (ATU) { LoadSerializedDiagnostics(DiagnosticsFile, num_unsaved_files, unsaved_files, ATU->getFileManager(), ATU->getSourceManager(), ATU->getDiagnostics()); + } else if (CXXIdx->getDisplayDiagnostics()) { + // We failed to load the ASTUnit, but we can still deserialize the + // diagnostics and emit them. + FileManager FileMgr; + SourceManager SourceMgr; + // FIXME: Faked LangOpts! + LangOptions LangOpts; + llvm::SmallVector Diags; + LoadSerializedDiagnostics(DiagnosticsFile, + num_unsaved_files, unsaved_files, + FileMgr, SourceMgr, Diags); + for (llvm::SmallVector::iterator D = Diags.begin(), + DEnd = Diags.end(); + D != DEnd; ++D) { + CXStoredDiagnostic Diag(*D, LangOpts); + clang_displayDiagnostic(&Diag, stderr, + clang_defaultDiagnosticDisplayOptions()); + } } + if (ATU) + ATU->unlinkTemporaryFile(); + for (unsigned i = 0, e = TemporaryFiles.size(); i != e; ++i) TemporaryFiles[i].eraseFromDisk(); diff --git a/tools/CIndex/CIndexer.h b/tools/CIndex/CIndexer.h index d559f13864..1fa3ca9387 100644 --- a/tools/CIndex/CIndexer.h +++ b/tools/CIndex/CIndexer.h @@ -34,11 +34,14 @@ namespace cxstring { class CIndexer { bool UseExternalASTGeneration; bool OnlyLocalDecls; - + bool DisplayDiagnostics; + llvm::sys::Path ClangPath; public: - CIndexer() : UseExternalASTGeneration(false), OnlyLocalDecls(false) { } + CIndexer() + : UseExternalASTGeneration(false), OnlyLocalDecls(false), + DisplayDiagnostics(false) { } /// \brief Whether we only want to see "local" declarations (that did not /// come from a previous precompiled header). If false, we want to see all @@ -46,6 +49,11 @@ public: bool getOnlyLocalDecls() const { return OnlyLocalDecls; } void setOnlyLocalDecls(bool Local = true) { OnlyLocalDecls = Local; } + bool getDisplayDiagnostics() const { return DisplayDiagnostics; } + void setDisplayDiagnostics(bool Display = true) { + DisplayDiagnostics = Display; + } + bool getUseExternalASTGeneration() const { return UseExternalASTGeneration; } void setUseExternalASTGeneration(bool Value) { UseExternalASTGeneration = Value; diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c index 99d8b5d5fa..f5492b757d 100644 --- a/tools/c-index-test/c-index-test.c +++ b/tools/c-index-test/c-index-test.c @@ -487,7 +487,8 @@ int perform_test_load_tu(const char *file, const char *filter, CXTranslationUnit TU; int result; Idx = clang_createIndex(/* excludeDeclsFromPCH */ - !strcmp(filter, "local") ? 1 : 0); + !strcmp(filter, "local") ? 1 : 0, + /* displayDiagnosics=*/1); if (!CreateTranslationUnit(Idx, file, &TU)) { clang_disposeIndex(Idx); @@ -511,7 +512,8 @@ int perform_test_load_source(int argc, const char **argv, int result; Idx = clang_createIndex(/* excludeDeclsFromPCH */ - !strcmp(filter, "local") ? 1 : 0); + !strcmp(filter, "local") ? 1 : 0, + /* displayDiagnosics=*/1); if (UseExternalASTs && strlen(UseExternalASTs)) clang_setUseExternalASTGeneration(Idx, 1); @@ -565,7 +567,8 @@ static int perform_file_scan(const char *ast_file, const char *source_file, unsigned line = 1, col = 1; unsigned start_line = 1, start_col = 1; - if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1))) { + if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1, + /* displayDiagnosics=*/1))) { fprintf(stderr, "Could not create Index\n"); return 1; } @@ -766,7 +769,7 @@ int perform_code_completion(int argc, const char **argv) { if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) return -1; - CIdx = clang_createIndex(0); + CIdx = clang_createIndex(0, 1); results = clang_codeComplete(CIdx, argv[argc - 1], argc - num_unsaved_files - 3, argv + num_unsaved_files + 2, @@ -830,7 +833,7 @@ int inspect_cursor_at(int argc, const char **argv) { &num_unsaved_files)) return -1; - CIdx = clang_createIndex(0); + CIdx = clang_createIndex(0, 1); TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1], argc - num_unsaved_files - 2 - NumLocations, argv + num_unsaved_files + 1 + NumLocations, @@ -888,7 +891,7 @@ int perform_token_annotation(int argc, const char **argv) { if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) return -1; - CIdx = clang_createIndex(0); + CIdx = clang_createIndex(0, 1); TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1], argc - num_unsaved_files - 3, argv + num_unsaved_files + 2,