Commit 864f07d4 authored by Roberto Raggi's avatar Roberto Raggi
Browse files

Initial work on the test suite for the C++ front-end semantic pass.

parent 286e45b4
load(qttest_p4)
include(../shared/shared.pri)
QT = core
SOURCES += tst_semantic.cpp
#include <QtTest>
#include <QtDebug>
#include <Control.h>
#include <Parser.h>
#include <AST.h>
#include <Semantic.h>
#include <Scope.h>
#include <Symbols.h>
#include <Names.h>
#include <Literals.h>
CPLUSPLUS_USE_NAMESPACE
class tst_Semantic: public QObject
{
Q_OBJECT
Control control;
public:
TranslationUnit *parse(const QByteArray &source,
TranslationUnit::ParseMode mode)
{
StringLiteral *fileId = control.findOrInsertFileName("<stdin>");
TranslationUnit *unit = new TranslationUnit(&control, fileId);
unit->setSource(source.constData(), source.length());
unit->parse(mode);
return unit;
}
class Document {
Q_DISABLE_COPY(Document)
public:
Document(TranslationUnit *unit)
: unit(unit), globals(new Scope())
{ }
~Document()
{ delete globals; }
void check()
{
QVERIFY(unit);
QVERIFY(unit->ast());
Semantic sem(unit->control());
TranslationUnitAST *ast = unit->ast()->asTranslationUnit();
QVERIFY(ast);
for (DeclarationAST *decl = ast->declarations; decl; decl = decl->next) {
sem.check(decl, globals);
}
}
TranslationUnit *unit;
Scope *globals;
};
QSharedPointer<Document> document(const QByteArray &source)
{
TranslationUnit *unit = parse(source, TranslationUnit::ParseTranlationUnit);
QSharedPointer<Document> doc(new Document(unit));
doc->check();
return doc;
}
private slots:
void function_declarations();
};
void tst_Semantic::function_declarations()
{
QSharedPointer<Document> doc = document("void foo();");
QCOMPARE(doc->globals->symbolCount(), 1U);
Declaration *decl = doc->globals->symbolAt(0)->asDeclaration();
QVERIFY(decl);
FullySpecifiedType declTy = decl->type();
Function *funTy = declTy->asFunction();
QVERIFY(funTy);
QVERIFY(funTy->returnType()->isVoidType());
QCOMPARE(funTy->argumentCount(), 0U);
QVERIFY(decl->name()->isNameId());
Identifier *funId = decl->name()->asNameId()->identifier();
QVERIFY(funId);
const QByteArray foo(funId->chars(), funId->size());
QCOMPARE(foo, QByteArray("foo"));
}
QTEST_APPLESS_MAIN(tst_Semantic)
#include "tst_semantic.moc"
Markdown is supported
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