[cfe-commits] r112149 - in /cfe/trunk: include/clang-c/Index.h lib/Frontend/ASTUnit.cpp lib/Sema/CodeCompleteConsumer.cpp lib/Sema/SemaCodeComplete.cpp tools/c-index-test/c-index-test.c tools/libclang/CIndexCodeCompletion.cpp tools/libclang/libclang.darwin.exports tools/libclang/libclang.exports

Douglas Gregor dgregor at apple.com
Wed Aug 25 19:23:45 PDT 2010


Author: dgregor
Date: Wed Aug 25 21:23:45 2010
New Revision: 112149

URL: http://llvm.org/viewvc/llvm-project?rev=112149&view=rev
Log:
Move the sorting of code-completion results out of the main path and
into the clients, e.g., the printing code-completion consumer and
c-index-test. Clients may want to re-sort the results anyway.

Provide a libclang function that sorts the results.

Modified:
    cfe/trunk/include/clang-c/Index.h
    cfe/trunk/lib/Frontend/ASTUnit.cpp
    cfe/trunk/lib/Sema/CodeCompleteConsumer.cpp
    cfe/trunk/lib/Sema/SemaCodeComplete.cpp
    cfe/trunk/tools/c-index-test/c-index-test.c
    cfe/trunk/tools/libclang/CIndexCodeCompletion.cpp
    cfe/trunk/tools/libclang/libclang.darwin.exports
    cfe/trunk/tools/libclang/libclang.exports

Modified: cfe/trunk/include/clang-c/Index.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang-c/Index.h?rev=112149&r1=112148&r2=112149&view=diff
==============================================================================
--- cfe/trunk/include/clang-c/Index.h (original)
+++ cfe/trunk/include/clang-c/Index.h Wed Aug 25 21:23:45 2010
@@ -2350,6 +2350,17 @@
                                             unsigned options);
 
 /**
+ * \brief Sort the code-completion results in case-insensitive alphabetical 
+ * order.
+ *
+ * \param Results The set of results to sort.
+ * \param NumResults The number of results in \p Results.
+ */
+CINDEX_LINKAGE
+void clang_sortCodeCompletionResults(CXCompletionResult *Results,
+                                     unsigned NumResults);
+  
+/**
  * \brief Free the given set of code-completion results.
  */
 CINDEX_LINKAGE

Modified: cfe/trunk/lib/Frontend/ASTUnit.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/ASTUnit.cpp?rev=112149&r1=112148&r2=112149&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/ASTUnit.cpp (original)
+++ cfe/trunk/lib/Frontend/ASTUnit.cpp Wed Aug 25 21:23:45 2010
@@ -1678,9 +1678,7 @@
     Next.ProcessCodeCompleteResults(S, Context, Results, NumResults);
     return;
   }
-
-  // Sort the completion results before passing them on to the actual consumer.
-  std::stable_sort(AllResults.begin(), AllResults.end());
+  
   Next.ProcessCodeCompleteResults(S, Context, AllResults.data(),
                                   AllResults.size());
   

Modified: cfe/trunk/lib/Sema/CodeCompleteConsumer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/CodeCompleteConsumer.cpp?rev=112149&r1=112148&r2=112149&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/CodeCompleteConsumer.cpp (original)
+++ cfe/trunk/lib/Sema/CodeCompleteConsumer.cpp Wed Aug 25 21:23:45 2010
@@ -443,6 +443,8 @@
                                                  CodeCompletionContext Context,
                                                  CodeCompletionResult *Results,
                                                          unsigned NumResults) {
+  std::stable_sort(Results, Results + NumResults);
+  
   // Print the results.
   for (unsigned I = 0; I != NumResults; ++I) {
     OS << "COMPLETION: ";
@@ -660,6 +662,11 @@
   if (cmp)
     return cmp < 0;
   
+  // If case-insensitive comparison fails, try case-sensitive comparison.
+  cmp = XStr.compare(YStr);
+  if (cmp)
+    return cmp < 0;
+
   // Non-hidden names precede hidden names.
   if (X.Hidden != Y.Hidden)
     return !X.Hidden;
@@ -695,7 +702,7 @@
                                                        unsigned NumCandidates) {
   for (unsigned I = 0; I != NumCandidates; ++I) {
     WriteUnsigned(OS, CXCursor_NotImplemented);
-    WriteUnsigned(OS, /*Priority=*/0);
+    WriteUnsigned(OS, /*Priority=*/I);
     WriteUnsigned(OS, /*Availability=*/CXAvailability_Available);
     CodeCompletionString *CCS
       = Candidates[I].CreateSignatureString(CurrentArg, SemaRef);

Modified: cfe/trunk/lib/Sema/SemaCodeComplete.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaCodeComplete.cpp?rev=112149&r1=112148&r2=112149&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaCodeComplete.cpp (original)
+++ cfe/trunk/lib/Sema/SemaCodeComplete.cpp Wed Aug 25 21:23:45 2010
@@ -2277,8 +2277,6 @@
                                       CodeCompletionContext Context,
                                       CodeCompletionResult *Results,
                                       unsigned NumResults) {
-  std::stable_sort(Results, Results + NumResults);
-
   if (CodeCompleter)
     CodeCompleter->ProcessCodeCompleteResults(*S, Context, Results, NumResults);
   

Modified: cfe/trunk/tools/c-index-test/c-index-test.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/c-index-test/c-index-test.c?rev=112149&r1=112148&r2=112149&view=diff
==============================================================================
--- cfe/trunk/tools/c-index-test/c-index-test.c (original)
+++ cfe/trunk/tools/c-index-test/c-index-test.c Wed Aug 25 21:23:45 2010
@@ -1,6 +1,7 @@
 /* c-index-test.c */
 
 #include "clang-c/Index.h"
+#include <ctype.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
@@ -903,6 +904,25 @@
   fprintf(file, "\n");
 }
 
+int my_stricmp(const char *s1, const char *s2) {
+  while (*s1 && *s2) {
+    int c1 = tolower(*s1), c2 = tolower(*s2);
+    if (c1 < c2)
+      return -1;
+    else if (c1 > c2)
+      return 1;
+    
+    ++s1;
+    ++s2;
+  }
+  
+  if (*s1)
+    return 1;
+  else if (*s2)
+    return -1;
+  return 0;
+}
+
 int perform_code_completion(int argc, const char **argv, int timing_only) {
   const char *input = argv[1];
   char *filename = 0;
@@ -958,9 +978,13 @@
 
   if (results) {
     unsigned i, n = results->NumResults;
-    if (!timing_only)
+    if (!timing_only) {      
+      /* Sort the code-completion results based on the typed text. */
+      clang_sortCodeCompletionResults(results->Results, results->NumResults);
+
       for (i = 0; i != n; ++i)
         print_completion_result(results->Results + i, stdout);
+    }
     n = clang_codeCompleteGetNumDiagnostics(results);
     for (i = 0; i != n; ++i) {
       CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);

Modified: cfe/trunk/tools/libclang/CIndexCodeCompletion.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndexCodeCompletion.cpp?rev=112149&r1=112148&r2=112149&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndexCodeCompletion.cpp (original)
+++ cfe/trunk/tools/libclang/CIndexCodeCompletion.cpp Wed Aug 25 21:23:45 2010
@@ -582,6 +582,8 @@
         AllocatedResults.Results[I].CompletionString = StoredCompletion;
       }
     }
+    
+    // FIXME: Add ProcessOverloadCandidates?
   };
 }
 
@@ -784,3 +786,36 @@
 
 
 } // end extern "C"
+
+namespace {
+  struct OrderCompletionResults {
+    bool operator()(const CXCompletionResult &XR, 
+                    const CXCompletionResult &YR) const {
+      CXStoredCodeCompletionString *X
+        = (CXStoredCodeCompletionString *)XR.CompletionString;
+      CXStoredCodeCompletionString *Y
+        = (CXStoredCodeCompletionString *)YR.CompletionString;
+      
+      const char *XText = X->getTypedText();
+      const char *YText = Y->getTypedText();
+      if (!XText || !YText)
+        return XText != 0;
+      
+      int result = llvm::StringRef(XText).compare_lower(YText);
+      if (result < 0)
+        return true;
+      if (result > 0)
+        return false;
+      
+      result = llvm::StringRef(XText).compare(YText);
+      return result;
+    }
+  };
+}
+
+extern "C" {
+  void clang_sortCodeCompletionResults(CXCompletionResult *Results,
+                                       unsigned NumResults) {
+    std::stable_sort(Results, Results + NumResults, OrderCompletionResults());
+  }
+}
\ No newline at end of file

Modified: cfe/trunk/tools/libclang/libclang.darwin.exports
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/libclang.darwin.exports?rev=112149&r1=112148&r2=112149&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/libclang.darwin.exports (original)
+++ cfe/trunk/tools/libclang/libclang.darwin.exports Wed Aug 25 21:23:45 2010
@@ -99,5 +99,6 @@
 _clang_reparseTranslationUnit
 _clang_saveTranslationUnit
 _clang_setUseExternalASTGeneration
+_clang_sortCodeCompletionResults
 _clang_tokenize
 _clang_visitChildren

Modified: cfe/trunk/tools/libclang/libclang.exports
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/libclang.exports?rev=112149&r1=112148&r2=112149&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/libclang.exports (original)
+++ cfe/trunk/tools/libclang/libclang.exports Wed Aug 25 21:23:45 2010
@@ -99,5 +99,6 @@
 clang_reparseTranslationUnit
 clang_saveTranslationUnit
 clang_setUseExternalASTGeneration
+clang_sortCodeCompletionResults
 clang_tokenize
 clang_visitChildren





More information about the cfe-commits mailing list