Newer
Older
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
**
**************************************************************************/
#include "CheckUndefinedSymbols.h"
#include "Overview.h"
#include <Names.h>
#include <Literals.h>
#include <Symbols.h>
#include <TranslationUnit.h>
#include <Scope.h>
#include <AST.h>
#include <QCoreApplication>
#include <QDebug>
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
226
227
228
229
230
231
232
233
234
235
236
namespace {
class CollectTypes: protected SymbolVisitor
{
Document::Ptr _doc;
Snapshot _snapshot;
QSet<QByteArray> _types;
QList<ScopedSymbol *> _scopes;
QList<NameAST *> _names;
bool _mainDocument;
public:
CollectTypes(Document::Ptr doc, const Snapshot &snapshot)
: _doc(doc), _snapshot(snapshot), _mainDocument(false)
{
QSet<Namespace *> processed;
process(doc, &processed);
}
const QSet<QByteArray> &types() const
{
return _types;
}
const QList<ScopedSymbol *> &scopes() const
{
return _scopes;
}
static Scope *findScope(unsigned tokenOffset, const QList<ScopedSymbol *> &scopes)
{
for (int i = scopes.size() - 1; i != -1; --i) {
ScopedSymbol *symbol = scopes.at(i);
const unsigned start = symbol->startOffset();
const unsigned end = symbol->endOffset();
if (tokenOffset >= start && tokenOffset < end)
return symbol->members();
}
return 0;
}
protected:
void process(Document::Ptr doc, QSet<Namespace *> *processed)
{
if (! doc)
return;
else if (! processed->contains(doc->globalNamespace())) {
processed->insert(doc->globalNamespace());
foreach (const Document::Include &i, doc->includes())
process(_snapshot.document(i.fileName()), processed);
_mainDocument = (doc == _doc); // ### improve
accept(doc->globalNamespace());
}
}
void addType(const Identifier *id)
{
if (id)
_types.insert(QByteArray::fromRawData(id->chars(), id->size()));
}
void addType(const Name *name)
{
if (! name) {
return;
} else if (const QualifiedNameId *q = name->asQualifiedNameId()) {
for (unsigned i = 0; i < q->nameCount(); ++i)
addType(q->nameAt(i));
} else if (name->isNameId() || name->isTemplateNameId()) {
addType(name->identifier());
}
}
void addScope(ScopedSymbol *symbol)
{
if (_mainDocument)
_scopes.append(symbol);
}
// nothing to do
virtual bool visit(UsingNamespaceDirective *) { return true; }
virtual bool visit(UsingDeclaration *) { return true; }
virtual bool visit(Argument *) { return true; }
virtual bool visit(BaseClass *) { return true; }
virtual bool visit(Function *symbol)
{
addScope(symbol);
return true;
}
virtual bool visit(Block *symbol)
{
addScope(symbol);
return true;
}
virtual bool visit(NamespaceAlias *symbol)
{
addType(symbol->name());
return true;
}
virtual bool visit(Declaration *symbol)
{
if (symbol->isTypedef())
addType(symbol->name());
return true;
}
virtual bool visit(TypenameArgument *symbol)
{
addType(symbol->name());
return true;
}
virtual bool visit(Enum *symbol)
{
addScope(symbol);
addType(symbol->name());
return true;
}
virtual bool visit(Namespace *symbol)
{
addScope(symbol);
addType(symbol->name());
return true;
}
virtual bool visit(Class *symbol)
{
addScope(symbol);
addType(symbol->name());
return true;
}
virtual bool visit(ForwardClassDeclaration *symbol)
{
addType(symbol->name());
return true;
}
// Objective-C
virtual bool visit(ObjCBaseClass *) { return true; }
virtual bool visit(ObjCBaseProtocol *) { return true; }
virtual bool visit(ObjCPropertyDeclaration *) { return true; }
virtual bool visit(ObjCMethod *symbol)
{
addScope(symbol);
return true;
}
virtual bool visit(ObjCClass *symbol)
{
addScope(symbol);
addType(symbol->name());
return true;
}
virtual bool visit(ObjCForwardClassDeclaration *symbol)
{
addType(symbol->name());
return true;
}
virtual bool visit(ObjCProtocol *symbol)
{
addScope(symbol);
addType(symbol->name());
return true;
}
virtual bool visit(ObjCForwardProtocolDeclaration *symbol)
{
addType(symbol->name());
return true;
}
};
} // end of anonymous namespace
CheckUndefinedSymbols::CheckUndefinedSymbols(TranslationUnit *unit, const LookupContext &context)
: ASTVisitor(unit), _context(context)
_fileName = context.thisDocument()->fileName();
CollectTypes collectTypes(context.thisDocument(), context.snapshot());
_potentialTypes = collectTypes.types();
_scopes = collectTypes.scopes();
CheckUndefinedSymbols::~CheckUndefinedSymbols()
{ }
QList<Document::DiagnosticMessage> CheckUndefinedSymbols::operator()(AST *ast)
_diagnosticMessages.clear();
accept(ast);
return _diagnosticMessages;
bool CheckUndefinedSymbols::warning(unsigned line, unsigned column, const QString &text, unsigned length)
Document::DiagnosticMessage m(Document::DiagnosticMessage::Warning, _fileName, line, column, text, length);
_diagnosticMessages.append(m);
bool CheckUndefinedSymbols::warning(AST *ast, const QString &text)
const Token &firstToken = tokenAt(ast->firstToken());
const Token &lastToken = tokenAt(ast->lastToken() - 1);
const unsigned length = lastToken.end() - firstToken.begin();
unsigned line = 1, column = 1;
getTokenStartPosition(ast->firstToken(), &line, &column);
warning(line, column, text, length);
return false;
bool CheckUndefinedSymbols::visit(NamespaceAST *ast)
if (ast->identifier_token) {
const Token &tok = tokenAt(ast->identifier_token);
if (! tok.generated()) {
unsigned line, column;
getTokenStartPosition(ast->identifier_token, &line, &column);
Use use(line, column, tok.length());
_typeUsages.append(use);
}
}
return true;
bool CheckUndefinedSymbols::visit(UsingDirectiveAST *)
{
return true;
}
bool CheckUndefinedSymbols::visit(SimpleDeclarationAST *)

Erik Verbruggen
committed
bool CheckUndefinedSymbols::visit(NamedTypeSpecifierAST *)
{

Erik Verbruggen
committed
}
void CheckUndefinedSymbols::checkNamespace(NameAST *name)
unsigned line, column;
getTokenStartPosition(name->firstToken(), &line, &column);
Scope *enclosingScope = _context.thisDocument()->scopeAt(line, column);
if (ClassOrNamespace *b = _context.lookupType(name->name, enclosingScope)) {
foreach (Symbol *s, b->symbols()) {
if (s->isNamespace())
return;
const unsigned length = tokenAt(name->lastToken() - 1).end() - tokenAt(name->firstToken()).begin();
warning(line, column, QCoreApplication::translate("CheckUndefinedSymbols", "Expected a namespace-name"), length);
void CheckUndefinedSymbols::checkName(NameAST *ast)
{
if (ast->name) {
const QByteArray id = QByteArray::fromRawData(ast->name->identifier()->chars(), // ### move
ast->name->identifier()->size());
if (_potentialTypes.contains(id)) {
const unsigned tokenOffset = tokenAt(ast->firstToken()).offset;
Scope *scope = CollectTypes::findScope(tokenOffset, _scopes); // ### move
if (! scope)
scope = _context.thisDocument()->globalSymbols();
const QList<Symbol *> candidates = _context.lookup(ast->name, scope);
addTypeUsage(candidates, ast);
}
}
bool CheckUndefinedSymbols::visit(SimpleNameAST *ast)
{
checkName(ast);
return true;
}
bool CheckUndefinedSymbols::visit(TemplateIdAST *ast)
{
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
return true;
}
bool CheckUndefinedSymbols::visit(DestructorNameAST *ast)
{
if (ast->name) {
const QByteArray id = QByteArray::fromRawData(ast->name->identifier()->chars(), // ### move
ast->name->identifier()->size());
if (_potentialTypes.contains(id)) {
Scope *scope = CollectTypes::findScope(tokenAt(ast->firstToken()).offset, _scopes); // ### move
if (! scope)
scope = _context.thisDocument()->globalSymbols();
ClassOrNamespace *b = _context.lookupType(ast->name, scope);
addTypeUsage(b, ast);
}
}
return true;
}
bool CheckUndefinedSymbols::visit(QualifiedNameAST *ast)
{
if (ast->name) {
Scope *scope = CollectTypes::findScope(tokenAt(ast->firstToken()).offset, _scopes); // ### move
if (! scope)
scope = _context.thisDocument()->globalSymbols();
ClassOrNamespace *b = 0;
if (NestedNameSpecifierListAST *it = ast->nested_name_specifier_list) {
NestedNameSpecifierAST *nested_name_specifier = it->value;
NameAST *class_or_namespace_name = nested_name_specifier->class_or_namespace_name; // ### remove shadowing
const Name *name = class_or_namespace_name->name;
b = _context.lookupType(name, scope);
addTypeUsage(b, class_or_namespace_name);
for (it = it->next; b && it; it = it->next) {
NestedNameSpecifierAST *nested_name_specifier = it->value;
if (NameAST *class_or_namespace_name = nested_name_specifier->class_or_namespace_name) {
b = b->findType(class_or_namespace_name->name);
addTypeUsage(b, class_or_namespace_name);
}
}
}
if (b && ast->unqualified_name)
addTypeUsage(b->find(ast->unqualified_name->name), ast->unqualified_name);
}
return false;
}
void CheckUndefinedSymbols::addTypeUsage(ClassOrNamespace *b, NameAST *ast)
{
if (! b)
return;
const Token &tok = tokenAt(ast->firstToken());
if (tok.generated())
return;
unsigned line, column;
getTokenStartPosition(ast->firstToken(), &line, &column);
const unsigned length = tok.length();
Use use(line, column, length);
_typeUsages.append(use);
//qDebug() << "added use" << oo(ast->name) << line << column << length;
}
void CheckUndefinedSymbols::addTypeUsage(const QList<Symbol *> &candidates, NameAST *ast)
{
const Token &tok = tokenAt(ast->firstToken());
if (tok.generated())
return;
unsigned line, column;
getTokenStartPosition(ast->firstToken(), &line, &column);
const unsigned length = tok.length();
foreach (Symbol *c, candidates) {
if (c->isUsingDeclaration()) // skip using declarations...
continue;
else if (c->isUsingNamespaceDirective()) // ... and using namespace directives.
continue;
else if (c->isTypedef() || c->isNamespace() ||
c->isClass() || c->isEnum() ||
c->isForwardClassDeclaration() || c->isTypenameArgument()) {
Use use(line, column, length);
_typeUsages.append(use);
//qDebug() << "added use" << oo(ast->name) << line << column << length;
break;
}
}
}
QList<CheckUndefinedSymbols::Use> CheckUndefinedSymbols::typeUsages() const
{
return _typeUsages;
}