LLDB mainline
DLanguage.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "DLanguage.h"
10
11#include "lldb/Core/Mangled.h"
14#include "lldb/Utility/Log.h"
15
16#include "llvm/ADT/StringExtras.h"
17#include "llvm/Demangle/Demangle.h"
18
19#include <cassert>
20#include <cstdlib>
21
22using namespace lldb;
23using namespace lldb_private;
24
26
27/// The mangling scheme that this plugin registered.
28static Mangled::ManglingScheme g_d_scheme = Mangled::eManglingSchemeNone;
29
30/// Returns true if \p name, which starts with "_D", is a D mangled name.
31static bool IsDMangledName(llvm::StringRef name) {
32 // A D mangled name begins with `_D`, followed by a numeric length. One known
33 // exception is the symbol `_Dmain`. See `SymbolName` and `LName` in
34 // https://dlang.org/spec/abi.html#name_mangling
35 llvm::StringRef rest = name.drop_front(2);
36 return (!rest.empty() && llvm::isDigit(rest.front())) || name == "_Dmain";
37}
38
45
50
51// Static Functions
53 if (language == eLanguageTypeD)
54 return new DLanguage();
55 return nullptr;
56}
57
58bool DLanguage::IsSourceFile(llvm::StringRef file_path) const {
59 return file_path.ends_with_insensitive(".d") ||
60 file_path.ends_with_insensitive(".di");
61}
62
65 const DemangleOptions &options) const {
66 assert(scheme == g_d_scheme);
67 llvm::StringRef mangled_str = mangled.GetStringRef();
68 char *demangled_cstr = llvm::dlangDemangle(mangled_str);
69
70 if (Log *log = GetLog(LLDBLog::Demangle)) {
71 if (demangled_cstr && demangled_cstr[0])
72 LLDB_LOG(log, "demangled dlang: {0} -> \"{1}\"", mangled_str,
73 demangled_cstr);
74 else
75 LLDB_LOG(log, "demangled dlang: {0} -> error: failed to demangle",
76 mangled_str);
77 }
78
79 DemangledName demangled;
80 if (demangled_cstr) {
81 demangled.name = demangled_cstr;
82 std::free(demangled_cstr);
83 }
84 return demangled;
85}
static bool IsDMangledName(llvm::StringRef name)
Returns true if name, which starts with "_D", is a D mangled name.
Definition DLanguage.cpp:31
static Mangled::ManglingScheme g_d_scheme
The mangling scheme that this plugin registered.
Definition DLanguage.cpp:28
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:375
#define LLDB_PLUGIN_DEFINE(PluginName)
A uniqued constant string class.
Definition ConstString.h:40
llvm::StringRef GetStringRef() const
Get the string value as a llvm::StringRef.
static Language * CreateInstance(lldb::LanguageType language)
Definition DLanguage.cpp:52
bool IsSourceFile(llvm::StringRef file_path) const override
Definition DLanguage.cpp:58
DemangledName DemangleSymbol(ConstString mangled, Mangled::ManglingScheme scheme, const DemangleOptions &options) const override
Demangle mangled, a name mangled with scheme, a scheme that this language registered with Mangled::Re...
Definition DLanguage.cpp:64
static llvm::StringRef GetPluginNameStatic()
Definition DLanguage.h:39
static void Initialize()
Definition DLanguage.cpp:39
static void Terminate()
Definition DLanguage.cpp:46
A class that handles mangled names.
Definition Mangled.h:36
static ManglingScheme RegisterManglingScheme(llvm::ArrayRef< llvm::StringRef > prefixes, lldb::LanguageType language, ManglingSchemeMatcher matches=nullptr)
Register a mangling scheme for language.
Definition Mangled.cpp:163
static void UnregisterManglingScheme(ManglingScheme scheme)
Stop recognizing the names of scheme, a scheme returned by RegisterManglingScheme().
Definition Mangled.cpp:169
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:338
LanguageType
Programming language type.
@ eLanguageTypeD
D.
How to demangle a name in DemangleSymbol().
Definition Language.h:334
A demangled name and, if requested, where its parts begin and end.
Definition Language.h:340
std::string name
Empty if the name couldn't be demangled.
Definition Language.h:342