Skip to content

Commit 045c483

Browse files
authored
Merge pull request #1953 from Teemperor/RemoveSaveExpressionTextToTempFile
[ExpressionSourceCode] Move Swift-only function SaveExpressionTextToTempFile to the Swift source. apple-llvm-split-commit: b409507cd31a0b7a4f111de6f07d67a6e2a854ca apple-llvm-split-dir: lldb/
2 parents 40a1b3a + 48d6a85 commit 045c483

File tree

5 files changed

+66
-66
lines changed

5 files changed

+66
-66
lines changed

lldb/include/lldb/Expression/ExpressionSourceCode.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ class ExpressionSourceCode {
4545
const EvaluateExpressionOptions &options,
4646
ExecutionContext &exe_ctx, uint32_t &first_body_line) const;
4747

48-
static bool
49-
SaveExpressionTextToTempFile(llvm::StringRef text,
50-
const EvaluateExpressionOptions &options,
51-
std::string &expr_source_path);
5248
// Given a string returned by GetText, find the beginning and end of the body
5349
// passed to CreateWrapped. Return true if the bounds could be found. This
5450
// will also work on text with FixItHints applied.

lldb/source/Expression/ExpressionSourceCode.cpp

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -226,65 +226,6 @@ static void AddLocalVariableDecls(const lldb::VariableListSP &var_list_sp,
226226
}
227227
}
228228

229-
bool ExpressionSourceCode::SaveExpressionTextToTempFile(
230-
llvm::StringRef text, const EvaluateExpressionOptions &options,
231-
std::string &expr_source_path) {
232-
bool success = false;
233-
234-
const uint32_t expr_number = options.GetExpressionNumber();
235-
236-
const bool playground = options.GetPlaygroundTransformEnabled();
237-
const bool repl = options.GetREPLEnabled();
238-
239-
llvm::StringRef file_prefix;
240-
if (playground)
241-
file_prefix = "playground";
242-
else if (repl)
243-
file_prefix = "repl";
244-
else
245-
file_prefix = "expr";
246-
247-
llvm::Twine prefix = llvm::Twine(file_prefix).concat(llvm::Twine(expr_number));
248-
249-
llvm::StringRef suffix;
250-
switch (options.GetLanguage()) {
251-
default:
252-
suffix = ".cpp";
253-
break;
254-
255-
case lldb::eLanguageTypeSwift:
256-
suffix = ".swift";
257-
break;
258-
}
259-
260-
int temp_fd;
261-
llvm::SmallString<128> buffer;
262-
std::error_code err =
263-
llvm::sys::fs::createTemporaryFile(prefix, suffix, temp_fd, buffer);
264-
if (!err) {
265-
lldb_private::File file(temp_fd, true);
266-
const size_t text_len = text.size();
267-
size_t bytes_written = text_len;
268-
if (file.Write(text.data(), bytes_written).Success()) {
269-
if (bytes_written == text_len) {
270-
// Make sure we have a newline in the file at the end
271-
bytes_written = 1;
272-
file.Write("\n", bytes_written);
273-
if (bytes_written == 1)
274-
success = true;
275-
}
276-
}
277-
if (!success)
278-
llvm::sys::fs::remove(expr_source_path);
279-
}
280-
if (!success)
281-
expr_source_path.clear();
282-
else
283-
expr_source_path = buffer.str().str();
284-
285-
return success;
286-
}
287-
288229
/// Format the OS name the way that Swift availability attributes do.
289230
static llvm::StringRef getAvailabilityName(const llvm::Triple &triple) {
290231
swift::LangOptions lang_options;

lldb/source/Plugins/ExpressionParser/Swift/SwiftASTManipulator.cpp

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ __builtin_logger_initialize()
123123
pound_file, pound_line, orig_text);
124124
text = fixed_text.GetString().data();
125125
} else if (generate_debug_info) {
126-
if (ExpressionSourceCode::SaveExpressionTextToTempFile(orig_text, options,
126+
if (SwiftASTManipulator::SaveExpressionTextToTempFile(orig_text, options,
127127
expr_source_path)) {
128128
fixed_text.Printf("#sourceLocation(file: \"%s\", line: 1)\n%s\n",
129129
expr_source_path.c_str(), orig_text);
@@ -1396,3 +1396,62 @@ swift::ValueDecl *SwiftASTManipulator::MakeGlobalTypealias(
13961396

13971397
return type_alias_decl;
13981398
}
1399+
1400+
bool SwiftASTManipulator::SaveExpressionTextToTempFile(
1401+
llvm::StringRef text, const EvaluateExpressionOptions &options,
1402+
std::string &expr_source_path) {
1403+
bool success = false;
1404+
1405+
const uint32_t expr_number = options.GetExpressionNumber();
1406+
1407+
const bool playground = options.GetPlaygroundTransformEnabled();
1408+
const bool repl = options.GetREPLEnabled();
1409+
1410+
llvm::StringRef file_prefix;
1411+
if (playground)
1412+
file_prefix = "playground";
1413+
else if (repl)
1414+
file_prefix = "repl";
1415+
else
1416+
file_prefix = "expr";
1417+
1418+
llvm::Twine prefix = llvm::Twine(file_prefix).concat(llvm::Twine(expr_number));
1419+
1420+
llvm::StringRef suffix;
1421+
switch (options.GetLanguage()) {
1422+
default:
1423+
suffix = ".cpp";
1424+
break;
1425+
1426+
case lldb::eLanguageTypeSwift:
1427+
suffix = ".swift";
1428+
break;
1429+
}
1430+
1431+
int temp_fd;
1432+
llvm::SmallString<128> buffer;
1433+
std::error_code err =
1434+
llvm::sys::fs::createTemporaryFile(prefix, suffix, temp_fd, buffer);
1435+
if (!err) {
1436+
lldb_private::File file(temp_fd, true);
1437+
const size_t text_len = text.size();
1438+
size_t bytes_written = text_len;
1439+
if (file.Write(text.data(), bytes_written).Success()) {
1440+
if (bytes_written == text_len) {
1441+
// Make sure we have a newline in the file at the end
1442+
bytes_written = 1;
1443+
file.Write("\n", bytes_written);
1444+
if (bytes_written == 1)
1445+
success = true;
1446+
}
1447+
}
1448+
if (!success)
1449+
llvm::sys::fs::remove(expr_source_path);
1450+
}
1451+
if (!success)
1452+
expr_source_path.clear();
1453+
else
1454+
expr_source_path = buffer.str().str();
1455+
1456+
return success;
1457+
}

lldb/source/Plugins/ExpressionParser/Swift/SwiftASTManipulator.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ class SwiftASTManipulator : public SwiftASTManipulatorBase {
182182
return "\n/*__LLDB_USER_END__*/";
183183
}
184184

185+
static bool
186+
SaveExpressionTextToTempFile(llvm::StringRef text,
187+
const EvaluateExpressionOptions &options,
188+
std::string &expr_source_path);
189+
185190
private:
186191
uint32_t m_tmpname_idx = 0;
187192

lldb/source/Plugins/ExpressionParser/Swift/SwiftExpressionParser.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -934,8 +934,7 @@ CreateMainFile(SwiftASTContext &swift_ast_context, StringRef filename,
934934

935935
if (generate_debug_info) {
936936
std::string temp_source_path;
937-
if (ExpressionSourceCode::SaveExpressionTextToTempFile(text, options,
938-
temp_source_path)) {
937+
if (SwiftASTManipulator::SaveExpressionTextToTempFile(text, options, temp_source_path)) {
939938
auto error_or_buffer_ap =
940939
llvm::MemoryBuffer::getFile(temp_source_path.c_str());
941940
if (error_or_buffer_ap.getError() == std::error_condition()) {

0 commit comments

Comments
 (0)