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