Fixed a problem where the AST importer would assert()
because the diagnostic client for one of the AST
contexts is NULL.  Now we provide a form of Miranda
rights to AST contexts: they are provided with a very
simple diagnostic client if they do not have one
themselves.


git-svn-id: https://llvm.org/svn/llvm-project/llvdb/trunk@121225 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/source/Symbol/ClangASTContext.cpp b/source/Symbol/ClangASTContext.cpp
index d93fc59..49f13dc 100644
--- a/source/Symbol/ClangASTContext.cpp
+++ b/source/Symbol/ClangASTContext.cpp
@@ -747,7 +747,25 @@
                            ASTContext *src_ast,
                            clang_type_t clang_type)
 {
-    // null_client's ownership is transferred to diagnostics
+    // we temporarily install diagnostic clients as needed to ensure that
+    // errors are properly handled
+    
+    std::auto_ptr<NullDiagnosticClient> diag_client;
+    
+    bool dst_needs_diag = !dst_ast->getDiagnostics().getClient();
+    bool src_needs_diag = !src_ast->getDiagnostics().getClient();
+    
+    if (dst_needs_diag || src_needs_diag)
+    {
+        diag_client.reset(new NullDiagnosticClient);
+    
+        if (dst_needs_diag)
+            dst_ast->getDiagnostics().setClient(diag_client.get(), false);
+        
+        if (src_needs_diag)
+            src_ast->getDiagnostics().setClient(diag_client.get(), false);
+    }
+        
     FileSystemOptions file_system_options;
     FileManager file_manager (file_system_options);
     ASTImporter importer(*dst_ast, file_manager,
@@ -756,6 +774,12 @@
     QualType src (QualType::getFromOpaquePtr(clang_type));
     QualType dst (importer.Import(src));
     
+    if (dst_needs_diag)
+        dst_ast->getDiagnostics().setClient(NULL, false);
+    
+    if (src_needs_diag)
+        src_ast->getDiagnostics().setClient(NULL, false);
+    
     return dst.getAsOpaquePtr();
 }
 
@@ -765,12 +789,36 @@
                            ASTContext *src_ast,
                            clang::Decl *source_decl)
 {
-    // null_client's ownership is transferred to diagnostics
+    // we temporarily install diagnostic clients as needed to ensure that
+    // errors are properly handled
+    
+    std::auto_ptr<NullDiagnosticClient> diag_client;
+    
+    bool dst_needs_diag = !dst_ast->getDiagnostics().getClient();
+    bool src_needs_diag = !src_ast->getDiagnostics().getClient();
+    
+    if (dst_needs_diag || src_needs_diag)
+    {
+        diag_client.reset(new NullDiagnosticClient);
+        
+        if (dst_needs_diag)
+            dst_ast->getDiagnostics().setClient(diag_client.get(), false);
+        
+        if (src_needs_diag)
+            src_ast->getDiagnostics().setClient(diag_client.get(), false);
+    }
+    
     FileSystemOptions file_system_options;
     FileManager file_manager (file_system_options);
     ASTImporter importer(*dst_ast, file_manager,
                          *src_ast, file_manager);
     
+    if (dst_needs_diag)
+        dst_ast->getDiagnostics().setClient(NULL, false);
+    
+    if (src_needs_diag)
+        src_ast->getDiagnostics().setClient(NULL, false);
+    
     return importer.Import(source_decl);
 }