Reflow paragraphs in comments.
This is intended as a clean up after the big clang-format commit
(r280751), which unfortunately resulted in many of the comment
paragraphs in LLDB being very hard to read.
FYI, the script I used was:
import textwrap
import commands
import os
import sys
import re
tmp = "%s.tmp"%sys.argv[1]
out = open(tmp, "w+")
with open(sys.argv[1], "r") as f:
header = ""
text = ""
comment = re.compile(r'^( *//) ([^ ].*)$')
special = re.compile(r'^((([A-Z]+[: ])|([0-9]+ )).*)|(.*;)$')
for line in f:
match = comment.match(line)
if match and not special.match(match.group(2)):
# skip intentionally short comments.
if not text and len(match.group(2)) < 40:
out.write(line)
continue
if text:
text += " " + match.group(2)
else:
header = match.group(1)
text = match.group(2)
continue
if text:
filled = textwrap.wrap(text, width=(78-len(header)),
break_long_words=False)
for l in filled:
out.write(header+" "+l+'\n')
text = ""
out.write(line)
os.rename(tmp, sys.argv[1])
Differential Revision: https://reviews.llvm.org/D46144
llvm-svn: 331197
This commit is contained in:
@@ -112,13 +112,12 @@ static bool g_initialized = false;
|
||||
|
||||
namespace {
|
||||
|
||||
// Initializing Python is not a straightforward process. We cannot control what
|
||||
// external code may have done before getting to this point in LLDB, including
|
||||
// potentially having already initialized Python, so we need to do a lot of work
|
||||
// to ensure that the existing state of the system is maintained across our
|
||||
// initialization. We do this by using an RAII pattern where we save off
|
||||
// initial
|
||||
// state at the beginning, and restore it at the end
|
||||
// Initializing Python is not a straightforward process. We cannot control
|
||||
// what external code may have done before getting to this point in LLDB,
|
||||
// including potentially having already initialized Python, so we need to do a
|
||||
// lot of work to ensure that the existing state of the system is maintained
|
||||
// across our initialization. We do this by using an RAII pattern where we
|
||||
// save off initial state at the beginning, and restore it at the end
|
||||
struct InitializePythonRAII {
|
||||
public:
|
||||
InitializePythonRAII()
|
||||
@@ -210,12 +209,11 @@ bool ScriptInterpreterPython::Locker::DoAcquireLock() {
|
||||
LLDB_LOGV(log, "Ensured PyGILState. Previous state = {0}locked",
|
||||
m_GILState == PyGILState_UNLOCKED ? "un" : "");
|
||||
|
||||
// we need to save the thread state when we first start the command
|
||||
// because we might decide to interrupt it while some action is taking
|
||||
// place outside of Python (e.g. printing to screen, waiting for the network,
|
||||
// ...)
|
||||
// in that case, _PyThreadState_Current will be NULL - and we would be unable
|
||||
// to set the asynchronous exception - not a desirable situation
|
||||
// we need to save the thread state when we first start the command because
|
||||
// we might decide to interrupt it while some action is taking place outside
|
||||
// of Python (e.g. printing to screen, waiting for the network, ...) in that
|
||||
// case, _PyThreadState_Current will be NULL - and we would be unable to set
|
||||
// the asynchronous exception - not a desirable situation
|
||||
m_python_interpreter->SetThreadState(PyThreadState_Get());
|
||||
m_python_interpreter->IncrementLockCount();
|
||||
return true;
|
||||
@@ -281,17 +279,16 @@ ScriptInterpreterPython::ScriptInterpreterPython(
|
||||
PyRun_SimpleString(run_string.GetData());
|
||||
|
||||
// Reloading modules requires a different syntax in Python 2 and Python 3.
|
||||
// This provides
|
||||
// a consistent syntax no matter what version of Python.
|
||||
// This provides a consistent syntax no matter what version of Python.
|
||||
run_string.Clear();
|
||||
run_string.Printf("run_one_line (%s, 'from six.moves import reload_module')",
|
||||
m_dictionary_name.c_str());
|
||||
PyRun_SimpleString(run_string.GetData());
|
||||
|
||||
// WARNING: temporary code that loads Cocoa formatters - this should be done
|
||||
// on a per-platform basis rather than loading the whole set
|
||||
// and letting the individual formatter classes exploit APIs to check whether
|
||||
// they can/cannot do their task
|
||||
// on a per-platform basis rather than loading the whole set and letting the
|
||||
// individual formatter classes exploit APIs to check whether they can/cannot
|
||||
// do their task
|
||||
run_string.Clear();
|
||||
run_string.Printf(
|
||||
"run_one_line (%s, 'import lldb.formatters, lldb.formatters.cpp, pydoc')",
|
||||
@@ -314,13 +311,11 @@ ScriptInterpreterPython::ScriptInterpreterPython(
|
||||
}
|
||||
|
||||
ScriptInterpreterPython::~ScriptInterpreterPython() {
|
||||
// the session dictionary may hold objects with complex state
|
||||
// which means that they may need to be torn down with some level of smarts
|
||||
// and that, in turn, requires a valid thread state
|
||||
// force Python to procure itself such a thread state, nuke the session
|
||||
// dictionary
|
||||
// and then release it for others to use and proceed with the rest of the
|
||||
// shutdown
|
||||
// the session dictionary may hold objects with complex state which means
|
||||
// that they may need to be torn down with some level of smarts and that, in
|
||||
// turn, requires a valid thread state force Python to procure itself such a
|
||||
// thread state, nuke the session dictionary and then release it for others
|
||||
// to use and proceed with the rest of the shutdown
|
||||
auto gil_state = PyGILState_Ensure();
|
||||
m_session_dict.Reset();
|
||||
PyGILState_Release(gil_state);
|
||||
@@ -452,16 +447,16 @@ void ScriptInterpreterPython::ResetOutputFileHandle(FILE *fh) {}
|
||||
void ScriptInterpreterPython::SaveTerminalState(int fd) {
|
||||
// Python mucks with the terminal state of STDIN. If we can possibly avoid
|
||||
// this by setting the file handles up correctly prior to entering the
|
||||
// interpreter we should. For now we save and restore the terminal state
|
||||
// on the input file handle.
|
||||
// interpreter we should. For now we save and restore the terminal state on
|
||||
// the input file handle.
|
||||
m_terminal_state.Save(fd, false);
|
||||
}
|
||||
|
||||
void ScriptInterpreterPython::RestoreTerminalState() {
|
||||
// Python mucks with the terminal state of STDIN. If we can possibly avoid
|
||||
// this by setting the file handles up correctly prior to entering the
|
||||
// interpreter we should. For now we save and restore the terminal state
|
||||
// on the input file handle.
|
||||
// interpreter we should. For now we save and restore the terminal state on
|
||||
// the input file handle.
|
||||
m_terminal_state.Restore();
|
||||
}
|
||||
|
||||
@@ -470,14 +465,11 @@ void ScriptInterpreterPython::LeaveSession() {
|
||||
if (log)
|
||||
log->PutCString("ScriptInterpreterPython::LeaveSession()");
|
||||
|
||||
// checking that we have a valid thread state - since we use our own threading
|
||||
// and locking
|
||||
// in some (rare) cases during cleanup Python may end up believing we have no
|
||||
// thread state
|
||||
// and PyImport_AddModule will crash if that is the case - since that seems to
|
||||
// only happen
|
||||
// when destroying the SBDebugger, we can make do without clearing up stdout
|
||||
// and stderr
|
||||
// checking that we have a valid thread state - since we use our own
|
||||
// threading and locking in some (rare) cases during cleanup Python may end
|
||||
// up believing we have no thread state and PyImport_AddModule will crash if
|
||||
// that is the case - since that seems to only happen when destroying the
|
||||
// SBDebugger, we can make do without clearing up stdout and stderr
|
||||
|
||||
// rdar://problem/11292882
|
||||
// When the current thread state is NULL, PyThreadState_Get() issues a fatal
|
||||
@@ -526,8 +518,7 @@ bool ScriptInterpreterPython::SetStdHandle(File &file, const char *py_name,
|
||||
bool ScriptInterpreterPython::EnterSession(uint16_t on_entry_flags, FILE *in,
|
||||
FILE *out, FILE *err) {
|
||||
// If we have already entered the session, without having officially 'left'
|
||||
// it, then there is no need to
|
||||
// 'enter' it again.
|
||||
// it, then there is no need to 'enter' it again.
|
||||
Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_SCRIPT));
|
||||
if (m_session_is_active) {
|
||||
if (log)
|
||||
@@ -560,8 +551,8 @@ bool ScriptInterpreterPython::EnterSession(uint16_t on_entry_flags, FILE *in,
|
||||
run_string.PutCString("; lldb.frame = lldb.thread.GetSelectedFrame ()");
|
||||
run_string.PutCString("')");
|
||||
} else {
|
||||
// If we aren't initing the globals, we should still always set the debugger
|
||||
// (since that is always unique.)
|
||||
// If we aren't initing the globals, we should still always set the
|
||||
// debugger (since that is always unique.)
|
||||
run_string.Printf("run_one_line (%s, 'lldb.debugger_unique_id = %" PRIu64,
|
||||
m_dictionary_name.c_str(),
|
||||
GetCommandInterpreter().GetDebugger().GetID());
|
||||
@@ -702,12 +693,11 @@ bool ScriptInterpreterPython::ExecuteOneLine(
|
||||
|
||||
if (command && command[0]) {
|
||||
// We want to call run_one_line, passing in the dictionary and the command
|
||||
// string. We cannot do this through
|
||||
// PyRun_SimpleString here because the command string may contain escaped
|
||||
// characters, and putting it inside
|
||||
// string. We cannot do this through PyRun_SimpleString here because the
|
||||
// command string may contain escaped characters, and putting it inside
|
||||
// another string to pass to PyRun_SimpleString messes up the escaping. So
|
||||
// we use the following more complicated
|
||||
// method to pass the command string directly down to Python.
|
||||
// we use the following more complicated method to pass the command string
|
||||
// directly down to Python.
|
||||
Debugger &debugger = m_interpreter.GetDebugger();
|
||||
|
||||
StreamFileSP input_file_sp;
|
||||
@@ -773,17 +763,14 @@ bool ScriptInterpreterPython::ExecuteOneLine(
|
||||
FILE *err_file = error_file_sp->GetFile().GetStream();
|
||||
bool success = false;
|
||||
{
|
||||
// WARNING! It's imperative that this RAII scope be as tight as possible.
|
||||
// In particular, the
|
||||
// scope must end *before* we try to join the read thread. The reason for
|
||||
// this is that a
|
||||
// pre-requisite for joining the read thread is that we close the write
|
||||
// handle (to break the
|
||||
// pipe and cause it to wake up and exit). But acquiring the GIL as below
|
||||
// will redirect Python's
|
||||
// stdio to use this same handle. If we close the handle while Python is
|
||||
// still using it, bad
|
||||
// things will happen.
|
||||
// WARNING! It's imperative that this RAII scope be as tight as
|
||||
// possible. In particular, the scope must end *before* we try to join
|
||||
// the read thread. The reason for this is that a pre-requisite for
|
||||
// joining the read thread is that we close the write handle (to break
|
||||
// the pipe and cause it to wake up and exit). But acquiring the GIL as
|
||||
// below will redirect Python's stdio to use this same handle. If we
|
||||
// close the handle while Python is still using it, bad things will
|
||||
// happen.
|
||||
Locker locker(
|
||||
this,
|
||||
ScriptInterpreterPython::Locker::AcquireLock |
|
||||
@@ -827,12 +814,12 @@ bool ScriptInterpreterPython::ExecuteOneLine(
|
||||
}
|
||||
|
||||
if (join_read_thread) {
|
||||
// Close the write end of the pipe since we are done with our
|
||||
// one line script. This should cause the read thread that
|
||||
// output_comm is using to exit
|
||||
// Close the write end of the pipe since we are done with our one line
|
||||
// script. This should cause the read thread that output_comm is using to
|
||||
// exit
|
||||
output_file_sp->GetFile().Close();
|
||||
// The close above should cause this thread to exit when it gets
|
||||
// to the end of file, so let it get all its data
|
||||
// The close above should cause this thread to exit when it gets to the
|
||||
// end of file, so let it get all its data
|
||||
output_comm.JoinReadThread();
|
||||
// Now we can close the read end of the pipe
|
||||
output_comm.Disconnect();
|
||||
@@ -889,21 +876,18 @@ public:
|
||||
ScriptInterpreterPython::Locker::FreeAcquiredLock |
|
||||
ScriptInterpreterPython::Locker::TearDownSession);
|
||||
|
||||
// The following call drops into the embedded interpreter loop and stays
|
||||
// there until the
|
||||
// user chooses to exit from the Python interpreter.
|
||||
// This embedded interpreter will, as any Python code that performs I/O,
|
||||
// unlock the GIL before
|
||||
// a system call that can hang, and lock it when the syscall has
|
||||
// returned.
|
||||
// The following call drops into the embedded interpreter loop and
|
||||
// stays there until the user chooses to exit from the Python
|
||||
// interpreter. This embedded interpreter will, as any Python code that
|
||||
// performs I/O, unlock the GIL before a system call that can hang, and
|
||||
// lock it when the syscall has returned.
|
||||
|
||||
// We need to surround the call to the embedded interpreter with calls
|
||||
// to PyGILState_Ensure and
|
||||
// PyGILState_Release (using the Locker above). This is because Python
|
||||
// has a global lock which must be held whenever we want
|
||||
// to touch any Python objects. Otherwise, if the user calls Python
|
||||
// code, the interpreter state will be off,
|
||||
// and things could hang (it's happened before).
|
||||
// to PyGILState_Ensure and PyGILState_Release (using the Locker
|
||||
// above). This is because Python has a global lock which must be held
|
||||
// whenever we want to touch any Python objects. Otherwise, if the user
|
||||
// calls Python code, the interpreter state will be off, and things
|
||||
// could hang (it's happened before).
|
||||
|
||||
StreamString run_string;
|
||||
run_string.Printf("run_python_interpreter (%s)",
|
||||
@@ -934,12 +918,10 @@ void ScriptInterpreterPython::ExecuteInterpreterLoop() {
|
||||
Debugger &debugger = GetCommandInterpreter().GetDebugger();
|
||||
|
||||
// At the moment, the only time the debugger does not have an input file
|
||||
// handle is when this is called
|
||||
// directly from Python, in which case it is both dangerous and unnecessary
|
||||
// (not to mention confusing) to
|
||||
// try to embed a running interpreter loop inside the already running Python
|
||||
// interpreter loop, so we won't
|
||||
// do it.
|
||||
// handle is when this is called directly from Python, in which case it is
|
||||
// both dangerous and unnecessary (not to mention confusing) to try to embed
|
||||
// a running interpreter loop inside the already running Python interpreter
|
||||
// loop, so we won't do it.
|
||||
|
||||
if (!debugger.GetInputFile()->GetFile().IsValid())
|
||||
return;
|
||||
@@ -1174,10 +1156,8 @@ Status ScriptInterpreterPython::ExecuteMultipleLines(
|
||||
|
||||
if (code_object.IsValid()) {
|
||||
// In Python 2.x, PyEval_EvalCode takes a PyCodeObject, but in Python 3.x, it
|
||||
// takes
|
||||
// a PyObject. They are convertible (hence the function
|
||||
// PyCode_Check(PyObject*), so
|
||||
// we have to do the cast for Python 2.x
|
||||
// takes a PyObject. They are convertible (hence the function
|
||||
// PyCode_Check(PyObject*), so we have to do the cast for Python 2.x
|
||||
#if PY_MAJOR_VERSION >= 3
|
||||
PyObject *py_code_obj = code_object.get();
|
||||
#else
|
||||
@@ -1243,10 +1223,9 @@ Status ScriptInterpreterPython::SetBreakpointCommandCallback(
|
||||
auto data_ap = llvm::make_unique<CommandDataPython>();
|
||||
|
||||
// Split the command_body_text into lines, and pass that to
|
||||
// GenerateBreakpointCommandCallbackData. That will
|
||||
// wrap the body in an auto-generated function, and return the function name
|
||||
// in script_source. That is what
|
||||
// the callback will actually invoke.
|
||||
// GenerateBreakpointCommandCallbackData. That will wrap the body in an
|
||||
// auto-generated function, and return the function name in script_source.
|
||||
// That is what the callback will actually invoke.
|
||||
|
||||
data_ap->user_source.SplitIntoLines(command_body_text);
|
||||
Status error = GenerateBreakpointCommandCallbackData(data_ap->user_source,
|
||||
@@ -1268,9 +1247,8 @@ void ScriptInterpreterPython::SetWatchpointCommandCallback(
|
||||
|
||||
// It's necessary to set both user_source and script_source to the oneliner.
|
||||
// The former is used to generate callback description (as in watchpoint
|
||||
// command list)
|
||||
// while the latter is used for Python to interpret during the actual
|
||||
// callback.
|
||||
// command list) while the latter is used for Python to interpret during the
|
||||
// actual callback.
|
||||
|
||||
data_ap->user_source.AppendString(oneliner);
|
||||
data_ap->script_source.assign(oneliner);
|
||||
@@ -1365,8 +1343,7 @@ bool ScriptInterpreterPython::GenerateTypeScriptFunction(
|
||||
return false;
|
||||
|
||||
// Take what the user wrote, wrap it all up inside one big auto-generated
|
||||
// Python function, passing in the
|
||||
// ValueObject as parameter to the function.
|
||||
// Python function, passing in the ValueObject as parameter to the function.
|
||||
|
||||
std::string auto_generated_function_name(
|
||||
GenerateUniqueName("lldb_autogen_python_type_print_func",
|
||||
@@ -1430,8 +1407,8 @@ bool ScriptInterpreterPython::GenerateTypeSynthClass(StringList &user_input,
|
||||
sstr.Printf("class %s:", auto_generated_class_name.c_str());
|
||||
auto_generated_class.AppendString(sstr.GetString());
|
||||
|
||||
// Wrap everything up inside the class, increasing the indentation.
|
||||
// we don't need to play any fancy indentation tricks here because there is no
|
||||
// Wrap everything up inside the class, increasing the indentation. we don't
|
||||
// need to play any fancy indentation tricks here because there is no
|
||||
// surrounding code whose indentation we need to honor
|
||||
for (int i = 0; i < num_lines; ++i) {
|
||||
sstr.Clear();
|
||||
@@ -1439,9 +1416,8 @@ bool ScriptInterpreterPython::GenerateTypeSynthClass(StringList &user_input,
|
||||
auto_generated_class.AppendString(sstr.GetString());
|
||||
}
|
||||
|
||||
// Verify that the results are valid Python.
|
||||
// (even though the method is ExportFunctionDefinitionToInterpreter, a class
|
||||
// will actually be exported)
|
||||
// Verify that the results are valid Python. (even though the method is
|
||||
// ExportFunctionDefinitionToInterpreter, a class will actually be exported)
|
||||
// (TODO: rename that method to ExportDefinitionToInterpreter)
|
||||
if (!ExportFunctionDefinitionToInterpreter(auto_generated_class).Success())
|
||||
return false;
|
||||
@@ -1585,8 +1561,8 @@ StructuredData::ArraySP ScriptInterpreterPython::OSPlugin_ThreadsInfo(
|
||||
|
||||
// GetPythonValueFormatString provides a system independent type safe way to
|
||||
// convert a variable's type into a python value format. Python value formats
|
||||
// are defined in terms of builtin C types and could change from system to
|
||||
// as the underlying typedef for uint* types, size_t, off_t and other values
|
||||
// are defined in terms of builtin C types and could change from system to as
|
||||
// the underlying typedef for uint* types, size_t, off_t and other values
|
||||
// change.
|
||||
|
||||
template <typename T> const char *GetPythonValueFormatString(T t);
|
||||
@@ -2044,8 +2020,7 @@ void ScriptInterpreterPython::Clear() {
|
||||
ScriptInterpreterPython::Locker::FreeAcquiredLock);
|
||||
|
||||
// This may be called as part of Py_Finalize. In that case the modules are
|
||||
// destroyed in random
|
||||
// order and we can't guarantee that we can access these.
|
||||
// destroyed in random order and we can't guarantee that we can access these.
|
||||
if (Py_IsInitialized())
|
||||
PyRun_SimpleString("lldb.debugger = None; lldb.target = None; lldb.process "
|
||||
"= None; lldb.thread = None; lldb.frame = None");
|
||||
@@ -2629,9 +2604,8 @@ bool ScriptInterpreterPython::LoadScriptingModule(
|
||||
command_stream.Clear();
|
||||
command_stream.Printf("sys.modules.__contains__('%s')", basename.c_str());
|
||||
bool does_contain = false;
|
||||
// this call will succeed if the module was ever imported in any Debugger in
|
||||
// the lifetime of the process
|
||||
// in which this LLDB framework is living
|
||||
// this call will succeed if the module was ever imported in any Debugger
|
||||
// in the lifetime of the process in which this LLDB framework is living
|
||||
bool was_imported_globally =
|
||||
(ExecuteOneLineWithReturn(
|
||||
command_stream.GetData(),
|
||||
@@ -2705,8 +2679,8 @@ bool ScriptInterpreterPython::IsReservedWord(const char *word) {
|
||||
|
||||
llvm::StringRef word_sr(word);
|
||||
|
||||
// filter out a few characters that would just confuse us
|
||||
// and that are clearly not keyword material anyway
|
||||
// filter out a few characters that would just confuse us and that are
|
||||
// clearly not keyword material anyway
|
||||
if (word_sr.find_first_of("'\"") != llvm::StringRef::npos)
|
||||
return false;
|
||||
|
||||
@@ -2834,9 +2808,9 @@ bool ScriptInterpreterPython::RunScriptBasedCommand(
|
||||
return ret_val;
|
||||
}
|
||||
|
||||
// in Python, a special attribute __doc__ contains the docstring
|
||||
// for an object (function, method, class, ...) if any is defined
|
||||
// Otherwise, the attribute's value is None
|
||||
// in Python, a special attribute __doc__ contains the docstring for an object
|
||||
// (function, method, class, ...) if any is defined Otherwise, the attribute's
|
||||
// value is None
|
||||
bool ScriptInterpreterPython::GetDocumentationForItem(const char *item,
|
||||
std::string &dest) {
|
||||
dest.clear();
|
||||
@@ -3106,10 +3080,9 @@ void ScriptInterpreterPython::InitializePrivate() {
|
||||
Timer scoped_timer(func_cat, LLVM_PRETTY_FUNCTION);
|
||||
|
||||
// RAII-based initialization which correctly handles multiple-initialization,
|
||||
// version-
|
||||
// specific differences among Python 2 and Python 3, and saving and restoring
|
||||
// various
|
||||
// other pieces of state that can get mucked with during initialization.
|
||||
// version- specific differences among Python 2 and Python 3, and saving and
|
||||
// restoring various other pieces of state that can get mucked with during
|
||||
// initialization.
|
||||
InitializePythonRAII initialize_guard;
|
||||
|
||||
if (g_swig_init_callback)
|
||||
@@ -3123,12 +3096,9 @@ void ScriptInterpreterPython::InitializePrivate() {
|
||||
|
||||
FileSpec file_spec;
|
||||
// Don't denormalize paths when calling file_spec.GetPath(). On platforms
|
||||
// that use
|
||||
// a backslash as the path separator, this will result in executing python
|
||||
// code containing
|
||||
// paths with unescaped backslashes. But Python also accepts forward slashes,
|
||||
// so to make
|
||||
// life easier we just use that.
|
||||
// that use a backslash as the path separator, this will result in executing
|
||||
// python code containing paths with unescaped backslashes. But Python also
|
||||
// accepts forward slashes, so to make life easier we just use that.
|
||||
if (HostInfo::GetLLDBPath(ePathTypePythonDir, file_spec))
|
||||
AddToSysPath(AddLocation::Beginning, file_spec.GetPath(false));
|
||||
if (HostInfo::GetLLDBPath(ePathTypeLLDBShlibDir, file_spec))
|
||||
|
||||
Reference in New Issue
Block a user