[CodeComplete] Fix crash when completing params function declarations.
authorSam McCall <sam.mccall@gmail.com>
Wed, 10 Oct 2018 10:51:48 +0000 (10:51 +0000)
committerSam McCall <sam.mccall@gmail.com>
Wed, 10 Oct 2018 10:51:48 +0000 (10:51 +0000)
Summary:
In a decl like `int AA(BB cc)` where BB isn't defined, we end up trying to
parse `BB cc` as an expression (vexing parse) and end up triggering the
parser's "recovery-in-function" completion with no actual function
scope.

This patch avoids the assumption that such a scope exists in this context.

Reviewers: kadircet

Subscribers: cfe-commits

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

llvm-svn: 344133

clang/lib/Sema/SemaCodeComplete.cpp
clang/test/CodeCompletion/crash-func-decl.cpp [new file with mode: 0644]

index 33bb122..bdf538b 100644 (file)
@@ -1881,7 +1881,8 @@ static void AddOrdinaryNameResults(Sema::ParserCompletionContext CCC,
     }
 
     // Switch-specific statements.
-    if (!SemaRef.getCurFunction()->SwitchStack.empty()) {
+    if (SemaRef.getCurFunction() &&
+        !SemaRef.getCurFunction()->SwitchStack.empty()) {
       // case expression:
       Builder.AddTypedTextChunk("case");
       Builder.AddChunk(CodeCompletionString::CK_HorizontalSpace);
diff --git a/clang/test/CodeCompletion/crash-func-decl.cpp b/clang/test/CodeCompletion/crash-func-decl.cpp
new file mode 100644 (file)
index 0000000..7abcdcd
--- /dev/null
@@ -0,0 +1,5 @@
+// Important that BB is unknown.
+// This triggers completion in PCC_RecoveryInFunction context, with no function.
+int AA(BB cc);
+// RUN: not %clang_cc1 -fsyntax-only -code-completion-at=%s:3:12 %s | FileCheck %s
+// CHECK: COMPLETION: char