[cfe-commits] r111581 - in /cfe/trunk: test/Index/Inputs/crash-recovery-code-complete-remap.c test/Index/crash-recovery-code-complete.c tools/libclang/CIndex.cpp tools/libclang/CIndexCodeCompletion.cpp

Daniel Dunbar daniel at zuster.org
Thu Aug 19 16:44:10 PDT 2010


Author: ddunbar
Date: Thu Aug 19 18:44:10 2010
New Revision: 111581

URL: http://llvm.org/viewvc/llvm-project?rev=111581&view=rev
Log:
libclang: Execute clang_codeCompleteAt() inside a crash recovery context.
 - Test case is disabled for now, because something isn't write with file
   remapping.

Added:
    cfe/trunk/test/Index/Inputs/crash-recovery-code-complete-remap.c
    cfe/trunk/test/Index/crash-recovery-code-complete.c
Modified:
    cfe/trunk/tools/libclang/CIndex.cpp
    cfe/trunk/tools/libclang/CIndexCodeCompletion.cpp

Added: cfe/trunk/test/Index/Inputs/crash-recovery-code-complete-remap.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/Inputs/crash-recovery-code-complete-remap.c?rev=111581&view=auto
==============================================================================
--- cfe/trunk/test/Index/Inputs/crash-recovery-code-complete-remap.c (added)
+++ cfe/trunk/test/Index/Inputs/crash-recovery-code-complete-remap.c Thu Aug 19 18:44:10 2010
@@ -0,0 +1,12 @@
+// RUN: echo env CINDEXTEST_EDITING=1 \
+// RUN:   not c-index-test -test-load-source-reparse 1 local \
+// RUN:   -remap-file="%s;%S/Inputs/crash-recovery-code-complete-remap.c" \
+// RUN:   %s 2> %t.err
+// RUN: FileCheck < %t.err -check-prefix=CHECK-CODE-COMPLETE-CRASH %s
+// CHECK-CODE-COMPLETE-CRASH: Unable to reparse translation unit
+//
+// XFAIL: win32
+
+#warning parsing original file
+
+#pragma clang __debug crash

Added: cfe/trunk/test/Index/crash-recovery-code-complete.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/crash-recovery-code-complete.c?rev=111581&view=auto
==============================================================================
--- cfe/trunk/test/Index/crash-recovery-code-complete.c (added)
+++ cfe/trunk/test/Index/crash-recovery-code-complete.c Thu Aug 19 18:44:10 2010
@@ -0,0 +1,11 @@
+// RUN: echo env CINDEXTEST_EDITING=1 \
+// RUN:   not c-index-test -code-completion-at=%s:20:1 \
+// RUN:   -remap-file="%s;%S/Inputs/crash-recovery-code-complete-remap.c" \
+// RUN:   %s 2> %t.err
+// RUN: FileCheck < %t.err -check-prefix=CHECK-CODE-COMPLETE-CRASH %s
+// CHECK-CODE-COMPLETE-CRASH: Unable to reparse translation unit
+//
+// XFAIL: win32
+// XFAIL: *
+
+#warning parsing original file

Modified: cfe/trunk/tools/libclang/CIndex.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndex.cpp?rev=111581&r1=111580&r2=111581&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndex.cpp (original)
+++ cfe/trunk/tools/libclang/CIndex.cpp Thu Aug 19 18:44:10 2010
@@ -1221,7 +1221,7 @@
   unsigned options;
   CXTranslationUnit result;
 };
-void clang_parseTranslationUnit_Impl(void *UserData) {
+static void clang_parseTranslationUnit_Impl(void *UserData) {
   ParseTranslationUnitInfo *PTUI =
     static_cast<ParseTranslationUnitInfo*>(UserData);
   CXIndex CIdx = PTUI->CIdx;
@@ -1491,7 +1491,7 @@
   llvm::CrashRecoveryContext CRC;
 
   if (!CRC.RunSafely(clang_parseTranslationUnit_Impl, &PTUI)) {
-    // FIXME: Find a way to report the crash.
+    fprintf(stderr, "libclang: crash detected during parsing");
     return 0;
   }
 
@@ -1532,7 +1532,7 @@
   unsigned options;
   int result;
 };
-void clang_reparseTranslationUnit_Impl(void *UserData) {
+static void clang_reparseTranslationUnit_Impl(void *UserData) {
   ReparseTranslationUnitInfo *RTUI =
     static_cast<ReparseTranslationUnitInfo*>(UserData);
   CXTranslationUnit TU = RTUI->TU;
@@ -1567,7 +1567,7 @@
   llvm::CrashRecoveryContext CRC;
 
   if (!CRC.RunSafely(clang_reparseTranslationUnit_Impl, &RTUI)) {
-    // FIXME: Find a way to report the crash.
+    fprintf(stderr, "libclang: crash detected during reparsing\n");
     static_cast<ASTUnit *>(TU)->setUnsafeToFree(true);
     return 1;
   }

Modified: cfe/trunk/tools/libclang/CIndexCodeCompletion.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CIndexCodeCompletion.cpp?rev=111581&r1=111580&r2=111581&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CIndexCodeCompletion.cpp (original)
+++ cfe/trunk/tools/libclang/CIndexCodeCompletion.cpp Thu Aug 19 18:44:10 2010
@@ -22,6 +22,7 @@
 #include "clang/Sema/CodeCompleteConsumer.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringExtras.h"
+#include "llvm/Support/CrashRecoveryContext.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/Timer.h"
 #include "llvm/Support/raw_ostream.h"
@@ -568,13 +569,27 @@
 }
 
 extern "C" {
-CXCodeCompleteResults *clang_codeCompleteAt(CXTranslationUnit TU,
-                                            const char *complete_filename,
-                                            unsigned complete_line,
-                                            unsigned complete_column,
-                                            struct CXUnsavedFile *unsaved_files,
-                                            unsigned num_unsaved_files,
-                                            unsigned options) {
+struct CodeCompleteAtInfo {
+  CXTranslationUnit TU;
+  const char *complete_filename;
+  unsigned complete_line;
+  unsigned complete_column;
+  struct CXUnsavedFile *unsaved_files;
+  unsigned num_unsaved_files;
+  unsigned options;
+  CXCodeCompleteResults *result;
+};
+void clang_codeCompleteAt_Impl(void *UserData) {
+  CodeCompleteAtInfo *CCAI = static_cast<CodeCompleteAtInfo*>(UserData);
+  CXTranslationUnit TU = CCAI->TU;
+  const char *complete_filename = CCAI->complete_filename;
+  unsigned complete_line = CCAI->complete_line;
+  unsigned complete_column = CCAI->complete_column;
+  struct CXUnsavedFile *unsaved_files = CCAI->unsaved_files;
+  unsigned num_unsaved_files = CCAI->num_unsaved_files;
+  unsigned options = CCAI->options;
+  CCAI->result = 0;
+
 #ifdef UDP_CODE_COMPLETION_LOGGER
 #ifdef UDP_CODE_COMPLETION_LOGGER_PORT
   const llvm::TimeRecord &StartTime =  llvm::TimeRecord::getCurrentTime();
@@ -585,7 +600,7 @@
   
   ASTUnit *AST = static_cast<ASTUnit *>(TU);
   if (!AST)
-    return 0;
+    return;
 
   // Perform the remapping of source files.
   llvm::SmallVector<ASTUnit::RemappedFile, 4> RemappedFiles;
@@ -697,7 +712,27 @@
   }
 #endif
 #endif
-  return Results;
+  CCAI->result = Results;
+}
+CXCodeCompleteResults *clang_codeCompleteAt(CXTranslationUnit TU,
+                                            const char *complete_filename,
+                                            unsigned complete_line,
+                                            unsigned complete_column,
+                                            struct CXUnsavedFile *unsaved_files,
+                                            unsigned num_unsaved_files,
+                                            unsigned options) {
+  CodeCompleteAtInfo CCAI = { TU, complete_filename, complete_line,
+                              complete_column, unsaved_files, num_unsaved_files,
+                              options, 0 };
+  llvm::CrashRecoveryContext CRC;
+
+  if (!CRC.RunSafely(clang_codeCompleteAt_Impl, &CCAI)) {
+    fprintf(stderr, "libclang: crash detected in code completion\n");
+    static_cast<ASTUnit *>(TU)->setUnsafeToFree(true);
+    return 0;
+  }
+
+  return CCAI.result;
 }
 
 unsigned clang_defaultCodeCompleteOptions(void) {





More information about the cfe-commits mailing list