Skip to content

Commit 94dd5f7

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 (cherry picked from commit 811bc94)
1 parent 5d9d659 commit 94dd5f7

35 files changed

+308
-125
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

@@ -201,7 +201,7 @@ class UserExpression : public Expression {
201201
virtual bool IsParseCacheable() { return true; }
202202
/// Return the language that should be used when parsing. To use the
203203
/// default, return eLanguageTypeUnknown.
204-
lldb::LanguageType Language() const override { return m_language; }
204+
SourceLanguage Language() const override { return m_language; }
205205

206206
/// Return the desired result type of the function, or eResultTypeAny if
207207
/// indifferent.
@@ -314,19 +314,22 @@ class UserExpression : public Expression {
314314
lldb::ProcessSP &process_sp,
315315
lldb::StackFrameSP &frame_sp);
316316

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

332335
} // namespace lldb_private

lldb/include/lldb/Symbol/TypeSystem.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -501,12 +501,10 @@ class TypeSystem : public PluginInterface,
501501
return IsPointerOrReferenceType(type, nullptr);
502502
}
503503

504-
virtual UserExpression *
505-
GetUserExpression(llvm::StringRef expr, llvm::StringRef prefix,
506-
lldb::LanguageType language,
507-
Expression::ResultType desired_type,
508-
const EvaluateExpressionOptions &options,
509-
ValueObject *ctx_obj) {
504+
virtual UserExpression *GetUserExpression(
505+
llvm::StringRef expr, llvm::StringRef prefix, SourceLanguage language,
506+
Expression::ResultType desired_type,
507+
const EvaluateExpressionOptions &options, ValueObject *ctx_obj) {
510508
return nullptr;
511509
}
512510

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
@@ -243,7 +243,7 @@ class TargetProperties : public Properties {
243243

244244
bool GetBreakpointsConsultPlatformAvoidList();
245245

246-
lldb::LanguageType GetLanguage() const;
246+
SourceLanguage GetLanguage() const;
247247

248248
llvm::StringRef GetExpressionPrefixContents();
249249

@@ -359,9 +359,18 @@ class EvaluateExpressionOptions {
359359
m_execution_policy = policy;
360360
}
361361

362-
lldb::LanguageType GetLanguage() const { return m_language; }
362+
SourceLanguage GetLanguage() const { return m_language; }
363363

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

366375
bool DoesCoerceToId() const { return m_coerce_to_id; }
367376

@@ -524,7 +533,7 @@ class EvaluateExpressionOptions {
524533

525534
private:
526535
ExecutionPolicy m_execution_policy = default_execution_policy;
527-
lldb::LanguageType m_language = lldb::eLanguageTypeUnknown;
536+
SourceLanguage m_language;
528537
std::string m_prefix;
529538
bool m_coerce_to_id = false;
530539
bool m_unwind_on_error = true;
@@ -1255,7 +1264,7 @@ class Target : public std::enable_shared_from_this<Target>,
12551264

12561265
UserExpression *
12571266
GetUserExpressionForLanguage(llvm::StringRef expr, llvm::StringRef prefix,
1258-
lldb::LanguageType language,
1267+
SourceLanguage language,
12591268
Expression::ResultType desired_type,
12601269
const EvaluateExpressionOptions &options,
12611270
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
@@ -96,6 +96,25 @@ struct RegisterSet {
9696
const uint32_t *registers;
9797
};
9898

99+
/// A type-erased pair of llvm::dwarf::SourceLanguageName and version.
100+
struct SourceLanguage {
101+
SourceLanguage() = default;
102+
SourceLanguage(lldb::LanguageType language_type);
103+
SourceLanguage(uint16_t name, uint32_t version)
104+
: name(name), version(version) {}
105+
SourceLanguage(std::optional<std::pair<uint16_t, uint32_t>> name_vers)
106+
: name(name_vers ? name_vers->first : 0),
107+
version(name_vers ? name_vers->second : 0) {}
108+
operator bool() const { return name > 0; }
109+
lldb::LanguageType AsLanguageType() const;
110+
llvm::StringRef GetDescription() const;
111+
bool IsC() const;
112+
bool IsObjC() const;
113+
bool IsCPlusPlus() const;
114+
uint16_t name = 0;
115+
uint32_t version = 0;
116+
};
117+
99118
struct OptionEnumValueElement {
100119
int64_t value;
101120
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
@@ -127,6 +127,7 @@
127127

128128
# LLDB library directory.
129129
lldb_libs_dir = None
130+
lldb_obj_root = None
130131

131132
libcxx_include_dir = None
132133
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
@@ -426,6 +426,7 @@ def parseOptionsAndInitTestdirs():
426426
configuration.lldb_module_cache_dir = os.path.join(
427427
configuration.test_build_dir, "module-cache-lldb"
428428
)
429+
429430
if args.clang_module_cache_dir:
430431
configuration.clang_module_cache_dir = args.clang_module_cache_dir
431432
else:
@@ -438,6 +439,8 @@ def parseOptionsAndInitTestdirs():
438439

439440
if args.lldb_libs_dir:
440441
configuration.lldb_libs_dir = args.lldb_libs_dir
442+
if args.lldb_obj_root:
443+
configuration.lldb_obj_root = args.lldb_obj_root
441444

442445
if args.enabled_plugins:
443446
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
@@ -247,11 +247,17 @@ def create_parser():
247247
metavar="The lib directory inside the Swift build directory",
248248
help="The lib directory inside the Swift build directory.",
249249
)
250+
group.add_argument(
251+
"--lldb-obj-root",
252+
dest="lldb_obj_root",
253+
metavar="path",
254+
help="The path to the LLDB object files.",
255+
)
250256
group.add_argument(
251257
"--lldb-libs-dir",
252258
dest="lldb_libs_dir",
253259
metavar="path",
254-
help="The path to LLDB library directory (containing liblldb)",
260+
help="The path to LLDB library directory (containing liblldb).",
255261
)
256262
group.add_argument(
257263
"--enable-plugin",

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

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,23 +1478,25 @@ def buildDriver(self, sources, exe_name):
14781478
d = {
14791479
"CXX_SOURCES": sources,
14801480
"EXE": exe_name,
1481-
"CFLAGS_EXTRAS": "%s %s -I%s"
1481+
"CFLAGS_EXTRAS": "%s %s -I%s -I%s"
14821482
% (
14831483
stdflag,
14841484
stdlibflag,
14851485
os.path.join(os.environ["LLDB_SRC"], "include"),
1486+
os.path.join(configuration.lldb_obj_root, "include"),
14861487
),
14871488
"LD_EXTRAS": "-L%s -lliblldb" % lib_dir,
14881489
}
14891490
else:
14901491
d = {
14911492
"CXX_SOURCES": sources,
14921493
"EXE": exe_name,
1493-
"CFLAGS_EXTRAS": "%s %s -I%s"
1494+
"CFLAGS_EXTRAS": "%s %s -I%s -I%s"
14941495
% (
14951496
stdflag,
14961497
stdlibflag,
14971498
os.path.join(os.environ["LLDB_SRC"], "include"),
1499+
os.path.join(configuration.lldb_obj_root, "include"),
14981500
),
14991501
"LD_EXTRAS": "-L%s -llldb -Wl,-rpath,%s" % (lib_dir, lib_dir),
15001502
}
@@ -1513,7 +1515,8 @@ def buildLibrary(self, sources, lib_name):
15131515
d = {
15141516
"DYLIB_CXX_SOURCES": sources,
15151517
"DYLIB_NAME": lib_name,
1516-
"CFLAGS_EXTRAS": "%s -stdlib=libc++" % stdflag,
1518+
"CFLAGS_EXTRAS": "%s -stdlib=libc++ -I%s"
1519+
% (stdflag, os.path.join(configuration.lldb_obj_root, "include")),
15171520
"FRAMEWORK_INCLUDES": "-F%s" % self.framework_dir,
15181521
"LD_EXTRAS": "%s -Wl,-rpath,%s -dynamiclib"
15191522
% (self.lib_lldb, self.framework_dir),
@@ -1522,16 +1525,24 @@ def buildLibrary(self, sources, lib_name):
15221525
d = {
15231526
"DYLIB_CXX_SOURCES": sources,
15241527
"DYLIB_NAME": lib_name,
1525-
"CFLAGS_EXTRAS": "%s -I%s "
1526-
% (stdflag, os.path.join(os.environ["LLDB_SRC"], "include")),
1528+
"CFLAGS_EXTRAS": "%s -I%s -I%s"
1529+
% (
1530+
stdflag,
1531+
os.path.join(os.environ["LLDB_SRC"], "include"),
1532+
os.path.join(configuration.lldb_obj_root, "include"),
1533+
),
15271534
"LD_EXTRAS": "-shared -l%s\liblldb.lib" % lib_dir,
15281535
}
15291536
else:
15301537
d = {
15311538
"DYLIB_CXX_SOURCES": sources,
15321539
"DYLIB_NAME": lib_name,
1533-
"CFLAGS_EXTRAS": "%s -I%s -fPIC"
1534-
% (stdflag, os.path.join(os.environ["LLDB_SRC"], "include")),
1540+
"CFLAGS_EXTRAS": "%s -I%s -I%s -fPIC"
1541+
% (
1542+
stdflag,
1543+
os.path.join(os.environ["LLDB_SRC"], "include"),
1544+
os.path.join(configuration.lldb_obj_root, "include"),
1545+
),
15351546
"LD_EXTRAS": "-shared -L%s -llldb -Wl,-rpath,%s" % (lib_dir, lib_dir),
15361547
}
15371548
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
@@ -110,6 +114,9 @@ add_lldb_library(liblldb SHARED ${option_framework}
110114
${lldb_python_wrapper}
111115
${lldb_lua_wrapper}
112116

117+
DEPENDS
118+
lldb-sbapi-dwarf-enums
119+
113120
LINK_LIBS
114121
lldbBreakpoint
115122
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)