diff --git a/clang/tools/c-index-test/c-index-test.c b/clang/tools/c-index-test/c-index-test.c index 472c0b0e9501..903bfc59d82b 100644 --- a/clang/tools/c-index-test/c-index-test.c +++ b/clang/tools/c-index-test/c-index-test.c @@ -1702,21 +1702,27 @@ static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo, } static void index_diagnostic(CXClientData client_data, - CXDiagnostic diag, void *reserved) { + CXDiagnosticSet diagSet, void *reserved) { CXString str; const char *cstr; + unsigned numDiags, i; + CXDiagnostic diag; IndexData *index_data; index_data = (IndexData *)client_data; printCheck(index_data); - str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions()); - cstr = clang_getCString(str); - printf("[diagnostic]: %s\n", cstr); - clang_disposeString(str); - - if (getenv("CINDEXTEST_FAILONERROR") && - clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) { - index_data->fail_for_error = 1; + numDiags = clang_getNumDiagnosticsInSet(diagSet); + for (i = 0; i != numDiags; ++i) { + diag = clang_getDiagnosticInSet(diagSet, i); + str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions()); + cstr = clang_getCString(str); + printf("[diagnostic]: %s\n", cstr); + clang_disposeString(str); + + if (getenv("CINDEXTEST_FAILONERROR") && + clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) { + index_data->fail_for_error = 1; + } } } diff --git a/clang/tools/libclang/CIndexDiagnostic.cpp b/clang/tools/libclang/CIndexDiagnostic.cpp index de0e8d4c8f53..a29e3a60e04b 100644 --- a/clang/tools/libclang/CIndexDiagnostic.cpp +++ b/clang/tools/libclang/CIndexDiagnostic.cpp @@ -26,6 +26,7 @@ using namespace clang; using namespace clang::cxloc; using namespace clang::cxstring; +using namespace clang::cxdiag; using namespace llvm; @@ -39,8 +40,8 @@ CXDiagnosticSetImpl::~CXDiagnosticSetImpl() { CXDiagnosticImpl::~CXDiagnosticImpl() {} -static CXDiagnosticSetImpl *lazyCreateDiags(CXTranslationUnit TU, - bool checkIfChanged = false) { +CXDiagnosticSetImpl *cxdiag::lazyCreateDiags(CXTranslationUnit TU, + bool checkIfChanged) { ASTUnit *AU = static_cast(TU->TUData); if (TU->Diagnostics && checkIfChanged) { diff --git a/clang/tools/libclang/CIndexDiagnostic.h b/clang/tools/libclang/CIndexDiagnostic.h index cc00cfe34698..72f57f8835e3 100644 --- a/clang/tools/libclang/CIndexDiagnostic.h +++ b/clang/tools/libclang/CIndexDiagnostic.h @@ -149,6 +149,11 @@ struct CXStoredDiagnostic : public CXDiagnosticImpl { } }; +namespace cxdiag { +CXDiagnosticSetImpl *lazyCreateDiags(CXTranslationUnit TU, + bool checkIfChanged = false); +} // end namespace cxdiag + } // end namespace clang #endif // LLVM_CLANG_CINDEX_DIAGNOSTIC_H diff --git a/clang/tools/libclang/Indexing.cpp b/clang/tools/libclang/Indexing.cpp index f462c14dbfbd..50c37bc51fdf 100644 --- a/clang/tools/libclang/Indexing.cpp +++ b/clang/tools/libclang/Indexing.cpp @@ -12,6 +12,7 @@ #include "CXSourceLocation.h" #include "CXTranslationUnit.h" #include "CXString.h" +#include "CIndexDiagnostic.h" #include "CIndexer.h" #include "clang/Frontend/ASTUnit.h" @@ -31,6 +32,8 @@ using namespace cxstring; using namespace cxtu; using namespace cxindex; +static void indexDiagnostics(CXTranslationUnit TU, IndexingContext &IdxCtx); + namespace { //===----------------------------------------------------------------------===// @@ -158,13 +161,15 @@ public: class IndexingFrontendAction : public ASTFrontendAction { IndexingContext IndexCtx; + CXTranslationUnit CXTU; public: IndexingFrontendAction(CXClientData clientData, IndexerCallbacks &indexCallbacks, unsigned indexOptions, CXTranslationUnit cxTU) - : IndexCtx(clientData, indexCallbacks, indexOptions, cxTU) { } + : IndexCtx(clientData, indexCallbacks, indexOptions, cxTU), + CXTU(cxTU) { } virtual ASTConsumer *CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { @@ -174,6 +179,10 @@ public: return new IndexingConsumer(IndexCtx); } + virtual void EndSourceFileAction() { + indexDiagnostics(CXTU, IndexCtx); + } + virtual TranslationUnitKind getTranslationUnitKind() { return TU_Prefix; } virtual bool hasCodeCompletionSupport() const { return false; } }; @@ -334,7 +343,6 @@ static void clang_indexSourceFile_Impl(void *UserData) { bool Persistent = requestedToGetTU; StringRef ResourceFilesPath = CXXIdx->getClangResourcesPath(); bool OnlyLocalDecls = false; - bool CaptureDiagnostics = true; bool PrecompilePreamble = false; bool CacheCodeCompletionResults = false; PreprocessorOptions &PPOpts = CInvok->getPreprocessorOpts(); @@ -360,13 +368,12 @@ static void clang_indexSourceFile_Impl(void *UserData) { Persistent, ResourceFilesPath, OnlyLocalDecls, - CaptureDiagnostics, + /*CaptureDiagnostics=*/true, PrecompilePreamble, CacheCodeCompletionResults); if (!Unit) return; - // FIXME: Set state of the ASTUnit according to the TU_options. if (out_TU) *out_TU = CXTU->takeTU(); @@ -450,8 +457,11 @@ static void indexTranslationUnit(ASTUnit &Unit, IndexingContext &IdxCtx) { } static void indexDiagnostics(CXTranslationUnit TU, IndexingContext &IdxCtx) { - // FIXME: Create a CXDiagnosticSet from TU; - // IdxCtx.handleDiagnosticSet(Set); + if (!IdxCtx.hasDiagnosticCallback()) + return; + + CXDiagnosticSetImpl *DiagSet = cxdiag::lazyCreateDiags(TU); + IdxCtx.handleDiagnosticSet(DiagSet); } static void clang_indexTranslationUnit_Impl(void *UserData) { diff --git a/clang/tools/libclang/IndexingContext.h b/clang/tools/libclang/IndexingContext.h index 9a9fb3f4f0b9..6f33e194fd50 100644 --- a/clang/tools/libclang/IndexingContext.h +++ b/clang/tools/libclang/IndexingContext.h @@ -312,6 +312,8 @@ public: bool shouldAbort(); + bool hasDiagnosticCallback() const { return CB.diagnostic; } + void enteredMainFile(const FileEntry *File); void ppIncludedFile(SourceLocation hashLoc,