[Lldb-commits] [lldb] f974d64 - [lldb] Deduplicate copy-pasted TypeSystemMap::GetTypeSystemForLanguage

Raphael Isemann via lldb-commits lldb-commits at lists.llvm.org
Fri Aug 14 02:59:25 PDT 2020


Author: Raphael Isemann
Date: 2020-08-14T11:58:54+02:00
New Revision: f974d64b372c5554783369bab901de8f4dee5e02

URL: https://github.com/llvm/llvm-project/commit/f974d64b372c5554783369bab901de8f4dee5e02
DIFF: https://github.com/llvm/llvm-project/commit/f974d64b372c5554783369bab901de8f4dee5e02.diff

LOG: [lldb] Deduplicate copy-pasted TypeSystemMap::GetTypeSystemForLanguage

There are two implementations for `TypeSystemMap::GetTypeSystemForLanguage`
which are both identical beside one taking a `Module` and one taking a `Target`
(and then passing that argument to the `TypeSystem::CreateInstance` function).

This merges both implementations into one function with a lambda that wraps the
different calls to `TypeSystem::CreateInstance`.

Reviewed By: #lldb, JDevlieghere

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

Added: 
    

Modified: 
    lldb/include/lldb/Symbol/TypeSystem.h
    lldb/source/Symbol/TypeSystem.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/include/lldb/Symbol/TypeSystem.h b/lldb/include/lldb/Symbol/TypeSystem.h
index c19909506349..4b851fea6e02 100644
--- a/lldb/include/lldb/Symbol/TypeSystem.h
+++ b/lldb/include/lldb/Symbol/TypeSystem.h
@@ -523,6 +523,22 @@ class TypeSystemMap {
                               ///multi-threaded environments.
   collection m_map;
   bool m_clear_in_progress;
+
+private:
+  typedef llvm::function_ref<lldb::TypeSystemSP()> CreateCallback;
+  /// Finds the type system for the given language. If no type system could be
+  /// found for a language and a CreateCallback was provided, the value returned
+  /// by the callback will be treated as the TypeSystem for the language.
+  ///
+  /// \param language The language for which the type system should be found.
+  /// \param create_callback A callback that will be called if no previously
+  ///                        created TypeSystem that fits the given language
+  ///                        could found. Can be omitted if a non-existent
+  ///                        type system should be treated as an error instead.
+  /// \return The found type system or an error.
+  llvm::Expected<TypeSystem &> GetTypeSystemForLanguage(
+      lldb::LanguageType language,
+      llvm::Optional<CreateCallback> create_callback = llvm::None);
 };
 
 } // namespace lldb_private

diff  --git a/lldb/source/Symbol/TypeSystem.cpp b/lldb/source/Symbol/TypeSystem.cpp
index 5e57813c28bd..985065926fc4 100644
--- a/lldb/source/Symbol/TypeSystem.cpp
+++ b/lldb/source/Symbol/TypeSystem.cpp
@@ -224,9 +224,9 @@ void TypeSystemMap::ForEach(std::function<bool(TypeSystem *)> const &callback) {
   }
 }
 
-llvm::Expected<TypeSystem &>
-TypeSystemMap::GetTypeSystemForLanguage(lldb::LanguageType language,
-                                        Module *module, bool can_create) {
+llvm::Expected<TypeSystem &> TypeSystemMap::GetTypeSystemForLanguage(
+    lldb::LanguageType language,
+    llvm::Optional<CreateCallback> create_callback) {
   llvm::Error error = llvm::Error::success();
   assert(!error); // Check the success value when assertions are enabled
   std::lock_guard<std::mutex> guard(m_mutex);
@@ -268,7 +268,7 @@ TypeSystemMap::GetTypeSystemForLanguage(lldb::LanguageType language,
       }
     }
 
-    if (!can_create) {
+    if (!create_callback) {
       error = llvm::make_error<llvm::StringError>(
           "Unable to find type system for language " +
               llvm::StringRef(Language::GetNameForLanguageType(language)),
@@ -276,7 +276,7 @@ TypeSystemMap::GetTypeSystemForLanguage(lldb::LanguageType language,
     } else {
       // Cache even if we get a shared pointer that contains a null type system
       // back
-      auto type_system_sp = TypeSystem::CreateInstance(language, module);
+      TypeSystemSP type_system_sp = (*create_callback)();
       m_map[language] = type_system_sp;
       if (type_system_sp.get()) {
         llvm::consumeError(std::move(error));
@@ -295,69 +295,24 @@ TypeSystemMap::GetTypeSystemForLanguage(lldb::LanguageType language,
 
 llvm::Expected<TypeSystem &>
 TypeSystemMap::GetTypeSystemForLanguage(lldb::LanguageType language,
-                                        Target *target, bool can_create) {
-  llvm::Error error = llvm::Error::success();
-  assert(!error); // Check the success value when assertions are enabled
-  std::lock_guard<std::mutex> guard(m_mutex);
-  if (m_clear_in_progress) {
-    error = llvm::make_error<llvm::StringError>(
-        "Unable to get TypeSystem because TypeSystemMap is being cleared",
-        llvm::inconvertibleErrorCode());
-  } else {
-    collection::iterator pos = m_map.find(language);
-    if (pos != m_map.end()) {
-      auto *type_system = pos->second.get();
-      if (type_system) {
-        llvm::consumeError(std::move(error));
-        return *type_system;
-      }
-      error = llvm::make_error<llvm::StringError>(
-          "TypeSystem for language " +
-              llvm::StringRef(Language::GetNameForLanguageType(language)) +
-              " doesn't exist",
-          llvm::inconvertibleErrorCode());
-      return std::move(error);
-    }
-
-    for (const auto &pair : m_map) {
-      if (pair.second && pair.second->SupportsLanguage(language)) {
-        // Add a new mapping for "language" to point to an already existing
-        // TypeSystem that supports this language
-        m_map[language] = pair.second;
-        if (pair.second.get()) {
-          llvm::consumeError(std::move(error));
-          return *pair.second.get();
-        }
-        error = llvm::make_error<llvm::StringError>(
-            "TypeSystem for language " +
-                llvm::StringRef(Language::GetNameForLanguageType(language)) +
-                " doesn't exist",
-            llvm::inconvertibleErrorCode());
-        return std::move(error);
-      }
-    }
-
-    if (!can_create) {
-      error = llvm::make_error<llvm::StringError>(
-          "Unable to find type system for language " +
-              llvm::StringRef(Language::GetNameForLanguageType(language)),
-          llvm::inconvertibleErrorCode());
-    } else {
-      // Cache even if we get a shared pointer that contains a null type system
-      // back
-      auto type_system_sp = TypeSystem::CreateInstance(language, target);
-      m_map[language] = type_system_sp;
-      if (type_system_sp.get()) {
-        llvm::consumeError(std::move(error));
-        return *type_system_sp.get();
-      }
-      error = llvm::make_error<llvm::StringError>(
-          "TypeSystem for language " +
-              llvm::StringRef(Language::GetNameForLanguageType(language)) +
-              " doesn't exist",
-          llvm::inconvertibleErrorCode());
-    }
+                                        Module *module, bool can_create) {
+  if (can_create) {
+    return GetTypeSystemForLanguage(
+        language, llvm::Optional<CreateCallback>([language, module]() {
+          return TypeSystem::CreateInstance(language, module);
+        }));
   }
+  return GetTypeSystemForLanguage(language);
+}
 
-  return std::move(error);
+llvm::Expected<TypeSystem &>
+TypeSystemMap::GetTypeSystemForLanguage(lldb::LanguageType language,
+                                        Target *target, bool can_create) {
+  if (can_create) {
+    return GetTypeSystemForLanguage(
+        language, llvm::Optional<CreateCallback>([language, target]() {
+          return TypeSystem::CreateInstance(language, target);
+        }));
+  }
+  return GetTypeSystemForLanguage(language);
 }


        


More information about the lldb-commits mailing list