2013-01-24 16:33:17 +01:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2014-01-07 13:27:11 +01:00
|
|
|
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
|
2013-01-24 16:33:17 +01:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
2014-10-01 13:21:18 +02:00
|
|
|
** conditions see http://www.qt.io/licensing. For further information
|
|
|
|
|
** use the contact form at http://www.qt.io/contact-us.
|
2013-01-24 16:33:17 +01:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2014-10-01 13:21:18 +02:00
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2013-01-24 16:33:17 +01:00
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
|
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "sourceutils.h"
|
|
|
|
|
|
|
|
|
|
#include "watchdata.h"
|
|
|
|
|
#include "watchutils.h"
|
|
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
#include <texteditor/texteditor.h>
|
2013-01-24 16:33:17 +01:00
|
|
|
#include <cpptools/abstracteditorsupport.h>
|
2013-03-27 10:32:28 +04:00
|
|
|
#include <cpptools/cppprojectfile.h>
|
2014-09-15 00:12:27 +02:00
|
|
|
#include <cpptools/cppmodelmanager.h>
|
2014-09-19 17:06:26 +02:00
|
|
|
#include <cplusplus/CppDocument.h>
|
2013-01-24 16:33:17 +01:00
|
|
|
#include <cplusplus/ExpressionUnderCursor.h>
|
|
|
|
|
#include <cplusplus/Overview.h>
|
|
|
|
|
|
2014-05-21 15:03:23 +02:00
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
2013-01-24 16:33:17 +01:00
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
|
|
enum { debug = 0 };
|
|
|
|
|
|
2014-09-19 17:06:26 +02:00
|
|
|
using namespace CppTools;
|
|
|
|
|
using namespace CPlusPlus;
|
|
|
|
|
using namespace TextEditor;
|
|
|
|
|
|
2013-01-24 16:33:17 +01:00
|
|
|
namespace CPlusPlus {
|
|
|
|
|
|
|
|
|
|
static void debugCppSymbolRecursion(QTextStream &str, const Overview &o,
|
|
|
|
|
const Symbol &s, bool doRecurse = true,
|
|
|
|
|
int recursion = 0)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < recursion; i++)
|
|
|
|
|
str << " ";
|
|
|
|
|
str << "Symbol: " << o.prettyName(s.name()) << " at line " << s.line();
|
|
|
|
|
if (s.isFunction())
|
|
|
|
|
str << " function";
|
|
|
|
|
if (s.isClass())
|
|
|
|
|
str << " class";
|
|
|
|
|
if (s.isDeclaration())
|
|
|
|
|
str << " declaration";
|
|
|
|
|
if (s.isBlock())
|
|
|
|
|
str << " block";
|
|
|
|
|
if (doRecurse && s.isScope()) {
|
|
|
|
|
const Scope *scoped = s.asScope();
|
|
|
|
|
const int size = scoped->memberCount();
|
|
|
|
|
str << " scoped symbol of " << size << '\n';
|
|
|
|
|
for (int m = 0; m < size; m++)
|
|
|
|
|
debugCppSymbolRecursion(str, o, *scoped->memberAt(m), true, recursion + 1);
|
|
|
|
|
} else {
|
|
|
|
|
str << '\n';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QDebug operator<<(QDebug d, const Symbol &s)
|
|
|
|
|
{
|
|
|
|
|
QString output;
|
2014-09-19 17:06:26 +02:00
|
|
|
Overview o;
|
2013-01-24 16:33:17 +01:00
|
|
|
QTextStream str(&output);
|
|
|
|
|
debugCppSymbolRecursion(str, o, s, true, 0);
|
|
|
|
|
d.nospace() << output;
|
|
|
|
|
return d;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QDebug operator<<(QDebug d, const Scope &scope)
|
|
|
|
|
{
|
|
|
|
|
QString output;
|
|
|
|
|
Overview o;
|
|
|
|
|
QTextStream str(&output);
|
|
|
|
|
const int size = scope.memberCount();
|
|
|
|
|
str << "Scope of " << size;
|
|
|
|
|
if (scope.isNamespace())
|
|
|
|
|
str << " namespace";
|
|
|
|
|
if (scope.isClass())
|
|
|
|
|
str << " class";
|
|
|
|
|
if (scope.isEnum())
|
|
|
|
|
str << " enum";
|
|
|
|
|
if (scope.isBlock())
|
|
|
|
|
str << " block";
|
|
|
|
|
if (scope.isFunction())
|
|
|
|
|
str << " function";
|
2014-05-05 17:09:38 +03:00
|
|
|
if (scope.isDeclaration())
|
2013-01-24 16:33:17 +01:00
|
|
|
str << " prototype";
|
|
|
|
|
#if 0 // ### port me
|
|
|
|
|
if (const Symbol *owner = &scope) {
|
|
|
|
|
str << " owner: ";
|
|
|
|
|
debugCppSymbolRecursion(str, o, *owner, false, 0);
|
|
|
|
|
} else {
|
|
|
|
|
str << " 0-owner\n";
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
for (int s = 0; s < size; s++)
|
|
|
|
|
debugCppSymbolRecursion(str, o, *scope.memberAt(s), true, 2);
|
|
|
|
|
d.nospace() << output;
|
|
|
|
|
return d;
|
|
|
|
|
}
|
2014-09-19 17:06:26 +02:00
|
|
|
|
2013-01-24 16:33:17 +01:00
|
|
|
} // namespace CPlusPlus
|
|
|
|
|
|
|
|
|
|
namespace Debugger {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
/* getUninitializedVariables(): Get variables that are not initialized
|
|
|
|
|
* at a certain line of a function from the code model to be able to
|
|
|
|
|
* indicate them as not in scope in the locals view.
|
|
|
|
|
* Find document + function in the code model, do a double check and
|
|
|
|
|
* collect declarative symbols that are in the function past or on
|
|
|
|
|
* the current line. blockRecursion() recurses up the scopes
|
|
|
|
|
* and collect symbols declared past or on the current line.
|
|
|
|
|
* Recursion goes up from the innermost scope, keeping a map
|
|
|
|
|
* of occurrences seen, to be able to derive the names of
|
|
|
|
|
* shadowed variables as the debugger sees them:
|
|
|
|
|
\code
|
|
|
|
|
int x; // Occurrence (1), should be reported as "x <shadowed 1>"
|
|
|
|
|
if (true) {
|
|
|
|
|
int x = 5; (2) // Occurrence (2), should be reported as "x"
|
|
|
|
|
}
|
|
|
|
|
\endcode
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
typedef QHash<QString, int> SeenHash;
|
|
|
|
|
|
2014-09-19 17:06:26 +02:00
|
|
|
static void blockRecursion(const Overview &overview,
|
|
|
|
|
const Scope *scope,
|
2013-01-24 16:33:17 +01:00
|
|
|
unsigned line,
|
|
|
|
|
QStringList *uninitializedVariables,
|
|
|
|
|
SeenHash *seenHash,
|
|
|
|
|
int level = 0)
|
|
|
|
|
{
|
|
|
|
|
// Go backwards in case someone has identical variables in the same scope.
|
|
|
|
|
// Fixme: loop variables or similar are currently seen in the outer scope
|
|
|
|
|
for (int s = scope->memberCount() - 1; s >= 0; --s){
|
2014-09-19 17:06:26 +02:00
|
|
|
const Symbol *symbol = scope->memberAt(s);
|
2013-01-24 16:33:17 +01:00
|
|
|
if (symbol->isDeclaration()) {
|
|
|
|
|
// Find out about shadowed symbols by bookkeeping
|
|
|
|
|
// the already seen occurrences in a hash.
|
|
|
|
|
const QString name = overview.prettyName(symbol->name());
|
|
|
|
|
SeenHash::iterator it = seenHash->find(name);
|
|
|
|
|
if (it == seenHash->end())
|
|
|
|
|
it = seenHash->insert(name, 0);
|
|
|
|
|
else
|
|
|
|
|
++(it.value());
|
|
|
|
|
// Is the declaration on or past the current line, that is,
|
|
|
|
|
// the variable not initialized.
|
|
|
|
|
if (symbol->line() >= line)
|
|
|
|
|
uninitializedVariables->push_back(WatchData::shadowedName(name, it.value()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Next block scope.
|
2014-09-19 17:06:26 +02:00
|
|
|
if (const Scope *enclosingScope = scope->enclosingBlock())
|
2013-01-24 16:33:17 +01:00
|
|
|
blockRecursion(overview, enclosingScope, line, uninitializedVariables, seenHash, level + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Inline helper with integer error return codes.
|
|
|
|
|
static inline
|
2014-09-19 17:06:26 +02:00
|
|
|
int getUninitializedVariablesI(const Snapshot &snapshot,
|
2013-01-24 16:33:17 +01:00
|
|
|
const QString &functionName,
|
|
|
|
|
const QString &file,
|
|
|
|
|
int line,
|
|
|
|
|
QStringList *uninitializedVariables)
|
|
|
|
|
{
|
|
|
|
|
uninitializedVariables->clear();
|
|
|
|
|
// Find document
|
|
|
|
|
if (snapshot.isEmpty() || functionName.isEmpty() || file.isEmpty() || line < 1)
|
|
|
|
|
return 1;
|
2014-09-19 17:06:26 +02:00
|
|
|
const Snapshot::const_iterator docIt = snapshot.find(file);
|
2013-01-24 16:33:17 +01:00
|
|
|
if (docIt == snapshot.end())
|
|
|
|
|
return 2;
|
2014-09-19 17:06:26 +02:00
|
|
|
const Document::Ptr doc = docIt.value();
|
2013-01-24 16:33:17 +01:00
|
|
|
// Look at symbol at line and find its function. Either it is the
|
|
|
|
|
// function itself or some expression/variable.
|
2014-09-19 17:06:26 +02:00
|
|
|
const Symbol *symbolAtLine = doc->lastVisibleSymbolAt(line, 0);
|
2013-01-24 16:33:17 +01:00
|
|
|
if (!symbolAtLine)
|
|
|
|
|
return 4;
|
|
|
|
|
// First figure out the function to do a safety name check
|
|
|
|
|
// and the innermost scope at cursor position
|
2014-09-19 17:06:26 +02:00
|
|
|
const Function *function = 0;
|
|
|
|
|
const Scope *innerMostScope = 0;
|
2013-01-24 16:33:17 +01:00
|
|
|
if (symbolAtLine->isFunction()) {
|
|
|
|
|
function = symbolAtLine->asFunction();
|
|
|
|
|
if (function->memberCount() == 1) // Skip over function block
|
2014-09-19 17:06:26 +02:00
|
|
|
if (Block *block = function->memberAt(0)->asBlock())
|
2013-01-24 16:33:17 +01:00
|
|
|
innerMostScope = block;
|
|
|
|
|
} else {
|
2014-09-19 17:06:26 +02:00
|
|
|
if (const Scope *functionScope = symbolAtLine->enclosingFunction()) {
|
2013-01-24 16:33:17 +01:00
|
|
|
function = functionScope->asFunction();
|
|
|
|
|
innerMostScope = symbolAtLine->isBlock() ?
|
|
|
|
|
symbolAtLine->asBlock() :
|
|
|
|
|
symbolAtLine->enclosingBlock();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!function || !innerMostScope)
|
|
|
|
|
return 7;
|
|
|
|
|
// 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
|
2014-09-19 17:06:26 +02:00
|
|
|
Overview overview;
|
2013-01-24 16:33:17 +01:00
|
|
|
const QString name = overview.prettyName(function->name());
|
|
|
|
|
if (!functionName.endsWith(name))
|
|
|
|
|
return 11;
|
|
|
|
|
if (functionName.size() > name.size()) {
|
|
|
|
|
const char previousChar = functionName.at(functionName.size() - name.size() - 1).toLatin1();
|
|
|
|
|
if (previousChar != ':' && previousChar != '!' )
|
|
|
|
|
return 11;
|
|
|
|
|
}
|
|
|
|
|
// Starting from the innermost block scope, collect declarations.
|
|
|
|
|
SeenHash seenHash;
|
|
|
|
|
blockRecursion(overview, innerMostScope, line, uninitializedVariables, &seenHash);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-19 17:06:26 +02:00
|
|
|
bool getUninitializedVariables(const Snapshot &snapshot,
|
2013-01-24 16:33:17 +01:00
|
|
|
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 << " '"
|
2014-08-23 01:19:53 +02:00
|
|
|
<< uninitializedVariables->join(QLatin1Char(',')) << '\'';
|
2013-01-24 16:33:17 +01:00
|
|
|
if (rc)
|
|
|
|
|
str << " of " << snapshot.size() << " documents";
|
|
|
|
|
qDebug() << msg;
|
|
|
|
|
}
|
|
|
|
|
return rc == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Editor tooltip support
|
2014-09-26 11:37:54 +02:00
|
|
|
bool isCppEditor(TextEditorWidget *editorWidget)
|
2013-01-24 16:33:17 +01:00
|
|
|
{
|
2014-09-22 18:43:31 +02:00
|
|
|
const TextDocument *document = editorWidget->textDocument();
|
2014-09-19 17:06:26 +02:00
|
|
|
return ProjectFile::classify(document->filePath()) != ProjectFile::Unclassified;
|
2013-01-24 16:33:17 +01:00
|
|
|
}
|
|
|
|
|
|
2014-09-19 17:06:26 +02:00
|
|
|
QString cppFunctionAt(const QString &fileName, int line, int column)
|
|
|
|
|
{
|
2014-11-15 13:33:40 +01:00
|
|
|
const Snapshot snapshot = CppModelManager::instance()->snapshot();
|
2014-09-19 17:06:26 +02:00
|
|
|
if (const Document::Ptr document = snapshot.document(fileName))
|
|
|
|
|
return document->functionAt(line, column);
|
|
|
|
|
|
|
|
|
|
return QString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2013-01-24 16:33:17 +01:00
|
|
|
// Return the Cpp expression, and, if desired, the function
|
2014-09-26 11:37:54 +02:00
|
|
|
QString cppExpressionAt(TextEditorWidget *editorWidget, int pos,
|
2014-11-15 13:33:40 +01:00
|
|
|
int *line, int *column, QString *function,
|
|
|
|
|
int *scopeFromLine, int *scopeToLine)
|
2013-01-24 16:33:17 +01:00
|
|
|
{
|
|
|
|
|
*line = *column = 0;
|
|
|
|
|
if (function)
|
|
|
|
|
function->clear();
|
|
|
|
|
|
2014-09-19 15:26:41 +02:00
|
|
|
QTextCursor tc = editorWidget->textCursor();
|
2014-05-21 15:03:23 +02:00
|
|
|
QString expr = tc.selectedText();
|
2014-11-15 13:33:40 +01:00
|
|
|
if (expr.isEmpty()) {
|
2013-01-24 16:33:17 +01:00
|
|
|
tc.setPosition(pos);
|
2014-09-19 15:26:41 +02:00
|
|
|
const QChar ch = editorWidget->characterAt(pos);
|
2013-01-24 16:33:17 +01:00
|
|
|
if (ch.isLetterOrNumber() || ch == QLatin1Char('_'))
|
|
|
|
|
tc.movePosition(QTextCursor::EndOfWord);
|
|
|
|
|
|
|
|
|
|
// Fetch the expression's code.
|
2014-09-19 17:06:26 +02:00
|
|
|
ExpressionUnderCursor expressionUnderCursor;
|
2013-01-24 16:33:17 +01:00
|
|
|
expr = expressionUnderCursor(tc);
|
|
|
|
|
*column = tc.positionInBlock();
|
|
|
|
|
*line = tc.blockNumber();
|
|
|
|
|
} else {
|
|
|
|
|
*column = tc.positionInBlock();
|
|
|
|
|
*line = tc.blockNumber();
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-15 13:33:40 +01:00
|
|
|
if (!expr.isEmpty()) {
|
|
|
|
|
QString fileName = editorWidget->textDocument()->filePath();
|
|
|
|
|
const Snapshot snapshot = CppModelManager::instance()->snapshot();
|
|
|
|
|
if (const Document::Ptr document = snapshot.document(fileName)) {
|
|
|
|
|
QString func = document->functionAt(*line, *column, scopeFromLine, scopeToLine);
|
|
|
|
|
if (function)
|
|
|
|
|
*function = func;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-01-24 16:33:17 +01:00
|
|
|
|
|
|
|
|
return expr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ensure an expression can be added as side-effect
|
|
|
|
|
// free debugger expression.
|
|
|
|
|
QString fixCppExpression(const QString &expIn)
|
|
|
|
|
{
|
2013-09-03 18:48:02 +02:00
|
|
|
QString exp = expIn.trimmed();
|
2013-01-24 16:33:17 +01:00
|
|
|
// Extract the first identifier, everything else is considered
|
|
|
|
|
// too dangerous.
|
|
|
|
|
int pos1 = 0, pos2 = exp.size();
|
|
|
|
|
bool inId = false;
|
|
|
|
|
for (int i = 0; i != exp.size(); ++i) {
|
|
|
|
|
const QChar c = exp.at(i);
|
|
|
|
|
const bool isIdChar = c.isLetterOrNumber() || c.unicode() == '_';
|
|
|
|
|
if (inId && !isIdChar) {
|
|
|
|
|
pos2 = i;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (!inId && isIdChar) {
|
|
|
|
|
inId = true;
|
|
|
|
|
pos1 = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
exp = exp.mid(pos1, pos2 - pos1);
|
|
|
|
|
return removeObviousSideEffects(exp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace Debugger
|