blob: 87b9f66a5c5ee8c8ae54011c92f07d1ac51255bd [file] [log] [blame]
Douglas Gregor4f9c3762010-01-28 00:27:43 +00001/*===-- 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 Gregorac0605e2010-01-28 06:00:51 +000017#include "clang/Frontend/FrontendDiagnostic.h"
18#include "llvm/Support/MemoryBuffer.h"
19
Douglas Gregor4f9c3762010-01-28 00:27:43 +000020using namespace clang;
21using namespace clang::cxloc;
22
23//-----------------------------------------------------------------------------
24// Opaque data structures
25//-----------------------------------------------------------------------------
26namespace {
27 /// \brief The storage behind a CXDiagnostic
28 struct CXStoredDiagnostic {
29 /// \brief The translation unit this diagnostic came from.
Daniel Dunbar9ee3a922010-01-30 23:31:49 +000030 const LangOptions *LangOptsPtr;
Douglas Gregor4f9c3762010-01-28 00:27:43 +000031
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//-----------------------------------------------------------------------------
43CIndexDiagnosticClient::~CIndexDiagnosticClient() { }
44
45void CIndexDiagnosticClient::BeginSourceFile(const LangOptions &LangOpts,
46 const Preprocessor *PP) {
Daniel Dunbar9ee3a922010-01-30 23:31:49 +000047 assert(!LangOptsPtr && "Invalid state!");
48 LangOptsPtr = &LangOpts;
49}
50
51void CIndexDiagnosticClient::EndSourceFile() {
52 assert(LangOptsPtr && "Invalid state!");
53 LangOptsPtr = 0;
Douglas Gregor4f9c3762010-01-28 00:27:43 +000054}
55
56void CIndexDiagnosticClient::HandleDiagnostic(Diagnostic::Level DiagLevel,
57 const DiagnosticInfo &Info) {
58 if (!Callback)
59 return;
Daniel Dunbar9ee3a922010-01-30 23:31:49 +000060
61 assert((LangOptsPtr || Info.getLocation().isInvalid()) &&
62 "Missing language options with located diagnostic!");
63 CXStoredDiagnostic Stored = { this->LangOptsPtr, DiagLevel, Info };
Douglas Gregor4f9c3762010-01-28 00:27:43 +000064 Callback(&Stored, ClientData);
65}
66
67//-----------------------------------------------------------------------------
68// C Interface Routines
69//-----------------------------------------------------------------------------
70extern "C" {
71
72enum 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
89CXSourceLocation 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 Dunbar9ee3a922010-01-30 23:31:49 +000095 *StoredDiag->LangOptsPtr,
Douglas Gregor4f9c3762010-01-28 00:27:43 +000096 StoredDiag->Info.getLocation());
97}
98
99CXString 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
109void 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 Dunbar9ee3a922010-01-30 23:31:49 +0000129 *StoredDiag->LangOptsPtr,
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000130 StoredDiag->Info.getRange(I));
131}
132
133void clang_disposeDiagnosticRanges(CXSourceRange *Ranges,
134 unsigned NumRanges) {
135 free(Ranges);
136}
137
138unsigned 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
146enum 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
162CXString clang_getDiagnosticFixItInsertion(CXDiagnostic Diag,
163 unsigned FixIt,
164 CXSourceLocation *Location) {
Douglas Gregor60b11f62010-01-29 00:41:11 +0000165 if (Location)
166 *Location = clang_getNullLocation();
167
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000168 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 Gregor60b11f62010-01-29 00:41:11 +0000174
175 if (Location && StoredDiag->Info.getLocation().isValid())
176 *Location = translateSourceLocation(
177 StoredDiag->Info.getLocation().getManager(),
Daniel Dunbar9ee3a922010-01-30 23:31:49 +0000178 *StoredDiag->LangOptsPtr,
Douglas Gregor60b11f62010-01-29 00:41:11 +0000179 Hint.InsertionLoc);
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000180 return CIndexer::createCXString(Hint.CodeToInsert);
181}
182
183CXSourceRange 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 Dunbar9ee3a922010-01-30 23:31:49 +0000193 *StoredDiag->LangOptsPtr,
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000194 Hint.RemoveRange);
195}
196
197CXString clang_getDiagnosticFixItReplacement(CXDiagnostic Diag,
198 unsigned FixIt,
199 CXSourceRange *Range) {
Douglas Gregor60b11f62010-01-29 00:41:11 +0000200 if (Range)
201 *Range = clang_getNullRange();
202
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000203 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 Dunbar9ee3a922010-01-30 23:31:49 +0000216 *StoredDiag->LangOptsPtr,
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000217 Hint.RemoveRange);
218 return CIndexer::createCXString(Hint.CodeToInsert);
219}
220
221} // end extern "C"
Douglas Gregorac0605e2010-01-28 06:00:51 +0000222
223void clang::ReportSerializedDiagnostics(const llvm::sys::Path &DiagnosticsPath,
224 Diagnostic &Diags,
225 unsigned num_unsaved_files,
Daniel Dunbar854d36b2010-01-30 23:31:40 +0000226 struct CXUnsavedFile *unsaved_files,
227 const LangOptions &LangOpts) {
Douglas Gregorac0605e2010-01-28 06:00:51 +0000228 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 Dunbar854d36b2010-01-30 23:31:40 +0000256 Diags.getClient()->BeginSourceFile(LangOpts, 0);
257
Douglas Gregorac0605e2010-01-28 06:00:51 +0000258 // 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 Dunbar854d36b2010-01-30 23:31:40 +0000268
269 Diags.getClient()->EndSourceFile();
Douglas Gregorac0605e2010-01-28 06:00:51 +0000270}