Commit 69504c25 authored by Nikolai Kosjar's avatar Nikolai Kosjar
Browse files

C++: Document::functionAt provides line information



Needed for the debugger.

Change-Id: I6465f6dc53017df212e403ea8a9a1c7977ac1671
Reviewed-by: default avatarhjk <hjk121@nokiamail.com>
parent fd3f544b
......@@ -493,8 +493,12 @@ void Document::setGlobalNamespace(Namespace *globalNamespace)
*
* \param line the line number, starting with line 1
* \param column the column number, starting with column 1
* \param lineOpeningDeclaratorParenthesis optional output parameter, the line of the opening
parenthesis of the declarator starting with 1
* \param lineClosingBrace optional output parameter, the line of the closing brace starting with 1
*/
QString Document::functionAt(int line, int column) const
QString Document::functionAt(int line, int column, int *lineOpeningDeclaratorParenthesis,
int *lineClosingBrace) const
{
if (line < 1 || column < 1)
return QString();
......@@ -517,7 +521,19 @@ QString Document::functionAt(int line, int column) const
if (!scope)
return QString();
// We found the function scope, extract its name.
// We found the function scope
if (lineOpeningDeclaratorParenthesis) {
unsigned line;
translationUnit()->getPosition(scope->startOffset(), &line);
*lineOpeningDeclaratorParenthesis = static_cast<int>(line);
}
if (lineClosingBrace) {
unsigned line;
translationUnit()->getPosition(scope->endOffset(), &line);
*lineClosingBrace = static_cast<int>(line);
}
const QList<const Name *> fullyQualifiedName = LookupContext::fullyQualifiedName(scope);
return Overview().prettyName(fullyQualifiedName);
}
......
......@@ -101,7 +101,8 @@ public:
QList<Macro> definedMacros() const
{ return _definedMacros; }
QString functionAt(int line, int column) const;
QString functionAt(int line, int column, int *lineOpeningDeclaratorParenthesis = 0,
int *lineClosingBrace = 0) const;
Symbol *lastVisibleSymbolAt(unsigned line, unsigned column = 0) const;
Scope *scopeAt(unsigned line, unsigned column = 0);
......
......@@ -163,6 +163,8 @@ void tst_Lookup::document_functionAt_data()
QTest::addColumn<int>("line");
QTest::addColumn<int>("column");
QTest::addColumn<QString>("expectedFunction");
QTest::addColumn<int>("expectedOpeningDeclaratorParenthesisLine");
QTest::addColumn<int>("expectedClosingBraceLine");
QByteArray source = "\n"
"void Foo::Bar() {\n" // line 1
......@@ -172,12 +174,12 @@ void tst_Lookup::document_functionAt_data()
" }\n" // line 5
"}\n";
QString expectedFunction = QString::fromLatin1("Foo::Bar");
QTest::newRow("nonInline1") << source << 1 << 2 << QString();
QTest::newRow("nonInline2") << source << 1 << 11 << expectedFunction;
QTest::newRow("nonInline3") << source << 2 << 2 << expectedFunction;
QTest::newRow("nonInline4") << source << 3 << 10 << expectedFunction;
QTest::newRow("nonInline5") << source << 4 << 3 << expectedFunction;
QTest::newRow("nonInline6") << source << 6 << 1 << expectedFunction;
QTest::newRow("nonInline1") << source << 1 << 2 << QString() << -1 << -1;
QTest::newRow("nonInline2") << source << 1 << 11 << expectedFunction << 1 << 6;
QTest::newRow("nonInline3") << source << 2 << 2 << expectedFunction << 1 << 6;
QTest::newRow("nonInline4") << source << 3 << 10 << expectedFunction << 1 << 6;
QTest::newRow("nonInline5") << source << 4 << 3 << expectedFunction << 1 << 6;
QTest::newRow("nonInline6") << source << 6 << 1 << expectedFunction << 1 << 6;
source = "\n"
"namespace N {\n" // line 1
......@@ -188,9 +190,16 @@ void tst_Lookup::document_functionAt_data()
"};\n"
"}\n"; // line 7
expectedFunction = QString::fromLatin1("N::C::f");
QTest::newRow("inline1") << source << 1 << 2 << QString();
QTest::newRow("inline2") << source << 2 << 10 << QString();
QTest::newRow("inline2") << source << 3 << 10 << expectedFunction;
QTest::newRow("inline1") << source << 1 << 2 << QString() << -1 << -1;
QTest::newRow("inline2") << source << 2 << 10 << QString() << -1 << -1;
QTest::newRow("inline2") << source << 3 << 10 << expectedFunction << 3 << 5;
source = "\n"
"void f(Helper helper = [](){})\n" // line 1
"{\n"
"}\n"; // line 3
expectedFunction = QString::fromLatin1("f");
QTest::newRow("inlineWithLambdaArg1") << source << 2 << 1 << expectedFunction << 1 << 3;
}
void tst_Lookup::document_functionAt()
......@@ -199,14 +208,23 @@ void tst_Lookup::document_functionAt()
QFETCH(int, line);
QFETCH(int, column);
QFETCH(QString, expectedFunction);
QFETCH(int, expectedOpeningDeclaratorParenthesisLine);
QFETCH(int, expectedClosingBraceLine);
Document::Ptr doc = Document::create("document_functionAt");
doc->setUtf8Source(source);
doc->parse();
doc->check();
QVERIFY(doc->diagnosticMessages().isEmpty());
QCOMPARE(doc->functionAt(line, column), expectedFunction);
int actualOpeningDeclaratorParenthesisLine = -1;
int actualClosingBraceLine = -1;
const QString actualFunction = doc->functionAt(line, column,
&actualOpeningDeclaratorParenthesisLine,
&actualClosingBraceLine);
QCOMPARE(actualFunction, expectedFunction);
QCOMPARE(actualOpeningDeclaratorParenthesisLine, expectedOpeningDeclaratorParenthesisLine);
QCOMPARE(actualClosingBraceLine, expectedClosingBraceLine);
}
void tst_Lookup::simple_class_1()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment