forked from qt-creator/qt-creator
Debugger: Simplify use of getUninitializedVariables()
Change-Id: I7962fe2d582fc3f2ad6a76fb600038f454b75dc0 Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
@@ -1168,10 +1168,9 @@ void CdbEngine::doUpdateLocals(const UpdateParameters &updateParameters)
|
||||
cmd.arg("displaystringlimit", action(DisplayStringLimit)->value().toString());
|
||||
|
||||
if (boolSetting(UseCodeModel)) {
|
||||
QStringList uninitializedVariables;
|
||||
getUninitializedVariables(m_codeModelSnapshot,
|
||||
frame.function, frame.file, frame.line, &uninitializedVariables);
|
||||
cmd.arg("uninitialized", uninitializedVariables);
|
||||
QStringList variables = getUninitializedVariables(m_codeModelSnapshot,
|
||||
frame.function, frame.file, frame.line);
|
||||
cmd.arg("uninitialized", variables);
|
||||
}
|
||||
|
||||
cmd.callback = [this](const DebuggerResponse &response) {
|
||||
@@ -1239,13 +1238,12 @@ void CdbEngine::doUpdateLocals(const UpdateParameters &updateParameters)
|
||||
// Uninitialized variables if desired. Quote as safeguard against shadowed
|
||||
// variables in case of errors in uninitializedVariables().
|
||||
if (boolSetting(UseCodeModel)) {
|
||||
QStringList uninitializedVariables;
|
||||
getUninitializedVariables(m_codeModelSnapshot,
|
||||
frame.function, frame.file, frame.line, &uninitializedVariables);
|
||||
if (!uninitializedVariables.isEmpty()) {
|
||||
const QStringList variables = getUninitializedVariables(m_codeModelSnapshot,
|
||||
frame.function, frame.file, frame.line);
|
||||
if (!variables.isEmpty()) {
|
||||
str << blankSeparator << "-u \"";
|
||||
int i = 0;
|
||||
foreach (const QString &u, uninitializedVariables) {
|
||||
for (const QString &u : variables) {
|
||||
if (i++)
|
||||
str << ',';
|
||||
str << localsPrefixC << u;
|
||||
|
@@ -182,27 +182,24 @@ static void blockRecursion(const Overview &overview,
|
||||
blockRecursion(overview, enclosingScope, line, uninitializedVariables, seenHash, level + 1);
|
||||
}
|
||||
|
||||
// Inline helper with integer error return codes.
|
||||
static inline
|
||||
int getUninitializedVariablesI(const Snapshot &snapshot,
|
||||
const QString &functionName,
|
||||
const QString &file,
|
||||
int line,
|
||||
QStringList *uninitializedVariables)
|
||||
QStringList getUninitializedVariables(const Snapshot &snapshot,
|
||||
const QString &functionName,
|
||||
const QString &file,
|
||||
int line)
|
||||
{
|
||||
uninitializedVariables->clear();
|
||||
QStringList result;
|
||||
// Find document
|
||||
if (snapshot.isEmpty() || functionName.isEmpty() || file.isEmpty() || line < 1)
|
||||
return 1;
|
||||
return result;
|
||||
const Snapshot::const_iterator docIt = snapshot.find(file);
|
||||
if (docIt == snapshot.end())
|
||||
return 2;
|
||||
return result;
|
||||
const Document::Ptr doc = docIt.value();
|
||||
// Look at symbol at line and find its function. Either it is the
|
||||
// function itself or some expression/variable.
|
||||
const Symbol *symbolAtLine = doc->lastVisibleSymbolAt(line, 0);
|
||||
if (!symbolAtLine)
|
||||
return 4;
|
||||
return result;
|
||||
// First figure out the function to do a safety name check
|
||||
// and the innermost scope at cursor position
|
||||
const Function *function = nullptr;
|
||||
@@ -221,46 +218,25 @@ int getUninitializedVariablesI(const Snapshot &snapshot,
|
||||
}
|
||||
}
|
||||
if (!function || !innerMostScope)
|
||||
return 7;
|
||||
return result;
|
||||
// Compare function names with a bit off fuzz,
|
||||
// skipping modules from a CDB symbol "lib!foo" or namespaces
|
||||
// that the code model does not show at this point
|
||||
Overview overview;
|
||||
const QString name = overview.prettyName(function->name());
|
||||
if (!functionName.endsWith(name))
|
||||
return 11;
|
||||
return result;
|
||||
if (functionName.size() > name.size()) {
|
||||
const char previousChar = functionName.at(functionName.size() - name.size() - 1).toLatin1();
|
||||
if (previousChar != ':' && previousChar != '!' )
|
||||
return 11;
|
||||
return result;
|
||||
}
|
||||
// Starting from the innermost block scope, collect declarations.
|
||||
SeenHash seenHash;
|
||||
blockRecursion(overview, innerMostScope, line, uninitializedVariables, &seenHash);
|
||||
return 0;
|
||||
blockRecursion(overview, innerMostScope, line, &result, &seenHash);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool getUninitializedVariables(const Snapshot &snapshot,
|
||||
const QString &function,
|
||||
const QString &file,
|
||||
int line,
|
||||
QStringList *uninitializedVariables)
|
||||
{
|
||||
const int rc = getUninitializedVariablesI(snapshot, function, file, line, uninitializedVariables);
|
||||
if (debug) {
|
||||
QString msg;
|
||||
QTextStream str(&msg);
|
||||
str << "getUninitializedVariables() " << function << ' ' << file << ':' << line
|
||||
<< " returns (int) " << rc << " '"
|
||||
<< uninitializedVariables->join(QLatin1Char(',')) << '\'';
|
||||
if (rc)
|
||||
str << " of " << snapshot.size() << " documents";
|
||||
qDebug() << msg;
|
||||
}
|
||||
return rc == 0;
|
||||
}
|
||||
|
||||
|
||||
QString cppFunctionAt(const QString &fileName, int line, int column)
|
||||
{
|
||||
const Snapshot snapshot = CppModelManager::instance()->snapshot();
|
||||
|
@@ -49,9 +49,8 @@ QString cppFunctionAt(const QString &fileName, int line, int column = 0);
|
||||
// Get variables that are not initialized at a certain line
|
||||
// of a function from the code model. Shadowed variables will
|
||||
// be reported using the debugger naming conventions '<shadowed n>'
|
||||
bool getUninitializedVariables(const CPlusPlus::Snapshot &snapshot,
|
||||
const QString &function, const QString &file, int line,
|
||||
QStringList *uninitializedVariables);
|
||||
QStringList getUninitializedVariables(const CPlusPlus::Snapshot &snapshot,
|
||||
const QString &function, const QString &file, int line);
|
||||
|
||||
ContextData getLocationContext(TextEditor::TextDocument *document, int lineNumber);
|
||||
|
||||
|
Reference in New Issue
Block a user