diff --git a/src/libs/cplusplus/CppDocument.cpp b/src/libs/cplusplus/CppDocument.cpp index 5f001f2af7e..ce05b77337e 100644 --- a/src/libs/cplusplus/CppDocument.cpp +++ b/src/libs/cplusplus/CppDocument.cpp @@ -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(line); + } + + if (lineClosingBrace) { + unsigned line; + translationUnit()->getPosition(scope->endOffset(), &line); + *lineClosingBrace = static_cast(line); + } + const QList fullyQualifiedName = LookupContext::fullyQualifiedName(scope); return Overview().prettyName(fullyQualifiedName); } diff --git a/src/libs/cplusplus/CppDocument.h b/src/libs/cplusplus/CppDocument.h index f9d4dba3c4a..c680a0751e9 100644 --- a/src/libs/cplusplus/CppDocument.h +++ b/src/libs/cplusplus/CppDocument.h @@ -101,7 +101,8 @@ public: QList 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); diff --git a/tests/auto/cplusplus/lookup/tst_lookup.cpp b/tests/auto/cplusplus/lookup/tst_lookup.cpp index 9dd5c81d53f..3f221d31746 100644 --- a/tests/auto/cplusplus/lookup/tst_lookup.cpp +++ b/tests/auto/cplusplus/lookup/tst_lookup.cpp @@ -163,6 +163,8 @@ void tst_Lookup::document_functionAt_data() QTest::addColumn("line"); QTest::addColumn("column"); QTest::addColumn("expectedFunction"); + QTest::addColumn("expectedOpeningDeclaratorParenthesisLine"); + QTest::addColumn("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()