<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><div>On Nov 10, 2011, at 12:43 AM, Ted Kremenek wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div>Author: kremenek<br>Date: Thu Nov 10 02:43:12 2011<br>New Revision: 144269<br><br>URL: <a href="http://llvm.org/viewvc/llvm-project?rev=144269&view=rev">http://llvm.org/viewvc/llvm-project?rev=144269&view=rev</a><br>Log:<br>serialized diagnostics: implement full deserialization of clang diagnostics via the libclang API.<br><br>I've tested it on simple cases and it works. Test cases to follow as well as a few tweaks.<br><br>Added:<br> cfe/trunk/tools/libclang/CXLoadedDiagnostic.cpp<br> cfe/trunk/tools/libclang/CXLoadedDiagnostic.h<br>Modified:<br> cfe/trunk/include/clang-c/Index.h<br> cfe/trunk/include/clang/Frontend/SerializedDiagnosticPrinter.h<br> cfe/trunk/lib/Frontend/SerializedDiagnosticPrinter.cpp<br> cfe/trunk/tools/c-index-test/c-index-test.c<br> cfe/trunk/tools/libclang/CIndex.cpp<br> cfe/trunk/tools/libclang/CIndexDiagnostic.cpp<br> cfe/trunk/tools/libclang/CIndexDiagnostic.h<br> cfe/trunk/tools/libclang/CMakeLists.txt<br> cfe/trunk/tools/libclang/CXSourceLocation.cpp<br> cfe/trunk/tools/libclang/CXStoredDiagnostic.cpp<br> cfe/trunk/tools/libclang/CXTranslationUnit.h<br> cfe/trunk/tools/libclang/libclang.exports<br><br>Modified: cfe/trunk/include/clang-c/Index.h<br>URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=144269&r1=144268&r2=144269&view=diff">http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=144269&r1=144268&r2=144269&view=diff</a><br>==============================================================================<br>--- cfe/trunk/include/clang-c/Index.h (original)<br>+++ cfe/trunk/include/clang-c/Index.h Thu Nov 10 02:43:12 2011<br>@@ -522,6 +522,86 @@<br> typedef void *CXDiagnostic;<br><br> /**<br>+ * \brief A group of CXDiagnostics.<br>+ */<br>+typedef void *CXDiagnosticSet;<br>+ <br>+/**<br>+ * \brief Determine the number of diagnostics in a CXDiagnosticSet.<br>+ */<br>+CINDEX_LINKAGE unsigned clang_getNumDiagnosticsInSet(CXDiagnosticSet Diags);<br>+<br>+/**<br>+ * \brief Retrieve a diagnostic associated with the given CXDiagnosticSet.<br>+ *<br>+ * \param Unit the CXDiagnosticSet to query.<br>+ * \param Index the zero-based diagnostic number to retrieve.<br>+ *<br>+ * \returns the requested diagnostic. This diagnostic must be freed<br>+ * via a call to \c clang_disposeDiagnostic().<br>+ */<br>+CINDEX_LINKAGE CXDiagnostic clang_getDiagnosticInSet(CXDiagnosticSet Diags,<br>+ unsigned Index); <br>+<br>+<br>+/**<br>+ * \brief Describes the kind of error that occurred (if any) in a call to<br>+ * \c clang_loadDiagnostics.<br>+ */<br>+enum CXLoadDiag_Error {<br>+ /**<br>+ * \brief Indicates that no error occurred.<br>+ */<br>+ CXLoadDiag_None = 0,<br>+ <br>+ /**<br>+ * \brief Indicates that an unknown error occurred while attempting to<br>+ * deserialize diagnostics.<br>+ */<br>+ CXLoadDiag_Unknown = 1,<br>+ <br>+ /**<br>+ * \brief Indicates that the file containing the serialized diagnostics<br>+ * could not be opened.<br>+ */<br>+ CXLoadDiag_CannotLoad = 2,<br>+ <br>+ /**<br>+ * \brief Indicates that the serialized diagnostics file is invalid or<br>+ * corrupt.<br>+ */<br>+ CXLoadDiag_InvalidFile = 3<br>+};<br></div></blockquote><div><br></div><div>CXLoadDiag_Error seems useful for other functions that load a file as well, like the one that reads an AST file, the one that reads file remappings, etc.</div><div>Maybe change it to CXLoadFile_Error so it can be reused ?</div><br><blockquote type="cite"><div>+ <br>+/**<br>+ * \brief Deserialize a set of diagnostics from a Clang diagnostics bitcode<br>+ * file.<br>+ *<br>+ * \param The name of the file to deserialize.<br>+ * \param A pointer to a enum value recording if there was a problem<br>+ * deserializing the diagnostics.<br>+ * \param A pointer to a CXString for recording the error string<br>+ * if the file was not successfully loaded.<br>+ *<br>+ * \returns A loaded CXDiagnosticSet if successful, and NULL otherwise. These<br>+ * diagnostics should be released using clang_disposeDiagnosticSet().<br>+ */<br>+CINDEX_LINKAGE CXDiagnosticSet clang_loadDiagnostics(const char *file,<br>+ enum CXLoadDiag_Error *error,<br>+ CXString *errorString);<br>+<br>+/**<br>+ * \brief Release a CXDiagnosticSet and all of its contained diagnostics.<br>+ */<br>+CINDEX_LINKAGE void clang_disposeDiagnosticSet(CXDiagnosticSet Diags);<br>+<br>+/**<br>+ * \brief Retrieve the child diagnostics of a CXDiagnostic. This<br>+ * CXDiagnosticSet does not need to be released by clang_diposeDiagnosticSet.<br>+ */<br>+CINDEX_LINKAGE CXDiagnosticSet clang_getChildDiagnostics(CXDiagnostic D);<br>+<br>+/**<br> * \brief Determine the number of diagnostics produced for the given<br> * translation unit.<br> */<br><br>Modified: cfe/trunk/include/clang/Frontend/SerializedDiagnosticPrinter.h<br>URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/SerializedDiagnosticPrinter.h?rev=144269&r1=144268&r2=144269&view=diff">http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/SerializedDiagnosticPrinter.h?rev=144269&r1=144268&r2=144269&view=diff</a><br>==============================================================================<br>--- cfe/trunk/include/clang/Frontend/SerializedDiagnosticPrinter.h (original)<br>+++ cfe/trunk/include/clang/Frontend/SerializedDiagnosticPrinter.h Thu Nov 10 02:43:12 2011<br>@@ -39,7 +39,9 @@<br> RECORD_DIAG_FLAG,<br> RECORD_CATEGORY,<br> RECORD_FILENAME,<br>- RECORD_FIXIT<br>+ RECORD_FIXIT,<br>+ RECORD_FIRST = RECORD_VERSION,<br>+ RECORD_LAST = RECORD_FIXIT<br> };<br><br> /// \brief Returns a DiagnosticConsumer that serializes diagnostics to<br><br>Modified: cfe/trunk/lib/Frontend/SerializedDiagnosticPrinter.cpp<br>URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/SerializedDiagnosticPrinter.cpp?rev=144269&r1=144268&r2=144269&view=diff">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/SerializedDiagnosticPrinter.cpp?rev=144269&r1=144268&r2=144269&view=diff</a><br>==============================================================================<br>--- cfe/trunk/lib/Frontend/SerializedDiagnosticPrinter.cpp (original)<br>+++ cfe/trunk/lib/Frontend/SerializedDiagnosticPrinter.cpp Thu Nov 10 02:43:12 2011<br>@@ -16,6 +16,7 @@<br> #include "clang/Basic/FileManager.h"<br> #include "clang/Basic/Diagnostic.h"<br> #include "clang/Basic/Version.h"<br>+#include "clang/Lex/Lexer.h"<br> #include "clang/Frontend/SerializedDiagnosticPrinter.h"<br><br> using namespace clang;<br>@@ -47,7 +48,8 @@<br> class SDiagsWriter : public DiagnosticConsumer {<br> public: <br> SDiagsWriter(DiagnosticsEngine &diags, llvm::raw_ostream *os) <br>- : Stream(Buffer), OS(os), Diags(diags), inNonNoteDiagnostic(false)<br>+ : LangOpts(0), Stream(Buffer), OS(os), Diags(diags),<br>+ inNonNoteDiagnostic(false)<br> { <br> EmitPreamble();<br> };<br>@@ -59,6 +61,11 @@<br><br> void EndSourceFile();<br><br>+ void BeginSourceFile(const LangOptions &LO,<br>+ const Preprocessor *PP) {<br>+ LangOpts = &LO;<br>+ }<br>+ <br> DiagnosticConsumer *clone(DiagnosticsEngine &Diags) const {<br> // It makes no sense to clone this.<br> return 0;<br>@@ -88,7 +95,8 @@<br> unsigned getEmitFile(SourceLocation Loc);<br><br> /// \brief Add SourceLocation information the specified record.<br>- void AddLocToRecord(SourceLocation Loc, RecordDataImpl &Record);<br>+ void AddLocToRecord(SourceLocation Loc, RecordDataImpl &Record,<br>+ unsigned TokSize = 0);<br><br> /// \brief Add CharSourceRange information the specified record.<br> void AddCharSourceRangeToRecord(CharSourceRange R, RecordDataImpl &Record);<br>@@ -96,6 +104,8 @@<br> /// \brief The version of the diagnostics file.<br> enum { Version = 1 };<br><br>+ const LangOptions *LangOpts;<br>+ <br> /// \brief The byte buffer for the serialized content.<br> std::vector<unsigned char> Buffer;<br><br>@@ -181,13 +191,14 @@<br> }<br><br> void SDiagsWriter::AddLocToRecord(SourceLocation Loc,<br>- RecordDataImpl &Record) {<br>+ RecordDataImpl &Record,<br>+ unsigned TokSize) {<br> if (Loc.isInvalid()) {<br> // Emit a "sentinel" location.<br>- Record.push_back((unsigned) 0); // File.<br>- Record.push_back(~(unsigned)0); // Line.<br>- Record.push_back(~(unsigned)0); // Column.<br>- Record.push_back(~(unsigned)0); // Offset.<br>+ Record.push_back((unsigned)0); // File.<br>+ Record.push_back((unsigned)0); // Line.<br>+ Record.push_back((unsigned)0); // Column.<br>+ Record.push_back((unsigned)0); // Offset.<br> return;<br> }<br><br>@@ -195,7 +206,7 @@<br> Loc = SM.getSpellingLoc(Loc);<br> Record.push_back(getEmitFile(Loc));<br> Record.push_back(SM.getSpellingLineNumber(Loc));<br>- Record.push_back(SM.getSpellingColumnNumber(Loc));<br>+ Record.push_back(SM.getSpellingColumnNumber(Loc)+TokSize);<br><br> std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);<br> FileID FID = LocInfo.first;<br>@@ -206,7 +217,13 @@<br> void SDiagsWriter::AddCharSourceRangeToRecord(CharSourceRange Range,<br> RecordDataImpl &Record) {<br> AddLocToRecord(Range.getBegin(), Record);<br>- AddLocToRecord(Range.getEnd(), Record);<br>+ unsigned TokSize = 0;<br>+ if (Range.isTokenRange())<br>+ TokSize = Lexer::MeasureTokenLength(Range.getEnd(),<br>+ Diags.getSourceManager(),<br>+ *LangOpts);<br>+ <br>+ AddLocToRecord(Range.getEnd(), Record, TokSize);<br> }<br></div></blockquote><div><br></div><div>Instead of passing the TokSize to AddLocToRecord you could adjust the source location, like:</div><div><br></div><div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "> AddLocToRecord(Range.getBegin(), Record);</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "> <span style="color: #056142">SourceLocation</span> CharEnd = Range.getEnd();</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "> <span style="color: #911d67">if</span> (Range.isTokenRange()) {</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "> <span style="color: #911d67">unsigned</span> TokSize = <span style="color: #056142">Lexer</span>::MeasureTokenLength(CharEnd,</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "> <span style="color: #0230c9">Diags</span>.getSourceManager(),</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "> *<span style="color: #0230c9">LangOpts</span>);</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "> CharEnd = CharEnd.getLocWithOffset(TokSize);</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "> }</div><p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco; min-height: 15.0px"> <br class="webkit-block-placeholder"></p><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Monaco; "> AddLocToRecord(CharEnd, Record);</div><div><br></div></div></div></div></div><br><blockquote type="cite"><div><br> unsigned SDiagsWriter::getEmitFile(SourceLocation Loc) {<br><br>Modified: cfe/trunk/tools/c-index-test/c-index-test.c<br>URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/c-index-test/c-index-test.c?rev=144269&r1=144268&r2=144269&view=diff">http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/c-index-test/c-index-test.c?rev=144269&r1=144268&r2=144269&view=diff</a><br>==============================================================================<br>--- cfe/trunk/tools/c-index-test/c-index-test.c (original)<br>+++ cfe/trunk/tools/c-index-test/c-index-test.c Thu Nov 10 02:43:12 2011<br>@@ -2431,6 +2431,155 @@<br> }<br><br> /******************************************************************************/<br>+/* Serialized diagnostics. */<br>+/******************************************************************************/<br>+<br>+static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) {<br>+ switch (error) {<br>+ case CXLoadDiag_CannotLoad: return "Cannot Load File";<br>+ case CXLoadDiag_None: break;<br>+ case CXLoadDiag_Unknown: return "Unknown";<br>+ case CXLoadDiag_InvalidFile: return "Invalid File";<br>+ }<br>+ return "None";<br>+}<br>+<br>+static const char *getSeverityString(enum CXDiagnosticSeverity severity) {<br>+ switch (severity) {<br>+ case CXDiagnostic_Note: return "note";<br>+ case CXDiagnostic_Error: return "error";<br>+ case CXDiagnostic_Fatal: return "fatal";<br>+ case CXDiagnostic_Ignored: return "ignored";<br>+ case CXDiagnostic_Warning: return "warning";<br>+ }<br>+ return "unknown";<br>+}<br>+<br>+static void printIndent(unsigned indent) {<br>+ while (indent > 0) {<br>+ fprintf(stderr, " ");<br>+ --indent;<br>+ }<br>+}<br>+<br>+static void printLocation(CXSourceLocation L) {<br>+ CXFile File;<br>+ CXString FileName;<br>+ unsigned line, column, offset;<br>+ <br>+ clang_getExpansionLocation(L, &File, &line, &column, &offset);<br>+ FileName = clang_getFileName(File);<br>+ <br>+ fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column);<br>+ clang_disposeString(FileName);<br>+}<br>+<br>+static void printRanges(CXDiagnostic D, unsigned indent) {<br>+ unsigned i, n = clang_getDiagnosticNumRanges(D);<br>+ <br>+ for (i = 0; i < n; ++i) {<br>+ CXSourceLocation Start, End;<br>+ CXSourceRange SR = clang_getDiagnosticRange(D, i);<br>+ Start = clang_getRangeStart(SR);<br>+ End = clang_getRangeEnd(SR);<br>+ <br>+ printIndent(indent);<br>+ fprintf(stderr, "Range: ");<br>+ printLocation(Start);<br>+ fprintf(stderr, " ");<br>+ printLocation(End);<br>+ fprintf(stderr, "\n");<br>+ }<br>+}<br>+<br>+static void printFixIts(CXDiagnostic D, unsigned indent) {<br>+ unsigned i, n = clang_getDiagnosticNumFixIts(D);<br>+ for (i = 0 ; i < n; ++i) {<br>+ CXSourceRange ReplacementRange;<br>+ CXString text;<br>+ text = clang_getDiagnosticFixIt(D, i, &ReplacementRange);<br>+ <br>+ printIndent(indent);<br>+ fprintf(stderr, "FIXIT: (");<br>+ printLocation(clang_getRangeStart(ReplacementRange));<br>+ fprintf(stderr, " - ");<br>+ printLocation(clang_getRangeEnd(ReplacementRange));<br>+ fprintf(stderr, "): \"%s\"\n", clang_getCString(text));<br>+ clang_disposeString(text);<br>+ } <br>+}<br>+<br>+static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {<br>+ if (!Diags)<br>+ return;<br>+ <br>+ fprintf(stderr, "\n");<br>+<br>+ unsigned i = 0;<br>+ unsigned n = clang_getNumDiagnosticsInSet(Diags);<br>+ for (i = 0; i < n; ++i) {<br>+ CXSourceLocation DiagLoc;<br>+ CXDiagnostic D;<br>+ CXFile File;<br>+ CXString FileName, DiagSpelling, DiagOption;<br>+ unsigned line, column, offset;<br>+ const char *DiagOptionStr = 0;<br>+ <br>+ D = clang_getDiagnosticInSet(Diags, i);<br>+ DiagLoc = clang_getDiagnosticLocation(D);<br>+ clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset);<br>+ FileName = clang_getFileName(File);<br>+ DiagSpelling = clang_getDiagnosticSpelling(D);<br>+ <br>+ printIndent(indent);<br>+ <br>+ fprintf(stderr, "%s:%d:%d: %s: %s",<br>+ clang_getCString(FileName),<br>+ line,<br>+ column,<br>+ getSeverityString(clang_getDiagnosticSeverity(D)),<br>+ clang_getCString(DiagSpelling));<br>+<br>+ DiagOption = clang_getDiagnosticOption(D, 0);<br>+ DiagOptionStr = clang_getCString(DiagOption);<br>+ if (DiagOptionStr) {<br>+ fprintf(stderr, " [%s]", DiagOptionStr);<br>+ }<br>+ <br>+ fprintf(stderr, "\n");<br>+ <br>+ printRanges(D, indent);<br>+ printFixIts(D, indent);<br>+ <br>+ // Print subdiagnostics.<br>+ printDiagnosticSet(clang_getChildDiagnostics(D), indent+2);<br>+<br>+ clang_disposeString(FileName);<br>+ clang_disposeString(DiagSpelling);<br>+ clang_disposeString(DiagOption);<br>+ } <br>+}<br>+<br>+static int read_diagnostics(const char *filename) {<br>+ enum CXLoadDiag_Error error;<br>+ CXString errorString;<br>+ CXDiagnosticSet Diags = 0;<br>+ <br>+ Diags = clang_loadDiagnostics(filename, &error, &errorString);<br>+ if (!Diags) {<br>+ fprintf(stderr, "Trouble deserializing file (%s): %s\n",<br>+ getDiagnosticCodeStr(error),<br>+ clang_getCString(errorString));<br>+ clang_disposeString(errorString);<br>+ return 1;<br>+ }<br>+ <br>+ printDiagnosticSet(Diags, 0);<br>+ clang_disposeDiagnosticSet(Diags);<br>+ return 0;<br>+}<br>+<br>+/******************************************************************************/<br> /* Command line processing. */<br> /******************************************************************************/<br><br>@@ -2475,7 +2624,9 @@<br> " c-index-test -test-print-typekind {<args>}*\n"<br> " c-index-test -print-usr [<CursorKind> {<args>}]*\n"<br> " c-index-test -print-usr-file <file>\n"<br>- " c-index-test -write-pch <file> <compiler arguments>\n\n");<br>+ " c-index-test -write-pch <file> <compiler arguments>\n");<br>+ fprintf(stderr,<br>+ " c-index-test -read-diagnostics <file>\n\n");<br> fprintf(stderr,<br> " <symbol filter> values:\n%s",<br> " all - load all symbols, including those from PCH\n"<br>@@ -2492,6 +2643,8 @@<br><br> int cindextest_main(int argc, const char **argv) {<br> clang_enableStackTraces();<br>+ if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0)<br>+ return read_diagnostics(argv[2]);<br> if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])<br> return perform_code_completion(argc, argv, 0);<br> if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])<br><br>Modified: cfe/trunk/tools/libclang/CIndex.cpp<br>URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndex.cpp?rev=144269&r1=144268&r2=144269&view=diff">http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndex.cpp?rev=144269&r1=144268&r2=144269&view=diff</a><br>==============================================================================<br>--- cfe/trunk/tools/libclang/CIndex.cpp (original)<br>+++ cfe/trunk/tools/libclang/CIndex.cpp Thu Nov 10 02:43:12 2011<br>@@ -58,6 +58,7 @@<br> CXTranslationUnit D = new CXTranslationUnitImpl();<br> D->TUData = TU;<br> D->StringPool = createCXStringPool();<br>+ D->Diagnostics = 0;<br> return D;<br> }<br><br>@@ -2562,6 +2563,7 @@<br><br> delete static_cast<ASTUnit *>(CTUnit->TUData);<br> disposeCXStringPool(CTUnit->StringPool);<br>+ delete static_cast<CXDiagnosticSetImpl *>(CTUnit->Diagnostics);<br> delete CTUnit;<br> }<br> }<br>@@ -2582,6 +2584,11 @@<br> ReparseTranslationUnitInfo *RTUI =<br> static_cast<ReparseTranslationUnitInfo*>(UserData);<br> CXTranslationUnit TU = RTUI->TU;<br>+<br>+ // Reset the associated diagnostics.<br>+ delete static_cast<CXDiagnosticSetImpl*>(TU->Diagnostics);<br>+ TU->Diagnostics = 0;<br>+<br> unsigned num_unsaved_files = RTUI->num_unsaved_files;<br> struct CXUnsavedFile *unsaved_files = RTUI->unsaved_files;<br> unsigned options = RTUI->options;<br><br>Modified: cfe/trunk/tools/libclang/CIndexDiagnostic.cpp<br>URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndexDiagnostic.cpp?rev=144269&r1=144268&r2=144269&view=diff">http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndexDiagnostic.cpp?rev=144269&r1=144268&r2=144269&view=diff</a><br>==============================================================================<br>--- cfe/trunk/tools/libclang/CIndexDiagnostic.cpp (original)<br>+++ cfe/trunk/tools/libclang/CIndexDiagnostic.cpp Thu Nov 10 02:43:12 2011<br>@@ -28,27 +28,58 @@<br> using namespace clang::cxstring;<br> using namespace llvm;<br><br>+<br>+CXDiagnosticSetImpl::~CXDiagnosticSetImpl() {<br>+ for (std::vector<CXDiagnosticImpl *>::iterator it = Diagnostics.begin(),<br>+ et = Diagnostics.end();<br>+ it != et; ++it) {<br>+ delete *it;<br>+ }<br>+}<br>+<br>+CXDiagnosticImpl::~CXDiagnosticImpl() {}<br>+<br>+static CXDiagnosticSetImpl *lazyCreateDiags(CXTranslationUnit TU) {<br>+ if (!TU->Diagnostics) {<br>+ ASTUnit *AU = static_cast<ASTUnit *>(TU->TUData);<br>+ CXDiagnosticSetImpl *Set = new CXDiagnosticSetImpl();<br>+ TU->Diagnostics = Set;<br>+ <br>+ for (ASTUnit::stored_diag_iterator it = AU->stored_diag_begin(),<br>+ ei = AU->stored_diag_end(); it != ei; ++it) {<br>+ CXStoredDiagnostic *D =<br>+ new CXStoredDiagnostic(*it, AU->getASTContext().getLangOptions());<br>+ Set->appendDiagnostic(D);<br>+ }<br>+ }<br>+ return static_cast<CXDiagnosticSetImpl*>(TU->Diagnostics);<br>+}<br>+<br> //-----------------------------------------------------------------------------<br> // C Interface Routines<br> //-----------------------------------------------------------------------------<br> extern "C" {<br><br> unsigned clang_getNumDiagnostics(CXTranslationUnit Unit) {<br>- ASTUnit *CXXUnit = static_cast<ASTUnit *>(Unit->TUData);<br>- return CXXUnit? CXXUnit->stored_diag_size() : 0;<br>+ if (!Unit->TUData)<br>+ return 0;<br>+ return lazyCreateDiags(Unit)->getNumDiagnostics();<br> }<br><br> CXDiagnostic clang_getDiagnostic(CXTranslationUnit Unit, unsigned Index) {<br>- ASTUnit *CXXUnit = static_cast<ASTUnit *>(Unit->TUData);<br>- if (!CXXUnit || Index >= CXXUnit->stored_diag_size())<br>+ if (!Unit->TUData)<br>+ return 0;<br>+<br>+ CXDiagnosticSetImpl *Diags = lazyCreateDiags(Unit);<br>+ if (Index >= Diags->getNumDiagnostics())<br> return 0;<br><br>- return new CXStoredDiagnostic(CXXUnit->stored_diag_begin()[Index],<br>- CXXUnit->getASTContext().getLangOptions());<br>+ return Diags->getDiagnostic(Index);<br> }<br><br> void clang_disposeDiagnostic(CXDiagnostic Diagnostic) {<br>- delete static_cast<CXDiagnosticImpl *>(Diagnostic);<br>+ // No-op. Kept as a legacy API. CXDiagnostics are now managed<br>+ // by the enclosing CXDiagnosticSet.<br> }<br><br> CXString clang_formatDiagnostic(CXDiagnostic Diagnostic, unsigned Options) {<br>@@ -243,4 +274,32 @@<br> return D->getFixIt(FixIt, ReplacementRange);<br> }<br><br>+void clang_disposeDiagnosticSet(CXDiagnosticSet Diags) {<br>+ CXDiagnosticSetImpl *D = static_cast<CXDiagnosticSetImpl*>(Diags);<br>+ if (D->isExternallyManaged())<br>+ delete D;<br>+}<br>+ <br>+CXDiagnostic clang_getDiagnosticInSet(CXDiagnosticSet Diags,<br>+ unsigned Index) {<br>+ if (CXDiagnosticSetImpl *D = static_cast<CXDiagnosticSetImpl*>(Diags))<br>+ if (Index < D->getNumDiagnostics())<br>+ return D->getDiagnostic(Index);<br>+ return 0; <br>+}<br>+ <br>+CXDiagnosticSet clang_getChildDiagnostics(CXDiagnostic Diag) {<br>+ if (CXDiagnosticImpl *D = static_cast<CXDiagnosticImpl *>(Diag)) {<br>+ CXDiagnosticSetImpl &ChildDiags = D->getChildDiagnostics();<br>+ return ChildDiags.empty() ? 0 : (CXDiagnosticSet) &ChildDiags;<br>+ }<br>+ return 0;<br>+}<br>+<br>+unsigned clang_getNumDiagnosticsInSet(CXDiagnosticSet Diags) {<br>+ if (CXDiagnosticSetImpl *D = static_cast<CXDiagnosticSetImpl*>(Diags))<br>+ return D->getNumDiagnostics();<br>+ return 0;<br>+}<br>+<br> } // end extern "C"<br><br>Modified: cfe/trunk/tools/libclang/CIndexDiagnostic.h<br>URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndexDiagnostic.h?rev=144269&r1=144268&r2=144269&view=diff">http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndexDiagnostic.h?rev=144269&r1=144268&r2=144269&view=diff</a><br>==============================================================================<br>--- cfe/trunk/tools/libclang/CIndexDiagnostic.h (original)<br>+++ cfe/trunk/tools/libclang/CIndexDiagnostic.h Thu Nov 10 02:43:12 2011<br>@@ -14,15 +14,47 @@<br> #define LLVM_CLANG_CINDEX_DIAGNOSTIC_H<br><br> #include "clang-c/Index.h"<br>+#include <vector><br>+#include <assert.h><br><br> namespace clang {<br><br> class LangOptions;<br> class StoredDiagnostic;<br>+class CXDiagnosticImpl;<br>+ <br>+class CXDiagnosticSetImpl {<br>+ std::vector<CXDiagnosticImpl *> Diagnostics;<br>+ const bool IsExternallyManaged;<br>+public:<br>+ CXDiagnosticSetImpl(bool isManaged = false)<br>+ : IsExternallyManaged(isManaged) {}<br>+<br>+ virtual ~CXDiagnosticSetImpl();<br>+ <br>+ size_t getNumDiagnostics() const {<br>+ return Diagnostics.size();<br>+ }<br>+ <br>+ CXDiagnosticImpl *getDiagnostic(unsigned i) const {<br>+ assert(i < getNumDiagnostics());<br>+ return Diagnostics[i];<br>+ }<br>+ <br>+ void appendDiagnostic(CXDiagnosticImpl *D) {<br>+ Diagnostics.push_back(D);<br>+ }<br>+ <br>+ bool empty() const {<br>+ return Diagnostics.empty();<br>+ }<br>+ <br>+ bool isExternallyManaged() const { return IsExternallyManaged; }<br>+};<br><br> class CXDiagnosticImpl {<br> public:<br>- enum Kind { StoredDiagnosticKind, SerializedDiagnosticKind };<br>+ enum Kind { StoredDiagnosticKind, LoadedDiagnosticKind };<br><br> virtual ~CXDiagnosticImpl();<br><br>@@ -55,9 +87,18 @@<br> CXSourceRange *ReplacementRange) const = 0;<br><br> Kind getKind() const { return K; }<br>-<br>+ <br>+ CXDiagnosticSetImpl &getChildDiagnostics() {<br>+ return ChildDiags;<br>+ }<br>+ <br> protected:<br> CXDiagnosticImpl(Kind k) : K(k) {}<br>+ CXDiagnosticSetImpl ChildDiags;<br>+ <br>+ void append(CXDiagnosticImpl *D) {<br>+ ChildDiags.appendDiagnostic(D);<br>+ }<br><br> private:<br> Kind K;<br><br>Modified: cfe/trunk/tools/libclang/CMakeLists.txt<br>URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CMakeLists.txt?rev=144269&r1=144268&r2=144269&view=diff">http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CMakeLists.txt?rev=144269&r1=144268&r2=144269&view=diff</a><br>==============================================================================<br>--- cfe/trunk/tools/libclang/CMakeLists.txt (original)<br>+++ cfe/trunk/tools/libclang/CMakeLists.txt Thu Nov 10 02:43:12 2011<br>@@ -29,6 +29,8 @@<br> CIndexer.h<br> CXCursor.cpp<br> CXCursor.h<br>+ CXLoadedDiagnostic.cpp<br>+ CXLoadedDiagnostic.h<br> CXSourceLocation.cpp<br> CXSourceLocation.h<br> CXStoredDiagnostic.cpp<br><br>Added: cfe/trunk/tools/libclang/CXLoadedDiagnostic.cpp<br>URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXLoadedDiagnostic.cpp?rev=144269&view=auto">http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXLoadedDiagnostic.cpp?rev=144269&view=auto</a><br>==============================================================================<br>--- cfe/trunk/tools/libclang/CXLoadedDiagnostic.cpp (added)<br>+++ cfe/trunk/tools/libclang/CXLoadedDiagnostic.cpp Thu Nov 10 02:43:12 2011<br>@@ -0,0 +1,655 @@<br>+/*===-- CXLoadedDiagnostic.cpp - Handling of persisent diags -*- C++ -*-===*\<br>+|* *|<br>+|* The LLVM Compiler Infrastructure *|<br>+|* *|<br>+|* This file is distributed under the University of Illinois Open Source *|<br>+|* License. See LICENSE.TXT for details. *|<br>+|* *|<br>+|*===----------------------------------------------------------------------===*|<br>+|* *|<br>+|* Implements handling of persisent diagnostics. *|<br>+|* *|<br>+\*===----------------------------------------------------------------------===*/<br>+<br>+#include "CXLoadedDiagnostic.h"<br>+#include "CXString.h"<br>+#include "clang/Basic/Diagnostic.h"<br>+#include "clang/Basic/FileManager.h"<br>+#include "clang/Frontend/SerializedDiagnosticPrinter.h"<br>+#include "llvm/ADT/StringRef.h"<br>+#include "llvm/ADT/Twine.h"<br>+#include "llvm/ADT/Optional.h"<br>+#include "clang/Basic/LLVM.h"<br>+#include "llvm/Support/ErrorHandling.h"<br>+#include "llvm/Bitcode/BitstreamReader.h"<br>+#include "llvm/Support/MemoryBuffer.h"<br>+#include <assert.h><br>+<br>+using namespace clang;<br>+using namespace clang::cxstring;<br>+<br>+//===----------------------------------------------------------------------===//<br>+// Extend CXDiagnosticSetImpl which contains strings for diagnostics.<br>+//===----------------------------------------------------------------------===//<br>+<br>+typedef llvm::DenseMap<unsigned, llvm::StringRef> Strings;<br>+<br>+namespace {<br>+class CXLoadedDiagnosticSetImpl : public CXDiagnosticSetImpl {<br>+public:<br>+ CXLoadedDiagnosticSetImpl() : CXDiagnosticSetImpl(true), FakeFiles(FO) {}<br>+ virtual ~CXLoadedDiagnosticSetImpl() {} <br>+<br>+ llvm::StringRef makeString(const char *blob, unsigned blobLen);<br>+ <br>+ llvm::BumpPtrAllocator Alloc;<br>+ Strings Categories;<br>+ Strings WarningFlags;<br>+ Strings FileNames;<br>+ <br>+ FileSystemOptions FO;<br>+ FileManager FakeFiles;<br>+ llvm::DenseMap<unsigned, const FileEntry *> Files;<br>+};<br>+}<br>+<br>+llvm::StringRef CXLoadedDiagnosticSetImpl::makeString(const char *blob,<br>+ unsigned bloblen) {<br>+ char *mem = Alloc.Allocate<char>(bloblen);<br>+ memcpy(mem, blob, bloblen);<br>+ return llvm::StringRef(mem, bloblen);<br>+}<br>+<br>+//===----------------------------------------------------------------------===//<br>+// Cleanup.<br>+//===----------------------------------------------------------------------===//<br>+<br>+CXLoadedDiagnostic::~CXLoadedDiagnostic() {}<br>+<br>+//===----------------------------------------------------------------------===//<br>+// Public CXLoadedDiagnostic methods.<br>+//===----------------------------------------------------------------------===//<br>+<br>+CXDiagnosticSeverity CXLoadedDiagnostic::getSeverity() const {<br>+ // FIXME: possibly refactor with logic in CXStoredDiagnostic.<br>+ switch (severity) {<br>+ case DiagnosticsEngine::Ignored: return CXDiagnostic_Ignored;<br>+ case DiagnosticsEngine::Note: return CXDiagnostic_Note;<br>+ case DiagnosticsEngine::Warning: return CXDiagnostic_Warning;<br>+ case DiagnosticsEngine::Error: return CXDiagnostic_Error;<br>+ case DiagnosticsEngine::Fatal: return CXDiagnostic_Fatal;<br>+ }<br>+ <br>+ llvm_unreachable("Invalid diagnostic level");<br>+ return CXDiagnostic_Ignored;<br>+}<br>+<br>+static CXSourceLocation makeLocation(const CXLoadedDiagnostic::Location *DLoc) {<br>+ // The lowest bit of ptr_data[0] is always set to 1 to indicate this<br>+ // is a persistent diagnostic.<br>+ uintptr_t V = (uintptr_t) DLoc;<br>+ V |= 0x1;<br>+ CXSourceLocation Loc = { { (void*) V, 0 }, 0 };<br>+ return Loc;<br>+} <br>+<br>+CXSourceLocation CXLoadedDiagnostic::getLocation() const {<br>+ // The lowest bit of ptr_data[0] is always set to 1 to indicate this<br>+ // is a persistent diagnostic.<br>+ return makeLocation(&DiagLoc);<br>+}<br>+<br>+CXString CXLoadedDiagnostic::getSpelling() const {<br>+ return cxstring::createCXString(Spelling, false); <br>+}<br>+<br>+CXString CXLoadedDiagnostic::getDiagnosticOption(CXString *Disable) const {<br>+ if (DiagOption.empty())<br>+ return createCXString("");<br>+<br>+ // FIXME: possibly refactor with logic in CXStoredDiagnostic.<br>+ if (Disable)<br>+ *Disable = createCXString((Twine("-Wno-") + DiagOption).str());<br>+ return createCXString((Twine("-W") + DiagOption).str());<br>+}<br>+<br>+unsigned CXLoadedDiagnostic::getCategory() const {<br>+ return category;<br>+}<br>+<br>+unsigned CXLoadedDiagnostic::getNumRanges() const {<br>+ return Ranges.size();<br>+}<br>+<br>+CXSourceRange CXLoadedDiagnostic::getRange(unsigned Range) const {<br>+ assert(Range < Ranges.size());<br>+ return Ranges[Range];<br>+}<br>+<br>+unsigned CXLoadedDiagnostic::getNumFixIts() const {<br>+ return FixIts.size();<br>+}<br>+<br>+CXString CXLoadedDiagnostic::getFixIt(unsigned FixIt,<br>+ CXSourceRange *ReplacementRange) const {<br>+ assert(FixIt < FixIts.size());<br>+ if (ReplacementRange)<br>+ *ReplacementRange = FixIts[FixIt].first;<br>+ return FixIts[FixIt].second;<br>+}<br>+<br>+void CXLoadedDiagnostic::decodeLocation(CXSourceLocation location,<br>+ CXFile *file,<br>+ unsigned int *line,<br>+ unsigned int *column,<br>+ unsigned int *offset) {<br>+ <br>+ <br>+ // CXSourceLocation consists of the following fields:<br>+ //<br>+ // void *ptr_data[2];<br>+ // unsigned int_data;<br>+ //<br>+ // The lowest bit of ptr_data[0] is always set to 1 to indicate this<br>+ // is a persistent diagnostic.<br>+ //<br>+ // For now, do the unoptimized approach and store the data in a side<br>+ // data structure. We can optimize this case later.<br>+ <br>+ uintptr_t V = (uintptr_t) location.ptr_data[0];<br>+ assert((V & 0x1) == 1);<br>+ V &= ~(uintptr_t)1;<br>+ <br>+ const Location &Loc = *((Location*)V);<br>+ <br>+ if (file)<br>+ *file = Loc.file; <br>+ if (line)<br>+ *line = Loc.line;<br>+ if (column)<br>+ *column = Loc.column;<br>+ if (offset)<br>+ *offset = Loc.offset;<br>+}<br></div></blockquote><div><br></div><div>Is this modification to CXSourceLocation so that the diagnostic location becomes independent of a SourceManager ?</div><div><br></div><div>Why not introduce a new kind of location (like CXFileLocation or something) instead ? If you remove the SourceManager you "disable" the functionality of CXSourceLocation anyway, e.g. while previously you could go through the expansion stack, get presumed location, etc., you can't do that with this new kind of location. Seems better to be clear in the API.</div><div><br></div><div>-Argyrios</div><br><blockquote type="cite"><div>+<br>+//===----------------------------------------------------------------------===//<br>+// Deserialize diagnostics.<br>+//===----------------------------------------------------------------------===//<br>+<br>+enum { MaxSupportedVersion = 1 };<br>+typedef SmallVector<uint64_t, 64> RecordData;<br>+enum LoadResult { Failure = 1, Success = 0 };<br>+enum StreamResult { Read_EndOfStream,<br>+ Read_BlockBegin,<br>+ Read_Failure,<br>+ Read_Record,<br>+ Read_BlockEnd };<br>+<br>+namespace {<br>+class DiagLoader {<br>+ enum CXLoadDiag_Error *error;<br>+ CXString *errorString;<br>+ <br>+ void reportBad(enum CXLoadDiag_Error code, llvm::StringRef err) {<br>+ if (error)<br>+ *error = code;<br>+ if (errorString)<br>+ *errorString = createCXString(err);<br>+ }<br>+ <br>+ void reportInvalidFile(llvm::StringRef err) {<br>+ return reportBad(CXLoadDiag_InvalidFile, err);<br>+ }<br>+<br>+ LoadResult readMetaBlock(llvm::BitstreamCursor &Stream);<br>+ <br>+ LoadResult readDiagnosticBlock(llvm::BitstreamCursor &Stream,<br>+ CXDiagnosticSetImpl &Diags,<br>+ CXLoadedDiagnosticSetImpl &TopDiags);<br>+<br>+ StreamResult readToNextRecordOrBlock(llvm::BitstreamCursor &Stream,<br>+ llvm::StringRef errorContext,<br>+ unsigned &BlockOrRecordID,<br>+ const bool atTopLevel = false);<br>+ <br>+ <br>+ LoadResult readString(CXLoadedDiagnosticSetImpl &TopDiags,<br>+ Strings &strings, llvm::StringRef errorContext,<br>+ RecordData &Record,<br>+ const char *BlobStart,<br>+ unsigned BlobLen);<br>+<br>+ LoadResult readString(CXLoadedDiagnosticSetImpl &TopDiags,<br>+ llvm::StringRef &RetStr,<br>+ llvm::StringRef errorContext,<br>+ RecordData &Record,<br>+ const char *BlobStart,<br>+ unsigned BlobLen);<br>+<br>+ LoadResult readRange(CXLoadedDiagnosticSetImpl &TopDiags,<br>+ RecordData &Record, unsigned RecStartIdx,<br>+ CXSourceRange &SR);<br>+ <br>+ LoadResult readLocation(CXLoadedDiagnosticSetImpl &TopDiags,<br>+ RecordData &Record, unsigned &offset,<br>+ CXLoadedDiagnostic::Location &Loc);<br>+ <br>+public:<br>+ DiagLoader(enum CXLoadDiag_Error *e, CXString *es)<br>+ : error(e), errorString(es) {<br>+ if (error)<br>+ *error = CXLoadDiag_None;<br>+ if (errorString)<br>+ *errorString = createCXString("");<br>+ }<br>+ <br>+ CXDiagnosticSet load(const char *file);<br>+};<br>+}<br>+<br>+CXDiagnosticSet DiagLoader::load(const char *file) {<br>+ // Open the diagnostics file.<br>+ std::string ErrStr;<br>+ FileSystemOptions FO;<br>+ FileManager FileMgr(FO);<br>+<br>+ llvm::OwningPtr<llvm::MemoryBuffer> Buffer;<br>+ Buffer.reset(FileMgr.getBufferForFile(file));<br>+<br>+ if (!Buffer) {<br>+ reportBad(CXLoadDiag_CannotLoad, ErrStr);<br>+ return 0;<br>+ }<br>+<br>+ llvm::BitstreamReader StreamFile;<br>+ StreamFile.init((const unsigned char *)Buffer->getBufferStart(),<br>+ (const unsigned char *)Buffer->getBufferEnd());<br>+<br>+ llvm::BitstreamCursor Stream;<br>+ Stream.init(StreamFile);<br>+<br>+ // Sniff for the signature.<br>+ if (Stream.Read(8) != 'D' ||<br>+ Stream.Read(8) != 'I' ||<br>+ Stream.Read(8) != 'A' ||<br>+ Stream.Read(8) != 'G') {<br>+ reportBad(CXLoadDiag_InvalidFile,<br>+ "Bad header in diagnostics file");<br>+ return 0;<br>+ }<br>+<br>+ llvm::OwningPtr<CXLoadedDiagnosticSetImpl><br>+ Diags(new CXLoadedDiagnosticSetImpl());<br>+<br>+ while (true) {<br>+ unsigned BlockID = 0;<br>+ StreamResult Res = readToNextRecordOrBlock(Stream, "Top-level", <br>+ BlockID, true);<br>+ switch (Res) {<br>+ case Read_EndOfStream:<br>+ return (CXDiagnosticSet) Diags.take();<br>+ case Read_Failure:<br>+ return 0;<br>+ case Read_Record:<br>+ llvm_unreachable("Top-level does not have records");<br>+ return 0;<br>+ case Read_BlockEnd:<br>+ continue;<br>+ case Read_BlockBegin:<br>+ break;<br>+ }<br>+ <br>+ switch (BlockID) {<br>+ case serialized_diags::BLOCK_META:<br>+ if (readMetaBlock(Stream))<br>+ return 0;<br>+ break;<br>+ case serialized_diags::BLOCK_DIAG:<br>+ if (readDiagnosticBlock(Stream, *Diags.get(), *Diags.get()))<br>+ return 0;<br>+ break;<br>+ default:<br>+ if (!Stream.SkipBlock()) {<br>+ reportInvalidFile("Malformed block at top-level of diagnostics file");<br>+ return 0;<br>+ }<br>+ break;<br>+ }<br>+ }<br>+}<br>+<br>+StreamResult DiagLoader::readToNextRecordOrBlock(llvm::BitstreamCursor &Stream,<br>+ llvm::StringRef errorContext,<br>+ unsigned &blockOrRecordID,<br>+ const bool atTopLevel) {<br>+ <br>+ blockOrRecordID = 0;<br>+<br>+ while (!Stream.AtEndOfStream()) {<br>+ unsigned Code = Stream.ReadCode();<br>+<br>+ // Handle the top-level specially.<br>+ if (atTopLevel) {<br>+ if (Code == llvm::bitc::ENTER_SUBBLOCK) {<br>+ unsigned BlockID = Stream.ReadSubBlockID();<br>+ if (BlockID == llvm::bitc::BLOCKINFO_BLOCK_ID) {<br>+ if (Stream.ReadBlockInfoBlock()) {<br>+ reportInvalidFile("Malformed BlockInfoBlock in diagnostics file");<br>+ return Read_Failure;<br>+ }<br>+ continue;<br>+ }<br>+ blockOrRecordID = BlockID;<br>+ return Read_BlockBegin;<br>+ }<br>+ reportInvalidFile("Only blocks can appear at the top of a "<br>+ "diagnostic file");<br>+ return Read_Failure;<br>+ }<br>+ <br>+ switch ((llvm::bitc::FixedAbbrevIDs)Code) {<br>+ case llvm::bitc::ENTER_SUBBLOCK:<br>+ blockOrRecordID = Stream.ReadSubBlockID();<br>+ return Read_BlockBegin;<br>+ <br>+ case llvm::bitc::END_BLOCK:<br>+ if (Stream.ReadBlockEnd()) {<br>+ reportInvalidFile("Cannot read end of block");<br>+ return Read_Failure;<br>+ }<br>+ return Read_BlockEnd;<br>+ <br>+ case llvm::bitc::DEFINE_ABBREV:<br>+ Stream.ReadAbbrevRecord();<br>+ continue;<br>+ <br>+ case llvm::bitc::UNABBREV_RECORD:<br>+ reportInvalidFile("Diagnostics file should have no unabbreviated "<br>+ "records");<br>+ return Read_Failure;<br>+ <br>+ default:<br>+ // We found a record.<br>+ blockOrRecordID = Code;<br>+ return Read_Record;<br>+ }<br>+ }<br>+ <br>+ if (atTopLevel)<br>+ return Read_EndOfStream;<br>+ <br>+ reportInvalidFile(Twine("Premature end of diagnostics file within ").str() + <br>+ errorContext.str());<br>+ return Read_Failure;<br>+}<br>+<br>+LoadResult DiagLoader::readMetaBlock(llvm::BitstreamCursor &Stream) {<br>+ if (Stream.EnterSubBlock(clang::serialized_diags::BLOCK_META)) {<br>+ reportInvalidFile("Malformed metadata block");<br>+ return Failure;<br>+ }<br>+<br>+ bool versionChecked = false;<br>+ <br>+ while (true) {<br>+ unsigned blockOrCode = 0;<br>+ StreamResult Res = readToNextRecordOrBlock(Stream, "Metadata Block",<br>+ blockOrCode);<br>+ <br>+ switch(Res) {<br>+ case Read_EndOfStream:<br>+ llvm_unreachable("EndOfStream handled by readToNextRecordOrBlock");<br>+ case Read_Failure:<br>+ return Failure;<br>+ case Read_Record:<br>+ break;<br>+ case Read_BlockBegin:<br>+ if (Stream.SkipBlock()) {<br>+ reportInvalidFile("Malformed metadata block");<br>+ return Failure;<br>+ }<br>+ case Read_BlockEnd:<br>+ if (!versionChecked) {<br>+ reportInvalidFile("Diagnostics file does not contain version"<br>+ " information");<br>+ return Failure;<br>+ }<br>+ return Success;<br>+ }<br>+ <br>+ RecordData Record;<br>+ const char *Blob;<br>+ unsigned BlobLen;<br>+ unsigned recordID = Stream.ReadRecord(blockOrCode, Record, &Blob, &BlobLen);<br>+ <br>+ if (recordID == serialized_diags::RECORD_VERSION) {<br>+ if (Record.size() < 1) {<br>+ reportInvalidFile("malformed VERSION identifier in diagnostics file");<br>+ return Failure;<br>+ }<br>+ if (Record[0] > MaxSupportedVersion) {<br>+ reportInvalidFile("diagnosics file is a newer version than the one "<br>+ "supported");<br>+ return Failure;<br>+ }<br>+ versionChecked = true;<br>+ }<br>+ }<br>+}<br>+<br>+LoadResult DiagLoader::readString(CXLoadedDiagnosticSetImpl &TopDiags,<br>+ llvm::StringRef &RetStr,<br>+ llvm::StringRef errorContext,<br>+ RecordData &Record,<br>+ const char *BlobStart,<br>+ unsigned BlobLen) {<br>+ <br>+ // Basic buffer overflow check.<br>+ if (BlobLen > 65536) {<br>+ reportInvalidFile(std::string("Out-of-bounds string in ") +<br>+ std::string(errorContext));<br>+ return Failure;<br>+ }<br>+ <br>+ if (Record.size() < 1 || BlobLen == 0) {<br>+ reportInvalidFile(std::string("Corrupted ") + std::string(errorContext)<br>+ + std::string(" entry"));<br>+ return Failure;<br>+ }<br>+ <br>+ RetStr = TopDiags.makeString(BlobStart, BlobLen);<br>+ return Success;<br>+}<br>+<br>+LoadResult DiagLoader::readString(CXLoadedDiagnosticSetImpl &TopDiags,<br>+ Strings &strings,<br>+ llvm::StringRef errorContext,<br>+ RecordData &Record,<br>+ const char *BlobStart,<br>+ unsigned BlobLen) {<br>+ llvm::StringRef RetStr;<br>+ if (readString(TopDiags, RetStr, errorContext, Record, BlobStart, BlobLen))<br>+ return Failure;<br>+ strings[Record[0]] = RetStr;<br>+ return Success;<br>+}<br>+<br>+LoadResult DiagLoader::readLocation(CXLoadedDiagnosticSetImpl &TopDiags,<br>+ RecordData &Record, unsigned &offset,<br>+ CXLoadedDiagnostic::Location &Loc) {<br>+ if (Record.size() < offset + 3) {<br>+ reportInvalidFile("Corrupted source location");<br>+ return Failure;<br>+ }<br>+ <br>+ unsigned fileID = Record[offset++];<br>+ if (fileID == 0) {<br>+ // Sentinel value.<br>+ Loc.file = 0;<br>+ Loc.line = 0;<br>+ Loc.column = 0;<br>+ Loc.offset = 0;<br>+ return Success;<br>+ }<br>+<br>+ const FileEntry *FE = TopDiags.Files[fileID];<br>+ if (!FE) {<br>+ reportInvalidFile("Corrupted file entry in source location");<br>+ return Failure;<br>+ }<br>+ Loc.file = (void*) FE;<br>+ Loc.line = Record[offset++];<br>+ Loc.column = Record[offset++];<br>+ Loc.offset = Record[offset++];<br>+ return Success;<br>+}<br>+<br>+LoadResult DiagLoader::readRange(CXLoadedDiagnosticSetImpl &TopDiags,<br>+ RecordData &Record,<br>+ unsigned int RecStartIdx,<br>+ CXSourceRange &SR) {<br>+ CXLoadedDiagnostic::Location *Start, *End;<br>+ Start = TopDiags.Alloc.Allocate<CXLoadedDiagnostic::Location>();<br>+ End = TopDiags.Alloc.Allocate<CXLoadedDiagnostic::Location>();<br>+ <br>+ if (readLocation(TopDiags, Record, RecStartIdx, *Start))<br>+ return Failure;<br>+ if (readLocation(TopDiags, Record, RecStartIdx, *End))<br>+ return Failure;<br>+ <br>+ CXSourceLocation startLoc = makeLocation(Start);<br>+ CXSourceLocation endLoc = makeLocation(End);<br>+ SR = clang_getRange(startLoc, endLoc);<br>+ return Success; <br>+}<br>+<br>+LoadResult DiagLoader::readDiagnosticBlock(llvm::BitstreamCursor &Stream,<br>+ CXDiagnosticSetImpl &Diags,<br>+ CXLoadedDiagnosticSetImpl &TopDiags){<br>+<br>+ if (Stream.EnterSubBlock(clang::serialized_diags::BLOCK_DIAG)) {<br>+ reportInvalidFile("malformed diagnostic block");<br>+ return Failure;<br>+ }<br>+ <br>+ llvm::OwningPtr<CXLoadedDiagnostic> D(new CXLoadedDiagnostic());<br>+ RecordData Record;<br>+ <br>+ while (true) {<br>+ unsigned blockOrCode = 0;<br>+ StreamResult Res = readToNextRecordOrBlock(Stream, "Diagnostic Block",<br>+ blockOrCode);<br>+ switch (Res) {<br>+ case Read_EndOfStream:<br>+ llvm_unreachable("EndOfStream handled in readToNextRecordOrBlock");<br>+ return Failure;<br>+ case Read_Failure:<br>+ return Failure;<br>+ case Read_BlockBegin: {<br>+ // The only blocks we care about are subdiagnostics.<br>+ if (blockOrCode != serialized_diags::BLOCK_DIAG) {<br>+ if (!Stream.SkipBlock()) {<br>+ reportInvalidFile("Invalid subblock in Diagnostics block");<br>+ return Failure;<br>+ }<br>+ } else if (readDiagnosticBlock(Stream, D->getChildDiagnostics(),<br>+ TopDiags)) {<br>+ return Failure;<br>+ }<br>+<br>+ continue;<br>+ }<br>+ case Read_BlockEnd:<br>+ Diags.appendDiagnostic(D.take()); <br>+ return Success;<br>+ case Read_Record:<br>+ break;<br>+ }<br>+ <br>+ // Read the record.<br>+ Record.clear();<br>+ const char *BlobStart = 0;<br>+ unsigned BlobLen = 0;<br>+ unsigned recID = Stream.ReadRecord(blockOrCode, Record,<br>+ BlobStart, BlobLen);<br>+ <br>+ if (recID < serialized_diags::RECORD_FIRST ||<br>+ recID > serialized_diags::RECORD_LAST)<br>+ continue;<br>+ <br>+ switch ((serialized_diags::RecordIDs)recID) { <br>+ case serialized_diags::RECORD_VERSION:<br>+ continue;<br>+ case serialized_diags::RECORD_CATEGORY:<br>+ if (readString(TopDiags, TopDiags.Categories, "category", Record,<br>+ BlobStart, BlobLen))<br>+ return Failure;<br>+ continue;<br>+ <br>+ case serialized_diags::RECORD_DIAG_FLAG:<br>+ if (readString(TopDiags, TopDiags.WarningFlags, "warning flag", Record,<br>+ BlobStart, BlobLen))<br>+ return Failure;<br>+ continue;<br>+ <br>+ case serialized_diags::RECORD_FILENAME: {<br>+ if (readString(TopDiags, TopDiags.FileNames, "filename", Record,<br>+ BlobStart, BlobLen))<br>+ return Failure;<br>+<br>+ if (Record.size() < 3) {<br>+ reportInvalidFile("Invalid file entry");<br>+ return Failure;<br>+ }<br>+ <br>+ const FileEntry *FE =<br>+ TopDiags.FakeFiles.getVirtualFile(TopDiags.FileNames[Record[0]],<br>+ /* size */ Record[1],<br>+ /* time */ Record[2]);<br>+ <br>+ TopDiags.Files[Record[0]] = FE;<br>+ continue;<br>+ }<br>+<br>+ case serialized_diags::RECORD_SOURCE_RANGE: {<br>+ CXSourceRange SR;<br>+ if (readRange(TopDiags, Record, 0, SR))<br>+ return Failure;<br>+ D->Ranges.push_back(SR);<br>+ continue;<br>+ }<br>+ <br>+ case serialized_diags::RECORD_FIXIT: {<br>+ CXSourceRange SR;<br>+ if (readRange(TopDiags, Record, 0, SR))<br>+ return Failure;<br>+ llvm::StringRef RetStr;<br>+ if (readString(TopDiags, RetStr, "FIXIT", Record, BlobStart, BlobLen))<br>+ return Failure;<br>+ D->FixIts.push_back(std::make_pair(SR, createCXString(RetStr, false)));<br>+ continue; <br>+ }<br>+ <br>+ case serialized_diags::RECORD_DIAG: {<br>+ D->severity = Record[0];<br>+ unsigned offset = 1;<br>+ if (readLocation(TopDiags, Record, offset, D->DiagLoc))<br>+ return Failure;<br>+ D->category = Record[offset++];<br>+ unsigned diagFlag = Record[offset++];<br>+ D->DiagOption = diagFlag ? TopDiags.WarningFlags[diagFlag] : "";<br>+ D->Spelling = TopDiags.makeString(BlobStart, BlobLen);<br>+ continue;<br>+ }<br>+ }<br>+ }<br>+}<br>+<br>+extern "C" {<br>+CXDiagnosticSet clang_loadDiagnostics(const char *file,<br>+ enum CXLoadDiag_Error *error,<br>+ CXString *errorString) {<br>+ DiagLoader L(error, errorString);<br>+ return L.load(file);<br>+}<br>+} // end extern 'C'.<br><br>Added: cfe/trunk/tools/libclang/CXLoadedDiagnostic.h<br>URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXLoadedDiagnostic.h?rev=144269&view=auto">http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXLoadedDiagnostic.h?rev=144269&view=auto</a><br>==============================================================================<br>--- cfe/trunk/tools/libclang/CXLoadedDiagnostic.h (added)<br>+++ cfe/trunk/tools/libclang/CXLoadedDiagnostic.h Thu Nov 10 02:43:12 2011<br>@@ -0,0 +1,90 @@<br>+/*===-- CXLoadedDiagnostic.h - Handling of persisent diags ------*- C++ -*-===*\<br>+|* *|<br>+|* The LLVM Compiler Infrastructure *|<br>+|* *|<br>+|* This file is distributed under the University of Illinois Open Source *|<br>+|* License. See LICENSE.TXT for details. *|<br>+|* *|<br>+|*===----------------------------------------------------------------------===*|<br>+|* *|<br>+|* Implements handling of persisent diagnostics. *|<br>+|* *|<br>+\*===----------------------------------------------------------------------===*/<br>+<br>+#ifndef LLVM_CLANG_CINDEX_LOADED_DIAGNOSTIC_H<br>+#define LLVM_CLANG_CINDEX_LOADED_DIAGNOSTIC_H<br>+<br>+#include "CIndexDiagnostic.h"<br>+#include "llvm/ADT/StringRef.h"<br>+#include "clang/Basic/LLVM.h"<br>+#include <string><br>+#include <vector><br>+<br>+namespace clang {<br>+class CXLoadedDiagnostic : public CXDiagnosticImpl {<br>+public:<br>+ CXLoadedDiagnostic() : CXDiagnosticImpl(LoadedDiagnosticKind),<br>+ severity(0), category(0) {}<br>+<br>+ virtual ~CXLoadedDiagnostic();<br>+ <br>+ /// \brief Return the severity of the diagnostic.<br>+ virtual CXDiagnosticSeverity getSeverity() const;<br>+ <br>+ /// \brief Return the location of the diagnostic.<br>+ virtual CXSourceLocation getLocation() const;<br>+ <br>+ /// \brief Return the spelling of the diagnostic.<br>+ virtual CXString getSpelling() const;<br>+ <br>+ /// \brief Return the text for the diagnostic option.<br>+ virtual CXString getDiagnosticOption(CXString *Disable) const;<br>+ <br>+ /// \brief Return the category of the diagnostic.<br>+ virtual unsigned getCategory() const;<br>+ <br>+ /// \brief Return the number of source ranges for the diagnostic.<br>+ virtual unsigned getNumRanges() const;<br>+ <br>+ /// \brief Return the source ranges for the diagnostic.<br>+ virtual CXSourceRange getRange(unsigned Range) const;<br>+ <br>+ /// \brief Return the number of FixIts.<br>+ virtual unsigned getNumFixIts() const;<br>+ <br>+ /// \brief Return the FixIt information (source range and inserted text).<br>+ virtual CXString getFixIt(unsigned FixIt,<br>+ CXSourceRange *ReplacementRange) const;<br>+ <br>+ static bool classof(const CXDiagnosticImpl *D) {<br>+ return D->getKind() == LoadedDiagnosticKind;<br>+ }<br>+ <br>+ /// \brief Decode the CXSourceLocation into file, line, column, and offset.<br>+ static void decodeLocation(CXSourceLocation location,<br>+ CXFile *file,<br>+ unsigned *line,<br>+ unsigned *column,<br>+ unsigned *offset);<br>+<br>+ struct Location {<br>+ CXFile file;<br>+ unsigned line;<br>+ unsigned column;<br>+ unsigned offset;<br>+ <br>+ Location() : line(0), column(0), offset(0) {} <br>+ };<br>+ <br>+ Location DiagLoc;<br>+<br>+ std::vector<CXSourceRange> Ranges;<br>+ std::vector<std::pair<CXSourceRange, CXString> > FixIts;<br>+ llvm::StringRef Spelling;<br>+ llvm::StringRef DiagOption;<br>+ unsigned severity;<br>+ unsigned category;<br>+};<br>+}<br>+<br>+#endif<br><br>Modified: cfe/trunk/tools/libclang/CXSourceLocation.cpp<br>URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXSourceLocation.cpp?rev=144269&r1=144268&r2=144269&view=diff">http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXSourceLocation.cpp?rev=144269&r1=144268&r2=144269&view=diff</a><br>==============================================================================<br>--- cfe/trunk/tools/libclang/CXSourceLocation.cpp (original)<br>+++ cfe/trunk/tools/libclang/CXSourceLocation.cpp Thu Nov 10 02:43:12 2011<br>@@ -17,6 +17,7 @@<br> #include "CXString.h"<br> #include "CXSourceLocation.h"<br> #include "CXTranslationUnit.h"<br>+#include "CXLoadedDiagnostic.h"<br><br> using namespace clang;<br> using namespace clang::cxstring;<br>@@ -54,6 +55,13 @@<br> }<br><br> CXSourceRange clang_getRange(CXSourceLocation begin, CXSourceLocation end) {<br>+ if (!isASTUnitSourceLocation(begin)) {<br>+ if (isASTUnitSourceLocation(end))<br>+ return clang_getNullRange();<br>+ CXSourceRange Result = { { begin.ptr_data[0], end.ptr_data[0] }, 0, 0 };<br>+ return Result;<br>+ }<br>+ <br> if (begin.ptr_data[0] != end.ptr_data[0] ||<br> begin.ptr_data[1] != end.ptr_data[1])<br> return clang_getNullRange();<br>@@ -64,7 +72,6 @@<br> return Result;<br> }<br><br>- <br> unsigned clang_equalRanges(CXSourceRange range1, CXSourceRange range2) {<br> return range1.ptr_data[0] == range2.ptr_data[0]<br> && range1.ptr_data[1] == range2.ptr_data[1]<br>@@ -78,12 +85,24 @@<br><br><br> CXSourceLocation clang_getRangeStart(CXSourceRange range) {<br>+ // Special decoding for CXSourceLocations for CXLoadedDiagnostics.<br>+ if ((uintptr_t)range.ptr_data[0] & 0x1) {<br>+ CXSourceLocation Result = { { range.ptr_data[0], 0 }, 0 };<br>+ return Result; <br>+ }<br>+ <br> CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },<br> range.begin_int_data };<br> return Result;<br> }<br><br> CXSourceLocation clang_getRangeEnd(CXSourceRange range) {<br>+ // Special decoding for CXSourceLocations for CXLoadedDiagnostics.<br>+ if ((uintptr_t)range.ptr_data[0] & 0x1) {<br>+ CXSourceLocation Result = { { range.ptr_data[1], 0 }, 0 };<br>+ return Result; <br>+ }<br>+<br> CXSourceLocation Result = { { range.ptr_data[0], range.ptr_data[1] },<br> range.end_int_data };<br> return Result;<br>@@ -182,41 +201,40 @@<br> unsigned *column,<br> unsigned *offset) {<br><br>- if (isASTUnitSourceLocation(location)) { <br>- SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);<br>- <br>- if (!location.ptr_data[0] || Loc.isInvalid()) {<br>- createNullLocation(file, line, column, offset);<br>- return;<br>- }<br>- <br>- const SourceManager &SM =<br>- *static_cast<const SourceManager*>(location.ptr_data[0]);<br>- SourceLocation ExpansionLoc = SM.getExpansionLoc(Loc);<br>- <br>- // Check that the FileID is invalid on the expansion location.<br>- // This can manifest in invalid code.<br>- FileID fileID = SM.getFileID(ExpansionLoc);<br>- bool Invalid = false;<br>- const SrcMgr::SLocEntry &sloc = SM.getSLocEntry(fileID, &Invalid);<br>- if (Invalid || !sloc.isFile()) {<br>- createNullLocation(file, line, column, offset);<br>- return;<br>- }<br>- <br>- if (file)<br>- *file = (void *)SM.getFileEntryForSLocEntry(sloc);<br>- if (line)<br>- *line = SM.getExpansionLineNumber(ExpansionLoc);<br>- if (column)<br>- *column = SM.getExpansionColumnNumber(ExpansionLoc);<br>- if (offset)<br>- *offset = SM.getDecomposedLoc(ExpansionLoc).second;<br>+ if (!isASTUnitSourceLocation(location)) {<br>+ CXLoadedDiagnostic::decodeLocation(location, file, line, column, offset);<br>+ return;<br>+ }<br>+<br>+ SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);<br>+<br>+ if (!location.ptr_data[0] || Loc.isInvalid()) {<br>+ createNullLocation(file, line, column, offset);<br>+ return;<br>+ }<br>+<br>+ const SourceManager &SM =<br>+ *static_cast<const SourceManager*>(location.ptr_data[0]);<br>+ SourceLocation ExpansionLoc = SM.getExpansionLoc(Loc);<br>+ <br>+ // Check that the FileID is invalid on the expansion location.<br>+ // This can manifest in invalid code.<br>+ FileID fileID = SM.getFileID(ExpansionLoc);<br>+ bool Invalid = false;<br>+ const SrcMgr::SLocEntry &sloc = SM.getSLocEntry(fileID, &Invalid);<br>+ if (Invalid || !sloc.isFile()) {<br>+ createNullLocation(file, line, column, offset);<br> return;<br> }<br><br>- // FIXME:<br>- createNullLocation(file, line, column, offset);<br>+ if (file)<br>+ *file = (void *)SM.getFileEntryForSLocEntry(sloc);<br>+ if (line)<br>+ *line = SM.getExpansionLineNumber(ExpansionLoc);<br>+ if (column)<br>+ *column = SM.getExpansionColumnNumber(ExpansionLoc);<br>+ if (offset)<br>+ *offset = SM.getDecomposedLoc(ExpansionLoc).second;<br> }<br><br> void clang_getPresumedLocation(CXSourceLocation location,<br>@@ -224,28 +242,29 @@<br> unsigned *line,<br> unsigned *column) {<br><br>- if (isASTUnitSourceLocation(location)) {<br>- SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);<br>- <br>- if (!location.ptr_data[0] || Loc.isInvalid())<br>- createNullLocation(filename, line, column);<br>- else {<br>- const SourceManager &SM =<br>- *static_cast<const SourceManager*>(location.ptr_data[0]);<br>- PresumedLoc PreLoc = SM.getPresumedLoc(Loc);<br>- <br>- if (filename)<br>- *filename = createCXString(PreLoc.getFilename());<br>- if (line)<br>- *line = PreLoc.getLine();<br>- if (column)<br>- *column = PreLoc.getColumn();<br>- }<br>+ if (!isASTUnitSourceLocation(location)) {<br>+ // Other SourceLocation implementations do not support presumed locations<br>+ // at this time.<br>+ createNullLocation(filename, line, column);<br> return;<br> }<br>- <br>- // FIXME:<br>- createNullLocation(filename, line, column);<br>+<br>+ SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);<br>+<br>+ if (!location.ptr_data[0] || Loc.isInvalid())<br>+ createNullLocation(filename, line, column);<br>+ else {<br>+ const SourceManager &SM =<br>+ *static_cast<const SourceManager*>(location.ptr_data[0]);<br>+ PresumedLoc PreLoc = SM.getPresumedLoc(Loc);<br>+ <br>+ if (filename)<br>+ *filename = createCXString(PreLoc.getFilename());<br>+ if (line)<br>+ *line = PreLoc.getLine();<br>+ if (column)<br>+ *column = PreLoc.getColumn();<br>+ }<br> }<br><br> void clang_getInstantiationLocation(CXSourceLocation location,<br>@@ -263,44 +282,44 @@<br> unsigned *column,<br> unsigned *offset) {<br><br>- if (isASTUnitSourceLocation(location)) {<br>- SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);<br>- <br>- if (!location.ptr_data[0] || Loc.isInvalid())<br>- return createNullLocation(file, line, column, offset);<br>- <br>- const SourceManager &SM =<br>- *static_cast<const SourceManager*>(location.ptr_data[0]);<br>- SourceLocation SpellLoc = Loc;<br>- if (SpellLoc.isMacroID()) {<br>- SourceLocation SimpleSpellingLoc = SM.getImmediateSpellingLoc(SpellLoc);<br>- if (SimpleSpellingLoc.isFileID() &&<br>- SM.getFileEntryForID(SM.getDecomposedLoc(SimpleSpellingLoc).first))<br>- SpellLoc = SimpleSpellingLoc;<br>- else<br>- SpellLoc = SM.getExpansionLoc(SpellLoc);<br>- }<br>- <br>- std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(SpellLoc);<br>- FileID FID = LocInfo.first;<br>- unsigned FileOffset = LocInfo.second;<br>- <br>- if (FID.isInvalid())<br>- return createNullLocation(file, line, column, offset);<br>- <br>- if (file)<br>- *file = (void *)SM.getFileEntryForID(FID);<br>- if (line)<br>- *line = SM.getLineNumber(FID, FileOffset);<br>- if (column)<br>- *column = SM.getColumnNumber(FID, FileOffset);<br>- if (offset)<br>- *offset = FileOffset;<br>+ if (!isASTUnitSourceLocation(location)) {<br>+ CXLoadedDiagnostic::decodeLocation(location, file, line,<br>+ column, offset);<br> return;<br> }<br>-<br>- // FIXME:<br>- createNullLocation(file, line, column, offset);<br>+ <br>+ SourceLocation Loc = SourceLocation::getFromRawEncoding(location.int_data);<br>+ <br>+ if (!location.ptr_data[0] || Loc.isInvalid())<br>+ return createNullLocation(file, line, column, offset);<br>+ <br>+ const SourceManager &SM =<br>+ *static_cast<const SourceManager*>(location.ptr_data[0]);<br>+ SourceLocation SpellLoc = Loc;<br>+ if (SpellLoc.isMacroID()) {<br>+ SourceLocation SimpleSpellingLoc = SM.getImmediateSpellingLoc(SpellLoc);<br>+ if (SimpleSpellingLoc.isFileID() &&<br>+ SM.getFileEntryForID(SM.getDecomposedLoc(SimpleSpellingLoc).first))<br>+ SpellLoc = SimpleSpellingLoc;<br>+ else<br>+ SpellLoc = SM.getExpansionLoc(SpellLoc);<br>+ }<br>+ <br>+ std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(SpellLoc);<br>+ FileID FID = LocInfo.first;<br>+ unsigned FileOffset = LocInfo.second;<br>+ <br>+ if (FID.isInvalid())<br>+ return createNullLocation(file, line, column, offset);<br>+ <br>+ if (file)<br>+ *file = (void *)SM.getFileEntryForID(FID);<br>+ if (line)<br>+ *line = SM.getLineNumber(FID, FileOffset);<br>+ if (column)<br>+ *column = SM.getColumnNumber(FID, FileOffset);<br>+ if (offset)<br>+ *offset = FileOffset;<br> }<br><br> } // end extern "C"<br><br>Modified: cfe/trunk/tools/libclang/CXStoredDiagnostic.cpp<br>URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXStoredDiagnostic.cpp?rev=144269&r1=144268&r2=144269&view=diff">http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXStoredDiagnostic.cpp?rev=144269&r1=144268&r2=144269&view=diff</a><br>==============================================================================<br>--- cfe/trunk/tools/libclang/CXStoredDiagnostic.cpp (original)<br>+++ cfe/trunk/tools/libclang/CXStoredDiagnostic.cpp Thu Nov 10 02:43:12 2011<br>@@ -28,9 +28,6 @@<br> using namespace clang::cxloc;<br> using namespace clang::cxstring;<br><br>-// Needed for vtable of CXPersisetntDiagnostic.<br>-CXDiagnosticImpl::~CXDiagnosticImpl() {}<br>-<br> CXDiagnosticSeverity CXStoredDiagnostic::getSeverity() const {<br> switch (Diag.getLevel()) {<br> case DiagnosticsEngine::Ignored: return CXDiagnostic_Ignored;<br><br>Modified: cfe/trunk/tools/libclang/CXTranslationUnit.h<br>URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXTranslationUnit.h?rev=144269&r1=144268&r2=144269&view=diff">http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXTranslationUnit.h?rev=144269&r1=144268&r2=144269&view=diff</a><br>==============================================================================<br>--- cfe/trunk/tools/libclang/CXTranslationUnit.h (original)<br>+++ cfe/trunk/tools/libclang/CXTranslationUnit.h Thu Nov 10 02:43:12 2011<br>@@ -18,6 +18,7 @@<br> struct CXTranslationUnitImpl {<br> void *TUData;<br> void *StringPool;<br>+ void *Diagnostics;<br> };<br> }<br><br>@@ -27,7 +28,7 @@<br> namespace cxtu {<br><br> CXTranslationUnitImpl *MakeCXTranslationUnit(ASTUnit *TU);<br>-<br>+ <br> class CXTUOwner {<br> CXTranslationUnitImpl *TU;<br><br><br>Modified: cfe/trunk/tools/libclang/libclang.exports<br>URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/libclang.exports?rev=144269&r1=144268&r2=144269&view=diff">http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/libclang.exports?rev=144269&r1=144268&r2=144269&view=diff</a><br>==============================================================================<br>--- cfe/trunk/tools/libclang/libclang.exports (original)<br>+++ cfe/trunk/tools/libclang/libclang.exports Thu Nov 10 02:43:12 2011<br>@@ -32,6 +32,7 @@<br> clang_disposeCXTUResourceUsage<br> clang_disposeCodeCompleteResults<br> clang_disposeDiagnostic<br>+clang_disposeDiagnosticSet<br> clang_disposeIndex<br> clang_disposeOverriddenCursors<br> clang_disposeString<br>@@ -53,6 +54,7 @@<br> clang_getCXXAccessSpecifier<br> clang_getCanonicalCursor<br> clang_getCanonicalType<br>+clang_getChildDiagnostics<br> clang_getClangVersion<br> clang_getCompletionAnnotation<br> clang_getCompletionAvailability<br>@@ -86,6 +88,7 @@<br> clang_getDiagnosticCategory<br> clang_getDiagnosticCategoryName<br> clang_getDiagnosticFixIt<br>+clang_getDiagnosticInSet<br> clang_getDiagnosticLocation<br> clang_getDiagnosticNumFixIts<br> clang_getDiagnosticNumRanges<br>@@ -108,6 +111,7 @@<br> clang_getNullRange<br> clang_getNumCompletionChunks<br> clang_getNumDiagnostics<br>+clang_getNumDiagnosticsInSet<br> clang_getNumOverloadedDecls<br> clang_getOverloadedDecl<br> clang_getOverriddenCursors<br>@@ -150,6 +154,7 @@<br> clang_isUnexposed<br> clang_isVirtualBase<br> clang_isVolatileQualifiedType<br>+clang_loadDiagnostics<br> clang_parseTranslationUnit<br> clang_remap_dispose<br> clang_remap_getFilenames<br><br><br>_______________________________________________<br>cfe-commits mailing list<br><a href="mailto:cfe-commits@cs.uiuc.edu">cfe-commits@cs.uiuc.edu</a><br>http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits<br></div></blockquote></div><br></body></html>