From: Ilya Biryukov Date: Thu, 13 Dec 2018 15:36:32 +0000 (+0000) Subject: [CodeComplete] Set preferred type to bool on conditions X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=4110967c7b6184b16a7c4ddffa99c7e3351ab520;p=platform%2Fupstream%2Fllvm.git [CodeComplete] Set preferred type to bool on conditions Reviewers: kadircet Reviewed By: kadircet Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D55431 llvm-svn: 349050 --- diff --git a/clang/lib/Sema/CodeCompleteConsumer.cpp b/clang/lib/Sema/CodeCompleteConsumer.cpp index 40db849..92e65c4 100644 --- a/clang/lib/Sema/CodeCompleteConsumer.cpp +++ b/clang/lib/Sema/CodeCompleteConsumer.cpp @@ -539,9 +539,12 @@ void PrintingCodeCompleteConsumer::ProcessCodeCompleteResults( 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; diff --git a/clang/lib/Sema/SemaCodeComplete.cpp b/clang/lib/Sema/SemaCodeComplete.cpp index 6d310ca..11c5e63 100644 --- a/clang/lib/Sema/SemaCodeComplete.cpp +++ b/clang/lib/Sema/SemaCodeComplete.cpp @@ -3521,7 +3521,7 @@ static void HandleCodeCompleteResults(Sema *S, 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 @@ mapCodeCompletionContext(Sema &S, Sema::ParserCompletionContext PCC) { 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; diff --git a/clang/test/CodeCompletion/preferred-type.cpp b/clang/test/CodeCompletion/preferred-type.cpp new file mode 100644 index 0000000..5048dfa --- /dev/null +++ b/clang/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); +}