1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "cplusplus/Overview.h"
#include <cplusplus/declarationcomments.h>
#include <cplusplus/CppDocument.h>
#include <cplusplus/SymbolVisitor.h>
#include <QTest>
#include <QTextDocument>
using namespace CPlusPlus;
using namespace Utils;
const char testSource[] = R"TheFile(
//! Unrelated comment, even though it mentions MyEnum.
//! Related comment because of proximity.
//! Related comment that mentions MyEnum.
//! Related comment that mentions MyEnumA.
enum MyEnum { MyEnumA, MyEnumB };
// Unrelated comment because of different comment type.
//! Related comment that mentions MyEnumClass::A.
enum class MyEnumClass { A, B };
// Related comment because of proximity.
void myFunc();
/*
* Related comment because of proximity.
*/
template<typename T> class MyClassTemplate {};
/*
* Related comment, because it mentions MyOtherClass.
*/
class MyOtherClass {
/// Related comment for function and parameter a2, but not parameter a.
/// @param a2
void memberFunc(int a, int a2);
};
//! Comment for member function at implementation site.
void MyOtherClass::memberFunc(int, int) {}
//! An unrelated comment
void funcWithoutComment();
)TheFile";
class SymbolFinder : public SymbolVisitor
{
public:
SymbolFinder(const QString &name, Document *doc, int expectedOccurrence)
: m_name(name), m_doc(doc), m_expectedOccurrence(expectedOccurrence) {}
const Symbol *find();
private:
bool preVisit(Symbol *) override { return !m_symbol; }
bool visit(Namespace *ns) override { return visitScope(ns); }
bool visit(Enum *e) override { return visitScope(e); }
bool visit(Class *c) override { return visitScope(c); }
bool visit(Function *f) override { return visitScope(f); }
bool visit(Argument *a) override;
bool visit(Declaration *d) override;
bool visitScope(Scope *scope);
const QString &m_name;
Document * const m_doc;
const Symbol *m_symbol = nullptr;
const int m_expectedOccurrence;
int m_tokenLocation = -1;
};
class TestDeclarationComments : public QObject
{
Q_OBJECT
private Q_SLOTS:
void initTestCase();
void commentsForDecl_data();
void commentsForDecl();
private:
Snapshot m_snapshot;
Document::Ptr m_cppDoc;
QTextDocument m_textDoc;
};
void TestDeclarationComments::initTestCase()
{
m_cppDoc = m_snapshot.preprocessedDocument(testSource, "dummy.cpp");
m_cppDoc->check();
const bool hasErrors = !m_cppDoc->diagnosticMessages().isEmpty();
if (hasErrors) {
for (const Document::DiagnosticMessage &msg : m_cppDoc->diagnosticMessages())
qDebug() << '\t' << msg.text();
}
QVERIFY(!hasErrors);
m_textDoc.setPlainText(testSource);
}
void TestDeclarationComments::commentsForDecl_data()
{
QTest::addColumn<QString>("symbolName");
QTest::addColumn<QString>("expectedCommentPrefix");
QTest::addColumn<int>("occurrence");
QTest::newRow("enum type") << "MyEnum" << "//! Related comment because of proximity" << 1;
QTest::newRow("enum value (positive)") << "MyEnumA"
<< "//! Related comment because of proximity" << 1;
QTest::newRow("enum value (negative") << "MyEnumB" << QString() << 1;
QTest::newRow("enum class type") << "MyEnumClass"
<< "//! Related comment that mentions MyEnumClass::A" << 1;
QTest::newRow("enum class value (positive)")
<< "A" << "//! Related comment that mentions MyEnumClass::A" << 1;
QTest::newRow("enum class value (negative") << "B" << QString() << 1;
QTest::newRow("function declaration with comment")
<< "myFunc" << "// Related comment because of proximity" << 1;
QTest::newRow("class template")
<< "MyClassTemplate" << "/*\n * Related comment because of proximity." << 1;
QTest::newRow("class")
<< "MyOtherClass" << "/*\n * Related comment, because it mentions MyOtherClass." << 1;
QTest::newRow("member function declaration")
<< "memberFunc" << "/// Related comment for function and parameter a2, but not parameter a."
<< 1;
QTest::newRow("function parameter (negative)") << "a" << QString() << 1;
QTest::newRow("function parameter (positive)") << "a2" << "/// Related comment for function "
"and parameter a2, but not parameter a." << 1;
QTest::newRow("member function definition") << "memberFunc" <<
"//! Comment for member function at implementation site." << 2;
QTest::newRow("function declaration without comment") << "funcWithoutComment" << QString() << 1;
}
void TestDeclarationComments::commentsForDecl()
{
QFETCH(QString, symbolName);
QFETCH(QString, expectedCommentPrefix);
QFETCH(int, occurrence);
SymbolFinder finder(symbolName, m_cppDoc.get(), occurrence);
const Symbol * const symbol = finder.find();
QVERIFY(symbol);
const QList<Token> commentTokens = commentsForDeclaration(symbol, m_textDoc, m_cppDoc);
if (expectedCommentPrefix.isEmpty()) {
QVERIFY(commentTokens.isEmpty());
return;
}
QVERIFY(!commentTokens.isEmpty());
const int firstCommentPos = m_cppDoc->translationUnit()->getTokenPositionInDocument(
commentTokens.first(), &m_textDoc);
const QString actualCommentPrefix = m_textDoc.toPlainText().mid(firstCommentPos,
expectedCommentPrefix.length());
QCOMPARE(actualCommentPrefix, expectedCommentPrefix);
}
const Symbol *SymbolFinder::find()
{
for (int i = 0, occurrences = 0; i < m_doc->translationUnit()->tokenCount(); ++i) {
const Token &tok = m_doc->translationUnit()->tokenAt(i);
if (tok.kind() != T_IDENTIFIER)
continue;
if (tok.spell() != m_name)
continue;
if (++occurrences == m_expectedOccurrence) {
m_tokenLocation = i;
break;
}
}
if (m_tokenLocation == -1)
return nullptr;
visit(m_doc->globalNamespace());
return m_symbol;
}
bool SymbolFinder::visit(Argument *a)
{
if (a->sourceLocation() == m_tokenLocation)
m_symbol = a;
return false;
}
bool SymbolFinder::visit(Declaration *d)
{
if (d->sourceLocation() == m_tokenLocation) {
m_symbol = d;
return false;
}
if (const auto func = d->type().type()->asFunctionType())
return visit(func);
return true;
}
bool SymbolFinder::visitScope(Scope *scope)
{
for (int i = 0; i < scope->memberCount(); ++i) {
Symbol * const s = scope->memberAt(i);
if (s->sourceLocation() == m_tokenLocation) {
m_symbol = s;
return false;
}
accept(s);
}
return false;
}
QTEST_APPLESS_MAIN(TestDeclarationComments)
#include "tst_declarationcomments.moc"
|