Skip to content

Commit 30cf609

Browse files
remove FILE* usage from SBStream.i
Summary: This patch removes FILE* and replaces it with SBFile and FileSP the SWIG interface for `SBStream.i`. And this is the last one. With this change, nothing in the python API will can access a FILE* method on the C++ side. Reviewers: JDevlieghere, jasonmolenda, labath Reviewed By: labath Subscribers: lldb-commits Tags: #lldb Differential Revision: https://reviews.llvm.org/D68960 llvm-svn: 374924
1 parent fdfd6ab commit 30cf609

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

lldb/include/lldb/API/SBStream.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ class LLDB_API SBStream {
3939

4040
void RedirectToFile(const char *path, bool append);
4141

42+
void RedirectToFile(lldb::SBFile file);
43+
44+
void RedirectToFile(lldb::FileSP file);
45+
4246
void RedirectToFileHandle(FILE *fh, bool transfer_fh_ownership);
4347

4448
void RedirectToFileDescriptor(int fd, bool transfer_fh_ownership);

lldb/packages/Python/lldbsuite/test/python_api/file_handle/TestFileHandle.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,3 +892,30 @@ def test_set_filehandle_none(self):
892892
sbf = self.debugger.GetInputFile()
893893
if sys.version_info.major >= 3:
894894
self.assertEqual(sbf.GetFile().fileno(), 0)
895+
896+
897+
@add_test_categories(['pyapi'])
898+
def test_sbstream(self):
899+
900+
with open(self.out_filename, 'w') as f:
901+
stream = lldb.SBStream()
902+
stream.RedirectToFile(f)
903+
stream.Print("zork")
904+
with open(self.out_filename, 'r') as f:
905+
self.assertEqual(f.read().strip(), "zork")
906+
907+
with open(self.out_filename, 'w') as f:
908+
stream = lldb.SBStream()
909+
stream.RedirectToFileHandle(f, True)
910+
stream.Print("Yendor")
911+
with open(self.out_filename, 'r') as f:
912+
self.assertEqual(f.read().strip(), "Yendor")
913+
914+
stream = lldb.SBStream()
915+
f = open(self.out_filename, 'w')
916+
stream.RedirectToFile(lldb.SBFile.Create(f, borrow=False))
917+
stream.Print("Frobozz")
918+
stream = None
919+
self.assertTrue(f.closed)
920+
with open(self.out_filename, 'r') as f:
921+
self.assertEqual(f.read().strip(), "Frobozz")

lldb/scripts/interface/SBStream.i

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,18 @@ public:
7575
RedirectToFile (const char *path, bool append);
7676

7777
void
78-
RedirectToFileHandle (FILE *fh, bool transfer_fh_ownership);
78+
RedirectToFile (lldb::SBFile file);
79+
80+
void
81+
RedirectToFile (lldb::FileSP file);
82+
83+
%extend {
84+
%feature("autodoc", "DEPRECATED, use RedirectToFile");
85+
void
86+
RedirectToFileHandle (lldb::FileSP file, bool transfer_fh_ownership) {
87+
self->RedirectToFile(file);
88+
}
89+
}
7990

8091
void
8192
RedirectToFileDescriptor (int fd, bool transfer_fh_ownership);

lldb/source/API/SBStream.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "lldb/API/SBStream.h"
1010

1111
#include "SBReproducerPrivate.h"
12+
#include "lldb/API/SBFile.h"
1213
#include "lldb/Core/StreamFile.h"
1314
#include "lldb/Host/FileSystem.h"
1415
#include "lldb/Utility/Status.h"
@@ -108,8 +109,19 @@ void SBStream::RedirectToFile(const char *path, bool append) {
108109
void SBStream::RedirectToFileHandle(FILE *fh, bool transfer_fh_ownership) {
109110
LLDB_RECORD_METHOD(void, SBStream, RedirectToFileHandle, (FILE *, bool), fh,
110111
transfer_fh_ownership);
112+
FileSP file = std::make_unique<NativeFile>(fh, transfer_fh_ownership);
113+
return RedirectToFile(file);
114+
}
115+
116+
void SBStream::RedirectToFile(SBFile file) {
117+
LLDB_RECORD_METHOD(void, SBStream, RedirectToFile, (SBFile), file)
118+
RedirectToFile(file.GetFile());
119+
}
120+
121+
void SBStream::RedirectToFile(FileSP file_sp) {
122+
LLDB_RECORD_METHOD(void, SBStream, RedirectToFile, (FileSP), file_sp);
111123

112-
if (fh == nullptr)
124+
if (!file_sp || !file_sp->IsValid())
113125
return;
114126

115127
std::string local_data;
@@ -120,7 +132,7 @@ void SBStream::RedirectToFileHandle(FILE *fh, bool transfer_fh_ownership) {
120132
local_data = static_cast<StreamString *>(m_opaque_up.get())->GetString();
121133
}
122134

123-
m_opaque_up = std::make_unique<StreamFile>(fh, transfer_fh_ownership);
135+
m_opaque_up = std::make_unique<StreamFile>(file_sp);
124136
m_is_file = true;
125137

126138
// If we had any data locally in our StreamString, then pass that along to
@@ -184,6 +196,8 @@ void RegisterMethods<SBStream>(Registry &R) {
184196
LLDB_REGISTER_METHOD(const char *, SBStream, GetData, ());
185197
LLDB_REGISTER_METHOD(size_t, SBStream, GetSize, ());
186198
LLDB_REGISTER_METHOD(void, SBStream, RedirectToFile, (const char *, bool));
199+
LLDB_REGISTER_METHOD(void, SBStream, RedirectToFile, (FileSP));
200+
LLDB_REGISTER_METHOD(void, SBStream, RedirectToFile, (SBFile));
187201
LLDB_REGISTER_METHOD(void, SBStream, RedirectToFileHandle, (FILE *, bool));
188202
LLDB_REGISTER_METHOD(void, SBStream, RedirectToFileDescriptor, (int, bool));
189203
LLDB_REGISTER_METHOD(void, SBStream, Clear, ());

0 commit comments

Comments
 (0)