Skip to content

Commit ac56dee

Browse files
committed
Add a new SBExpressionOptions::SetLanguage() API (NFCI) (llvm#89981)
that separates out language and version. To avoid reinventing the wheel and introducing subtle incompatibilities, this API uses the table of languages and versiond defined by the upcoming DWARF 6 standard (https://dwarfstd.org/languages-v6.html). While the DWARF 6 spec is not finialized, the list of languages is broadly considered stable. The primary motivation for this is to allow the Swift language plugin to switch between language dialects between, e.g., Swift 5.9 and 6.0 with out introducing a ton of new language codes. On the main branch this change is considered NFC. Depends on llvm#89980 (cherry picked from commit 975eca0) Conflicts: lldb/packages/Python/lldbsuite/test/dotest_args.py lldb/source/Commands/CommandObjectDWIMPrint.cpp lldb/source/Expression/UserExpression.cpp
1 parent 85bfcc5 commit ac56dee

35 files changed

+310
-130
lines changed

lldb/include/lldb/API/SBExpressionOptions.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define LLDB_API_SBEXPRESSIONOPTIONS_H
1111

1212
#include "lldb/API/SBDefines.h"
13+
#include "lldb/API/SBLanguages.h"
1314

1415
#include <vector>
1516

@@ -67,6 +68,10 @@ class LLDB_API SBExpressionOptions {
6768
void SetTrapExceptions(bool trap_exceptions = true);
6869

6970
void SetLanguage(lldb::LanguageType language);
71+
/// Set the language using a pair of language code and version as
72+
/// defined by the DWARF 6 specification.
73+
/// WARNING: These codes may change until DWARF 6 is finalized.
74+
void SetLanguage(SBSourceLanguageName name, uint32_t version);
7075

7176
#ifndef SWIG
7277
void SetCancelCallback(lldb::ExpressionCancelCallback callback, void *baton);

lldb/include/lldb/Expression/Expression.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,8 @@ class Expression {
4747
/// expression. Text() should contain the definition of this function.
4848
virtual const char *FunctionName() = 0;
4949

50-
/// Return the language that should be used when parsing. To use the
51-
/// default, return eLanguageTypeUnknown.
52-
virtual lldb::LanguageType Language() const {
53-
return lldb::eLanguageTypeUnknown;
54-
}
50+
/// Return the language that should be used when parsing.
51+
virtual SourceLanguage Language() const { return {}; }
5552

5653
/// Return the Materializer that the parser should use when registering
5754
/// external values.

lldb/include/lldb/Expression/LLVMUserExpression.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class LLVMUserExpression : public UserExpression {
5252
};
5353

5454
LLVMUserExpression(ExecutionContextScope &exe_scope, llvm::StringRef expr,
55-
llvm::StringRef prefix, lldb::LanguageType language,
55+
llvm::StringRef prefix, SourceLanguage language,
5656
ResultType desired_type,
5757
const EvaluateExpressionOptions &options);
5858
~LLVMUserExpression() override;

lldb/include/lldb/Expression/UserExpression.h

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class UserExpression : public Expression {
5656
/// If not eResultTypeAny, the type to use for the expression
5757
/// result.
5858
UserExpression(ExecutionContextScope &exe_scope, llvm::StringRef expr,
59-
llvm::StringRef prefix, lldb::LanguageType language,
59+
llvm::StringRef prefix, SourceLanguage language,
6060
ResultType desired_type,
6161
const EvaluateExpressionOptions &options);
6262

@@ -196,7 +196,7 @@ class UserExpression : public Expression {
196196
virtual bool IsParseCacheable() { return true; }
197197
/// Return the language that should be used when parsing. To use the
198198
/// default, return eLanguageTypeUnknown.
199-
lldb::LanguageType Language() const override { return m_language; }
199+
SourceLanguage Language() const override { return m_language; }
200200

201201
/// Return the desired result type of the function, or eResultTypeAny if
202202
/// indifferent.
@@ -308,19 +308,22 @@ class UserExpression : public Expression {
308308
lldb::ProcessSP &process_sp,
309309
lldb::StackFrameSP &frame_sp);
310310

311-
Address m_address; ///< The address the process is stopped in.
312-
std::string m_expr_text; ///< The text of the expression, as typed by the user
313-
std::string m_expr_prefix; ///< The text of the translation-level definitions,
314-
///as provided by the user
315-
std::string m_fixed_text; ///< The text of the expression with fix-its applied
316-
///- this won't be set if the fixed text doesn't
317-
///parse.
318-
lldb::LanguageType m_language; ///< The language to use when parsing
319-
///(eLanguageTypeUnknown means use defaults)
320-
ResultType m_desired_type; ///< The type to coerce the expression's result to.
321-
///If eResultTypeAny, inferred from the expression.
322-
EvaluateExpressionOptions
323-
m_options; ///< Additional options provided by the user.
311+
/// The address the process is stopped in.
312+
Address m_address;
313+
/// The text of the expression, as typed by the user.
314+
std::string m_expr_text;
315+
/// The text of the translation-level definitions, as provided by the user.
316+
std::string m_expr_prefix;
317+
/// The text of the expression with fix-its applied this won't be set if the
318+
/// fixed text doesn't parse.
319+
std::string m_fixed_text;
320+
/// The language to use when parsing (unknown means use defaults).
321+
SourceLanguage m_language;
322+
/// The type to coerce the expression's result to. If eResultTypeAny, inferred
323+
/// from the expression.
324+
ResultType m_desired_type;
325+
/// Additional options provided by the user.
326+
EvaluateExpressionOptions m_options;
324327
};
325328

326329
} // namespace lldb_private

lldb/include/lldb/Symbol/TypeSystem.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -515,12 +515,10 @@ class TypeSystem : public PluginInterface,
515515
return IsPointerOrReferenceType(type, nullptr);
516516
}
517517

518-
virtual UserExpression *
519-
GetUserExpression(llvm::StringRef expr, llvm::StringRef prefix,
520-
lldb::LanguageType language,
521-
Expression::ResultType desired_type,
522-
const EvaluateExpressionOptions &options,
523-
ValueObject *ctx_obj) {
518+
virtual UserExpression *GetUserExpression(
519+
llvm::StringRef expr, llvm::StringRef prefix, SourceLanguage language,
520+
Expression::ResultType desired_type,
521+
const EvaluateExpressionOptions &options, ValueObject *ctx_obj) {
524522
return nullptr;
525523
}
526524

lldb/include/lldb/Target/StackFrame.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
//===-- StackFrame.h --------------------------------------------*- C++ -*-===//
23
//
34
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
@@ -446,13 +447,12 @@ class StackFrame : public ExecutionContextScope,
446447
/// Query this frame to determine what the default language should be when
447448
/// parsing expressions given the execution context.
448449
///
449-
/// \return
450-
/// The language of the frame if known, else lldb::eLanguageTypeUnknown.
451-
lldb::LanguageType GetLanguage();
450+
/// \return The language of the frame if known.
451+
SourceLanguage GetLanguage();
452452

453-
// similar to GetLanguage(), but is allowed to take a potentially incorrect
454-
// guess if exact information is not available
455-
lldb::LanguageType GuessLanguage();
453+
/// Similar to GetLanguage(), but is allowed to take a potentially incorrect
454+
/// guess if exact information is not available.
455+
SourceLanguage GuessLanguage();
456456

457457
/// Attempt to econstruct the ValueObject for a given raw address touched by
458458
/// the current instruction. The ExpressionPath should indicate how to get

lldb/include/lldb/Target/Target.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ class TargetProperties : public Properties {
241241

242242
bool GetBreakpointsConsultPlatformAvoidList();
243243

244-
lldb::LanguageType GetLanguage() const;
244+
SourceLanguage GetLanguage() const;
245245

246246
llvm::StringRef GetExpressionPrefixContents();
247247

@@ -355,9 +355,18 @@ class EvaluateExpressionOptions {
355355
m_execution_policy = policy;
356356
}
357357

358-
lldb::LanguageType GetLanguage() const { return m_language; }
358+
SourceLanguage GetLanguage() const { return m_language; }
359359

360-
void SetLanguage(lldb::LanguageType language) { m_language = language; }
360+
void SetLanguage(lldb::LanguageType language_type) {
361+
m_language = SourceLanguage(language_type);
362+
}
363+
364+
/// Set the language using a pair of language code and version as
365+
/// defined by the DWARF 6 specification.
366+
/// WARNING: These codes may change until DWARF 6 is finalized.
367+
void SetLanguage(uint16_t name, uint32_t version) {
368+
m_language = SourceLanguage(name, version);
369+
}
361370

362371
bool DoesCoerceToId() const { return m_coerce_to_id; }
363372

@@ -520,7 +529,7 @@ class EvaluateExpressionOptions {
520529

521530
private:
522531
ExecutionPolicy m_execution_policy = default_execution_policy;
523-
lldb::LanguageType m_language = lldb::eLanguageTypeUnknown;
532+
SourceLanguage m_language;
524533
std::string m_prefix;
525534
bool m_coerce_to_id = false;
526535
bool m_unwind_on_error = true;
@@ -1248,7 +1257,7 @@ class Target : public std::enable_shared_from_this<Target>,
12481257

12491258
UserExpression *
12501259
GetUserExpressionForLanguage(llvm::StringRef expr, llvm::StringRef prefix,
1251-
lldb::LanguageType language,
1260+
SourceLanguage language,
12521261
Expression::ResultType desired_type,
12531262
const EvaluateExpressionOptions &options,
12541263
ValueObject *ctx_obj, Status &error);

lldb/include/lldb/lldb-private-types.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,25 @@ struct RegisterSet {
9494
const uint32_t *registers;
9595
};
9696

97+
/// A type-erased pair of llvm::dwarf::SourceLanguageName and version.
98+
struct SourceLanguage {
99+
SourceLanguage() = default;
100+
SourceLanguage(lldb::LanguageType language_type);
101+
SourceLanguage(uint16_t name, uint32_t version)
102+
: name(name), version(version) {}
103+
SourceLanguage(std::optional<std::pair<uint16_t, uint32_t>> name_vers)
104+
: name(name_vers ? name_vers->first : 0),
105+
version(name_vers ? name_vers->second : 0) {}
106+
operator bool() const { return name > 0; }
107+
lldb::LanguageType AsLanguageType() const;
108+
llvm::StringRef GetDescription() const;
109+
bool IsC() const;
110+
bool IsObjC() const;
111+
bool IsCPlusPlus() const;
112+
uint16_t name = 0;
113+
uint32_t version = 0;
114+
};
115+
97116
struct OptionEnumValueElement {
98117
int64_t value;
99118
const char *string_value;

lldb/packages/Python/lldbsuite/test/configuration.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130

131131
# LLDB library directory.
132132
lldb_libs_dir = None
133+
lldb_obj_root = None
133134

134135
libcxx_include_dir = None
135136
libcxx_include_target_dir = None

lldb/packages/Python/lldbsuite/test/dotest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ def parseOptionsAndInitTestdirs():
429429
configuration.lldb_module_cache_dir = os.path.join(
430430
configuration.test_build_dir, "module-cache-lldb"
431431
)
432+
432433
if args.clang_module_cache_dir:
433434
configuration.clang_module_cache_dir = args.clang_module_cache_dir
434435
else:
@@ -441,6 +442,8 @@ def parseOptionsAndInitTestdirs():
441442

442443
if args.lldb_libs_dir:
443444
configuration.lldb_libs_dir = args.lldb_libs_dir
445+
if args.lldb_obj_root:
446+
configuration.lldb_obj_root = args.lldb_obj_root
444447

445448
if args.enabled_plugins:
446449
configuration.enabled_plugins = args.enabled_plugins

lldb/packages/Python/lldbsuite/test/dotest_args.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,17 @@ def create_parser():
249249
metavar="The lib directory inside the Swift build directory",
250250
help="The lib directory inside the Swift build directory.",
251251
)
252+
group.add_argument(
253+
"--lldb-obj-root",
254+
dest="lldb_obj_root",
255+
metavar="path",
256+
help="The path to the LLDB object files.",
257+
)
252258
group.add_argument(
253259
"--lldb-libs-dir",
254260
dest="lldb_libs_dir",
255261
metavar="path",
256-
help="The path to LLDB library directory (containing liblldb)",
262+
help="The path to LLDB library directory (containing liblldb).",
257263
)
258264
group.add_argument(
259265
"--enable-plugin",

lldb/packages/Python/lldbsuite/test/lldbtest.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,23 +1465,25 @@ def buildDriver(self, sources, exe_name):
14651465
d = {
14661466
"CXX_SOURCES": sources,
14671467
"EXE": exe_name,
1468-
"CFLAGS_EXTRAS": "%s %s -I%s"
1468+
"CFLAGS_EXTRAS": "%s %s -I%s -I%s"
14691469
% (
14701470
stdflag,
14711471
stdlibflag,
14721472
os.path.join(os.environ["LLDB_SRC"], "include"),
1473+
os.path.join(configuration.lldb_obj_root, "include"),
14731474
),
14741475
"LD_EXTRAS": "-L%s -lliblldb" % lib_dir,
14751476
}
14761477
else:
14771478
d = {
14781479
"CXX_SOURCES": sources,
14791480
"EXE": exe_name,
1480-
"CFLAGS_EXTRAS": "%s %s -I%s"
1481+
"CFLAGS_EXTRAS": "%s %s -I%s -I%s"
14811482
% (
14821483
stdflag,
14831484
stdlibflag,
14841485
os.path.join(os.environ["LLDB_SRC"], "include"),
1486+
os.path.join(configuration.lldb_obj_root, "include"),
14851487
),
14861488
"LD_EXTRAS": "-L%s -llldb -Wl,-rpath,%s" % (lib_dir, lib_dir),
14871489
}
@@ -1500,7 +1502,8 @@ def buildLibrary(self, sources, lib_name):
15001502
d = {
15011503
"DYLIB_CXX_SOURCES": sources,
15021504
"DYLIB_NAME": lib_name,
1503-
"CFLAGS_EXTRAS": "%s -stdlib=libc++" % stdflag,
1505+
"CFLAGS_EXTRAS": "%s -stdlib=libc++ -I%s"
1506+
% (stdflag, os.path.join(configuration.lldb_obj_root, "include")),
15041507
"FRAMEWORK_INCLUDES": "-F%s" % self.framework_dir,
15051508
"LD_EXTRAS": "%s -Wl,-rpath,%s -dynamiclib"
15061509
% (self.lib_lldb, self.framework_dir),
@@ -1509,16 +1512,24 @@ def buildLibrary(self, sources, lib_name):
15091512
d = {
15101513
"DYLIB_CXX_SOURCES": sources,
15111514
"DYLIB_NAME": lib_name,
1512-
"CFLAGS_EXTRAS": "%s -I%s "
1513-
% (stdflag, os.path.join(os.environ["LLDB_SRC"], "include")),
1515+
"CFLAGS_EXTRAS": "%s -I%s -I%s"
1516+
% (
1517+
stdflag,
1518+
os.path.join(os.environ["LLDB_SRC"], "include"),
1519+
os.path.join(configuration.lldb_obj_root, "include"),
1520+
),
15141521
"LD_EXTRAS": "-shared -l%s\liblldb.lib" % lib_dir,
15151522
}
15161523
else:
15171524
d = {
15181525
"DYLIB_CXX_SOURCES": sources,
15191526
"DYLIB_NAME": lib_name,
1520-
"CFLAGS_EXTRAS": "%s -I%s -fPIC"
1521-
% (stdflag, os.path.join(os.environ["LLDB_SRC"], "include")),
1527+
"CFLAGS_EXTRAS": "%s -I%s -I%s -fPIC"
1528+
% (
1529+
stdflag,
1530+
os.path.join(os.environ["LLDB_SRC"], "include"),
1531+
os.path.join(configuration.lldb_obj_root, "include"),
1532+
),
15221533
"LD_EXTRAS": "-shared -L%s -llldb -Wl,-rpath,%s" % (lib_dir, lib_dir),
15231534
}
15241535
if self.TraceOn():

lldb/source/API/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ if(LLDB_ENABLE_LUA)
3030
set(lldb_lua_wrapper ${lua_bindings_dir}/LLDBWrapLua.cpp)
3131
endif()
3232

33+
lldb_tablegen(../../include/lldb/API/SBLanguages.h -gen-lldb-sbapi-dwarf-enum
34+
SOURCE ${LLVM_MAIN_INCLUDE_DIR}/llvm/BinaryFormat/Dwarf.def
35+
TARGET lldb-sbapi-dwarf-enums)
36+
3337
add_lldb_library(liblldb SHARED ${option_framework}
3438
SBAddress.cpp
3539
SBAttachInfo.cpp
@@ -108,6 +112,9 @@ add_lldb_library(liblldb SHARED ${option_framework}
108112
${lldb_python_wrapper}
109113
${lldb_lua_wrapper}
110114

115+
DEPENDS
116+
lldb-sbapi-dwarf-enums
117+
111118
LINK_LIBS
112119
lldbBreakpoint
113120
lldbCore

lldb/source/API/SBExpressionOptions.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,13 @@ void SBExpressionOptions::SetLanguage(lldb::LanguageType language) {
156156
m_opaque_up->SetLanguage(language);
157157
}
158158

159+
void SBExpressionOptions::SetLanguage(SBSourceLanguageName name,
160+
uint32_t version) {
161+
LLDB_INSTRUMENT_VA(this, name, version);
162+
163+
m_opaque_up->SetLanguage(name, version);
164+
}
165+
159166
void SBExpressionOptions::SetCancelCallback(
160167
lldb::ExpressionCancelCallback callback, void *baton) {
161168
LLDB_INSTRUMENT_VA(this, callback, baton);

0 commit comments

Comments
 (0)