[Lldb-commits] [lldb] r360760 - [ASTImporter] Use llvm::Expected and Error in the importer API

Gabor Marton via lldb-commits lldb-commits at lists.llvm.org
Wed May 15 03:29:48 PDT 2019


Author: martong
Date: Wed May 15 03:29:48 2019
New Revision: 360760

URL: http://llvm.org/viewvc/llvm-project?rev=360760&view=rev
Log:
[ASTImporter] Use llvm::Expected and Error in the importer API

Summary:
This is the final phase of the refactoring towards using llvm::Expected
and llvm::Error in the ASTImporter API.
This involves the following:
- remove old Import functions which returned with a pointer,
- use the Import_New functions (which return with Err or Expected) everywhere
  and handle their return value
- rename Import_New functions to Import
This affects both Clang and LLDB.

Reviewers: shafik, teemperor, aprantl, a_sidorin, balazske, a.sidorin

Subscribers: rnkovacs, dkrupp, Szelethus, gamesh411, cfe-commits, lldb-commits

Tags: #clang, #lldb

Differential Revision: https://reviews.llvm.org/D61438

Modified:
    lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp
    lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp
    lldb/trunk/source/Symbol/ClangASTContext.cpp
    lldb/trunk/source/Symbol/ClangASTImporter.cpp
    lldb/trunk/source/Symbol/CxxModuleHandler.cpp

Modified: lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp?rev=360760&r1=360759&r2=360760&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp (original)
+++ lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangASTSource.cpp Wed May 15 03:29:48 2019
@@ -1968,7 +1968,14 @@ clang::QualType ClangASTSource::CopyType
     return QualType();
   }
 
-  return merger.ImporterForOrigin(from_context).Import(type);
+  if (llvm::Expected<QualType> type_or_error =
+          merger.ImporterForOrigin(from_context).Import(type)) {
+    return *type_or_error;
+  } else {
+    Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
+    LLDB_LOG_ERROR(log, type_or_error.takeError(), "Couldn't import type: {0}");
+    return QualType();
+  }
 }
 
 clang::Decl *ClangASTSource::CopyDecl(Decl *src_decl) {
@@ -1981,7 +1988,16 @@ clang::Decl *ClangASTSource::CopyDecl(De
       return nullptr;
     }
 
-    return m_merger_up->ImporterForOrigin(from_context).Import(src_decl);
+    if (llvm::Expected<Decl *> decl_or_error =
+            m_merger_up->ImporterForOrigin(from_context).Import(src_decl)) {
+      return *decl_or_error;
+    } else {
+      Log *log =
+          lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
+      LLDB_LOG_ERROR(log, decl_or_error.takeError(),
+                     "Couldn't import decl: {0}");
+      return nullptr;
+    }
   } else {
     lldbassert(0 && "No mechanism for copying a decl!");
     return nullptr;

Modified: lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp?rev=360760&r1=360759&r2=360760&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp (original)
+++ lldb/trunk/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp Wed May 15 03:29:48 2019
@@ -272,9 +272,15 @@ static clang::QualType ExportAllDeclared
   merger.AddSources(importer_source);
   clang::ASTImporter &exporter = merger.ImporterForOrigin(source);
   CompleteAllDeclContexts(exporter, file, root);
-  clang::QualType ret = exporter.Import(root);
+  llvm::Expected<clang::QualType> ret_or_error = exporter.Import(root);
   merger.RemoveSources(importer_source);
-  return ret;
+  if (ret_or_error) {
+    return *ret_or_error;
+  } else {
+    Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
+    LLDB_LOG_ERROR(log, ret_or_error.takeError(), "Couldn't import type: {0}");
+    return clang::QualType();
+  }
 }
 
 TypeFromUser ClangExpressionDeclMap::DeportType(ClangASTContext &target,

Modified: lldb/trunk/source/Symbol/ClangASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Symbol/ClangASTContext.cpp?rev=360760&r1=360759&r2=360760&view=diff
==============================================================================
--- lldb/trunk/source/Symbol/ClangASTContext.cpp (original)
+++ lldb/trunk/source/Symbol/ClangASTContext.cpp Wed May 15 03:29:48 2019
@@ -1415,7 +1415,14 @@ clang::Decl *ClangASTContext::CopyDecl(A
   FileManager file_manager(file_system_options);
   ASTImporter importer(*dst_ast, file_manager, *src_ast, file_manager, false);
 
-  return importer.Import(source_decl);
+  if (llvm::Expected<clang::Decl *> ret_or_error =
+          importer.Import(source_decl)) {
+    return *ret_or_error;
+  } else {
+    Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
+    LLDB_LOG_ERROR(log, ret_or_error.takeError(), "Couldn't import decl: {0}");
+    return nullptr;
+  }
 }
 
 bool ClangASTContext::AreTypesSame(CompilerType type1, CompilerType type2,

Modified: lldb/trunk/source/Symbol/ClangASTImporter.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Symbol/ClangASTImporter.cpp?rev=360760&r1=360759&r2=360760&view=diff
==============================================================================
--- lldb/trunk/source/Symbol/ClangASTImporter.cpp (original)
+++ lldb/trunk/source/Symbol/ClangASTImporter.cpp Wed May 15 03:29:48 2019
@@ -62,10 +62,18 @@ clang::QualType ClangASTImporter::CopyTy
 
   ASTImporterDelegate::CxxModuleScope std_scope(*delegate_sp, dst_ast);
 
-  if (delegate_sp)
-    return delegate_sp->Import(type);
+  if (!delegate_sp)
+    return QualType();
 
-  return QualType();
+  llvm::Expected<QualType> ret_or_error = delegate_sp->Import(type);
+  if (!ret_or_error) {
+    Log *log =
+      lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
+    LLDB_LOG_ERROR(log, ret_or_error.takeError(),
+        "Couldn't import type: {0}");
+    return QualType();
+  }
+  return *ret_or_error;
 }
 
 lldb::opaque_compiler_type_t
@@ -105,34 +113,33 @@ clang::Decl *ClangASTImporter::CopyDecl(
 
   ASTImporterDelegate::CxxModuleScope std_scope(*delegate_sp, dst_ast);
 
-  if (delegate_sp) {
-    clang::Decl *result = delegate_sp->Import(decl);
-
-    if (!result) {
-      Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
-
-      if (log) {
-        lldb::user_id_t user_id = LLDB_INVALID_UID;
-        ClangASTMetadata *metadata = GetDeclMetadata(decl);
-        if (metadata)
-          user_id = metadata->GetUserID();
+  if (!delegate_sp)
+    return nullptr;
 
-        if (NamedDecl *named_decl = dyn_cast<NamedDecl>(decl))
-          log->Printf("  [ClangASTImporter] WARNING: Failed to import a %s "
-                      "'%s', metadata 0x%" PRIx64,
-                      decl->getDeclKindName(),
-                      named_decl->getNameAsString().c_str(), user_id);
-        else
-          log->Printf("  [ClangASTImporter] WARNING: Failed to import a %s, "
-                      "metadata 0x%" PRIx64,
-                      decl->getDeclKindName(), user_id);
-      }
+  llvm::Expected<clang::Decl *> result = delegate_sp->Import(decl);
+  if (!result) {
+    Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
+    LLDB_LOG_ERROR(log, result.takeError(), "Couldn't import decl: {0}");
+    if (log) {
+      lldb::user_id_t user_id = LLDB_INVALID_UID;
+      ClangASTMetadata *metadata = GetDeclMetadata(decl);
+      if (metadata)
+        user_id = metadata->GetUserID();
+
+      if (NamedDecl *named_decl = dyn_cast<NamedDecl>(decl))
+        log->Printf("  [ClangASTImporter] WARNING: Failed to import a %s "
+                    "'%s', metadata 0x%" PRIx64,
+                    decl->getDeclKindName(),
+                    named_decl->getNameAsString().c_str(), user_id);
+      else
+        log->Printf("  [ClangASTImporter] WARNING: Failed to import a %s, "
+                    "metadata 0x%" PRIx64,
+                    decl->getDeclKindName(), user_id);
     }
-
-    return result;
+    return nullptr;
   }
 
-  return nullptr;
+  return *result;
 }
 
 class DeclContextOverride {
@@ -624,6 +631,8 @@ bool ClangASTImporter::CompleteAndFetchC
   if (!RequireCompleteType(type))
     return false;
 
+  Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
+
   if (const TagType *tag_type = type->getAs<TagType>()) {
     TagDecl *tag_decl = tag_type->getDecl();
 
@@ -641,7 +650,13 @@ bool ClangASTImporter::CompleteAndFetchC
     TagDecl *origin_tag_decl = llvm::dyn_cast<TagDecl>(decl_origin.decl);
 
     for (Decl *origin_child_decl : origin_tag_decl->decls()) {
-      delegate_sp->Import(origin_child_decl);
+      llvm::Expected<Decl *> imported_or_err =
+          delegate_sp->Import(origin_child_decl);
+      if (!imported_or_err) {
+        LLDB_LOG_ERROR(log, imported_or_err.takeError(),
+                       "Couldn't import decl: {0}");
+        return false;
+      }
     }
 
     if (RecordDecl *record_decl = dyn_cast<RecordDecl>(origin_tag_decl)) {
@@ -666,7 +681,13 @@ bool ClangASTImporter::CompleteAndFetchC
           llvm::dyn_cast<ObjCInterfaceDecl>(decl_origin.decl);
 
       for (Decl *origin_child_decl : origin_interface_decl->decls()) {
-        delegate_sp->Import(origin_child_decl);
+        llvm::Expected<Decl *> imported_or_err =
+            delegate_sp->Import(origin_child_decl);
+        if (!imported_or_err) {
+          LLDB_LOG_ERROR(log, imported_or_err.takeError(),
+                         "Couldn't import decl: {0}");
+          return false;
+        }
       }
 
       return true;
@@ -919,7 +940,13 @@ void ClangASTImporter::ASTImporterDelega
       to_cxx_record->startDefinition();
   */
 
-  ImportDefinition(from);
+  Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
+
+  if (llvm::Error err = ImportDefinition(from)) {
+    LLDB_LOG_ERROR(log, std::move(err),
+                   "[ClangASTImporter] Error during importing definition: {0}");
+    return;
+  }
 
   if (clang::TagDecl *to_tag = dyn_cast<clang::TagDecl>(to)) {
     if (clang::TagDecl *from_tag = dyn_cast<clang::TagDecl>(from)) {
@@ -949,13 +976,17 @@ void ClangASTImporter::ASTImporterDelega
       if (!from_superclass)
         break;
 
-      Decl *imported_from_superclass_decl = Import(from_superclass);
+      llvm::Expected<Decl *> imported_from_superclass_decl =
+          Import(from_superclass);
 
-      if (!imported_from_superclass_decl)
+      if (!imported_from_superclass_decl) {
+        LLDB_LOG_ERROR(log, imported_from_superclass_decl.takeError(),
+                       "Couldn't import decl: {0}");
         break;
+      }
 
       ObjCInterfaceDecl *imported_from_superclass =
-          dyn_cast<ObjCInterfaceDecl>(imported_from_superclass_decl);
+          dyn_cast<ObjCInterfaceDecl>(*imported_from_superclass_decl);
 
       if (!imported_from_superclass)
         break;

Modified: lldb/trunk/source/Symbol/CxxModuleHandler.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Symbol/CxxModuleHandler.cpp?rev=360760&r1=360759&r2=360760&view=diff
==============================================================================
--- lldb/trunk/source/Symbol/CxxModuleHandler.cpp (original)
+++ lldb/trunk/source/Symbol/CxxModuleHandler.cpp Wed May 15 03:29:48 2019
@@ -9,6 +9,7 @@
 #include "lldb/Symbol/CxxModuleHandler.h"
 
 #include "lldb/Symbol/ClangASTContext.h"
+#include "lldb/Utility/Log.h"
 #include "clang/Sema/Lookup.h"
 #include "llvm/Support/Error.h"
 
@@ -214,13 +215,15 @@ llvm::Optional<Decl *> CxxModuleHandler:
   // Import the foreign template arguments.
   llvm::SmallVector<TemplateArgument, 4> imported_args;
 
+  Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS);
+
   // If this logic is changed, also update templateArgsAreSupported.
   for (const TemplateArgument &arg : foreign_args.asArray()) {
     switch (arg.getKind()) {
     case TemplateArgument::Type: {
-      llvm::Expected<QualType> type = m_importer->Import_New(arg.getAsType());
+      llvm::Expected<QualType> type = m_importer->Import(arg.getAsType());
       if (!type) {
-        llvm::consumeError(type.takeError());
+        LLDB_LOG_ERROR(log, type.takeError(), "Couldn't import type: {0}");
         return {};
       }
       imported_args.push_back(TemplateArgument(*type));
@@ -229,9 +232,9 @@ llvm::Optional<Decl *> CxxModuleHandler:
     case TemplateArgument::Integral: {
       llvm::APSInt integral = arg.getAsIntegral();
       llvm::Expected<QualType> type =
-          m_importer->Import_New(arg.getIntegralType());
+          m_importer->Import(arg.getIntegralType());
       if (!type) {
-        llvm::consumeError(type.takeError());
+        LLDB_LOG_ERROR(log, type.takeError(), "Couldn't import type: {0}");
         return {};
       }
       imported_args.push_back(




More information about the lldb-commits mailing list