]> granicus.if.org Git - clang/commitdiff
Fixed a crash in code completion.
authorIlya Biryukov <ibiryukov@google.com>
Fri, 8 Sep 2017 13:36:38 +0000 (13:36 +0000)
committerIlya Biryukov <ibiryukov@google.com>
Fri, 8 Sep 2017 13:36:38 +0000 (13:36 +0000)
Summary: The crash occured when FunctionDecl was parsed with an initializer.

Reviewers: bkramer, klimek, francisco.lopes

Reviewed By: bkramer

Subscribers: cfe-commits

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@312788 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Parse/ParseDecl.cpp
test/CodeCompletion/crash-func-init.cpp [new file with mode: 0644]

index b20b0db7d30a469a8b7d95f7f157de3ce47c8073..15fefaa5a663ea27614133cc3607368b7d93de26 100644 (file)
@@ -2264,11 +2264,23 @@ Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(
       Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
     }
 
-    if (ParseExpressionList(Exprs, CommaLocs, [&] {
-          Actions.CodeCompleteConstructor(getCurScope(),
-                 cast<VarDecl>(ThisDecl)->getType()->getCanonicalTypeInternal(),
-                                          ThisDecl->getLocation(), Exprs);
-       })) {
+    llvm::function_ref<void()> ExprListCompleter;
+    auto ThisVarDecl = dyn_cast_or_null<VarDecl>(ThisDecl);
+    auto ConstructorCompleter = [&, ThisVarDecl] {
+      Actions.CodeCompleteConstructor(
+          getCurScope(), ThisVarDecl->getType()->getCanonicalTypeInternal(),
+          ThisDecl->getLocation(), Exprs);
+    };
+    if (ThisVarDecl) {
+      // ParseExpressionList can sometimes succeed even when ThisDecl is not
+      // VarDecl. This is an error and it is reported in a call to
+      // Actions.ActOnInitializerError(). However, we call
+      // CodeCompleteConstructor only on VarDecls, falling back to default
+      // completer in other cases.
+      ExprListCompleter = ConstructorCompleter;
+    }
+
+    if (ParseExpressionList(Exprs, CommaLocs, ExprListCompleter)) {
       Actions.ActOnInitializerError(ThisDecl);
       SkipUntil(tok::r_paren, StopAtSemi);
 
diff --git a/test/CodeCompletion/crash-func-init.cpp b/test/CodeCompletion/crash-func-init.cpp
new file mode 100644 (file)
index 0000000..6d84b84
--- /dev/null
@@ -0,0 +1,4 @@
+int (*foo(int a))(flo
+// RUN: %clang_cc1 -fsyntax-only -code-completion-at=%s:1:21 %s -o - \
+// RUN:            | FileCheck %s
+// CHECK: COMPLETION: float