Files
clang-p2996/lldb/source/Target/Language.cpp
Enrico Granata 2233895a3b Add support for language plugins to provide data formatters
Historically, data formatters all exist in a global repository (the category map)
On top of that, some formatters can be "hardcoded" when the conditions under which they apply are not expressible as a typename (or typename regex)

This change paves the way to move formatters into per-language buckets such that the C++ plugin is responsible for ownership of the C++ formatters, and so on
The advantages of this are:
a) language formatters only get created when they might apply
b) formatters for a language are clearly owned by the matching language plugin

The current model is one of static instantiation, that is a language knows the full set of formatters it vends and that is only asked-for once, and then handed off to the FormatManager
In a future revision it might be interesting to add similar ability to the language runtimes, and monitor for certain shared library events to add even more library-specific formatters

No formatters are moved as part of this change, so practically speaking this is NFC

llvm-svn: 246515
2015-09-01 01:01:48 +00:00

109 lines
2.6 KiB
C++

//===-- Language.cpp -------------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include <functional>
#include <map>
#include <mutex>
#include "lldb/Target/Language.h"
#include "lldb/Host/Mutex.h"
#include "lldb/Core/PluginManager.h"
using namespace lldb;
using namespace lldb_private;
typedef std::unique_ptr<Language> LanguageUP;
typedef std::map<lldb::LanguageType, LanguageUP> LanguagesMap;
static LanguagesMap&
GetLanguagesMap ()
{
static LanguagesMap *g_map = nullptr;
static std::once_flag g_initialize;
std::call_once(g_initialize, [] {
g_map = new LanguagesMap(); // NOTE: INTENTIONAL LEAK due to global destructor chain
});
return *g_map;
}
static Mutex&
GetLanguagesMutex ()
{
static Mutex *g_mutex = nullptr;
static std::once_flag g_initialize;
std::call_once(g_initialize, [] {
g_mutex = new Mutex(); // NOTE: INTENTIONAL LEAK due to global destructor chain
});
return *g_mutex;
}
Language*
Language::FindPlugin (lldb::LanguageType language)
{
Mutex::Locker locker(GetLanguagesMutex());
LanguagesMap& map(GetLanguagesMap());
auto iter = map.find(language), end = map.end();
if (iter != end)
return iter->second.get();
Language *language_ptr = nullptr;
LanguageCreateInstance create_callback;
for (uint32_t idx = 0;
(create_callback = PluginManager::GetLanguageCreateCallbackAtIndex(idx)) != nullptr;
++idx)
{
language_ptr = create_callback(language);
if (language_ptr)
{
map[language] = std::unique_ptr<Language>(language_ptr);
return language_ptr;
}
}
return nullptr;
}
void
Language::ForEach (std::function<bool(Language*)> callback)
{
Mutex::Locker locker(GetLanguagesMutex());
LanguagesMap& map(GetLanguagesMap());
for (const auto& entry : map)
{
if (!callback(entry.second.get()))
break;
}
}
lldb::TypeCategoryImplSP
Language::GetFormatters ()
{
return nullptr;
}
//----------------------------------------------------------------------
// Constructor
//----------------------------------------------------------------------
Language::Language()
{
}
//----------------------------------------------------------------------
// Destructor
//----------------------------------------------------------------------
Language::~Language()
{
}