[clang] c2c36c4 - [clang][index] Fix a crash for accessing a null field decl.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Thu May 21 00:48:34 PDT 2020


Author: Haojian Wu
Date: 2020-05-21T09:47:36+02:00
New Revision: c2c36c4f4b69ade6d8610b1dc98ff9f02c94320d

URL: https://github.com/llvm/llvm-project/commit/c2c36c4f4b69ade6d8610b1dc98ff9f02c94320d
DIFF: https://github.com/llvm/llvm-project/commit/c2c36c4f4b69ade6d8610b1dc98ff9f02c94320d.diff

LOG: [clang][index] Fix a crash for accessing a null field decl.

getField() may return a nullptr, we already did that in
BodyIndexer::VisitDesignatedInitExpr, but missed one place.

Added: 
    clang/test/Index/index-designated-init-recovery.cpp

Modified: 
    clang/lib/Index/IndexBody.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Index/IndexBody.cpp b/clang/lib/Index/IndexBody.cpp
index 07a94f30c883..01cf559d7057 100644
--- a/clang/lib/Index/IndexBody.cpp
+++ b/clang/lib/Index/IndexBody.cpp
@@ -414,7 +414,7 @@ class BodyIndexer : public RecursiveASTVisitor<BodyIndexer> {
 
     auto visitSyntacticDesignatedInitExpr = [&](DesignatedInitExpr *E) -> bool {
       for (DesignatedInitExpr::Designator &D : llvm::reverse(E->designators())) {
-        if (D.isFieldDesignator())
+        if (D.isFieldDesignator() && D.getField())
           return IndexCtx.handleReference(D.getField(), D.getFieldLoc(),
                                           Parent, ParentDC, SymbolRoleSet(),
                                           {}, E);

diff  --git a/clang/test/Index/index-designated-init-recovery.cpp b/clang/test/Index/index-designated-init-recovery.cpp
new file mode 100644
index 000000000000..182a1aeeb8e6
--- /dev/null
+++ b/clang/test/Index/index-designated-init-recovery.cpp
@@ -0,0 +1,8 @@
+struct Bar {};
+struct Foo {
+  void method(Bar bar) {}
+};
+void NoCrash(Foo t) {
+  t.method({.abc = 50}); // CHECK: field designator 'abc' does not refer to any field in type 'Bar'
+}
+// RUN: c-index-test -index-file %s -Xclang -frecovery-ast 2>&1 | FileCheck %s


        


More information about the cfe-commits mailing list