blob: f22f5eeff64afa51218c88607cf4cc8f76a5a5c2 [file] [log] [blame]
Ted Kremenekab188932010-01-05 19:32:54 +00001//===- CIndexCodeCompletion.cpp - Code Completion API hooks ---------------===//
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// This file implements the Clang-C Source Indexing library hooks for
11// code completion.
12//
13//===----------------------------------------------------------------------===//
14
15#include "CIndexer.h"
Douglas Gregor936ea3b2010-01-28 00:56:43 +000016#include "CIndexDiagnostic.h"
Benjamin Kramerb846deb2010-04-12 19:45:50 +000017#include "clang/Basic/SourceManager.h"
18#include "clang/Basic/FileManager.h"
Douglas Gregor1abc6bc2010-08-04 16:47:14 +000019#include "clang/Frontend/ASTUnit.h"
Benjamin Kramerb846deb2010-04-12 19:45:50 +000020#include "clang/Frontend/CompilerInstance.h"
Douglas Gregor936ea3b2010-01-28 00:56:43 +000021#include "clang/Frontend/FrontendDiagnostic.h"
Ted Kremenekab188932010-01-05 19:32:54 +000022#include "clang/Sema/CodeCompleteConsumer.h"
Douglas Gregordf95a132010-08-09 20:45:32 +000023#include "llvm/ADT/SmallString.h"
Ted Kremenekab188932010-01-05 19:32:54 +000024#include "llvm/ADT/StringExtras.h"
Daniel Dunbarb1fd3452010-08-19 23:44:10 +000025#include "llvm/Support/CrashRecoveryContext.h"
Ted Kremenekab188932010-01-05 19:32:54 +000026#include "llvm/Support/MemoryBuffer.h"
Douglas Gregordf95a132010-08-09 20:45:32 +000027#include "llvm/Support/Timer.h"
28#include "llvm/Support/raw_ostream.h"
Ted Kremenekab188932010-01-05 19:32:54 +000029#include "llvm/System/Program.h"
Douglas Gregor3d398aa2010-07-26 16:29:14 +000030#include <cstdlib>
31#include <cstdio>
Ted Kremenekab188932010-01-05 19:32:54 +000032
Douglas Gregordf95a132010-08-09 20:45:32 +000033
Ted Kremenekda7af322010-04-15 01:02:28 +000034#ifdef UDP_CODE_COMPLETION_LOGGER
35#include "clang/Basic/Version.h"
Ted Kremenekda7af322010-04-15 01:02:28 +000036#include <arpa/inet.h>
37#include <sys/socket.h>
38#include <sys/types.h>
39#include <unistd.h>
40#endif
41
Ted Kremenekab188932010-01-05 19:32:54 +000042using namespace clang;
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +000043using namespace clang::cxstring;
Ted Kremenekab188932010-01-05 19:32:54 +000044
Douglas Gregor12e13132010-05-26 22:00:08 +000045namespace {
46 /// \brief Stored representation of a completion string.
47 ///
48 /// This is the representation behind a CXCompletionString.
49 class CXStoredCodeCompletionString : public CodeCompletionString {
50 unsigned Priority;
Douglas Gregor58ddb602010-08-23 23:00:57 +000051 CXAvailabilityKind Availability;
Douglas Gregor12e13132010-05-26 22:00:08 +000052
53 public:
Douglas Gregor58ddb602010-08-23 23:00:57 +000054 CXStoredCodeCompletionString(unsigned Priority,
55 CXAvailabilityKind Availability)
56 : Priority(Priority), Availability(Availability) { }
Douglas Gregor12e13132010-05-26 22:00:08 +000057
58 unsigned getPriority() const { return Priority; }
Douglas Gregor58ddb602010-08-23 23:00:57 +000059 CXAvailabilityKind getAvailability() const { return Availability; }
Douglas Gregor12e13132010-05-26 22:00:08 +000060 };
61}
62
Ted Kremenekab188932010-01-05 19:32:54 +000063extern "C" {
64
65enum CXCompletionChunkKind
66clang_getCompletionChunkKind(CXCompletionString completion_string,
67 unsigned chunk_number) {
Douglas Gregor12e13132010-05-26 22:00:08 +000068 CXStoredCodeCompletionString *CCStr
69 = (CXStoredCodeCompletionString *)completion_string;
Ted Kremenekab188932010-01-05 19:32:54 +000070 if (!CCStr || chunk_number >= CCStr->size())
71 return CXCompletionChunk_Text;
72
73 switch ((*CCStr)[chunk_number].Kind) {
74 case CodeCompletionString::CK_TypedText:
75 return CXCompletionChunk_TypedText;
76 case CodeCompletionString::CK_Text:
77 return CXCompletionChunk_Text;
78 case CodeCompletionString::CK_Optional:
79 return CXCompletionChunk_Optional;
80 case CodeCompletionString::CK_Placeholder:
81 return CXCompletionChunk_Placeholder;
82 case CodeCompletionString::CK_Informative:
83 return CXCompletionChunk_Informative;
84 case CodeCompletionString::CK_ResultType:
85 return CXCompletionChunk_ResultType;
86 case CodeCompletionString::CK_CurrentParameter:
87 return CXCompletionChunk_CurrentParameter;
88 case CodeCompletionString::CK_LeftParen:
89 return CXCompletionChunk_LeftParen;
90 case CodeCompletionString::CK_RightParen:
91 return CXCompletionChunk_RightParen;
92 case CodeCompletionString::CK_LeftBracket:
93 return CXCompletionChunk_LeftBracket;
94 case CodeCompletionString::CK_RightBracket:
95 return CXCompletionChunk_RightBracket;
96 case CodeCompletionString::CK_LeftBrace:
97 return CXCompletionChunk_LeftBrace;
98 case CodeCompletionString::CK_RightBrace:
99 return CXCompletionChunk_RightBrace;
100 case CodeCompletionString::CK_LeftAngle:
101 return CXCompletionChunk_LeftAngle;
102 case CodeCompletionString::CK_RightAngle:
103 return CXCompletionChunk_RightAngle;
104 case CodeCompletionString::CK_Comma:
105 return CXCompletionChunk_Comma;
Douglas Gregor01dfea02010-01-10 23:08:15 +0000106 case CodeCompletionString::CK_Colon:
107 return CXCompletionChunk_Colon;
108 case CodeCompletionString::CK_SemiColon:
109 return CXCompletionChunk_SemiColon;
110 case CodeCompletionString::CK_Equal:
111 return CXCompletionChunk_Equal;
112 case CodeCompletionString::CK_HorizontalSpace:
113 return CXCompletionChunk_HorizontalSpace;
114 case CodeCompletionString::CK_VerticalSpace:
115 return CXCompletionChunk_VerticalSpace;
Ted Kremenekab188932010-01-05 19:32:54 +0000116 }
117
118 // Should be unreachable, but let's be careful.
119 return CXCompletionChunk_Text;
120}
121
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000122CXString clang_getCompletionChunkText(CXCompletionString completion_string,
123 unsigned chunk_number) {
Douglas Gregor12e13132010-05-26 22:00:08 +0000124 CXStoredCodeCompletionString *CCStr
125 = (CXStoredCodeCompletionString *)completion_string;
Ted Kremenekab188932010-01-05 19:32:54 +0000126 if (!CCStr || chunk_number >= CCStr->size())
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000127 return createCXString(0);
Ted Kremenekab188932010-01-05 19:32:54 +0000128
129 switch ((*CCStr)[chunk_number].Kind) {
130 case CodeCompletionString::CK_TypedText:
131 case CodeCompletionString::CK_Text:
132 case CodeCompletionString::CK_Placeholder:
133 case CodeCompletionString::CK_CurrentParameter:
134 case CodeCompletionString::CK_Informative:
135 case CodeCompletionString::CK_LeftParen:
136 case CodeCompletionString::CK_RightParen:
137 case CodeCompletionString::CK_LeftBracket:
138 case CodeCompletionString::CK_RightBracket:
139 case CodeCompletionString::CK_LeftBrace:
140 case CodeCompletionString::CK_RightBrace:
141 case CodeCompletionString::CK_LeftAngle:
142 case CodeCompletionString::CK_RightAngle:
143 case CodeCompletionString::CK_Comma:
144 case CodeCompletionString::CK_ResultType:
Douglas Gregor01dfea02010-01-10 23:08:15 +0000145 case CodeCompletionString::CK_Colon:
146 case CodeCompletionString::CK_SemiColon:
147 case CodeCompletionString::CK_Equal:
148 case CodeCompletionString::CK_HorizontalSpace:
Douglas Gregor21c241f2010-05-25 06:14:46 +0000149 case CodeCompletionString::CK_VerticalSpace:
Douglas Gregor5a9c0bc2010-10-08 20:39:29 +0000150 return createCXString((*CCStr)[chunk_number].Text, false);
Douglas Gregor21c241f2010-05-25 06:14:46 +0000151
Ted Kremenekab188932010-01-05 19:32:54 +0000152 case CodeCompletionString::CK_Optional:
153 // Note: treated as an empty text block.
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000154 return createCXString("");
Ted Kremenekab188932010-01-05 19:32:54 +0000155 }
156
157 // Should be unreachable, but let's be careful.
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000158 return createCXString(0);
Ted Kremenekab188932010-01-05 19:32:54 +0000159}
160
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000161
Ted Kremenekab188932010-01-05 19:32:54 +0000162CXCompletionString
163clang_getCompletionChunkCompletionString(CXCompletionString completion_string,
164 unsigned chunk_number) {
Douglas Gregor12e13132010-05-26 22:00:08 +0000165 CXStoredCodeCompletionString *CCStr
166 = (CXStoredCodeCompletionString *)completion_string;
Ted Kremenekab188932010-01-05 19:32:54 +0000167 if (!CCStr || chunk_number >= CCStr->size())
168 return 0;
169
170 switch ((*CCStr)[chunk_number].Kind) {
171 case CodeCompletionString::CK_TypedText:
172 case CodeCompletionString::CK_Text:
173 case CodeCompletionString::CK_Placeholder:
174 case CodeCompletionString::CK_CurrentParameter:
175 case CodeCompletionString::CK_Informative:
176 case CodeCompletionString::CK_LeftParen:
177 case CodeCompletionString::CK_RightParen:
178 case CodeCompletionString::CK_LeftBracket:
179 case CodeCompletionString::CK_RightBracket:
180 case CodeCompletionString::CK_LeftBrace:
181 case CodeCompletionString::CK_RightBrace:
182 case CodeCompletionString::CK_LeftAngle:
183 case CodeCompletionString::CK_RightAngle:
184 case CodeCompletionString::CK_Comma:
185 case CodeCompletionString::CK_ResultType:
Douglas Gregor01dfea02010-01-10 23:08:15 +0000186 case CodeCompletionString::CK_Colon:
187 case CodeCompletionString::CK_SemiColon:
188 case CodeCompletionString::CK_Equal:
189 case CodeCompletionString::CK_HorizontalSpace:
190 case CodeCompletionString::CK_VerticalSpace:
Ted Kremenekab188932010-01-05 19:32:54 +0000191 return 0;
192
193 case CodeCompletionString::CK_Optional:
194 // Note: treated as an empty text block.
195 return (*CCStr)[chunk_number].Optional;
196 }
197
198 // Should be unreachable, but let's be careful.
199 return 0;
200}
201
202unsigned clang_getNumCompletionChunks(CXCompletionString completion_string) {
Douglas Gregor12e13132010-05-26 22:00:08 +0000203 CXStoredCodeCompletionString *CCStr
204 = (CXStoredCodeCompletionString *)completion_string;
Ted Kremenekab188932010-01-05 19:32:54 +0000205 return CCStr? CCStr->size() : 0;
206}
207
Douglas Gregor12e13132010-05-26 22:00:08 +0000208unsigned clang_getCompletionPriority(CXCompletionString completion_string) {
209 CXStoredCodeCompletionString *CCStr
210 = (CXStoredCodeCompletionString *)completion_string;
Bill Wendlinga2ace582010-05-27 18:35:05 +0000211 return CCStr? CCStr->getPriority() : unsigned(CCP_Unlikely);
Douglas Gregor12e13132010-05-26 22:00:08 +0000212}
213
Douglas Gregor58ddb602010-08-23 23:00:57 +0000214enum CXAvailabilityKind
215clang_getCompletionAvailability(CXCompletionString completion_string) {
216 CXStoredCodeCompletionString *CCStr
217 = (CXStoredCodeCompletionString *)completion_string;
218 return CCStr? CCStr->getAvailability() : CXAvailability_Available;
219}
220
Ted Kremenekab188932010-01-05 19:32:54 +0000221/// \brief The CXCodeCompleteResults structure we allocate internally;
222/// the client only sees the initial CXCodeCompleteResults structure.
223struct AllocatedCXCodeCompleteResults : public CXCodeCompleteResults {
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000224 AllocatedCXCodeCompleteResults();
225 ~AllocatedCXCodeCompleteResults();
226
Douglas Gregora88084b2010-02-18 18:08:43 +0000227 /// \brief Diagnostics produced while performing code completion.
228 llvm::SmallVector<StoredDiagnostic, 8> Diagnostics;
229
Douglas Gregorf715ca12010-03-16 00:06:06 +0000230 /// \brief Diag object
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000231 llvm::IntrusiveRefCntPtr<Diagnostic> Diag;
Douglas Gregorf715ca12010-03-16 00:06:06 +0000232
Douglas Gregora88084b2010-02-18 18:08:43 +0000233 /// \brief Language options used to adjust source locations.
Daniel Dunbar35b84402010-01-30 23:31:40 +0000234 LangOptions LangOpts;
Douglas Gregora88084b2010-02-18 18:08:43 +0000235
236 /// \brief File manager, used for diagnostics.
237 FileManager FileMgr;
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000238
239 FileSystemOptions FileSystemOpts;
240
241 /// \brief Source manager, used for diagnostics.
242 SourceManager SourceMgr;
Douglas Gregor313e26c2010-02-18 23:35:40 +0000243
244 /// \brief Temporary files that should be removed once we have finished
245 /// with the code-completion results.
246 std::vector<llvm::sys::Path> TemporaryFiles;
Douglas Gregorb75d3df2010-08-04 17:07:00 +0000247
248 /// \brief Temporary buffers that will be deleted once we have finished with the code-completion results.
249 llvm::SmallVector<const llvm::MemoryBuffer *, 1> TemporaryBuffers;
Ted Kremenekab188932010-01-05 19:32:54 +0000250};
251
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000252AllocatedCXCodeCompleteResults::AllocatedCXCodeCompleteResults()
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000253 : CXCodeCompleteResults(), Diag(new Diagnostic),
254 SourceMgr(*Diag, FileMgr, FileSystemOpts) { }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000255
256AllocatedCXCodeCompleteResults::~AllocatedCXCodeCompleteResults() {
257 for (unsigned I = 0, N = NumResults; I != N; ++I)
Douglas Gregor12e13132010-05-26 22:00:08 +0000258 delete (CXStoredCodeCompletionString *)Results[I].CompletionString;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000259 delete [] Results;
Douglas Gregor313e26c2010-02-18 23:35:40 +0000260
261 for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I)
262 TemporaryFiles[I].eraseFromDisk();
Douglas Gregorb75d3df2010-08-04 17:07:00 +0000263 for (unsigned I = 0, N = TemporaryBuffers.size(); I != N; ++I)
264 delete TemporaryBuffers[I];
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000265}
266
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000267} // end extern "C"
268
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000269namespace {
270 class CaptureCompletionResults : public CodeCompleteConsumer {
271 AllocatedCXCodeCompleteResults &AllocatedResults;
Douglas Gregor32be4a52010-10-11 21:37:58 +0000272 llvm::SmallVector<CXCompletionResult, 16> StoredResults;
273
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000274 public:
275 explicit CaptureCompletionResults(AllocatedCXCodeCompleteResults &Results)
Douglas Gregor8071e422010-08-15 06:18:01 +0000276 : CodeCompleteConsumer(true, false, true, false),
277 AllocatedResults(Results) { }
Douglas Gregor32be4a52010-10-11 21:37:58 +0000278 ~CaptureCompletionResults() { Finish(); }
279
Douglas Gregore6b1bb62010-08-11 21:23:17 +0000280 virtual void ProcessCodeCompleteResults(Sema &S,
281 CodeCompletionContext Context,
John McCall0a2c5e22010-08-25 06:19:51 +0000282 CodeCompletionResult *Results,
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000283 unsigned NumResults) {
Douglas Gregor32be4a52010-10-11 21:37:58 +0000284 StoredResults.reserve(StoredResults.size() + NumResults);
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000285 for (unsigned I = 0; I != NumResults; ++I) {
286 CXStoredCodeCompletionString *StoredCompletion
Douglas Gregor58ddb602010-08-23 23:00:57 +0000287 = new CXStoredCodeCompletionString(Results[I].Priority,
288 Results[I].Availability);
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000289 (void)Results[I].CreateCodeCompletionString(S, StoredCompletion);
Douglas Gregor32be4a52010-10-11 21:37:58 +0000290
291 CXCompletionResult R;
292 R.CursorKind = Results[I].CursorKind;
293 R.CompletionString = StoredCompletion;
294 StoredResults.push_back(R);
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000295 }
296 }
Douglas Gregor1e5e6682010-08-26 13:48:20 +0000297
Douglas Gregor32be4a52010-10-11 21:37:58 +0000298 virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
299 OverloadCandidate *Candidates,
300 unsigned NumCandidates) {
301 StoredResults.reserve(StoredResults.size() + NumCandidates);
302 for (unsigned I = 0; I != NumCandidates; ++I) {
303 // FIXME: Set priority, availability appropriately.
304 CXStoredCodeCompletionString *StoredCompletion
305 = new CXStoredCodeCompletionString(1, CXAvailability_Available);
306 (void)Candidates[I].CreateSignatureString(CurrentArg, S,
307 StoredCompletion);
308
309 CXCompletionResult R;
310 R.CursorKind = CXCursor_NotImplemented;
311 R.CompletionString = StoredCompletion;
312 StoredResults.push_back(R);
313 }
314 }
315
316 private:
317 void Finish() {
318 AllocatedResults.Results = new CXCompletionResult [StoredResults.size()];
319 AllocatedResults.NumResults = StoredResults.size();
320 std::memcpy(AllocatedResults.Results, StoredResults.data(),
321 StoredResults.size() * sizeof(CXCompletionResult));
322 StoredResults.clear();
323 }
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000324 };
325}
326
327extern "C" {
Daniel Dunbarb1fd3452010-08-19 23:44:10 +0000328struct CodeCompleteAtInfo {
329 CXTranslationUnit TU;
330 const char *complete_filename;
331 unsigned complete_line;
332 unsigned complete_column;
333 struct CXUnsavedFile *unsaved_files;
334 unsigned num_unsaved_files;
335 unsigned options;
336 CXCodeCompleteResults *result;
337};
338void clang_codeCompleteAt_Impl(void *UserData) {
339 CodeCompleteAtInfo *CCAI = static_cast<CodeCompleteAtInfo*>(UserData);
340 CXTranslationUnit TU = CCAI->TU;
341 const char *complete_filename = CCAI->complete_filename;
342 unsigned complete_line = CCAI->complete_line;
343 unsigned complete_column = CCAI->complete_column;
344 struct CXUnsavedFile *unsaved_files = CCAI->unsaved_files;
345 unsigned num_unsaved_files = CCAI->num_unsaved_files;
346 unsigned options = CCAI->options;
347 CCAI->result = 0;
348
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000349#ifdef UDP_CODE_COMPLETION_LOGGER
350#ifdef UDP_CODE_COMPLETION_LOGGER_PORT
351 const llvm::TimeRecord &StartTime = llvm::TimeRecord::getCurrentTime();
352#endif
353#endif
354
355 bool EnableLogging = getenv("LIBCLANG_CODE_COMPLETION_LOGGING") != 0;
356
357 ASTUnit *AST = static_cast<ASTUnit *>(TU);
358 if (!AST)
Daniel Dunbarb1fd3452010-08-19 23:44:10 +0000359 return;
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000360
Douglas Gregor593b0c12010-09-23 18:47:53 +0000361 ASTUnit::ConcurrencyCheck Check(*AST);
362
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000363 // Perform the remapping of source files.
364 llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles;
365 for (unsigned I = 0; I != num_unsaved_files; ++I) {
366 llvm::StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length);
367 const llvm::MemoryBuffer *Buffer
368 = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename);
369 RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename,
370 Buffer));
371 }
372
373 if (EnableLogging) {
374 // FIXME: Add logging.
375 }
376
377 // Parse the resulting source file to find code-completion results.
378 AllocatedCXCodeCompleteResults *Results = new AllocatedCXCodeCompleteResults;
379 Results->Results = 0;
380 Results->NumResults = 0;
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000381
382 // Create a code-completion consumer to capture the results.
383 CaptureCompletionResults Capture(*Results);
384
385 // Perform completion.
386 AST->CodeComplete(complete_filename, complete_line, complete_column,
Douglas Gregorcee235c2010-08-05 09:09:23 +0000387 RemappedFiles.data(), RemappedFiles.size(),
388 (options & CXCodeComplete_IncludeMacros),
389 (options & CXCodeComplete_IncludeCodePatterns),
390 Capture,
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000391 *Results->Diag, Results->LangOpts, Results->SourceMgr,
Douglas Gregor2283d792010-08-20 00:59:43 +0000392 Results->FileMgr, Results->Diagnostics,
393 Results->TemporaryBuffers);
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000394
395
396
397#ifdef UDP_CODE_COMPLETION_LOGGER
398#ifdef UDP_CODE_COMPLETION_LOGGER_PORT
399 const llvm::TimeRecord &EndTime = llvm::TimeRecord::getCurrentTime();
400 llvm::SmallString<256> LogResult;
401 llvm::raw_svector_ostream os(LogResult);
402
403 // Figure out the language and whether or not it uses PCH.
404 const char *lang = 0;
405 bool usesPCH = false;
406
407 for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
408 I != E; ++I) {
409 if (*I == 0)
410 continue;
411 if (strcmp(*I, "-x") == 0) {
412 if (I + 1 != E) {
413 lang = *(++I);
414 continue;
415 }
416 }
417 else if (strcmp(*I, "-include") == 0) {
418 if (I+1 != E) {
419 const char *arg = *(++I);
420 llvm::SmallString<512> pchName;
421 {
422 llvm::raw_svector_ostream os(pchName);
423 os << arg << ".pth";
424 }
425 pchName.push_back('\0');
426 struct stat stat_results;
427 if (stat(pchName.data(), &stat_results) == 0)
428 usesPCH = true;
429 continue;
430 }
431 }
432 }
433
434 os << "{ ";
435 os << "\"wall\": " << (EndTime.getWallTime() - StartTime.getWallTime());
436 os << ", \"numRes\": " << Results->NumResults;
437 os << ", \"diags\": " << Results->Diagnostics.size();
438 os << ", \"pch\": " << (usesPCH ? "true" : "false");
439 os << ", \"lang\": \"" << (lang ? lang : "<unknown>") << '"';
440 const char *name = getlogin();
441 os << ", \"user\": \"" << (name ? name : "unknown") << '"';
442 os << ", \"clangVer\": \"" << getClangFullVersion() << '"';
443 os << " }";
444
445 llvm::StringRef res = os.str();
446 if (res.size() > 0) {
447 do {
448 // Setup the UDP socket.
449 struct sockaddr_in servaddr;
450 bzero(&servaddr, sizeof(servaddr));
451 servaddr.sin_family = AF_INET;
452 servaddr.sin_port = htons(UDP_CODE_COMPLETION_LOGGER_PORT);
453 if (inet_pton(AF_INET, UDP_CODE_COMPLETION_LOGGER,
454 &servaddr.sin_addr) <= 0)
455 break;
456
457 int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
458 if (sockfd < 0)
459 break;
460
461 sendto(sockfd, res.data(), res.size(), 0,
462 (struct sockaddr *)&servaddr, sizeof(servaddr));
463 close(sockfd);
464 }
465 while (false);
466 }
467#endif
468#endif
Daniel Dunbarb1fd3452010-08-19 23:44:10 +0000469 CCAI->result = Results;
470}
471CXCodeCompleteResults *clang_codeCompleteAt(CXTranslationUnit TU,
472 const char *complete_filename,
473 unsigned complete_line,
474 unsigned complete_column,
475 struct CXUnsavedFile *unsaved_files,
476 unsigned num_unsaved_files,
477 unsigned options) {
478 CodeCompleteAtInfo CCAI = { TU, complete_filename, complete_line,
479 complete_column, unsaved_files, num_unsaved_files,
480 options, 0 };
481 llvm::CrashRecoveryContext CRC;
482
Daniel Dunbarbf44c3b2010-11-05 07:19:31 +0000483 if (!RunSafely(CRC, clang_codeCompleteAt_Impl, &CCAI)) {
Daniel Dunbarb1fd3452010-08-19 23:44:10 +0000484 fprintf(stderr, "libclang: crash detected in code completion\n");
485 static_cast<ASTUnit *>(TU)->setUnsafeToFree(true);
486 return 0;
487 }
488
489 return CCAI.result;
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000490}
491
Douglas Gregorcee235c2010-08-05 09:09:23 +0000492unsigned clang_defaultCodeCompleteOptions(void) {
493 return CXCodeComplete_IncludeMacros;
494}
495
Ted Kremenekab188932010-01-05 19:32:54 +0000496void clang_disposeCodeCompleteResults(CXCodeCompleteResults *ResultsIn) {
497 if (!ResultsIn)
498 return;
499
500 AllocatedCXCodeCompleteResults *Results
501 = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn);
Ted Kremenekab188932010-01-05 19:32:54 +0000502 delete Results;
503}
Douglas Gregor58ddb602010-08-23 23:00:57 +0000504
Douglas Gregora88084b2010-02-18 18:08:43 +0000505unsigned
506clang_codeCompleteGetNumDiagnostics(CXCodeCompleteResults *ResultsIn) {
507 AllocatedCXCodeCompleteResults *Results
508 = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn);
509 if (!Results)
510 return 0;
511
512 return Results->Diagnostics.size();
513}
514
515CXDiagnostic
516clang_codeCompleteGetDiagnostic(CXCodeCompleteResults *ResultsIn,
517 unsigned Index) {
518 AllocatedCXCodeCompleteResults *Results
519 = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn);
520 if (!Results || Index >= Results->Diagnostics.size())
521 return 0;
522
523 return new CXStoredDiagnostic(Results->Diagnostics[Index], Results->LangOpts);
524}
525
526
Ted Kremenekab188932010-01-05 19:32:54 +0000527} // end extern "C"
Douglas Gregor1e5e6682010-08-26 13:48:20 +0000528
Douglas Gregorc7b7b7a2010-10-18 21:05:04 +0000529/// \brief Simple utility function that appends a \p New string to the given
530/// \p Old string, using the \p Buffer for storage.
531///
532/// \param Old The string to which we are appending. This parameter will be
533/// updated to reflect the complete string.
534///
535///
536/// \param New The string to append to \p Old.
537///
538/// \param Buffer A buffer that stores the actual, concatenated string. It will
539/// be used if the old string is already-non-empty.
540static void AppendToString(llvm::StringRef &Old, llvm::StringRef New,
541 llvm::SmallString<256> &Buffer) {
542 if (Old.empty()) {
543 Old = New;
544 return;
545 }
546
547 if (Buffer.empty())
548 Buffer.append(Old.begin(), Old.end());
549 Buffer.append(New.begin(), New.end());
550 Old = Buffer.str();
551}
552
553/// \brief Get the typed-text blocks from the given code-completion string
554/// and return them as a single string.
555///
556/// \param String The code-completion string whose typed-text blocks will be
557/// concatenated.
558///
559/// \param Buffer A buffer used for storage of the completed name.
560static llvm::StringRef GetTypedName(CodeCompletionString *String,
561 llvm::SmallString<256> &Buffer) {
562 llvm::StringRef Result;
563 for (CodeCompletionString::iterator C = String->begin(), CEnd = String->end();
564 C != CEnd; ++C) {
565 if (C->Kind == CodeCompletionString::CK_TypedText)
566 AppendToString(Result, C->Text, Buffer);
567 }
568
569 return Result;
570}
571
Douglas Gregor1e5e6682010-08-26 13:48:20 +0000572namespace {
573 struct OrderCompletionResults {
574 bool operator()(const CXCompletionResult &XR,
575 const CXCompletionResult &YR) const {
576 CXStoredCodeCompletionString *X
577 = (CXStoredCodeCompletionString *)XR.CompletionString;
578 CXStoredCodeCompletionString *Y
579 = (CXStoredCodeCompletionString *)YR.CompletionString;
580
Douglas Gregorc7b7b7a2010-10-18 21:05:04 +0000581 llvm::SmallString<256> XBuffer;
582 llvm::StringRef XText = GetTypedName(X, XBuffer);
583 llvm::SmallString<256> YBuffer;
584 llvm::StringRef YText = GetTypedName(Y, YBuffer);
Douglas Gregor1e5e6682010-08-26 13:48:20 +0000585
Douglas Gregorc7b7b7a2010-10-18 21:05:04 +0000586 if (XText.empty() || YText.empty())
587 return !XText.empty();
588
589 int result = XText.compare_lower(YText);
Douglas Gregor1e5e6682010-08-26 13:48:20 +0000590 if (result < 0)
591 return true;
592 if (result > 0)
593 return false;
594
Douglas Gregorc7b7b7a2010-10-18 21:05:04 +0000595 result = XText.compare(YText);
Douglas Gregor1aad3402010-09-10 23:05:54 +0000596 return result < 0;
Douglas Gregor1e5e6682010-08-26 13:48:20 +0000597 }
598 };
599}
600
601extern "C" {
602 void clang_sortCodeCompletionResults(CXCompletionResult *Results,
603 unsigned NumResults) {
604 std::stable_sort(Results, Results + NumResults, OrderCompletionResults());
605 }
Douglas Gregor458433d2010-08-26 15:07:07 +0000606}