[PATCH] D47896: [CodeComplete] suppress define X X macros

Sam McCall via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 7 11:30:56 PDT 2018


sammccall created this revision.
sammccall added a reviewer: ilya-biryukov.
Herald added a subscriber: cfe-commits.

In particular, stderr etc where the equivalent symbols exist in the global
namespace. Having the symbol instead of the macro helps with ranking, and avoids
the current duplicate stderr suggestions.

@ilya-biryukov: think this is the right way to fix the duplicate completions?
Hiding the shadowed decl might be more correct, but it'd be hard *and* give
unfortunate ranking.


Repository:
  rC Clang

https://reviews.llvm.org/D47896

Files:
  lib/Sema/SemaCodeComplete.cpp
  test/Index/complete-macros.c


Index: test/Index/complete-macros.c
===================================================================
--- test/Index/complete-macros.c
+++ test/Index/complete-macros.c
@@ -25,6 +25,12 @@
   
 }
 
+int shadow;
+#define shadow shadow
+void test_shadow() {
+
+}
+
 // RUN: c-index-test -code-completion-at=%s:7:1 %s -I%S | FileCheck -check-prefix=CHECK-CC0 %s
 // CHECK-CC0-NOT: FOO
 // RUN: env CINDEXTEST_EDITING=1 CINDEXTEST_COMPLETION_CACHING=1 c-index-test -code-completion-at=%s:7:1 %s -I%S | FileCheck -check-prefix=CHECK-CC1 %s
@@ -45,3 +51,7 @@
 
 // RUN: env CINDEXTEST_EDITING=1 CINDEXTEST_COMPLETION_CACHING=1 c-index-test -code-completion-at=%s:15:5 %s -I%S | FileCheck -check-prefix=CHECK-CC4 %s
 // CHECK-CC4-NOT: COMPLETE_MACROS_H_GUARD
+
+// RUN: c-index-test -code-completion-at=%s:31:1 %s -I%S | FileCheck -check-prefix=CHECK-SHADOW %s
+// CHECK-SHADOW: FunctionDecl:{ResultType void}{TypedText g}
+// CHECK-SHADOW-NOT: macro definition:{TypedText shadow}
Index: lib/Sema/SemaCodeComplete.cpp
===================================================================
--- lib/Sema/SemaCodeComplete.cpp
+++ lib/Sema/SemaCodeComplete.cpp
@@ -3308,9 +3308,18 @@
        M != MEnd; ++M) {
     auto MD = PP.getMacroDefinition(M->first);
     if (IncludeUndefined || MD) {
-      if (MacroInfo *MI = MD.getMacroInfo())
+      if (MacroInfo *MI = MD.getMacroInfo()) {
         if (MI->isUsedForHeaderGuard())
           continue;
+        // Some standard libraries e.g. #define stderr stderr, the underlying
+        // decl is more useful.
+        if (MI->getNumTokens() == 1 && MI->isObjectLike()) {
+          const Token &Tok = MI->getReplacementToken(0);
+          if (Tok.is(tok::identifier) &&
+              Tok.getIdentifierInfo()->getName() == M->first->getName())
+            continue;
+        }
+      }
 
       Results.AddResult(Result(M->first,
                              getMacroUsagePriority(M->first->getName(),


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D47896.150369.patch
Type: text/x-patch
Size: 1943 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180607/0c10c5f3/attachment.bin>


More information about the cfe-commits mailing list