libclang: report error code for bad PCH files

This commit improves libclang to report the error condition when
CXTranslationUnit can not be created because of a stale PCH file.  This allows
the caller, for example, to rebuild the PCH file and retry the request.

There two are APIs in libclang that return a CXTranslationUnit and don't
support reporting detailed errors (the only error condition is a NULL result).
For these APIs, a second, superior, version is introduced --
clang_createTranslationUnit2 and clang_parseTranslationUnit2.  These functions
return a CXTranslationUnit indirectly and also return an error code.  Old
functions are still supported and are nothing more than convenience wrappers
that ignore extended error codes.

As a cleanup, this commit also categorizes some libclang errors in the
functions I had to modify anyway.

llvm-svn: 201249
diff --git a/clang/tools/libclang/Indexing.cpp b/clang/tools/libclang/Indexing.cpp
index dc7a0c0..9b83aa0 100644
--- a/clang/tools/libclang/Indexing.cpp
+++ b/clang/tools/libclang/Indexing.cpp
@@ -514,16 +514,22 @@
   unsigned num_unsaved_files = ITUI->num_unsaved_files;
   CXTranslationUnit *out_TU  = ITUI->out_TU;
   unsigned TU_options = ITUI->TU_options;
-  ITUI->result = 1; // init as error.
-  
+
+  // Set up the initial return value.
+  ITUI->result = CXError_Failure;
+
   if (out_TU)
     *out_TU = 0;
-  bool requestedToGetTU = (out_TU != 0); 
+  bool requestedToGetTU = (out_TU != 0);
 
-  if (!cxIdxAction)
+  if (!cxIdxAction) {
+    ITUI->result = CXError_InvalidArguments;
     return;
-  if (!client_index_callbacks || index_callbacks_size == 0)
+  }
+  if (!client_index_callbacks || index_callbacks_size == 0) {
+    ITUI->result = CXError_InvalidArguments;
     return;
+  }
 
   IndexerCallbacks CB;
   memset(&CB, 0, sizeof(CB));
@@ -671,13 +677,18 @@
   if (DiagTrap.hasErrorOccurred() && CXXIdx->getDisplayDiagnostics())
     printDiagsToStderr(Unit);
 
+  if (isASTReadError(Unit)) {
+    ITUI->result = CXError_ASTReadError;
+    return;
+  }
+
   if (!Success)
     return;
 
   if (out_TU)
     *out_TU = CXTU->takeTU();
 
-  ITUI->result = 0; // success.
+  ITUI->result = CXError_Success;
 }
 
 //===----------------------------------------------------------------------===//
@@ -754,14 +765,20 @@
   IndexerCallbacks *client_index_callbacks = ITUI->index_callbacks;
   unsigned index_callbacks_size = ITUI->index_callbacks_size;
   unsigned index_options = ITUI->index_options;
-  ITUI->result = 1; // init as error.
 
+  // Set up the initial return value.
+  ITUI->result = CXError_Failure;
+
+  // Check arguments.
   if (isNotUsableTU(TU)) {
     LOG_BAD_TU(TU);
+    ITUI->result = CXError_InvalidArguments;
     return;
   }
-  if (!client_index_callbacks || index_callbacks_size == 0)
+  if (!client_index_callbacks || index_callbacks_size == 0) {
+    ITUI->result = CXError_InvalidArguments;
     return;
+  }
 
   CIndexer *CXXIdx = TU->CIdx;
   if (CXXIdx->isOptEnabled(CXGlobalOpt_ThreadBackgroundPriorityForIndexing))
@@ -809,7 +826,7 @@
   indexTranslationUnit(*Unit, *IndexCtx);
   indexDiagnostics(TU, *IndexCtx);
 
-  ITUI->result = 0;
+  ITUI->result = CXError_Success;
 }
 
 //===----------------------------------------------------------------------===//
@@ -981,7 +998,8 @@
                                index_callbacks_size, index_options,
                                source_filename, command_line_args,
                                num_command_line_args, unsaved_files,
-                               num_unsaved_files, out_TU, TU_options, 0 };
+                               num_unsaved_files, out_TU, TU_options,
+                               CXError_Failure };
 
   if (getenv("LIBCLANG_NOTHREADS")) {
     clang_indexSourceFile_Impl(&ITUI);