[lldb] (Semi-automatically) format .swig files

I've found my recent ventures into the swig land painful because
of the strange way they are formatted. This patch attempts to alleviate
future headaches by formatting these files into something resembling the
normal llvm style.

Unfortunately, completely formatting these files automatically does not
work because clang format gets confused by swigs % syntax, so I have
employed a hybrid approach where I formatted blocks of c++ code with
clang-format and then manually massaged the code until it looked
reasonable (and compiled).

I don't expect these files to remain perfectly formatted (although, if
one's editor is configured to configure the current line/block on
request, one can get pretty good results by using it judiciously), but
at least it will prevent the (mangled form of the) old lldb style being
proliferated endlessly.

Differential Revision: https://reviews.llvm.org/D115736
This commit is contained in:
Pavel Labath 2021-12-14 17:20:23 +01:00
parent 5b139a583d
commit 9d5e37ed8c
5 changed files with 1253 additions and 1401 deletions

View File

@ -1,27 +1,20 @@
template <typename SBClass>
void
PushSBClass (lua_State* L, SBClass* obj);
template <typename SBClass> void PushSBClass(lua_State *L, SBClass *obj);
void
PushSBClass (lua_State* L, lldb::SBFrame* frame_sb)
{
SWIG_NewPointerObj(L, frame_sb, SWIGTYPE_p_lldb__SBFrame, 0);
void PushSBClass(lua_State *L, lldb::SBFrame *frame_sb) {
SWIG_NewPointerObj(L, frame_sb, SWIGTYPE_p_lldb__SBFrame, 0);
}
void
PushSBClass (lua_State* L, lldb::SBBreakpointLocation* breakpoint_location_sb)
{
SWIG_NewPointerObj(L, breakpoint_location_sb, SWIGTYPE_p_lldb__SBBreakpointLocation, 0);
void PushSBClass(lua_State *L,
lldb::SBBreakpointLocation *breakpoint_location_sb) {
SWIG_NewPointerObj(L, breakpoint_location_sb,
SWIGTYPE_p_lldb__SBBreakpointLocation, 0);
}
void
PushSBClass (lua_State* L, lldb::SBWatchpoint* watchpoint_sb)
{
SWIG_NewPointerObj(L, watchpoint_sb, SWIGTYPE_p_lldb__SBWatchpoint, 0);
void PushSBClass(lua_State *L, lldb::SBWatchpoint *watchpoint_sb) {
SWIG_NewPointerObj(L, watchpoint_sb, SWIGTYPE_p_lldb__SBWatchpoint, 0);
}
void
PushSBClass (lua_State* L, lldb::SBStructuredData* structured_data_sb)
{
SWIG_NewPointerObj(L, structured_data_sb, SWIGTYPE_p_lldb__SBStructuredData, 0);
void PushSBClass(lua_State *L, lldb::SBStructuredData *structured_data_sb) {
SWIG_NewPointerObj(L, structured_data_sb, SWIGTYPE_p_lldb__SBStructuredData,
0);
}

View File

@ -76,11 +76,11 @@ LLDB_NUMBER_TYPEMAP(enum SWIGTYPE);
// typemap for a char buffer
%typemap(in) (char *dst, size_t dst_len) {
$2 = luaL_checkinteger(L, $input);
if ($2 <= 0) {
return luaL_error(L, "Positive integer expected");
}
$1 = (char *) malloc($2);
$2 = luaL_checkinteger(L, $input);
if ($2 <= 0) {
return luaL_error(L, "Positive integer expected");
}
$1 = (char *)malloc($2);
}
// SBProcess::ReadCStringFromMemory() uses a void*, but needs to be treated
@ -92,14 +92,14 @@ LLDB_NUMBER_TYPEMAP(enum SWIGTYPE);
// Return the char buffer. Discarding any previous return result
%typemap(argout) (char *dst, size_t dst_len) {
lua_pop(L, 1); // Blow away the previous result
if ($result == 0) {
lua_pushliteral(L, "");
} else {
lua_pushlstring(L, (const char *)$1, $result);
}
free($1);
// SWIG_arg was already incremented
lua_pop(L, 1); // Blow away the previous result
if ($result == 0) {
lua_pushliteral(L, "");
} else {
lua_pushlstring(L, (const char *)$1, $result);
}
free($1);
// SWIG_arg was already incremented
}
// SBProcess::ReadCStringFromMemory() uses a void*, but needs to be treated
@ -114,18 +114,18 @@ LLDB_NUMBER_TYPEMAP(enum SWIGTYPE);
// Typemap for handling a snprintf-like API like SBThread::GetStopDescription.
%typemap(in) (char *dst_or_null, size_t dst_len) {
$2 = luaL_checkinteger(L, $input);
if ($2 <= 0) {
return luaL_error(L, "Positive integer expected");
}
$1 = (char *)malloc($2);
$2 = luaL_checkinteger(L, $input);
if ($2 <= 0) {
return luaL_error(L, "Positive integer expected");
}
$1 = (char *)malloc($2);
}
%typemap(argout) (char *dst_or_null, size_t dst_len) {
lua_pop(L, 1); // Blow away the previous result
lua_pushlstring(L, (const char *)$1, $result);
free($1);
// SWIG_arg was already incremented
lua_pop(L, 1); // Blow away the previous result
lua_pushlstring(L, (const char *)$1, $result);
free($1);
// SWIG_arg was already incremented
}
//===----------------------------------------------------------------------===//
@ -133,22 +133,22 @@ LLDB_NUMBER_TYPEMAP(enum SWIGTYPE);
// Typemap for handling SBModule::GetVersion
%typemap(in) (uint32_t *versions, uint32_t num_versions) {
$2 = 99;
$1 = (uint32_t *)malloc(sizeof(uint32_t) * $2);
$2 = 99;
$1 = (uint32_t *)malloc(sizeof(uint32_t) * $2);
}
%typemap(argout) (uint32_t *versions, uint32_t num_versions) {
uint32_t count = result;
if (count >= $2)
count = $2;
lua_newtable(L);
int i = 0;
while (i++ < count) {
lua_pushinteger(L, $1[i - 1]);
lua_seti(L, -2, i);
}
SWIG_arg++;
free($1);
uint32_t count = result;
if (count >= $2)
count = $2;
lua_newtable(L);
int i = 0;
while (i++ < count) {
lua_pushinteger(L, $1[i - 1]);
lua_seti(L, -2, i);
}
SWIG_arg++;
free($1);
}
//===----------------------------------------------------------------------===//
@ -156,15 +156,15 @@ LLDB_NUMBER_TYPEMAP(enum SWIGTYPE);
// Typemap for handling SBDebugger::SetLoggingCallback
%typemap(in) (lldb::LogOutputCallback log_callback, void *baton) {
$1 = LLDBSwigLuaCallLuaLogOutputCallback;
$2 = (void *)L;
$1 = LLDBSwigLuaCallLuaLogOutputCallback;
$2 = (void *)L;
luaL_checktype(L, 2, LUA_TFUNCTION);
lua_settop(L, 2);
luaL_checktype(L, 2, LUA_TFUNCTION);
lua_settop(L, 2);
lua_pushlightuserdata(L, (void *)&LLDBSwigLuaCallLuaLogOutputCallback);
lua_insert(L, 2);
lua_settable(L, LUA_REGISTRYINDEX);
lua_pushlightuserdata(L, (void *)&LLDBSwigLuaCallLuaLogOutputCallback);
lua_insert(L, 2);
lua_settable(L, LUA_REGISTRYINDEX);
}
//===----------------------------------------------------------------------===//
@ -172,20 +172,20 @@ LLDB_NUMBER_TYPEMAP(enum SWIGTYPE);
// Typemap for handling SBEvent::SBEvent(uint32_t event, const char *cstr, uint32_t cstr_len)
%typemap(in) (const char *cstr, uint32_t cstr_len) {
$1 = (char *)luaL_checklstring(L, $input, (size_t *)&$2);
$1 = (char *)luaL_checklstring(L, $input, (size_t *)&$2);
}
// Typemap for handling SBProcess::PutSTDIN
%typemap(in) (const char *src, size_t src_len) {
$1 = (char *)luaL_checklstring(L, $input, &$2);
$1 = (char *)luaL_checklstring(L, $input, &$2);
}
// Typemap for handling SBProcess::WriteMemory, SBTarget::GetInstructions...
%typemap(in) (const void *buf, size_t size),
(const void *data, size_t data_len) {
$1 = (void *)luaL_checklstring(L, $input, &$2);
$1 = (void *)luaL_checklstring(L, $input, &$2);
}
//===----------------------------------------------------------------------===//
@ -195,35 +195,35 @@ LLDB_NUMBER_TYPEMAP(enum SWIGTYPE);
// It should accept a Lua table of strings, for stuff like "argv" and "envp".
%typemap(in) char ** {
if (lua_istable(L, $input)) {
size_t size = lua_rawlen(L, $input);
$1 = (char **)malloc((size + 1) * sizeof(char *));
int i = 0, j = 0;
while (i++ < size) {
lua_rawgeti(L, $input, i);
if (!lua_isstring(L, -1)) {
// if current element cannot be converted to string, raise an error
lua_pop(L, 1);
return luaL_error(L, "List should only contain strings");
}
$1[j++] = (char *)lua_tostring(L, -1);
lua_pop(L, 1);
if (lua_istable(L, $input)) {
size_t size = lua_rawlen(L, $input);
$1 = (char **)malloc((size + 1) * sizeof(char *));
int i = 0, j = 0;
while (i++ < size) {
lua_rawgeti(L, $input, i);
if (!lua_isstring(L, -1)) {
// if current element cannot be converted to string, raise an error
lua_pop(L, 1);
return luaL_error(L, "List should only contain strings");
}
$1[j] = 0;
} else if (lua_isnil(L, $input)) {
// "nil" is also acceptable, equivalent as an empty table
$1 = NULL;
} else {
return luaL_error(L, "A list of strings expected");
}
$1[j++] = (char *)lua_tostring(L, -1);
lua_pop(L, 1);
}
$1[j] = 0;
} else if (lua_isnil(L, $input)) {
// "nil" is also acceptable, equivalent as an empty table
$1 = NULL;
} else {
return luaL_error(L, "A list of strings expected");
}
}
%typemap(freearg) char ** {
free((char *) $1);
free((char *) $1);
}
%typecheck(SWIG_TYPECHECK_STRING_ARRAY) char ** {
$1 = (lua_istable(L, $input) || lua_isnil(L, $input));
$1 = (lua_istable(L, $input) || lua_isnil(L, $input));
}
//===----------------------------------------------------------------------===//
@ -231,30 +231,30 @@ LLDB_NUMBER_TYPEMAP(enum SWIGTYPE);
// Typemap for file handles (e.g. used in SBDebugger::SetOutputFile)
%typemap(in) lldb::FileSP {
luaL_Stream *p = (luaL_Stream *)luaL_checkudata(L, $input, LUA_FILEHANDLE);
lldb::FileSP file_sp;
file_sp = std::make_shared<lldb_private::NativeFile>(p->f, false);
if (!file_sp->IsValid())
return luaL_error(L, "Invalid file");
$1 = file_sp;
luaL_Stream *p = (luaL_Stream *)luaL_checkudata(L, $input, LUA_FILEHANDLE);
lldb::FileSP file_sp;
file_sp = std::make_shared<lldb_private::NativeFile>(p->f, false);
if (!file_sp->IsValid())
return luaL_error(L, "Invalid file");
$1 = file_sp;
}
%typecheck(SWIG_TYPECHECK_POINTER) lldb::FileSP {
$1 = (lua_isuserdata(L, $input)) &&
(luaL_testudata(L, $input, LUA_FILEHANDLE) != nullptr);
$1 = (lua_isuserdata(L, $input)) &&
(luaL_testudata(L, $input, LUA_FILEHANDLE) != nullptr);
}
// Typemap for file handles (e.g. used in SBDebugger::GetOutputFileHandle)
%typemap(out) lldb::FileSP {
lldb::FileSP &sp = $1;
if (sp && sp->IsValid()) {
luaL_Stream *p = (luaL_Stream *)lua_newuserdata(L, sizeof(luaL_Stream));
p->closef = &LLDBSwigLuaCloseFileHandle;
p->f = sp->GetStream();
luaL_setmetatable(L, LUA_FILEHANDLE);
SWIG_arg++;
}
lldb::FileSP &sp = $1;
if (sp && sp->IsValid()) {
luaL_Stream *p = (luaL_Stream *)lua_newuserdata(L, sizeof(luaL_Stream));
p->closef = &LLDBSwigLuaCloseFileHandle;
p->f = sp->GetStream();
luaL_setmetatable(L, LUA_FILEHANDLE);
SWIG_arg++;
}
}
//===----------------------------------------------------------------------===//
@ -266,29 +266,29 @@ LLDB_NUMBER_TYPEMAP(enum SWIGTYPE);
(int64_t* array, size_t array_len),
(int32_t* array, size_t array_len),
(double* array, size_t array_len) {
if (lua_istable(L, $input)) {
// It should accept a table of numbers.
$2 = lua_rawlen(L, $input);
$1 = ($1_ltype)malloc(($2) * sizeof($*1_type));
int i = 0, j = 0;
while (i++ < $2) {
lua_rawgeti(L, $input, i);
if (!lua_isnumber(L, -1)) {
// if current element cannot be converted to number, raise an error
lua_pop(L, 1);
return luaL_error(L, "List should only contain numbers");
}
$1[j++] = ($*1_ltype)lua_tonumber(L, -1);
lua_pop(L, 1);
if (lua_istable(L, $input)) {
// It should accept a table of numbers.
$2 = lua_rawlen(L, $input);
$1 = ($1_ltype)malloc(($2) * sizeof($*1_type));
int i = 0, j = 0;
while (i++ < $2) {
lua_rawgeti(L, $input, i);
if (!lua_isnumber(L, -1)) {
// if current element cannot be converted to number, raise an error
lua_pop(L, 1);
return luaL_error(L, "List should only contain numbers");
}
} else if (lua_isnil(L, $input)) {
// "nil" is also acceptable, equivalent as an empty table
$1 = NULL;
$2 = 0;
} else {
// else raise an error
return luaL_error(L, "A list of numbers expected.");
}
$1[j++] = ($*1_ltype) lua_tonumber(L, -1);
lua_pop(L, 1);
}
} else if (lua_isnil(L, $input)) {
// "nil" is also acceptable, equivalent as an empty table
$1 = NULL;
$2 = 0;
} else {
// else raise an error
return luaL_error(L, "A list of numbers expected.");
}
}
%typemap(freearg) (uint64_t* array, size_t array_len),
@ -296,7 +296,7 @@ LLDB_NUMBER_TYPEMAP(enum SWIGTYPE);
(int64_t* array, size_t array_len),
(int32_t* array, size_t array_len),
(double* array, size_t array_len) {
free($1);
free($1);
}
//===----------------------------------------------------------------------===//
@ -304,13 +304,12 @@ LLDB_NUMBER_TYPEMAP(enum SWIGTYPE);
// Typemap for SBCommandReturnObject::PutCString
%typemap(in) (const char *string, int len) {
if (lua_isnil(L, $input)) {
$1 = NULL;
$2 = 0;
}
else {
$1 = (char *)luaL_checklstring(L, $input, (size_t *)&$2);
}
if (lua_isnil(L, $input)) {
$1 = NULL;
$2 = 0;
} else {
$1 = (char *)luaL_checklstring(L, $input, (size_t *)&$2);
}
}
//===----------------------------------------------------------------------===//

View File

@ -1,101 +1,87 @@
%header %{
template <typename T>
void
PushSBClass(lua_State* L, T* obj);
template <typename T> void PushSBClass(lua_State * L, T * obj);
// This function is called from Lua::CallBreakpointCallback
llvm::Expected<bool>
lldb_private::LLDBSwigLuaBreakpointCallbackFunction
(
lua_State *L,
lldb::StackFrameSP stop_frame_sp,
lldb::BreakpointLocationSP bp_loc_sp,
const StructuredDataImpl &extra_args_impl
)
{
lldb::SBFrame sb_frame(stop_frame_sp);
lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp);
int nargs = 2;
llvm::Expected<bool> lldb_private::LLDBSwigLuaBreakpointCallbackFunction(
lua_State * L, lldb::StackFrameSP stop_frame_sp,
lldb::BreakpointLocationSP bp_loc_sp,
const StructuredDataImpl &extra_args_impl) {
lldb::SBFrame sb_frame(stop_frame_sp);
lldb::SBBreakpointLocation sb_bp_loc(bp_loc_sp);
int nargs = 2;
lldb::SBStructuredData extra_args(extra_args_impl);
lldb::SBStructuredData extra_args(extra_args_impl);
// Push the Lua wrappers
PushSBClass(L, &sb_frame);
PushSBClass(L, &sb_bp_loc);
// Push the Lua wrappers
PushSBClass(L, &sb_frame);
PushSBClass(L, &sb_bp_loc);
if (extra_args.IsValid()) {
PushSBClass(L, &extra_args);
nargs++;
}
if (extra_args.IsValid()) {
PushSBClass(L, &extra_args);
nargs++;
}
// Call into the Lua callback passing 'sb_frame' and 'sb_bp_loc'.
// Expects a boolean return.
if (lua_pcall(L, nargs, 1, 0) != LUA_OK) {
llvm::Error E = llvm::make_error<llvm::StringError>(
llvm::formatv("{0}\n", lua_tostring(L, -1)),
llvm::inconvertibleErrorCode());
// Pop error message from the stack.
lua_pop(L, 1);
return std::move(E);
}
// Call into the Lua callback passing 'sb_frame' and 'sb_bp_loc'.
// Expects a boolean return.
if (lua_pcall(L, nargs, 1, 0) != LUA_OK) {
llvm::Error E = llvm::make_error<llvm::StringError>(
llvm::formatv("{0}\n", lua_tostring(L, -1)),
llvm::inconvertibleErrorCode());
// Pop error message from the stack.
lua_pop(L, 1);
return std::move(E);
}
// Boolean return from the callback
bool stop = lua_toboolean(L, -1);
lua_pop(L, 1);
// Boolean return from the callback
bool stop = lua_toboolean(L, -1);
lua_pop(L, 1);
return stop;
return stop;
}
// This function is called from Lua::CallWatchpointCallback
llvm::Expected<bool>
lldb_private::LLDBSwigLuaWatchpointCallbackFunction
(
lua_State *L,
lldb::StackFrameSP stop_frame_sp,
lldb::WatchpointSP wp_sp
)
{
lldb::SBFrame sb_frame(stop_frame_sp);
lldb::SBWatchpoint sb_wp(wp_sp);
int nargs = 2;
llvm::Expected<bool> lldb_private::LLDBSwigLuaWatchpointCallbackFunction(
lua_State * L, lldb::StackFrameSP stop_frame_sp, lldb::WatchpointSP wp_sp) {
lldb::SBFrame sb_frame(stop_frame_sp);
lldb::SBWatchpoint sb_wp(wp_sp);
int nargs = 2;
// Push the Lua wrappers
PushSBClass(L, &sb_frame);
PushSBClass(L, &sb_wp);
// Push the Lua wrappers
PushSBClass(L, &sb_frame);
PushSBClass(L, &sb_wp);
// Call into the Lua callback passing 'sb_frame' and 'sb_wp'.
// Expects a boolean return.
if (lua_pcall(L, nargs, 1, 0) != LUA_OK) {
llvm::Error E = llvm::make_error<llvm::StringError>(
llvm::formatv("{0}\n", lua_tostring(L, -1)),
llvm::inconvertibleErrorCode());
// Pop error message from the stack.
lua_pop(L, 1);
return std::move(E);
}
// Call into the Lua callback passing 'sb_frame' and 'sb_wp'.
// Expects a boolean return.
if (lua_pcall(L, nargs, 1, 0) != LUA_OK) {
llvm::Error E = llvm::make_error<llvm::StringError>(
llvm::formatv("{0}\n", lua_tostring(L, -1)),
llvm::inconvertibleErrorCode());
// Pop error message from the stack.
lua_pop(L, 1);
return std::move(E);
}
// Boolean return from the callback
bool stop = lua_toboolean(L, -1);
lua_pop(L, 1);
// Boolean return from the callback
bool stop = lua_toboolean(L, -1);
lua_pop(L, 1);
return stop;
return stop;
}
static void
LLDBSwigLuaCallLuaLogOutputCallback(const char *str, void *baton) {
lua_State *L = (lua_State *)baton;
static void LLDBSwigLuaCallLuaLogOutputCallback(const char *str, void *baton) {
lua_State *L = (lua_State *)baton;
lua_pushlightuserdata(L, (void *)&LLDBSwigLuaCallLuaLogOutputCallback);
lua_gettable(L, LUA_REGISTRYINDEX);
lua_pushlightuserdata(L, (void *)&LLDBSwigLuaCallLuaLogOutputCallback);
lua_gettable(L, LUA_REGISTRYINDEX);
// FIXME: There's no way to report errors back to the user
lua_pushstring(L, str);
lua_pcall(L, 1, 0, 0);
// FIXME: There's no way to report errors back to the user
lua_pushstring(L, str);
lua_pcall(L, 1, 0, 0);
}
static int LLDBSwigLuaCloseFileHandle(lua_State *L) {
return luaL_error(L, "You cannot close a file handle used by lldb.");
static int LLDBSwigLuaCloseFileHandle(lua_State * L) {
return luaL_error(L, "You cannot close a file handle used by lldb.");
}
%}

View File

@ -12,22 +12,22 @@
PythonList list(PyRefType::Borrowed, $input);
int size = list.GetSize();
int i = 0;
$1 = (char**)malloc((size+1)*sizeof(char*));
$1 = (char **)malloc((size + 1) * sizeof(char *));
for (i = 0; i < size; i++) {
PythonString py_str = list.GetItemAtIndex(i).AsType<PythonString>();
if (!py_str.IsAllocated()) {
PyErr_SetString(PyExc_TypeError,"list must contain strings");
PyErr_SetString(PyExc_TypeError, "list must contain strings");
free($1);
return nullptr;
}
$1[i] = const_cast<char*>(py_str.GetString().data());
$1[i] = const_cast<char *>(py_str.GetString().data());
}
$1[i] = 0;
} else if ($input == Py_None) {
$1 = NULL;
$1 = NULL;
} else {
PyErr_SetString(PyExc_TypeError,"not a list");
PyErr_SetString(PyExc_TypeError, "not a list");
return NULL;
}
}
@ -41,12 +41,12 @@
int i = 0;
for (i = 0; i < size; i++) {
PythonString s = list.GetItemAtIndex(i).AsType<PythonString>();
if (!s.IsAllocated()) { $1 = 0; }
if (!s.IsAllocated()) {
$1 = 0;
}
}
}
else
{
$1 = ( ($input == Py_None) ? 1 : 0);
} else {
$1 = (($input == Py_None) ? 1 : 0);
}
}
@ -58,7 +58,8 @@
int len;
int i;
len = 0;
while ($1[len]) len++;
while ($1[len])
len++;
PythonList list(len);
for (i = 0; i < len; i++)
list.SetItemAtIndex(i, PythonString($1[i]));
@ -76,7 +77,7 @@
%typemap(in) lldb::StateType {
PythonObject obj = Retain<PythonObject>($input);
unsigned long long state_type_value =
unwrapOrSetPythonException(As<unsigned long long>(obj));
unwrapOrSetPythonException(As<unsigned long long>(obj));
if (PyErr_Occurred())
return nullptr;
if (state_type_value > lldb::StateType::kLastStateType) {
@ -90,16 +91,16 @@
// typemap for a char buffer
%typemap(in) (char *dst, size_t dst_len) {
if (!PyInt_Check($input)) {
PyErr_SetString(PyExc_ValueError, "Expecting an integer");
return NULL;
}
$2 = PyInt_AsLong($input);
if ($2 <= 0) {
PyErr_SetString(PyExc_ValueError, "Positive integer expected");
return NULL;
}
$1 = (char *) malloc($2);
if (!PyInt_Check($input)) {
PyErr_SetString(PyExc_ValueError, "Expecting an integer");
return NULL;
}
$2 = PyInt_AsLong($input);
if ($2 <= 0) {
PyErr_SetString(PyExc_ValueError, "Positive integer expected");
return NULL;
}
$1 = (char *)malloc($2);
}
// SBProcess::ReadCStringFromMemory() uses a void*, but needs to be treated
// as char data instead of byte data.
@ -107,17 +108,17 @@
// Return the char buffer. Discarding any previous return result
%typemap(argout) (char *dst, size_t dst_len) {
Py_XDECREF($result); /* Blow away any previous result */
if (result == 0) {
PythonString string("");
$result = string.release();
Py_INCREF($result);
} else {
llvm::StringRef ref(static_cast<const char*>($1), result);
PythonString string(ref);
$result = string.release();
}
free($1);
Py_XDECREF($result); /* Blow away any previous result */
if (result == 0) {
PythonString string("");
$result = string.release();
Py_INCREF($result);
} else {
llvm::StringRef ref(static_cast<const char *>($1), result);
PythonString string(ref);
$result = string.release();
}
free($1);
}
// SBProcess::ReadCStringFromMemory() uses a void*, but needs to be treated
// as char data instead of byte data.
@ -126,23 +127,23 @@
// typemap for handling an snprintf-like API like SBThread::GetStopDescription.
%typemap(in) (char *dst_or_null, size_t dst_len) {
if (!PyInt_Check($input)) {
PyErr_SetString(PyExc_ValueError, "Expecting an integer");
return NULL;
}
$2 = PyInt_AsLong($input);
if ($2 <= 0) {
PyErr_SetString(PyExc_ValueError, "Positive integer expected");
return NULL;
}
$1 = (char *) malloc($2);
if (!PyInt_Check($input)) {
PyErr_SetString(PyExc_ValueError, "Expecting an integer");
return NULL;
}
$2 = PyInt_AsLong($input);
if ($2 <= 0) {
PyErr_SetString(PyExc_ValueError, "Positive integer expected");
return NULL;
}
$1 = (char *)malloc($2);
}
%typemap(argout) (char *dst_or_null, size_t dst_len) {
Py_XDECREF($result); /* Blow away any previous result */
llvm::StringRef ref($1);
PythonString string(ref);
$result = string.release();
free($1);
Py_XDECREF($result); /* Blow away any previous result */
llvm::StringRef ref($1);
PythonString string(ref);
$result = string.release();
free($1);
}
@ -151,80 +152,74 @@
// Ditto for SBProcess::PutSTDIN(const char *src, size_t src_len).
%typemap(in) (const char *cstr, uint32_t cstr_len),
(const char *src, size_t src_len) {
if (PythonString::Check($input)) {
PythonString str(PyRefType::Borrowed, $input);
$1 = (char*)str.GetString().data();
$2 = str.GetSize();
}
else if(PythonByteArray::Check($input)) {
PythonByteArray bytearray(PyRefType::Borrowed, $input);
$1 = (char*)bytearray.GetBytes().data();
$2 = bytearray.GetSize();
}
else if (PythonBytes::Check($input)) {
PythonBytes bytes(PyRefType::Borrowed, $input);
$1 = (char*)bytes.GetBytes().data();
$2 = bytes.GetSize();
}
else {
PyErr_SetString(PyExc_ValueError, "Expecting a string");
return NULL;
}
if (PythonString::Check($input)) {
PythonString str(PyRefType::Borrowed, $input);
$1 = (char *)str.GetString().data();
$2 = str.GetSize();
} else if (PythonByteArray::Check($input)) {
PythonByteArray bytearray(PyRefType::Borrowed, $input);
$1 = (char *)bytearray.GetBytes().data();
$2 = bytearray.GetSize();
} else if (PythonBytes::Check($input)) {
PythonBytes bytes(PyRefType::Borrowed, $input);
$1 = (char *)bytes.GetBytes().data();
$2 = bytes.GetSize();
} else {
PyErr_SetString(PyExc_ValueError, "Expecting a string");
return NULL;
}
}
// For SBProcess::WriteMemory, SBTarget::GetInstructions and SBDebugger::DispatchInput.
%typemap(in) (const void *buf, size_t size),
(const void *data, size_t data_len) {
if (PythonString::Check($input)) {
PythonString str(PyRefType::Borrowed, $input);
$1 = (void*)str.GetString().data();
$2 = str.GetSize();
}
else if(PythonByteArray::Check($input)) {
PythonByteArray bytearray(PyRefType::Borrowed, $input);
$1 = (void*)bytearray.GetBytes().data();
$2 = bytearray.GetSize();
}
else if (PythonBytes::Check($input)) {
PythonBytes bytes(PyRefType::Borrowed, $input);
$1 = (void*)bytes.GetBytes().data();
$2 = bytes.GetSize();
}
else {
PyErr_SetString(PyExc_ValueError, "Expecting a buffer");
return NULL;
}
if (PythonString::Check($input)) {
PythonString str(PyRefType::Borrowed, $input);
$1 = (void *)str.GetString().data();
$2 = str.GetSize();
} else if (PythonByteArray::Check($input)) {
PythonByteArray bytearray(PyRefType::Borrowed, $input);
$1 = (void *)bytearray.GetBytes().data();
$2 = bytearray.GetSize();
} else if (PythonBytes::Check($input)) {
PythonBytes bytes(PyRefType::Borrowed, $input);
$1 = (void *)bytes.GetBytes().data();
$2 = bytes.GetSize();
} else {
PyErr_SetString(PyExc_ValueError, "Expecting a buffer");
return NULL;
}
}
// typemap for an incoming buffer
// See also SBProcess::ReadMemory.
%typemap(in) (void *buf, size_t size) {
if (PyInt_Check($input)) {
$2 = PyInt_AsLong($input);
} else if (PyLong_Check($input)) {
$2 = PyLong_AsLong($input);
} else {
PyErr_SetString(PyExc_ValueError, "Expecting an integer or long object");
return NULL;
}
if ($2 <= 0) {
PyErr_SetString(PyExc_ValueError, "Positive integer expected");
return NULL;
}
$1 = (void *) malloc($2);
if (PyInt_Check($input)) {
$2 = PyInt_AsLong($input);
} else if (PyLong_Check($input)) {
$2 = PyLong_AsLong($input);
} else {
PyErr_SetString(PyExc_ValueError, "Expecting an integer or long object");
return NULL;
}
if ($2 <= 0) {
PyErr_SetString(PyExc_ValueError, "Positive integer expected");
return NULL;
}
$1 = (void *)malloc($2);
}
// Return the buffer. Discarding any previous return result
// See also SBProcess::ReadMemory.
%typemap(argout) (void *buf, size_t size) {
Py_XDECREF($result); /* Blow away any previous result */
if (result == 0) {
$result = Py_None;
Py_INCREF($result);
} else {
PythonBytes bytes(static_cast<const uint8_t*>($1), result);
$result = bytes.release();
}
free($1);
Py_XDECREF($result); /* Blow away any previous result */
if (result == 0) {
$result = Py_None;
Py_INCREF($result);
} else {
PythonBytes bytes(static_cast<const uint8_t *>($1), result);
$result = bytes.release();
}
free($1);
}
%{
@ -250,19 +245,18 @@ template <> int32_t PyLongAsT<int32_t>(PyObject *obj) {
return static_cast<int32_t>(PyLong_AsLong(obj));
}
template <class T>
bool SetNumberFromPyObject(T &number, PyObject *obj) {
template <class T> bool SetNumberFromPyObject(T &number, PyObject *obj) {
if (PyInt_Check(obj))
number = static_cast<T>(PyInt_AsLong(obj));
else if (PyLong_Check(obj))
number = PyLongAsT<T>(obj);
else return false;
else
return false;
return true;
}
template <>
bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
template <> bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
if (PyFloat_Check(obj)) {
number = PyFloat_AsDouble(obj);
return true;
@ -287,11 +281,11 @@ bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
int size = PyList_Size($input);
int i = 0;
$2 = size;
$1 = ($1_type) malloc(size * sizeof($*1_type));
$1 = ($1_type)malloc(size * sizeof($*1_type));
for (i = 0; i < size; i++) {
PyObject *o = PyList_GetItem($input,i);
PyObject *o = PyList_GetItem($input, i);
if (!SetNumberFromPyObject($1[i], o)) {
PyErr_SetString(PyExc_TypeError,"list must contain numbers");
PyErr_SetString(PyExc_TypeError, "list must contain numbers");
free($1);
return NULL;
}
@ -302,10 +296,10 @@ bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
}
}
} else if ($input == Py_None) {
$1 = NULL;
$1 = NULL;
$2 = 0;
} else {
PyErr_SetString(PyExc_TypeError,"not a list");
PyErr_SetString(PyExc_TypeError, "not a list");
return NULL;
}
}
@ -322,43 +316,42 @@ bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
// to the more Pythonic style where a list is returned and no previous allocation
// is necessary - this will break if more than 50 versions are ever returned
%typemap(typecheck) (uint32_t *versions, uint32_t num_versions) {
$1 = ($input == Py_None ? 1 : 0);
$1 = ($input == Py_None ? 1 : 0);
}
%typemap(in, numinputs=0) (uint32_t *versions) {
$1 = (uint32_t*)malloc(sizeof(uint32_t) * 50);
$1 = (uint32_t *)malloc(sizeof(uint32_t) * 50);
}
%typemap(in, numinputs=0) (uint32_t num_versions) {
$1 = 50;
$1 = 50;
}
%typemap(argout) (uint32_t *versions, uint32_t num_versions) {
uint32_t count = result;
if (count >= $2)
count = $2;
PyObject* list = PyList_New(count);
for (uint32_t j = 0; j < count; j++)
{
PyObject* item = PyInt_FromLong($1[j]);
int ok = PyList_SetItem(list,j,item);
if (ok != 0)
{
$result = Py_None;
break;
}
uint32_t count = result;
if (count >= $2)
count = $2;
PyObject *list = PyList_New(count);
for (uint32_t j = 0; j < count; j++) {
PyObject *item = PyInt_FromLong($1[j]);
int ok = PyList_SetItem(list, j, item);
if (ok != 0) {
$result = Py_None;
break;
}
$result = list;
}
$result = list;
}
%typemap(freearg) (uint32_t *versions) {
free($1);
free($1);
}
// For Log::LogOutputCallback
%typemap(in) (lldb::LogOutputCallback log_callback, void *baton) {
if (!($input == Py_None || PyCallable_Check(reinterpret_cast<PyObject*>($input)))) {
if (!($input == Py_None ||
PyCallable_Check(reinterpret_cast<PyObject *>($input)))) {
PyErr_SetString(PyExc_TypeError, "Need a callable object or None!");
return NULL;
}
@ -376,7 +369,7 @@ bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
%typemap(typecheck) (lldb::LogOutputCallback log_callback, void *baton) {
$1 = $input == Py_None;
$1 = $1 || PyCallable_Check(reinterpret_cast<PyObject*>($input));
$1 = $1 || PyCallable_Check(reinterpret_cast<PyObject *>($input));
}
@ -398,7 +391,8 @@ bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
PyErr_SetString(PyExc_TypeError, "not a file");
return nullptr;
}
auto sp = unwrapOrSetPythonException(py_file.ConvertToFileForcingUseOfScriptingIOMethods());
auto sp = unwrapOrSetPythonException(
py_file.ConvertToFileForcingUseOfScriptingIOMethods());
if (!sp)
return nullptr;
$1 = sp;
@ -410,7 +404,8 @@ bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
PyErr_SetString(PyExc_TypeError, "not a file");
return nullptr;
}
auto sp = unwrapOrSetPythonException(py_file.ConvertToFile(/*borrowed=*/true));
auto sp =
unwrapOrSetPythonException(py_file.ConvertToFile(/*borrowed=*/true));
if (!sp)
return nullptr;
$1 = sp;
@ -422,7 +417,8 @@ bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
PyErr_SetString(PyExc_TypeError, "not a file");
return nullptr;
}
auto sp = unwrapOrSetPythonException(py_file.ConvertToFileForcingUseOfScriptingIOMethods(/*borrowed=*/true));
auto sp = unwrapOrSetPythonException(
py_file.ConvertToFileForcingUseOfScriptingIOMethods(/*borrowed=*/true));
if (!sp)
return nullptr;
$1 = sp;
@ -446,40 +442,34 @@ bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
return nullptr;
$result = pyfile.release();
}
if (!$result)
{
$result = Py_None;
Py_INCREF(Py_None);
if (!$result) {
$result = Py_None;
Py_INCREF(Py_None);
}
}
%typemap(in) (const char* string, int len) {
if ($input == Py_None)
{
$1 = NULL;
$2 = 0;
}
else if (PythonString::Check($input))
{
PythonString py_str(PyRefType::Borrowed, $input);
llvm::StringRef str = py_str.GetString();
$1 = const_cast<char*>(str.data());
$2 = str.size();
// In Python 2, if $input is a PyUnicode object then this
// will trigger a Unicode -> String conversion, in which
// case the `PythonString` will now own the PyString. Thus
// if it goes out of scope, the data will be deleted. The
// only way to avoid this is to leak the Python object in
// that case. Note that if there was no conversion, then
// releasing the string will not leak anything, since we
// created this as a borrowed reference.
py_str.release();
}
else
{
PyErr_SetString(PyExc_TypeError,"not a string-like object");
return NULL;
}
if ($input == Py_None) {
$1 = NULL;
$2 = 0;
} else if (PythonString::Check($input)) {
PythonString py_str(PyRefType::Borrowed, $input);
llvm::StringRef str = py_str.GetString();
$1 = const_cast<char *>(str.data());
$2 = str.size();
// In Python 2, if $input is a PyUnicode object then this
// will trigger a Unicode -> String conversion, in which
// case the `PythonString` will now own the PyString. Thus
// if it goes out of scope, the data will be deleted. The
// only way to avoid this is to leak the Python object in
// that case. Note that if there was no conversion, then
// releasing the string will not leak anything, since we
// created this as a borrowed reference.
py_str.release();
} else {
PyErr_SetString(PyExc_TypeError, "not a string-like object");
return NULL;
}
}
// These two pybuffer macros are copied out of swig/Lib/python/pybuffer.i,
@ -491,7 +481,9 @@ bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
%define %pybuffer_mutable_binary(TYPEMAP, SIZE)
%typemap(in) (TYPEMAP, SIZE) (Py_buffer_RAII view) {
int res; Py_ssize_t size = 0; void *buf = 0;
int res;
Py_ssize_t size = 0;
void *buf = 0;
res = PyObject_GetBuffer($input, &view.buffer, PyBUF_WRITABLE);
if (res < 0) {
PyErr_Clear();
@ -499,14 +491,16 @@ bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
}
size = view.buffer.len;
buf = view.buffer.buf;
$1 = ($1_ltype) buf;
$2 = ($2_ltype) (size/sizeof($*1_type));
$1 = ($1_ltype)buf;
$2 = ($2_ltype)(size / sizeof($*1_type));
}
%enddef
%define %pybuffer_binary(TYPEMAP, SIZE)
%typemap(in) (TYPEMAP, SIZE) (Py_buffer_RAII view) {
int res; Py_ssize_t size = 0; const void *buf = 0;
int res;
Py_ssize_t size = 0;
const void *buf = 0;
res = PyObject_GetBuffer($input, &view.buffer, PyBUF_CONTIG_RO);
if (res < 0) {
PyErr_Clear();
@ -514,8 +508,8 @@ bool SetNumberFromPyObject<double>(double &number, PyObject *obj) {
}
size = view.buffer.len;
buf = view.buffer.buf;
$1 = ($1_ltype) buf;
$2 = ($2_ltype) (size / sizeof($*1_type));
$1 = ($1_ltype)buf;
$2 = ($2_ltype)(size / sizeof($*1_type));
}
%enddef

File diff suppressed because it is too large Load Diff