1 //===- 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"
16 #include "CXTranslationUnit.h"
17 #include "CXString.h"
18 #include "CIndexDiagnostic.h"
19 #include "clang/Basic/SourceManager.h"
20 #include "clang/Basic/FileManager.h"
21 #include "clang/Frontend/ASTUnit.h"
22 #include "clang/Frontend/CompilerInstance.h"
23 #include "clang/Frontend/FrontendDiagnostic.h"
24 #include "clang/Sema/CodeCompleteConsumer.h"
25 #include "llvm/ADT/SmallString.h"
26 #include "llvm/ADT/StringExtras.h"
27 #include "llvm/Support/Atomic.h"
28 #include "llvm/Support/CrashRecoveryContext.h"
29 #include "llvm/Support/MemoryBuffer.h"
30 #include "llvm/Support/Timer.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include "llvm/Support/Program.h"
33 #include <cstdlib>
34 #include <cstdio>
35 
36 
37 #ifdef UDP_CODE_COMPLETION_LOGGER
38 #include "clang/Basic/Version.h"
39 #include <arpa/inet.h>
40 #include <sys/socket.h>
41 #include <sys/types.h>
42 #include <unistd.h>
43 #endif
44 
45 using namespace clang;
46 using namespace clang::cxstring;
47 
48 extern "C" {
49 
50 enum CXCompletionChunkKind
51 clang_getCompletionChunkKind(CXCompletionString completion_string,
52                              unsigned chunk_number) {
53   CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
54   if (!CCStr || chunk_number >= CCStr->size())
55     return CXCompletionChunk_Text;
56 
57   switch ((*CCStr)[chunk_number].Kind) {
58   case CodeCompletionString::CK_TypedText:
59     return CXCompletionChunk_TypedText;
60   case CodeCompletionString::CK_Text:
61     return CXCompletionChunk_Text;
62   case CodeCompletionString::CK_Optional:
63     return CXCompletionChunk_Optional;
64   case CodeCompletionString::CK_Placeholder:
65     return CXCompletionChunk_Placeholder;
66   case CodeCompletionString::CK_Informative:
67     return CXCompletionChunk_Informative;
68   case CodeCompletionString::CK_ResultType:
69     return CXCompletionChunk_ResultType;
70   case CodeCompletionString::CK_CurrentParameter:
71     return CXCompletionChunk_CurrentParameter;
72   case CodeCompletionString::CK_LeftParen:
73     return CXCompletionChunk_LeftParen;
74   case CodeCompletionString::CK_RightParen:
75     return CXCompletionChunk_RightParen;
76   case CodeCompletionString::CK_LeftBracket:
77     return CXCompletionChunk_LeftBracket;
78   case CodeCompletionString::CK_RightBracket:
79     return CXCompletionChunk_RightBracket;
80   case CodeCompletionString::CK_LeftBrace:
81     return CXCompletionChunk_LeftBrace;
82   case CodeCompletionString::CK_RightBrace:
83     return CXCompletionChunk_RightBrace;
84   case CodeCompletionString::CK_LeftAngle:
85     return CXCompletionChunk_LeftAngle;
86   case CodeCompletionString::CK_RightAngle:
87     return CXCompletionChunk_RightAngle;
88   case CodeCompletionString::CK_Comma:
89     return CXCompletionChunk_Comma;
90   case CodeCompletionString::CK_Colon:
91     return CXCompletionChunk_Colon;
92   case CodeCompletionString::CK_SemiColon:
93     return CXCompletionChunk_SemiColon;
94   case CodeCompletionString::CK_Equal:
95     return CXCompletionChunk_Equal;
96   case CodeCompletionString::CK_HorizontalSpace:
97     return CXCompletionChunk_HorizontalSpace;
98   case CodeCompletionString::CK_VerticalSpace:
99     return CXCompletionChunk_VerticalSpace;
100   }
101 
102   // Should be unreachable, but let's be careful.
103   return CXCompletionChunk_Text;
104 }
105 
106 CXString clang_getCompletionChunkText(CXCompletionString completion_string,
107                                       unsigned chunk_number) {
108   CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
109   if (!CCStr || chunk_number >= CCStr->size())
110     return createCXString((const char*)0);
111 
112   switch ((*CCStr)[chunk_number].Kind) {
113   case CodeCompletionString::CK_TypedText:
114   case CodeCompletionString::CK_Text:
115   case CodeCompletionString::CK_Placeholder:
116   case CodeCompletionString::CK_CurrentParameter:
117   case CodeCompletionString::CK_Informative:
118   case CodeCompletionString::CK_LeftParen:
119   case CodeCompletionString::CK_RightParen:
120   case CodeCompletionString::CK_LeftBracket:
121   case CodeCompletionString::CK_RightBracket:
122   case CodeCompletionString::CK_LeftBrace:
123   case CodeCompletionString::CK_RightBrace:
124   case CodeCompletionString::CK_LeftAngle:
125   case CodeCompletionString::CK_RightAngle:
126   case CodeCompletionString::CK_Comma:
127   case CodeCompletionString::CK_ResultType:
128   case CodeCompletionString::CK_Colon:
129   case CodeCompletionString::CK_SemiColon:
130   case CodeCompletionString::CK_Equal:
131   case CodeCompletionString::CK_HorizontalSpace:
132   case CodeCompletionString::CK_VerticalSpace:
133     return createCXString((*CCStr)[chunk_number].Text, false);
134 
135   case CodeCompletionString::CK_Optional:
136     // Note: treated as an empty text block.
137     return createCXString("");
138   }
139 
140   // Should be unreachable, but let's be careful.
141   return createCXString((const char*)0);
142 }
143 
144 
145 CXCompletionString
146 clang_getCompletionChunkCompletionString(CXCompletionString completion_string,
147                                          unsigned chunk_number) {
148   CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
149   if (!CCStr || chunk_number >= CCStr->size())
150     return 0;
151 
152   switch ((*CCStr)[chunk_number].Kind) {
153   case CodeCompletionString::CK_TypedText:
154   case CodeCompletionString::CK_Text:
155   case CodeCompletionString::CK_Placeholder:
156   case CodeCompletionString::CK_CurrentParameter:
157   case CodeCompletionString::CK_Informative:
158   case CodeCompletionString::CK_LeftParen:
159   case CodeCompletionString::CK_RightParen:
160   case CodeCompletionString::CK_LeftBracket:
161   case CodeCompletionString::CK_RightBracket:
162   case CodeCompletionString::CK_LeftBrace:
163   case CodeCompletionString::CK_RightBrace:
164   case CodeCompletionString::CK_LeftAngle:
165   case CodeCompletionString::CK_RightAngle:
166   case CodeCompletionString::CK_Comma:
167   case CodeCompletionString::CK_ResultType:
168   case CodeCompletionString::CK_Colon:
169   case CodeCompletionString::CK_SemiColon:
170   case CodeCompletionString::CK_Equal:
171   case CodeCompletionString::CK_HorizontalSpace:
172   case CodeCompletionString::CK_VerticalSpace:
173     return 0;
174 
175   case CodeCompletionString::CK_Optional:
176     // Note: treated as an empty text block.
177     return (*CCStr)[chunk_number].Optional;
178   }
179 
180   // Should be unreachable, but let's be careful.
181   return 0;
182 }
183 
184 unsigned clang_getNumCompletionChunks(CXCompletionString completion_string) {
185   CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
186   return CCStr? CCStr->size() : 0;
187 }
188 
189 unsigned clang_getCompletionPriority(CXCompletionString completion_string) {
190   CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
191   return CCStr? CCStr->getPriority() : unsigned(CCP_Unlikely);
192 }
193 
194 enum CXAvailabilityKind
195 clang_getCompletionAvailability(CXCompletionString completion_string) {
196   CodeCompletionString *CCStr = (CodeCompletionString *)completion_string;
197   return CCStr? static_cast<CXAvailabilityKind>(CCStr->getAvailability())
198               : CXAvailability_Available;
199 }
200 
201 /// \brief The CXCodeCompleteResults structure we allocate internally;
202 /// the client only sees the initial CXCodeCompleteResults structure.
203 struct AllocatedCXCodeCompleteResults : public CXCodeCompleteResults {
204   AllocatedCXCodeCompleteResults(const FileSystemOptions& FileSystemOpts);
205   ~AllocatedCXCodeCompleteResults();
206 
207   /// \brief Diagnostics produced while performing code completion.
208   llvm::SmallVector<StoredDiagnostic, 8> Diagnostics;
209 
210   /// \brief Diag object
211   llvm::IntrusiveRefCntPtr<Diagnostic> Diag;
212 
213   /// \brief Language options used to adjust source locations.
214   LangOptions LangOpts;
215 
216   FileSystemOptions FileSystemOpts;
217 
218   /// \brief File manager, used for diagnostics.
219   llvm::IntrusiveRefCntPtr<FileManager> FileMgr;
220 
221   /// \brief Source manager, used for diagnostics.
222   llvm::IntrusiveRefCntPtr<SourceManager> SourceMgr;
223 
224   /// \brief Temporary files that should be removed once we have finished
225   /// with the code-completion results.
226   std::vector<llvm::sys::Path> TemporaryFiles;
227 
228   /// \brief Temporary buffers that will be deleted once we have finished with
229   /// the code-completion results.
230   llvm::SmallVector<const llvm::MemoryBuffer *, 1> TemporaryBuffers;
231 
232   /// \brief Allocator used to store globally cached code-completion results.
233   llvm::IntrusiveRefCntPtr<clang::GlobalCodeCompletionAllocator>
234     CachedCompletionAllocator;
235 
236   /// \brief Allocator used to store code completion results.
237   clang::CodeCompletionAllocator CodeCompletionAllocator;
238 };
239 
240 /// \brief Tracks the number of code-completion result objects that are
241 /// currently active.
242 ///
243 /// Used for debugging purposes only.
244 static llvm::sys::cas_flag CodeCompletionResultObjects;
245 
246 AllocatedCXCodeCompleteResults::AllocatedCXCodeCompleteResults(
247                                       const FileSystemOptions& FileSystemOpts)
248   : CXCodeCompleteResults(),
249     Diag(new Diagnostic(
250                    llvm::IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs))),
251     FileSystemOpts(FileSystemOpts),
252     FileMgr(new FileManager(FileSystemOpts)),
253     SourceMgr(new SourceManager(*Diag, *FileMgr)) {
254   if (getenv("LIBCLANG_OBJTRACKING")) {
255     llvm::sys::AtomicIncrement(&CodeCompletionResultObjects);
256     fprintf(stderr, "+++ %d completion results\n", CodeCompletionResultObjects);
257   }
258 }
259 
260 AllocatedCXCodeCompleteResults::~AllocatedCXCodeCompleteResults() {
261   delete [] Results;
262 
263   for (unsigned I = 0, N = TemporaryFiles.size(); I != N; ++I)
264     TemporaryFiles[I].eraseFromDisk();
265   for (unsigned I = 0, N = TemporaryBuffers.size(); I != N; ++I)
266     delete TemporaryBuffers[I];
267 
268   if (getenv("LIBCLANG_OBJTRACKING")) {
269     llvm::sys::AtomicDecrement(&CodeCompletionResultObjects);
270     fprintf(stderr, "--- %d completion results\n", CodeCompletionResultObjects);
271   }
272 }
273 
274 } // end extern "C"
275 
276 namespace {
277   class CaptureCompletionResults : public CodeCompleteConsumer {
278     AllocatedCXCodeCompleteResults &AllocatedResults;
279     llvm::SmallVector<CXCompletionResult, 16> StoredResults;
280   public:
281     CaptureCompletionResults(AllocatedCXCodeCompleteResults &Results)
282       : CodeCompleteConsumer(true, false, true, false),
283         AllocatedResults(Results) { }
284     ~CaptureCompletionResults() { Finish(); }
285 
286     virtual void ProcessCodeCompleteResults(Sema &S,
287                                             CodeCompletionContext Context,
288                                             CodeCompletionResult *Results,
289                                             unsigned NumResults) {
290       StoredResults.reserve(StoredResults.size() + NumResults);
291       for (unsigned I = 0; I != NumResults; ++I) {
292         CodeCompletionString *StoredCompletion
293           = Results[I].CreateCodeCompletionString(S,
294                                       AllocatedResults.CodeCompletionAllocator);
295 
296         CXCompletionResult R;
297         R.CursorKind = Results[I].CursorKind;
298         R.CompletionString = StoredCompletion;
299         StoredResults.push_back(R);
300       }
301     }
302 
303     virtual void ProcessOverloadCandidates(Sema &S, unsigned CurrentArg,
304                                            OverloadCandidate *Candidates,
305                                            unsigned NumCandidates) {
306       StoredResults.reserve(StoredResults.size() + NumCandidates);
307       for (unsigned I = 0; I != NumCandidates; ++I) {
308         CodeCompletionString *StoredCompletion
309           = Candidates[I].CreateSignatureString(CurrentArg, S,
310                                       AllocatedResults.CodeCompletionAllocator);
311 
312         CXCompletionResult R;
313         R.CursorKind = CXCursor_NotImplemented;
314         R.CompletionString = StoredCompletion;
315         StoredResults.push_back(R);
316       }
317     }
318 
319     virtual CodeCompletionAllocator &getAllocator() {
320       return AllocatedResults.CodeCompletionAllocator;
321     }
322 
323   private:
324     void Finish() {
325       AllocatedResults.Results = new CXCompletionResult [StoredResults.size()];
326       AllocatedResults.NumResults = StoredResults.size();
327       std::memcpy(AllocatedResults.Results, StoredResults.data(),
328                   StoredResults.size() * sizeof(CXCompletionResult));
329       StoredResults.clear();
330     }
331   };
332 }
333 
334 extern "C" {
335 struct CodeCompleteAtInfo {
336   CXTranslationUnit TU;
337   const char *complete_filename;
338   unsigned complete_line;
339   unsigned complete_column;
340   struct CXUnsavedFile *unsaved_files;
341   unsigned num_unsaved_files;
342   unsigned options;
343   CXCodeCompleteResults *result;
344 };
345 void clang_codeCompleteAt_Impl(void *UserData) {
346   CodeCompleteAtInfo *CCAI = static_cast<CodeCompleteAtInfo*>(UserData);
347   CXTranslationUnit TU = CCAI->TU;
348   const char *complete_filename = CCAI->complete_filename;
349   unsigned complete_line = CCAI->complete_line;
350   unsigned complete_column = CCAI->complete_column;
351   struct CXUnsavedFile *unsaved_files = CCAI->unsaved_files;
352   unsigned num_unsaved_files = CCAI->num_unsaved_files;
353   unsigned options = CCAI->options;
354   CCAI->result = 0;
355 
356 #ifdef UDP_CODE_COMPLETION_LOGGER
357 #ifdef UDP_CODE_COMPLETION_LOGGER_PORT
358   const llvm::TimeRecord &StartTime =  llvm::TimeRecord::getCurrentTime();
359 #endif
360 #endif
361 
362   bool EnableLogging = getenv("LIBCLANG_CODE_COMPLETION_LOGGING") != 0;
363 
364   ASTUnit *AST = static_cast<ASTUnit *>(TU->TUData);
365   if (!AST)
366     return;
367 
368   ASTUnit::ConcurrencyCheck Check(*AST);
369 
370   // Perform the remapping of source files.
371   llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles;
372   for (unsigned I = 0; I != num_unsaved_files; ++I) {
373     llvm::StringRef Data(unsaved_files[I].Contents, unsaved_files[I].Length);
374     const llvm::MemoryBuffer *Buffer
375       = llvm::MemoryBuffer::getMemBufferCopy(Data, unsaved_files[I].Filename);
376     RemappedFiles.push_back(std::make_pair(unsaved_files[I].Filename,
377                                            Buffer));
378   }
379 
380   if (EnableLogging) {
381     // FIXME: Add logging.
382   }
383 
384   // Parse the resulting source file to find code-completion results.
385   AllocatedCXCodeCompleteResults *Results =
386         new AllocatedCXCodeCompleteResults(AST->getFileSystemOpts());
387   Results->Results = 0;
388   Results->NumResults = 0;
389 
390   // Create a code-completion consumer to capture the results.
391   CaptureCompletionResults Capture(*Results);
392 
393   // Perform completion.
394   AST->CodeComplete(complete_filename, complete_line, complete_column,
395                     RemappedFiles.data(), RemappedFiles.size(),
396                     (options & CXCodeComplete_IncludeMacros),
397                     (options & CXCodeComplete_IncludeCodePatterns),
398                     Capture,
399                     *Results->Diag, Results->LangOpts, *Results->SourceMgr,
400                     *Results->FileMgr, Results->Diagnostics,
401                     Results->TemporaryBuffers);
402 
403   // Keep a reference to the allocator used for cached global completions, so
404   // that we can be sure that the memory used by our code completion strings
405   // doesn't get freed due to subsequent reparses (while the code completion
406   // results are still active).
407   Results->CachedCompletionAllocator = AST->getCachedCompletionAllocator();
408 
409 
410 
411 #ifdef UDP_CODE_COMPLETION_LOGGER
412 #ifdef UDP_CODE_COMPLETION_LOGGER_PORT
413   const llvm::TimeRecord &EndTime =  llvm::TimeRecord::getCurrentTime();
414   llvm::SmallString<256> LogResult;
415   llvm::raw_svector_ostream os(LogResult);
416 
417   // Figure out the language and whether or not it uses PCH.
418   const char *lang = 0;
419   bool usesPCH = false;
420 
421   for (std::vector<const char*>::iterator I = argv.begin(), E = argv.end();
422        I != E; ++I) {
423     if (*I == 0)
424       continue;
425     if (strcmp(*I, "-x") == 0) {
426       if (I + 1 != E) {
427         lang = *(++I);
428         continue;
429       }
430     }
431     else if (strcmp(*I, "-include") == 0) {
432       if (I+1 != E) {
433         const char *arg = *(++I);
434         llvm::SmallString<512> pchName;
435         {
436           llvm::raw_svector_ostream os(pchName);
437           os << arg << ".pth";
438         }
439         pchName.push_back('\0');
440         struct stat stat_results;
441         if (stat(pchName.data(), &stat_results) == 0)
442           usesPCH = true;
443         continue;
444       }
445     }
446   }
447 
448   os << "{ ";
449   os << "\"wall\": " << (EndTime.getWallTime() - StartTime.getWallTime());
450   os << ", \"numRes\": " << Results->NumResults;
451   os << ", \"diags\": " << Results->Diagnostics.size();
452   os << ", \"pch\": " << (usesPCH ? "true" : "false");
453   os << ", \"lang\": \"" << (lang ? lang : "<unknown>") << '"';
454   const char *name = getlogin();
455   os << ", \"user\": \"" << (name ? name : "unknown") << '"';
456   os << ", \"clangVer\": \"" << getClangFullVersion() << '"';
457   os << " }";
458 
459   llvm::StringRef res = os.str();
460   if (res.size() > 0) {
461     do {
462       // Setup the UDP socket.
463       struct sockaddr_in servaddr;
464       bzero(&servaddr, sizeof(servaddr));
465       servaddr.sin_family = AF_INET;
466       servaddr.sin_port = htons(UDP_CODE_COMPLETION_LOGGER_PORT);
467       if (inet_pton(AF_INET, UDP_CODE_COMPLETION_LOGGER,
468                     &servaddr.sin_addr) <= 0)
469         break;
470 
471       int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
472       if (sockfd < 0)
473         break;
474 
475       sendto(sockfd, res.data(), res.size(), 0,
476              (struct sockaddr *)&servaddr, sizeof(servaddr));
477       close(sockfd);
478     }
479     while (false);
480   }
481 #endif
482 #endif
483   CCAI->result = Results;
484 }
485 CXCodeCompleteResults *clang_codeCompleteAt(CXTranslationUnit TU,
486                                             const char *complete_filename,
487                                             unsigned complete_line,
488                                             unsigned complete_column,
489                                             struct CXUnsavedFile *unsaved_files,
490                                             unsigned num_unsaved_files,
491                                             unsigned options) {
492   CodeCompleteAtInfo CCAI = { TU, complete_filename, complete_line,
493                               complete_column, unsaved_files, num_unsaved_files,
494                               options, 0 };
495   llvm::CrashRecoveryContext CRC;
496 
497   if (!RunSafely(CRC, clang_codeCompleteAt_Impl, &CCAI)) {
498     fprintf(stderr, "libclang: crash detected in code completion\n");
499     static_cast<ASTUnit *>(TU->TUData)->setUnsafeToFree(true);
500     return 0;
501   }
502 
503   return CCAI.result;
504 }
505 
506 unsigned clang_defaultCodeCompleteOptions(void) {
507   return CXCodeComplete_IncludeMacros;
508 }
509 
510 void clang_disposeCodeCompleteResults(CXCodeCompleteResults *ResultsIn) {
511   if (!ResultsIn)
512     return;
513 
514   AllocatedCXCodeCompleteResults *Results
515     = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn);
516   delete Results;
517 }
518 
519 unsigned
520 clang_codeCompleteGetNumDiagnostics(CXCodeCompleteResults *ResultsIn) {
521   AllocatedCXCodeCompleteResults *Results
522     = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn);
523   if (!Results)
524     return 0;
525 
526   return Results->Diagnostics.size();
527 }
528 
529 CXDiagnostic
530 clang_codeCompleteGetDiagnostic(CXCodeCompleteResults *ResultsIn,
531                                 unsigned Index) {
532   AllocatedCXCodeCompleteResults *Results
533     = static_cast<AllocatedCXCodeCompleteResults*>(ResultsIn);
534   if (!Results || Index >= Results->Diagnostics.size())
535     return 0;
536 
537   return new CXStoredDiagnostic(Results->Diagnostics[Index], Results->LangOpts);
538 }
539 
540 
541 } // end extern "C"
542 
543 /// \brief Simple utility function that appends a \p New string to the given
544 /// \p Old string, using the \p Buffer for storage.
545 ///
546 /// \param Old The string to which we are appending. This parameter will be
547 /// updated to reflect the complete string.
548 ///
549 ///
550 /// \param New The string to append to \p Old.
551 ///
552 /// \param Buffer A buffer that stores the actual, concatenated string. It will
553 /// be used if the old string is already-non-empty.
554 static void AppendToString(llvm::StringRef &Old, llvm::StringRef New,
555                            llvm::SmallString<256> &Buffer) {
556   if (Old.empty()) {
557     Old = New;
558     return;
559   }
560 
561   if (Buffer.empty())
562     Buffer.append(Old.begin(), Old.end());
563   Buffer.append(New.begin(), New.end());
564   Old = Buffer.str();
565 }
566 
567 /// \brief Get the typed-text blocks from the given code-completion string
568 /// and return them as a single string.
569 ///
570 /// \param String The code-completion string whose typed-text blocks will be
571 /// concatenated.
572 ///
573 /// \param Buffer A buffer used for storage of the completed name.
574 static llvm::StringRef GetTypedName(CodeCompletionString *String,
575                                     llvm::SmallString<256> &Buffer) {
576   llvm::StringRef Result;
577   for (CodeCompletionString::iterator C = String->begin(), CEnd = String->end();
578        C != CEnd; ++C) {
579     if (C->Kind == CodeCompletionString::CK_TypedText)
580       AppendToString(Result, C->Text, Buffer);
581   }
582 
583   return Result;
584 }
585 
586 namespace {
587   struct OrderCompletionResults {
588     bool operator()(const CXCompletionResult &XR,
589                     const CXCompletionResult &YR) const {
590       CodeCompletionString *X
591         = (CodeCompletionString *)XR.CompletionString;
592       CodeCompletionString *Y
593         = (CodeCompletionString *)YR.CompletionString;
594 
595       llvm::SmallString<256> XBuffer;
596       llvm::StringRef XText = GetTypedName(X, XBuffer);
597       llvm::SmallString<256> YBuffer;
598       llvm::StringRef YText = GetTypedName(Y, YBuffer);
599 
600       if (XText.empty() || YText.empty())
601         return !XText.empty();
602 
603       int result = XText.compare_lower(YText);
604       if (result < 0)
605         return true;
606       if (result > 0)
607         return false;
608 
609       result = XText.compare(YText);
610       return result < 0;
611     }
612   };
613 }
614 
615 extern "C" {
616   void clang_sortCodeCompletionResults(CXCompletionResult *Results,
617                                        unsigned NumResults) {
618     std::stable_sort(Results, Results + NumResults, OrderCompletionResults());
619   }
620 }
621