[PATCH] D55431: [CodeComplete] Set preferred type to bool on conditions

Phabricator via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 13 07:39:54 PST 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rL349050: [CodeComplete] Set preferred type to bool on conditions (authored by ibiryukov, committed by ).
Herald added a subscriber: llvm-commits.

Repository:
  rL LLVM

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D55431/new/

https://reviews.llvm.org/D55431

Files:
  cfe/trunk/lib/Sema/CodeCompleteConsumer.cpp
  cfe/trunk/lib/Sema/SemaCodeComplete.cpp
  cfe/trunk/test/CodeCompletion/preferred-type.cpp


Index: cfe/trunk/test/CodeCompletion/preferred-type.cpp
===================================================================
--- cfe/trunk/test/CodeCompletion/preferred-type.cpp
+++ cfe/trunk/test/CodeCompletion/preferred-type.cpp
@@ -0,0 +1,15 @@
+void test(bool x) {
+  if (x) {}
+  // RUN: %clang_cc1 -fsyntax-only -code-completion-at=%s:2:7 %s | FileCheck %s
+  // CHECK: PREFERRED-TYPE: _Bool
+
+  while (x) {}
+  // RUN: %clang_cc1 -fsyntax-only -code-completion-at=%s:6:10 %s | FileCheck %s
+
+  for (; x;) {}
+  // RUN: %clang_cc1 -fsyntax-only -code-completion-at=%s:9:10 %s | FileCheck %s
+
+  // FIXME(ibiryukov): the condition in do-while is parsed as expression, so we
+  // fail to detect it should be converted to bool.
+  // do {} while (x);
+}
Index: cfe/trunk/lib/Sema/SemaCodeComplete.cpp
===================================================================
--- cfe/trunk/lib/Sema/SemaCodeComplete.cpp
+++ cfe/trunk/lib/Sema/SemaCodeComplete.cpp
@@ -3521,7 +3521,7 @@
     CodeCompleter->ProcessCodeCompleteResults(*S, Context, Results, NumResults);
 }
 
-static enum CodeCompletionContext::Kind
+static CodeCompletionContext
 mapCodeCompletionContext(Sema &S, Sema::ParserCompletionContext PCC) {
   switch (PCC) {
   case Sema::PCC_Namespace:
@@ -3558,8 +3558,10 @@
       return CodeCompletionContext::CCC_Expression;
 
   case Sema::PCC_Expression:
-  case Sema::PCC_Condition:
     return CodeCompletionContext::CCC_Expression;
+  case Sema::PCC_Condition:
+    return CodeCompletionContext(CodeCompletionContext::CCC_Expression,
+                                 S.getASTContext().BoolTy);
 
   case Sema::PCC_Statement:
     return CodeCompletionContext::CCC_Statement;
Index: cfe/trunk/lib/Sema/CodeCompleteConsumer.cpp
===================================================================
--- cfe/trunk/lib/Sema/CodeCompleteConsumer.cpp
+++ cfe/trunk/lib/Sema/CodeCompleteConsumer.cpp
@@ -539,9 +539,12 @@
     unsigned NumResults) {
   std::stable_sort(Results, Results + NumResults);
 
-  StringRef Filter = SemaRef.getPreprocessor().getCodeCompletionFilter();
+  if (!Context.getPreferredType().isNull())
+    OS << "PREFERRED-TYPE: " << Context.getPreferredType().getAsString()
+       << "\n";
 
-  // Print the results.
+  StringRef Filter = SemaRef.getPreprocessor().getCodeCompletionFilter();
+  // Print the completions.
   for (unsigned I = 0; I != NumResults; ++I) {
     if (!Filter.empty() && isResultFilteredOut(Filter, Results[I]))
       continue;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D55431.178067.patch
Type: text/x-patch
Size: 2488 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181213/e569157d/attachment.bin>


More information about the llvm-commits mailing list