Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame^] | 1 | /*===-- CIndexDiagnostics.cpp - Diagnostics C Interface ---------*- C++ -*-===*\ |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 2 | |* *| |
| 3 | |* The LLVM Compiler Infrastructure *| |
| 4 | |* *| |
| 5 | |* This file is distributed under the University of Illinois Open Source *| |
| 6 | |* License. See LICENSE.TXT for details. *| |
| 7 | |* *| |
| 8 | |*===----------------------------------------------------------------------===*| |
| 9 | |* *| |
| 10 | |* Implements the diagnostic functions of the Clang C interface. *| |
| 11 | |* *| |
| 12 | \*===----------------------------------------------------------------------===*/ |
| 13 | #include "CIndexDiagnostic.h" |
| 14 | #include "CIndexer.h" |
| 15 | #include "CXSourceLocation.h" |
| 16 | |
Douglas Gregor | ac0605e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 17 | #include "clang/Frontend/FrontendDiagnostic.h" |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame^] | 18 | #include "llvm/ADT/Twine.h" |
Douglas Gregor | ac0605e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 19 | #include "llvm/Support/MemoryBuffer.h" |
| 20 | |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | using namespace clang::cxloc; |
Ted Kremenek | 5cca6eb | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 23 | using namespace clang::cxstring; |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame^] | 24 | using namespace llvm; |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 25 | |
| 26 | //----------------------------------------------------------------------------- |
Ted Kremenek | 5cca6eb | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 27 | // C Interface Routines |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 28 | //----------------------------------------------------------------------------- |
| 29 | extern "C" { |
Ted Kremenek | 5cca6eb | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 30 | |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame^] | 31 | unsigned clang_getNumDiagnostics(CXTranslationUnit Unit) { |
| 32 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(Unit); |
| 33 | return CXXUnit? CXXUnit->diag_size() : 0; |
| 34 | } |
| 35 | |
| 36 | CXDiagnostic clang_getDiagnostic(CXTranslationUnit Unit, unsigned Index) { |
| 37 | ASTUnit *CXXUnit = static_cast<ASTUnit *>(Unit); |
| 38 | if (!CXXUnit || Index >= CXXUnit->diag_size()) |
| 39 | return 0; |
| 40 | |
| 41 | return new CXStoredDiagnostic(CXXUnit->diag_begin()[Index], |
| 42 | CXXUnit->getASTContext().getLangOptions()); |
| 43 | } |
| 44 | |
| 45 | void clang_disposeDiagnostic(CXDiagnostic Diagnostic) { |
| 46 | CXStoredDiagnostic *Stored = static_cast<CXStoredDiagnostic *>(Diagnostic); |
| 47 | delete Stored; |
| 48 | } |
| 49 | |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 50 | enum CXDiagnosticSeverity clang_getDiagnosticSeverity(CXDiagnostic Diag) { |
| 51 | CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag); |
| 52 | if (!StoredDiag) |
| 53 | return CXDiagnostic_Ignored; |
Ted Kremenek | 5cca6eb | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 54 | |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame^] | 55 | switch (StoredDiag->Diag.getLevel()) { |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 56 | case Diagnostic::Ignored: return CXDiagnostic_Ignored; |
| 57 | case Diagnostic::Note: return CXDiagnostic_Note; |
| 58 | case Diagnostic::Warning: return CXDiagnostic_Warning; |
| 59 | case Diagnostic::Error: return CXDiagnostic_Error; |
| 60 | case Diagnostic::Fatal: return CXDiagnostic_Fatal; |
| 61 | } |
Ted Kremenek | 5cca6eb | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 62 | |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 63 | llvm_unreachable("Invalid diagnostic level"); |
| 64 | return CXDiagnostic_Ignored; |
| 65 | } |
Ted Kremenek | 5cca6eb | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 66 | |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 67 | CXSourceLocation clang_getDiagnosticLocation(CXDiagnostic Diag) { |
| 68 | CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag); |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame^] | 69 | if (!StoredDiag || StoredDiag->Diag.getLocation().isInvalid()) |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 70 | return clang_getNullLocation(); |
Ted Kremenek | 5cca6eb | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 71 | |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame^] | 72 | return translateSourceLocation(StoredDiag->Diag.getLocation().getManager(), |
| 73 | StoredDiag->LangOpts, |
| 74 | StoredDiag->Diag.getLocation()); |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | CXString clang_getDiagnosticSpelling(CXDiagnostic Diag) { |
| 78 | CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag); |
| 79 | if (!StoredDiag) |
Ted Kremenek | 5cca6eb | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 80 | return createCXString(""); |
| 81 | |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame^] | 82 | return createCXString(StoredDiag->Diag.getMessage(), false); |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Douglas Gregor | 4b8fd6d | 2010-02-08 23:11:56 +0000 | [diff] [blame] | 85 | unsigned clang_getDiagnosticNumRanges(CXDiagnostic Diag) { |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 86 | CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag); |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame^] | 87 | if (!StoredDiag || StoredDiag->Diag.getLocation().isInvalid()) |
Douglas Gregor | 4b8fd6d | 2010-02-08 23:11:56 +0000 | [diff] [blame] | 88 | return 0; |
Ted Kremenek | 5cca6eb | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 89 | |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame^] | 90 | return StoredDiag->Diag.range_size(); |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 91 | } |
Ted Kremenek | 5cca6eb | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 92 | |
Douglas Gregor | 4b8fd6d | 2010-02-08 23:11:56 +0000 | [diff] [blame] | 93 | CXSourceRange clang_getDiagnosticRange(CXDiagnostic Diag, unsigned Range) { |
| 94 | CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag); |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame^] | 95 | if (!StoredDiag || Range >= StoredDiag->Diag.range_size() || |
| 96 | StoredDiag->Diag.getLocation().isInvalid()) |
Douglas Gregor | 4b8fd6d | 2010-02-08 23:11:56 +0000 | [diff] [blame] | 97 | return clang_getNullRange(); |
Ted Kremenek | 5cca6eb | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 98 | |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame^] | 99 | return translateSourceRange(StoredDiag->Diag.getLocation().getManager(), |
| 100 | StoredDiag->LangOpts, |
| 101 | StoredDiag->Diag.range_begin()[Range]); |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | unsigned clang_getDiagnosticNumFixIts(CXDiagnostic Diag) { |
| 105 | CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag); |
| 106 | if (!StoredDiag) |
| 107 | return 0; |
Ted Kremenek | 5cca6eb | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 108 | |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame^] | 109 | return StoredDiag->Diag.fixit_size(); |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Ted Kremenek | 5cca6eb | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 112 | enum CXFixItKind clang_getDiagnosticFixItKind(CXDiagnostic Diag, |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 113 | unsigned FixIt) { |
| 114 | CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag); |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame^] | 115 | if (!StoredDiag || FixIt >= StoredDiag->Diag.fixit_size()) |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 116 | return CXFixIt_Insertion; |
Ted Kremenek | 5cca6eb | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 117 | |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame^] | 118 | const CodeModificationHint &Hint = StoredDiag->Diag.fixit_begin()[FixIt]; |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 119 | if (Hint.RemoveRange.isInvalid()) |
| 120 | return CXFixIt_Insertion; |
| 121 | if (Hint.InsertionLoc.isInvalid()) |
| 122 | return CXFixIt_Removal; |
Ted Kremenek | 5cca6eb | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 123 | |
| 124 | return CXFixIt_Replacement; |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Ted Kremenek | 5cca6eb | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 127 | CXString clang_getDiagnosticFixItInsertion(CXDiagnostic Diag, |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 128 | unsigned FixIt, |
| 129 | CXSourceLocation *Location) { |
Douglas Gregor | 60b11f6 | 2010-01-29 00:41:11 +0000 | [diff] [blame] | 130 | if (Location) |
| 131 | *Location = clang_getNullLocation(); |
| 132 | |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 133 | CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag); |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame^] | 134 | if (!StoredDiag || FixIt >= StoredDiag->Diag.fixit_size()) |
Ted Kremenek | 5cca6eb | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 135 | return createCXString(""); |
| 136 | |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame^] | 137 | const CodeModificationHint &Hint = StoredDiag->Diag.fixit_begin()[FixIt]; |
Douglas Gregor | 60b11f6 | 2010-01-29 00:41:11 +0000 | [diff] [blame] | 138 | |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame^] | 139 | if (Location && StoredDiag->Diag.getLocation().isValid()) |
Douglas Gregor | 60b11f6 | 2010-01-29 00:41:11 +0000 | [diff] [blame] | 140 | *Location = translateSourceLocation( |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame^] | 141 | StoredDiag->Diag.getLocation().getManager(), |
| 142 | StoredDiag->LangOpts, |
Douglas Gregor | 60b11f6 | 2010-01-29 00:41:11 +0000 | [diff] [blame] | 143 | Hint.InsertionLoc); |
Ted Kremenek | 5cca6eb | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 144 | return createCXString(Hint.CodeToInsert); |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Ted Kremenek | 5cca6eb | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 147 | CXSourceRange clang_getDiagnosticFixItRemoval(CXDiagnostic Diag, |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 148 | unsigned FixIt) { |
| 149 | CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag); |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame^] | 150 | if (!StoredDiag || FixIt >= StoredDiag->Diag.fixit_size() || |
| 151 | StoredDiag->Diag.getLocation().isInvalid()) |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 152 | return clang_getNullRange(); |
Ted Kremenek | 5cca6eb | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 153 | |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame^] | 154 | const CodeModificationHint &Hint = StoredDiag->Diag.fixit_begin()[FixIt]; |
| 155 | return translateSourceRange(StoredDiag->Diag.getLocation().getManager(), |
| 156 | StoredDiag->LangOpts, |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 157 | Hint.RemoveRange); |
| 158 | } |
| 159 | |
Ted Kremenek | 5cca6eb | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 160 | CXString clang_getDiagnosticFixItReplacement(CXDiagnostic Diag, |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 161 | unsigned FixIt, |
| 162 | CXSourceRange *Range) { |
Douglas Gregor | 60b11f6 | 2010-01-29 00:41:11 +0000 | [diff] [blame] | 163 | if (Range) |
| 164 | *Range = clang_getNullRange(); |
| 165 | |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 166 | CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag); |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame^] | 167 | if (!StoredDiag || FixIt >= StoredDiag->Diag.fixit_size() || |
| 168 | StoredDiag->Diag.getLocation().isInvalid()) { |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 169 | if (Range) |
| 170 | *Range = clang_getNullRange(); |
Ted Kremenek | 5cca6eb | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 171 | |
| 172 | return createCXString(""); |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 173 | } |
Ted Kremenek | 5cca6eb | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 174 | |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame^] | 175 | const CodeModificationHint &Hint = StoredDiag->Diag.fixit_begin()[FixIt]; |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 176 | if (Range) |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame^] | 177 | *Range = translateSourceRange(StoredDiag->Diag.getLocation().getManager(), |
| 178 | StoredDiag->LangOpts, |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 179 | Hint.RemoveRange); |
Ted Kremenek | 5cca6eb | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 180 | return createCXString(Hint.CodeToInsert); |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 181 | } |
Ted Kremenek | 5cca6eb | 2010-02-17 00:41:08 +0000 | [diff] [blame] | 182 | |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 183 | } // end extern "C" |
Douglas Gregor | ac0605e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 184 | |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame^] | 185 | void clang::LoadSerializedDiagnostics(const llvm::sys::Path &DiagnosticsPath, |
| 186 | unsigned num_unsaved_files, |
| 187 | struct CXUnsavedFile *unsaved_files, |
| 188 | FileManager &FileMgr, |
| 189 | SourceManager &SourceMgr, |
| 190 | SmallVectorImpl<StoredDiagnostic> &Diags) { |
Douglas Gregor | ac0605e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 191 | using llvm::MemoryBuffer; |
| 192 | using llvm::StringRef; |
| 193 | MemoryBuffer *F = MemoryBuffer::getFile(DiagnosticsPath.c_str()); |
| 194 | if (!F) |
| 195 | return; |
| 196 | |
| 197 | // Enter the unsaved files into the file manager. |
Douglas Gregor | ac0605e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 198 | for (unsigned I = 0; I != num_unsaved_files; ++I) { |
| 199 | const FileEntry *File = FileMgr.getVirtualFile(unsaved_files[I].Filename, |
| 200 | unsaved_files[I].Length, |
| 201 | 0); |
| 202 | if (!File) { |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame^] | 203 | // FIXME: Hard to localize when we have no diagnostics engine! |
| 204 | Diags.push_back(StoredDiagnostic(Diagnostic::Fatal, |
| 205 | (Twine("could not remap from missing file ") + |
| 206 | unsaved_files[I].Filename).str())); |
Douglas Gregor | ac0605e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 207 | return; |
| 208 | } |
| 209 | |
| 210 | MemoryBuffer *Buffer |
| 211 | = MemoryBuffer::getMemBuffer(unsaved_files[I].Contents, |
| 212 | unsaved_files[I].Contents + unsaved_files[I].Length); |
| 213 | if (!Buffer) |
| 214 | return; |
| 215 | |
| 216 | SourceMgr.overrideFileContents(File, Buffer); |
| 217 | } |
| 218 | |
| 219 | // Parse the diagnostics, emitting them one by one until we've |
| 220 | // exhausted the data. |
| 221 | StringRef Buffer = F->getBuffer(); |
| 222 | const char *Memory = Buffer.data(), *MemoryEnd = Memory + Buffer.size(); |
| 223 | while (Memory != MemoryEnd) { |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame^] | 224 | StoredDiagnostic Stored = StoredDiagnostic::Deserialize(FileMgr, SourceMgr, |
| 225 | Memory, MemoryEnd); |
| 226 | if (!Stored) |
Douglas Gregor | ac0605e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 227 | return; |
Daniel Dunbar | 854d36b | 2010-01-30 23:31:40 +0000 | [diff] [blame] | 228 | |
Douglas Gregor | 33cdd81 | 2010-02-18 18:08:43 +0000 | [diff] [blame^] | 229 | Diags.push_back(Stored); |
| 230 | } |
Douglas Gregor | ac0605e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 231 | } |