Annotate the ClassSpecifierAST node with the class symbol.

This commit is contained in:
Roberto Raggi
2009-02-09 11:35:57 +01:00
parent 1dcabe5a3a
commit 6672f89ff5
3 changed files with 42 additions and 0 deletions

View File

@@ -515,6 +515,9 @@ public:
DeclarationAST *member_specifiers; DeclarationAST *member_specifiers;
unsigned rbrace_token; unsigned rbrace_token;
public: // annotations
Class *class_symbol;
public: public:
virtual unsigned firstToken() const; virtual unsigned firstToken() const;
virtual unsigned lastToken() const; virtual unsigned lastToken() const;

View File

@@ -300,6 +300,7 @@ bool CheckSpecifier::visit(ClassSpecifierAST *ast)
{ {
Name *className = semantic()->check(ast->name, _scope); Name *className = semantic()->check(ast->name, _scope);
Class *klass = control()->newClass(ast->firstToken(), className); Class *klass = control()->newClass(ast->firstToken(), className);
ast->class_symbol = klass;
unsigned classKey = tokenKind(ast->classkey_token); unsigned classKey = tokenKind(ast->classkey_token);
if (classKey == T_CLASS) if (classKey == T_CLASS)
klass->setClassKey(Class::ClassKey); klass->setClassKey(Class::ClassKey);

View File

@@ -1,6 +1,10 @@
#include <QtTest> #include <QtTest>
#include <QObject> #include <QObject>
#include <AST.h>
#include <ASTVisitor.h>
#include <TranslationUnit.h>
#include <CppDocument.h> #include <CppDocument.h>
#include <LookupContext.h> #include <LookupContext.h>
#include <Symbols.h> #include <Symbols.h>
@@ -8,6 +12,29 @@
CPLUSPLUS_USE_NAMESPACE CPLUSPLUS_USE_NAMESPACE
class ClassSymbols: protected ASTVisitor,
public QMap<ClassSpecifierAST *, Class *>
{
public:
ClassSymbols(Control *control)
: ASTVisitor(control)
{ }
void operator()(AST *ast)
{ accept(ast); }
protected:
virtual bool visit(ClassSpecifierAST *ast)
{
Class *classSymbol = ast->class_symbol;
Q_ASSERT(classSymbol != 0);
insert(ast, classSymbol);
return true;
}
};
class tst_Lookup: public QObject class tst_Lookup: public QObject
{ {
Q_OBJECT Q_OBJECT
@@ -50,6 +77,17 @@ void tst_Lookup::base_class_defined_1()
QCOMPARE(candidates.size(), 1); QCOMPARE(candidates.size(), 1);
QCOMPARE(candidates.at(0), baseClass); QCOMPARE(candidates.at(0), baseClass);
TranslationUnit *unit = doc->translationUnit();
QVERIFY(unit != 0);
TranslationUnitAST *ast = unit->ast()->asTranslationUnit();
QVERIFY(ast != 0);
ClassSymbols classSymbols(doc->control());
classSymbols(ast);
QCOMPARE(classSymbols.size(), 2);
} }
QTEST_APPLESS_MAIN(tst_Lookup) QTEST_APPLESS_MAIN(tst_Lookup)