Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 1 | /*===-- CIndexDiagnostics.cpp - Diagnostics C Interface -----------*- C -*-===*\ |
| 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" |
| 18 | #include "llvm/Support/MemoryBuffer.h" |
| 19 | |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | using namespace clang::cxloc; |
| 22 | |
| 23 | //----------------------------------------------------------------------------- |
| 24 | // Opaque data structures |
| 25 | //----------------------------------------------------------------------------- |
| 26 | namespace { |
| 27 | /// \brief The storage behind a CXDiagnostic |
| 28 | struct CXStoredDiagnostic { |
| 29 | /// \brief The translation unit this diagnostic came from. |
Daniel Dunbar | 9ee3a92 | 2010-01-30 23:31:49 +0000 | [diff] [blame^] | 30 | const LangOptions *LangOptsPtr; |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 31 | |
| 32 | /// \brief The severity level of this diagnostic. |
| 33 | Diagnostic::Level Level; |
| 34 | |
| 35 | /// \brief A reference to the diagnostic information. |
| 36 | const DiagnosticInfo &Info; |
| 37 | }; |
| 38 | } |
| 39 | |
| 40 | //----------------------------------------------------------------------------- |
| 41 | // CIndex Diagnostic Client |
| 42 | //----------------------------------------------------------------------------- |
| 43 | CIndexDiagnosticClient::~CIndexDiagnosticClient() { } |
| 44 | |
| 45 | void CIndexDiagnosticClient::BeginSourceFile(const LangOptions &LangOpts, |
| 46 | const Preprocessor *PP) { |
Daniel Dunbar | 9ee3a92 | 2010-01-30 23:31:49 +0000 | [diff] [blame^] | 47 | assert(!LangOptsPtr && "Invalid state!"); |
| 48 | LangOptsPtr = &LangOpts; |
| 49 | } |
| 50 | |
| 51 | void CIndexDiagnosticClient::EndSourceFile() { |
| 52 | assert(LangOptsPtr && "Invalid state!"); |
| 53 | LangOptsPtr = 0; |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void CIndexDiagnosticClient::HandleDiagnostic(Diagnostic::Level DiagLevel, |
| 57 | const DiagnosticInfo &Info) { |
| 58 | if (!Callback) |
| 59 | return; |
Daniel Dunbar | 9ee3a92 | 2010-01-30 23:31:49 +0000 | [diff] [blame^] | 60 | |
| 61 | assert((LangOptsPtr || Info.getLocation().isInvalid()) && |
| 62 | "Missing language options with located diagnostic!"); |
| 63 | CXStoredDiagnostic Stored = { this->LangOptsPtr, DiagLevel, Info }; |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 64 | Callback(&Stored, ClientData); |
| 65 | } |
| 66 | |
| 67 | //----------------------------------------------------------------------------- |
| 68 | // C Interface Routines |
| 69 | //----------------------------------------------------------------------------- |
| 70 | extern "C" { |
| 71 | |
| 72 | enum CXDiagnosticSeverity clang_getDiagnosticSeverity(CXDiagnostic Diag) { |
| 73 | CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag); |
| 74 | if (!StoredDiag) |
| 75 | return CXDiagnostic_Ignored; |
| 76 | |
| 77 | switch (StoredDiag->Level) { |
| 78 | case Diagnostic::Ignored: return CXDiagnostic_Ignored; |
| 79 | case Diagnostic::Note: return CXDiagnostic_Note; |
| 80 | case Diagnostic::Warning: return CXDiagnostic_Warning; |
| 81 | case Diagnostic::Error: return CXDiagnostic_Error; |
| 82 | case Diagnostic::Fatal: return CXDiagnostic_Fatal; |
| 83 | } |
| 84 | |
| 85 | llvm_unreachable("Invalid diagnostic level"); |
| 86 | return CXDiagnostic_Ignored; |
| 87 | } |
| 88 | |
| 89 | CXSourceLocation clang_getDiagnosticLocation(CXDiagnostic Diag) { |
| 90 | CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag); |
| 91 | if (!StoredDiag || StoredDiag->Info.getLocation().isInvalid()) |
| 92 | return clang_getNullLocation(); |
| 93 | |
| 94 | return translateSourceLocation(StoredDiag->Info.getLocation().getManager(), |
Daniel Dunbar | 9ee3a92 | 2010-01-30 23:31:49 +0000 | [diff] [blame^] | 95 | *StoredDiag->LangOptsPtr, |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 96 | StoredDiag->Info.getLocation()); |
| 97 | } |
| 98 | |
| 99 | CXString clang_getDiagnosticSpelling(CXDiagnostic Diag) { |
| 100 | CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag); |
| 101 | if (!StoredDiag) |
| 102 | return CIndexer::createCXString(""); |
| 103 | |
| 104 | llvm::SmallString<64> Spelling; |
| 105 | StoredDiag->Info.FormatDiagnostic(Spelling); |
| 106 | return CIndexer::createCXString(Spelling.str(), true); |
| 107 | } |
| 108 | |
| 109 | void clang_getDiagnosticRanges(CXDiagnostic Diag, |
| 110 | CXSourceRange **Ranges, |
| 111 | unsigned *NumRanges) { |
| 112 | if (Ranges) |
| 113 | *Ranges = 0; |
| 114 | if (NumRanges) |
| 115 | *NumRanges = 0; |
| 116 | |
| 117 | CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag); |
| 118 | if (!StoredDiag || !Ranges || !NumRanges || |
| 119 | !StoredDiag->Info.getNumRanges() || |
| 120 | StoredDiag->Info.getLocation().isInvalid()) |
| 121 | return; |
| 122 | |
| 123 | unsigned N = StoredDiag->Info.getNumRanges(); |
| 124 | *Ranges = (CXSourceRange *)malloc(sizeof(CXSourceRange) * N); |
| 125 | *NumRanges = N; |
| 126 | for (unsigned I = 0; I != N; ++I) |
| 127 | (*Ranges)[I] = translateSourceRange( |
| 128 | StoredDiag->Info.getLocation().getManager(), |
Daniel Dunbar | 9ee3a92 | 2010-01-30 23:31:49 +0000 | [diff] [blame^] | 129 | *StoredDiag->LangOptsPtr, |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 130 | StoredDiag->Info.getRange(I)); |
| 131 | } |
| 132 | |
| 133 | void clang_disposeDiagnosticRanges(CXSourceRange *Ranges, |
| 134 | unsigned NumRanges) { |
| 135 | free(Ranges); |
| 136 | } |
| 137 | |
| 138 | unsigned clang_getDiagnosticNumFixIts(CXDiagnostic Diag) { |
| 139 | CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag); |
| 140 | if (!StoredDiag) |
| 141 | return 0; |
| 142 | |
| 143 | return StoredDiag->Info.getNumCodeModificationHints(); |
| 144 | } |
| 145 | |
| 146 | enum CXFixItKind clang_getDiagnosticFixItKind(CXDiagnostic Diag, |
| 147 | unsigned FixIt) { |
| 148 | CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag); |
| 149 | if (!StoredDiag || FixIt >= StoredDiag->Info.getNumCodeModificationHints()) |
| 150 | return CXFixIt_Insertion; |
| 151 | |
| 152 | const CodeModificationHint &Hint |
| 153 | = StoredDiag->Info.getCodeModificationHint(FixIt); |
| 154 | if (Hint.RemoveRange.isInvalid()) |
| 155 | return CXFixIt_Insertion; |
| 156 | if (Hint.InsertionLoc.isInvalid()) |
| 157 | return CXFixIt_Removal; |
| 158 | |
| 159 | return CXFixIt_Replacement; |
| 160 | } |
| 161 | |
| 162 | CXString clang_getDiagnosticFixItInsertion(CXDiagnostic Diag, |
| 163 | unsigned FixIt, |
| 164 | CXSourceLocation *Location) { |
Douglas Gregor | 60b11f6 | 2010-01-29 00:41:11 +0000 | [diff] [blame] | 165 | if (Location) |
| 166 | *Location = clang_getNullLocation(); |
| 167 | |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 168 | CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag); |
| 169 | if (!StoredDiag || FixIt >= StoredDiag->Info.getNumCodeModificationHints()) |
| 170 | return CIndexer::createCXString(""); |
| 171 | |
| 172 | const CodeModificationHint &Hint |
| 173 | = StoredDiag->Info.getCodeModificationHint(FixIt); |
Douglas Gregor | 60b11f6 | 2010-01-29 00:41:11 +0000 | [diff] [blame] | 174 | |
| 175 | if (Location && StoredDiag->Info.getLocation().isValid()) |
| 176 | *Location = translateSourceLocation( |
| 177 | StoredDiag->Info.getLocation().getManager(), |
Daniel Dunbar | 9ee3a92 | 2010-01-30 23:31:49 +0000 | [diff] [blame^] | 178 | *StoredDiag->LangOptsPtr, |
Douglas Gregor | 60b11f6 | 2010-01-29 00:41:11 +0000 | [diff] [blame] | 179 | Hint.InsertionLoc); |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 180 | return CIndexer::createCXString(Hint.CodeToInsert); |
| 181 | } |
| 182 | |
| 183 | CXSourceRange clang_getDiagnosticFixItRemoval(CXDiagnostic Diag, |
| 184 | unsigned FixIt) { |
| 185 | CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag); |
| 186 | if (!StoredDiag || FixIt >= StoredDiag->Info.getNumCodeModificationHints() || |
| 187 | StoredDiag->Info.getLocation().isInvalid()) |
| 188 | return clang_getNullRange(); |
| 189 | |
| 190 | const CodeModificationHint &Hint |
| 191 | = StoredDiag->Info.getCodeModificationHint(FixIt); |
| 192 | return translateSourceRange(StoredDiag->Info.getLocation().getManager(), |
Daniel Dunbar | 9ee3a92 | 2010-01-30 23:31:49 +0000 | [diff] [blame^] | 193 | *StoredDiag->LangOptsPtr, |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 194 | Hint.RemoveRange); |
| 195 | } |
| 196 | |
| 197 | CXString clang_getDiagnosticFixItReplacement(CXDiagnostic Diag, |
| 198 | unsigned FixIt, |
| 199 | CXSourceRange *Range) { |
Douglas Gregor | 60b11f6 | 2010-01-29 00:41:11 +0000 | [diff] [blame] | 200 | if (Range) |
| 201 | *Range = clang_getNullRange(); |
| 202 | |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 203 | CXStoredDiagnostic *StoredDiag = static_cast<CXStoredDiagnostic *>(Diag); |
| 204 | if (!StoredDiag || FixIt >= StoredDiag->Info.getNumCodeModificationHints() || |
| 205 | StoredDiag->Info.getLocation().isInvalid()) { |
| 206 | if (Range) |
| 207 | *Range = clang_getNullRange(); |
| 208 | |
| 209 | return CIndexer::createCXString(""); |
| 210 | } |
| 211 | |
| 212 | const CodeModificationHint &Hint |
| 213 | = StoredDiag->Info.getCodeModificationHint(FixIt); |
| 214 | if (Range) |
| 215 | *Range = translateSourceRange(StoredDiag->Info.getLocation().getManager(), |
Daniel Dunbar | 9ee3a92 | 2010-01-30 23:31:49 +0000 | [diff] [blame^] | 216 | *StoredDiag->LangOptsPtr, |
Douglas Gregor | 4f9c376 | 2010-01-28 00:27:43 +0000 | [diff] [blame] | 217 | Hint.RemoveRange); |
| 218 | return CIndexer::createCXString(Hint.CodeToInsert); |
| 219 | } |
| 220 | |
| 221 | } // end extern "C" |
Douglas Gregor | ac0605e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 222 | |
| 223 | void clang::ReportSerializedDiagnostics(const llvm::sys::Path &DiagnosticsPath, |
| 224 | Diagnostic &Diags, |
| 225 | unsigned num_unsaved_files, |
Daniel Dunbar | 854d36b | 2010-01-30 23:31:40 +0000 | [diff] [blame] | 226 | struct CXUnsavedFile *unsaved_files, |
| 227 | const LangOptions &LangOpts) { |
Douglas Gregor | ac0605e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 228 | using llvm::MemoryBuffer; |
| 229 | using llvm::StringRef; |
| 230 | MemoryBuffer *F = MemoryBuffer::getFile(DiagnosticsPath.c_str()); |
| 231 | if (!F) |
| 232 | return; |
| 233 | |
| 234 | // Enter the unsaved files into the file manager. |
| 235 | SourceManager SourceMgr; |
| 236 | FileManager FileMgr; |
| 237 | for (unsigned I = 0; I != num_unsaved_files; ++I) { |
| 238 | const FileEntry *File = FileMgr.getVirtualFile(unsaved_files[I].Filename, |
| 239 | unsaved_files[I].Length, |
| 240 | 0); |
| 241 | if (!File) { |
| 242 | Diags.Report(diag::err_fe_remap_missing_from_file) |
| 243 | << unsaved_files[I].Filename; |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | MemoryBuffer *Buffer |
| 248 | = MemoryBuffer::getMemBuffer(unsaved_files[I].Contents, |
| 249 | unsaved_files[I].Contents + unsaved_files[I].Length); |
| 250 | if (!Buffer) |
| 251 | return; |
| 252 | |
| 253 | SourceMgr.overrideFileContents(File, Buffer); |
| 254 | } |
| 255 | |
Daniel Dunbar | 854d36b | 2010-01-30 23:31:40 +0000 | [diff] [blame] | 256 | Diags.getClient()->BeginSourceFile(LangOpts, 0); |
| 257 | |
Douglas Gregor | ac0605e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 258 | // Parse the diagnostics, emitting them one by one until we've |
| 259 | // exhausted the data. |
| 260 | StringRef Buffer = F->getBuffer(); |
| 261 | const char *Memory = Buffer.data(), *MemoryEnd = Memory + Buffer.size(); |
| 262 | while (Memory != MemoryEnd) { |
| 263 | DiagnosticBuilder DB = Diags.Deserialize(FileMgr, SourceMgr, |
| 264 | Memory, MemoryEnd); |
| 265 | if (!DB.isActive()) |
| 266 | return; |
| 267 | } |
Daniel Dunbar | 854d36b | 2010-01-30 23:31:40 +0000 | [diff] [blame] | 268 | |
| 269 | Diags.getClient()->EndSourceFile(); |
Douglas Gregor | ac0605e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 270 | } |