r336427 - [SemaCodeComplete] Expose a method to create CodeCompletionString for macros.

Eric Liu via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 6 02:43:57 PDT 2018


Author: ioeric
Date: Fri Jul  6 02:43:57 2018
New Revision: 336427

URL: http://llvm.org/viewvc/llvm-project?rev=336427&view=rev
Log:
[SemaCodeComplete] Expose a method to create CodeCompletionString for macros.

Summary:
The method only takes PPreprocessor and don't require structures that
might not be available (e.g. Sema and ASTContext) when CodeCompletionString
needs to be generated for macros.

Reviewers: sammccall

Reviewed By: sammccall

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D48973

Modified:
    cfe/trunk/include/clang/Sema/CodeCompleteConsumer.h
    cfe/trunk/lib/Sema/SemaCodeComplete.cpp

Modified: cfe/trunk/include/clang/Sema/CodeCompleteConsumer.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/CodeCompleteConsumer.h?rev=336427&r1=336426&r2=336427&view=diff
==============================================================================
--- cfe/trunk/include/clang/Sema/CodeCompleteConsumer.h (original)
+++ cfe/trunk/include/clang/Sema/CodeCompleteConsumer.h Fri Jul  6 02:43:57 2018
@@ -918,6 +918,13 @@ public:
                                            CodeCompletionAllocator &Allocator,
                                            CodeCompletionTUInfo &CCTUInfo,
                                            bool IncludeBriefComments);
+  /// Creates a new code-completion string for the macro result. Similar to the
+  /// above overloads, except this only requires preprocessor information.
+  /// The result kind must be `RK_Macro`.
+  CodeCompletionString *
+  CreateCodeCompletionStringForMacro(Preprocessor &PP,
+                                     CodeCompletionAllocator &Allocator,
+                                     CodeCompletionTUInfo &CCTUInfo);
 
   /// Retrieve the name that should be used to order a result.
   ///

Modified: cfe/trunk/lib/Sema/SemaCodeComplete.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaCodeComplete.cpp?rev=336427&r1=336426&r2=336427&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaCodeComplete.cpp (original)
+++ cfe/trunk/lib/Sema/SemaCodeComplete.cpp Fri Jul  6 02:43:57 2018
@@ -2745,6 +2745,52 @@ CodeCompletionString *CodeCompletionResu
                                     CCTUInfo, IncludeBriefComments);
 }
 
+CodeCompletionString *CodeCompletionResult::CreateCodeCompletionStringForMacro(
+    Preprocessor &PP, CodeCompletionAllocator &Allocator,
+    CodeCompletionTUInfo &CCTUInfo) {
+  assert(Kind == RK_Macro);
+  CodeCompletionBuilder Result(Allocator, CCTUInfo, Priority, Availability);
+  const MacroInfo *MI = PP.getMacroInfo(Macro);
+  Result.AddTypedTextChunk(Result.getAllocator().CopyString(Macro->getName()));
+
+  if (!MI || !MI->isFunctionLike())
+    return Result.TakeString();
+
+  // Format a function-like macro with placeholders for the arguments.
+  Result.AddChunk(CodeCompletionString::CK_LeftParen);
+  MacroInfo::param_iterator A = MI->param_begin(), AEnd = MI->param_end();
+
+  // C99 variadic macros add __VA_ARGS__ at the end. Skip it.
+  if (MI->isC99Varargs()) {
+    --AEnd;
+
+    if (A == AEnd) {
+      Result.AddPlaceholderChunk("...");
+    }
+  }
+
+  for (MacroInfo::param_iterator A = MI->param_begin(); A != AEnd; ++A) {
+    if (A != MI->param_begin())
+      Result.AddChunk(CodeCompletionString::CK_Comma);
+
+    if (MI->isVariadic() && (A + 1) == AEnd) {
+      SmallString<32> Arg = (*A)->getName();
+      if (MI->isC99Varargs())
+        Arg += ", ...";
+      else
+        Arg += "...";
+      Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg));
+      break;
+    }
+
+    // Non-variadic macros are simple.
+    Result.AddPlaceholderChunk(
+        Result.getAllocator().CopyString((*A)->getName()));
+  }
+  Result.AddChunk(CodeCompletionString::CK_RightParen);
+  return Result.TakeString();
+}
+
 /// If possible, create a new code completion string for the given
 /// result.
 ///
@@ -2758,6 +2804,9 @@ CodeCompletionResult::CreateCodeCompleti
                                            CodeCompletionAllocator &Allocator,
                                            CodeCompletionTUInfo &CCTUInfo,
                                            bool IncludeBriefComments) {
+  if (Kind == RK_Macro)
+    return CreateCodeCompletionStringForMacro(PP, Allocator, CCTUInfo);
+
   CodeCompletionBuilder Result(Allocator, CCTUInfo, Priority, Availability);
 
   PrintingPolicy Policy = getCompletionPrintingPolicy(Ctx, PP);
@@ -2782,50 +2831,6 @@ CodeCompletionResult::CreateCodeCompleti
     Result.AddTypedTextChunk(Keyword);
     return Result.TakeString();
   }
-
-  if (Kind == RK_Macro) {
-    const MacroInfo *MI = PP.getMacroInfo(Macro);
-    Result.AddTypedTextChunk(
-                            Result.getAllocator().CopyString(Macro->getName()));
-
-    if (!MI || !MI->isFunctionLike())
-      return Result.TakeString();
-
-    // Format a function-like macro with placeholders for the arguments.
-    Result.AddChunk(CodeCompletionString::CK_LeftParen);
-    MacroInfo::param_iterator A = MI->param_begin(), AEnd = MI->param_end();
-
-    // C99 variadic macros add __VA_ARGS__ at the end. Skip it.
-    if (MI->isC99Varargs()) {
-      --AEnd;
-
-      if (A == AEnd) {
-        Result.AddPlaceholderChunk("...");
-      }
-    }
-
-    for (MacroInfo::param_iterator A = MI->param_begin(); A != AEnd; ++A) {
-      if (A != MI->param_begin())
-        Result.AddChunk(CodeCompletionString::CK_Comma);
-
-      if (MI->isVariadic() && (A+1) == AEnd) {
-        SmallString<32> Arg = (*A)->getName();
-        if (MI->isC99Varargs())
-          Arg += ", ...";
-        else
-          Arg += "...";
-        Result.AddPlaceholderChunk(Result.getAllocator().CopyString(Arg));
-        break;
-      }
-
-      // Non-variadic macros are simple.
-      Result.AddPlaceholderChunk(
-                          Result.getAllocator().CopyString((*A)->getName()));
-    }
-    Result.AddChunk(CodeCompletionString::CK_RightParen);
-    return Result.TakeString();
-  }
-
   assert(Kind == RK_Declaration && "Missed a result kind?");
   const NamedDecl *ND = Declaration;
   Result.addParentContext(ND->getDeclContext());




More information about the cfe-commits mailing list