Skip to content

Commit

Permalink
fix: Capturing of return types in FunctionSymbol
Browse files Browse the repository at this point in the history
FunctionSymbols can now deal with:
- pointers
- references
- const specifiers
  • Loading branch information
LeonMatthesKDAB committed Jul 26, 2024
1 parent f662fc5 commit f76298d
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 5 deletions.
16 changes: 12 additions & 4 deletions src/core/cppdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,35 @@ auto queryFunctionSymbols(CodeDocument *const document) -> QList<Core::Symbol *>

auto pointerDeclarator = QString(R"EOF(
[%1
(_ "*"? @return "&"? @return "&&"? @return %1)
(_ ["*" "&" "&&"]? @return (type_qualifier)? @return %1)
(_ ["*" "&" "&&"]? @return
(_ ["*" "&" "&&"]? @return %1))]
(type_qualifier)? @return
(_ ["*" "&" "&&"]? @return (type_qualifier)? @return %1))]
)EOF")
.arg(functionDeclarator);

// TODO: Add support for pointers & references
auto functions = document->query(QString(R"EOF(
[; Free functions
[; Free function implementations
(function_definition
(type_qualifier)? @return
type: (_)? @return
; If using trailing return type, we need to remove the auto type at the start
(#exclude! @return placeholder_type_specifier)
declarator: %2) @range
; Free Function declarations
(declaration
(type_qualifier)? @return
type: (_)? @return
declarator: %2) @range
; Constructor/Destructors
(declaration
declarator: %1) @range
; Member functions
(field_declaration
(type_qualifier)? @return
type: (_)? @return
; If using trailing return type, we need to remove the auto type at the start
(#exclude! @return placeholder_type_specifier)
Expand Down
2 changes: 1 addition & 1 deletion src/core/functionsymbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ QString FunctionSymbol::signature() const

QString FunctionSymbol::returnTypeFromQueryMatch() const
{
return m_queryMatch.getAllJoined("return").text();
return m_queryMatch.getAllJoined("return").text().simplified();
}

QString FunctionSymbol::returnType() const
Expand Down
50 changes: 50 additions & 0 deletions test_data/tst_symbol/return_type.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#pragma once

#include <string>

using namespace std;

void *freePtr();
void *freePtrImpl() { return nullptr; }

const std::string &freeReference();
const std::string &freeReferenceImpl() {
return "";
}

const class T *const freeConstPtr();
const class T *const freeConstPtrImpl() { return nullptr; }

class MyClass {
MyClass() = default();
~MyClass() = default();

void *memberPtr();
const string& memberReference();
const class T* const memberConstPtr();
};

void*MyClass::memberPtrImpl() {
return nullptr;
}

const std::string& MyClass::memberReferenceImpl() {
return m_thing;
}

const
class
T
*
const
MyClass::memberConstPtrImpl() {
return nullptr;
}

MyClassImpl::MyClassImpl()
{
}

MyClassImpl::~MyClassImpl()
{
}
48 changes: 48 additions & 0 deletions tests/tst_symbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,54 @@ private slots:
testTypedSymbol("TestClass::m_rvalue_reference", "std::string&&");
testTypedSymbol("TestClass::m_const_ptr", "const class T*const");
}

void functionSymbolReturnType_data()
{
QTest::addColumn<QString>("symbolName");
QTest::addColumn<QString>("returnType");

auto addRow = [&](const QString &symbolName, const QString &returnType) {
QTest::newRow(symbolName.toUtf8().constData()) << symbolName << returnType;
};

addRow("freePtr", "void *");
addRow("freePtrImpl", "void *");
addRow("freeReference", "const std::string &");
addRow("freeReferenceImpl", "const std::string &");
addRow("freeConstPtr", "const class T *const");
addRow("freeConstPtrImpl", "const class T *const");

addRow("MyClass::memberPtr", "void *");
addRow("MyClass::memberReference", "const string&");
addRow("MyClass::memberConstPtr", "const class T* const");

addRow("MyClass::memberPtrImpl", "void*");
addRow("MyClass::memberReferenceImpl", "const std::string&");
addRow("MyClass::memberConstPtrImpl", "const class T * const");

addRow("MyClass::MyClass", "");
addRow("MyClass::~MyClass", "");
addRow("MyClassImpl::MyClassImpl", "");
addRow("MyClassImpl::~MyClassImpl", "");
}

void functionSymbolReturnType()
{
QFETCH(QString, symbolName);
QFETCH(QString, returnType);

Core::KnutCore core;
Core::Project::instance()->setRoot(Test::testDataPath() + "/tst_symbol/");

auto codeDocument = qobject_cast<Core::CodeDocument *>(Core::Project::instance()->open("return_type.h"));
QVERIFY(codeDocument);

auto symbol = codeDocument->findSymbol(symbolName);
QVERIFY(symbol);
auto functionSymbol = qobject_cast<Core::FunctionSymbol *>(symbol);
QVERIFY(functionSymbol);
QCOMPARE(functionSymbol->returnType(), returnType);
}
};

QTEST_MAIN(TestSymbol)
Expand Down

0 comments on commit f76298d

Please sign in to comment.