r187914 - [PCH] Fix a PCH serialization crash, with invalid code related to forward enum references.

Argyrios Kyrtzidis akyrtzi at gmail.com
Wed Aug 7 14:17:33 PDT 2013


Author: akirtzidis
Date: Wed Aug  7 16:17:33 2013
New Revision: 187914

URL: http://llvm.org/viewvc/llvm-project?rev=187914&view=rev
Log:
[PCH] Fix a PCH serialization crash, with invalid code related to forward enum references.

The problem was that an enum without closing semicolon could be associated as a forward enum
in an erroneous declaration, leading to the identifier being associated with the enum decl but
without a declaration actually referencing it.
This resulted in not having it serialized before serializing the identifier that is associated with.

Also prevent the ASTUnit from querying the serialized DeclID for an invalid top-level decl; it may not
have been serialized.

rdar://14539667

Added:
    cfe/trunk/test/Index/Inputs/preamble-with-error.h
Modified:
    cfe/trunk/lib/Frontend/ASTUnit.cpp
    cfe/trunk/lib/Serialization/ASTWriter.cpp
    cfe/trunk/test/Index/pch-with-errors.m
    cfe/trunk/test/Index/preamble.c

Modified: cfe/trunk/lib/Frontend/ASTUnit.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/ASTUnit.cpp?rev=187914&r1=187913&r2=187914&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/ASTUnit.cpp (original)
+++ cfe/trunk/lib/Frontend/ASTUnit.cpp Wed Aug  7 16:17:33 2013
@@ -1030,9 +1030,13 @@ public:
       // parsing into declaration IDs in the precompiled
       // preamble. This will allow us to deserialize those top-level
       // declarations when requested.
-      for (unsigned I = 0, N = TopLevelDecls.size(); I != N; ++I)
-        Unit.addTopLevelDeclFromPreamble(
-                                      getWriter().getDeclID(TopLevelDecls[I]));
+      for (unsigned I = 0, N = TopLevelDecls.size(); I != N; ++I) {
+        Decl *D = TopLevelDecls[I];
+        // Invalid top-level decls may not have been serialized.
+        if (D->isInvalidDecl())
+          continue;
+        Unit.addTopLevelDeclFromPreamble(getWriter().getDeclID(D));
+      }
 
       Action->setHasEmittedPreamblePCH();
     }

Modified: cfe/trunk/lib/Serialization/ASTWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Serialization/ASTWriter.cpp?rev=187914&r1=187913&r2=187914&view=diff
==============================================================================
--- cfe/trunk/lib/Serialization/ASTWriter.cpp (original)
+++ cfe/trunk/lib/Serialization/ASTWriter.cpp Wed Aug  7 16:17:33 2013
@@ -4047,6 +4047,21 @@ void ASTWriter::WriteASTCore(Sema &SemaR
     GetDeclRef(*I);
   }
 
+  // Make sure all decls associated with an identifier are registered for
+  // serialization.
+  for (IdentifierTable::iterator ID = PP.getIdentifierTable().begin(),
+                              IDEnd = PP.getIdentifierTable().end();
+       ID != IDEnd; ++ID) {
+    const IdentifierInfo *II = ID->second;
+    if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization()) {
+      for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
+                                     DEnd = SemaRef.IdResolver.end();
+           D != DEnd; ++D) {
+        GetDeclRef(*D);
+      }
+    }
+  }
+
   // Resolve any declaration pointers within the declaration updates block.
   ResolveDeclUpdatesBlocks();
   

Added: cfe/trunk/test/Index/Inputs/preamble-with-error.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/Inputs/preamble-with-error.h?rev=187914&view=auto
==============================================================================
--- cfe/trunk/test/Index/Inputs/preamble-with-error.h (added)
+++ cfe/trunk/test/Index/Inputs/preamble-with-error.h Wed Aug  7 16:17:33 2013
@@ -0,0 +1,3 @@
+typedef int Int;
+enum FFF
+extern Int *const www;

Modified: cfe/trunk/test/Index/pch-with-errors.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/pch-with-errors.m?rev=187914&r1=187913&r2=187914&view=diff
==============================================================================
--- cfe/trunk/test/Index/pch-with-errors.m (original)
+++ cfe/trunk/test/Index/pch-with-errors.m Wed Aug  7 16:17:33 2013
@@ -9,6 +9,12 @@
 -(void)meth;
 @end
 
+struct FFF1
+extern I2 *somevar1;
+
+enum FFF2
+extern I2 *somevar2;
+
 #else
 
 void foo(I2 *i) {

Modified: cfe/trunk/test/Index/preamble.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/preamble.c?rev=187914&r1=187913&r2=187914&view=diff
==============================================================================
--- cfe/trunk/test/Index/preamble.c (original)
+++ cfe/trunk/test/Index/preamble.c Wed Aug  7 16:17:33 2013
@@ -1,5 +1,7 @@
 #include "prefix.h"
 #include "preamble.h"
+#include "preamble-with-error.h"
+
 int wibble(int);
 
 void f(int x) {
@@ -14,10 +16,10 @@ void f(int x) {
 // CHECK: preamble.h:4:9: UnexposedExpr=ptr1:3:10 Extent=[4:9 - 4:13]
 // CHECK: preamble.h:4:9: DeclRefExpr=ptr1:3:10 Extent=[4:9 - 4:13]
 // CHECK: preamble.h:5:10: IntegerLiteral= Extent=[5:10 - 5:11]
-// CHECK: preamble.c:3:5: FunctionDecl=wibble:3:5 Extent=[3:1 - 3:16]
-// CHECK: preamble.c:3:15: ParmDecl=:3:15 (Definition) Extent=[3:12 - 3:16]
+// CHECK: preamble.c:5:5: FunctionDecl=wibble:5:5 Extent=[5:1 - 5:16]
+// CHECK: preamble.c:5:15: ParmDecl=:5:15 (Definition) Extent=[5:12 - 5:16]
 // CHECK-DIAG: preamble.h:4:7:{4:9-4:13}: warning: incompatible pointer types assigning to 'int *' from 'float *'
-// RUN: env CINDEXTEST_EDITING=1 c-index-test -code-completion-at=%s:6:1 -I %S/Inputs -include %t %s 2> %t.stderr.txt | FileCheck -check-prefix CHECK-CC %s
+// RUN: env CINDEXTEST_EDITING=1 c-index-test -code-completion-at=%s:8:1 -I %S/Inputs -include %t %s 2> %t.stderr.txt | FileCheck -check-prefix CHECK-CC %s
 // CHECK-CC: FunctionDecl:{ResultType int}{TypedText bar}{LeftParen (}{Placeholder int i}{RightParen )} (50)
 // CHECK-CC: FunctionDecl:{ResultType void}{TypedText f}{LeftParen (}{Placeholder int x}{RightParen )} (50)
 // CHECK-CC: FunctionDecl:{ResultType int}{TypedText foo}{LeftParen (}{Placeholder int}{RightParen )} (50)





More information about the cfe-commits mailing list