During code completion, give the "nil" and "NULL" macros the same
priority as other constants. And, if we're in a place where we prefer
a pointer type, consider "nil" and "NULL" to be close matches.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@107910 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/SemaCodeComplete.cpp b/lib/Sema/SemaCodeComplete.cpp
index 2877d52..3e48253 100644
--- a/lib/Sema/SemaCodeComplete.cpp
+++ b/lib/Sema/SemaCodeComplete.cpp
@@ -2174,12 +2174,25 @@
   };
 }
 
-static void AddMacroResults(Preprocessor &PP, ResultBuilder &Results) {
+static void AddMacroResults(Preprocessor &PP, ResultBuilder &Results,
+                            bool TargetTypeIsPointer = false) {
+  typedef CodeCompleteConsumer::Result Result;
+  
   Results.EnterNewScope();
   for (Preprocessor::macro_iterator M = PP.macro_begin(), 
                                  MEnd = PP.macro_end();
-       M != MEnd; ++M)
-    Results.AddResult(M->first);
+       M != MEnd; ++M) {
+    unsigned Priority = CCP_Macro;
+    
+    // Treat the "nil" and "NULL" macros as null pointer constants.
+    if (M->first->isStr("nil") || M->first->isStr("NULL")) {
+      Priority = CCP_Constant;
+      if (TargetTypeIsPointer)
+        Priority = Priority / CCF_SimilarTypeMatch;
+    }
+      
+    Results.AddResult(Result(M->first, Priority));
+  }
   Results.ExitScope();
 }
 
@@ -2261,8 +2274,13 @@
   AddOrdinaryNameResults(CCC_Expression, S, *this, Results);
   Results.ExitScope();
   
+  bool PreferredTypeIsPointer = false;
+  if (!T.isNull())
+    PreferredTypeIsPointer = T->isAnyPointerType() || 
+      T->isMemberPointerType() || T->isBlockPointerType();
+  
   if (CodeCompleter->includeMacros())
-    AddMacroResults(PP, Results);
+    AddMacroResults(PP, Results, PreferredTypeIsPointer);
   HandleCodeCompleteResults(this, CodeCompleter, Results.data(),Results.size());
 }
 
diff --git a/test/Index/complete-macros.c b/test/Index/complete-macros.c
index c33d8c0..9a898e1 100644
--- a/test/Index/complete-macros.c
+++ b/test/Index/complete-macros.c
@@ -2,10 +2,23 @@
 // matter in this test.
 
 #define FOO(Arg1,Arg2) foobar
-
+#define nil 0
 void f() {
 
 }
 
+void g(int);
+
+void f2() {
+  int *ip = nil;
+  ip = nil;
+  g(nil);
+}
+
 // RUN: c-index-test -code-completion-at=%s:7:1 %s | FileCheck -check-prefix=CHECK-CC1 %s
 // CHECK-CC1: macro definition:{TypedText FOO}{LeftParen (}{Placeholder Arg1}{Comma , }{Placeholder Arg2}{RightParen )}
+// RUN: c-index-test -code-completion-at=%s:13:13 %s | FileCheck -check-prefix=CHECK-CC2 %s
+// RUN: c-index-test -code-completion-at=%s:14:8 %s | FileCheck -check-prefix=CHECK-CC2 %s
+// CHECK-CC2: macro definition:{TypedText nil} (30)
+// RUN: c-index-test -code-completion-at=%s:15:5 %s | FileCheck -check-prefix=CHECK-CC3 %s
+// CHECK-CC3: macro definition:{TypedText nil} (60)