[cfe-commits] r143243 - /cfe/trunk/tools/c-index-test/c-index-test.c

Argyrios Kyrtzidis akyrtzi at gmail.com
Fri Oct 28 15:54:36 PDT 2011


Author: akirtzidis
Date: Fri Oct 28 17:54:36 2011
New Revision: 143243

URL: http://llvm.org/viewvc/llvm-project?rev=143243&view=rev
Log:
[libclang] Introduce use of CINDEXTEST_FAILONERROR environment variable which, if set,
will cause c-index-test to return as failed if a compiler error occurred.

Modified:
    cfe/trunk/tools/c-index-test/c-index-test.c

Modified: cfe/trunk/tools/c-index-test/c-index-test.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/c-index-test/c-index-test.c?rev=143243&r1=143242&r2=143243&view=diff
==============================================================================
--- cfe/trunk/tools/c-index-test/c-index-test.c (original)
+++ cfe/trunk/tools/c-index-test/c-index-test.c Fri Oct 28 17:54:36 2011
@@ -995,6 +995,31 @@
   return "Unknown";
 }
 
+static int checkForErrors(CXTranslationUnit TU) {
+  unsigned Num, i;
+  CXDiagnostic Diag;
+  CXString DiagStr;
+
+  if (!getenv("CINDEXTEST_FAILONERROR"))
+    return 0;
+
+  Num = clang_getNumDiagnostics(TU);
+  for (i = 0; i != Num; ++i) {
+    Diag = clang_getDiagnostic(TU, i);
+    if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) {
+      DiagStr = clang_formatDiagnostic(Diag,
+                                       clang_defaultDiagnosticDisplayOptions());
+      fprintf(stderr, "%s\n", clang_getCString(DiagStr));
+      clang_disposeString(DiagStr);
+      clang_disposeDiagnostic(Diag);
+      return -1;
+    }
+    clang_disposeDiagnostic(Diag);
+  }
+
+  return 0;
+}
+
 void print_completion_string(CXCompletionString completion_string, FILE *file) {
   int I, N;
 
@@ -1347,6 +1372,9 @@
     return -1;
   }
 
+  if (checkForErrors(TU) != 0)
+    return -1;
+
   for (I = 0; I != Repeats; ++I) {
     if (Repeats > 1 &&
         clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files, 
@@ -1354,6 +1382,9 @@
       clang_disposeTranslationUnit(TU);
       return 1;
     }
+
+    if (checkForErrors(TU) != 0)
+      return -1;
     
     for (Loc = 0; Loc < NumLocations; ++Loc) {
       CXFile file = clang_getFile(TU, Locations[Loc].filename);
@@ -1363,6 +1394,10 @@
       Cursor = clang_getCursor(TU,
                                clang_getLocation(TU, file, Locations[Loc].line,
                                                  Locations[Loc].column));
+
+      if (checkForErrors(TU) != 0)
+        return -1;
+
       if (I + 1 == Repeats) {
         CXCompletionString completionString = clang_getCursorCompletionString(
                                                                         Cursor);
@@ -1446,6 +1481,9 @@
     return -1;
   }
 
+  if (checkForErrors(TU) != 0)
+    return -1;
+
   for (I = 0; I != Repeats; ++I) {
     if (Repeats > 1 &&
         clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files, 
@@ -1453,6 +1491,9 @@
       clang_disposeTranslationUnit(TU);
       return 1;
     }
+
+    if (checkForErrors(TU) != 0)
+      return -1;
     
     for (Loc = 0; Loc < NumLocations; ++Loc) {
       CXFile file = clang_getFile(TU, Locations[Loc].filename);
@@ -1462,12 +1503,19 @@
       Cursor = clang_getCursor(TU,
                                clang_getLocation(TU, file, Locations[Loc].line,
                                                  Locations[Loc].column));
+
+      if (checkForErrors(TU) != 0)
+        return -1;
+
       if (I + 1 == Repeats) {
         CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
         PrintCursor(Cursor);
         printf("\n");
         clang_findReferencesInFile(Cursor, file, visitor);
         free(Locations[Loc].filename);
+
+        if (checkForErrors(TU) != 0)
+          return -1;
       }
     }
   }
@@ -1483,6 +1531,7 @@
 typedef struct {
   const char *check_prefix;
   int first_check_printed;
+  int fail_for_error;
 } IndexData;
 
 static void printCheck(IndexData *data) {
@@ -1622,6 +1671,11 @@
   cstr = clang_getCString(str);
   printf("diagnostic: %s\n", cstr);
   clang_disposeString(str);  
+
+  if (getenv("CINDEXTEST_FAILONERROR") &&
+      clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) {
+    index_data->fail_for_error = 1;
+  }
 }
 
 static CXIdxFile index_recordFile(CXClientData client_data,
@@ -1966,6 +2020,7 @@
   const char *check_prefix;
   CXIndex CIdx;
   IndexData index_data;
+  int result;
 
   check_prefix = 0;
   if (argc > 0) {
@@ -1984,9 +2039,15 @@
   CIdx = clang_createIndex(0, 1);
   index_data.check_prefix = check_prefix;
   index_data.first_check_printed = 0;
+  index_data.fail_for_error = 0;
+
+  result = clang_indexTranslationUnit(CIdx, &index_data,
+                                      &IndexCB,sizeof(IndexCB),
+                                      0, 0, argv, argc, 0, 0, 0, 0);
+  if (index_data.fail_for_error)
+    return -1;
 
-  return clang_indexTranslationUnit(CIdx, &index_data, &IndexCB,sizeof(IndexCB),
-                                    0, 0, argv, argc, 0, 0, 0, 0);
+  return result;
 }
 
 int perform_token_annotation(int argc, const char **argv) {
@@ -2031,6 +2092,9 @@
   }
   errorCode = 0;
 
+  if (checkForErrors(TU) != 0)
+    return -1;
+
   if (getenv("CINDEXTEST_EDITING")) {
     for (i = 0; i < 5; ++i) {
       if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
@@ -2042,6 +2106,11 @@
     }
   }
 
+  if (checkForErrors(TU) != 0) {
+    errorCode = -1;
+    goto teardown;
+  }
+
   file = clang_getFile(TU, filename);
   if (!file) {
     fprintf(stderr, "file %s is not in this translation unit\n", filename);
@@ -2067,8 +2136,20 @@
 
   range = clang_getRange(startLoc, endLoc);
   clang_tokenize(TU, range, &tokens, &num_tokens);
+
+  if (checkForErrors(TU) != 0) {
+    errorCode = -1;
+    goto teardown;
+  }
+
   cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
   clang_annotateTokens(TU, tokens, num_tokens, cursors);
+
+  if (checkForErrors(TU) != 0) {
+    errorCode = -1;
+    goto teardown;
+  }
+
   for (i = 0; i != num_tokens; ++i) {
     const char *kind = "<unknown>";
     CXString spelling = clang_getTokenSpelling(TU, tokens[i]);





More information about the cfe-commits mailing list