2009-07-13 11:40:54 +02:00
|
|
|
/**************************************************************************
|
|
|
|
**
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
**
|
2010-03-05 11:25:49 +01:00
|
|
|
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
2009-07-13 11:40:54 +02:00
|
|
|
**
|
|
|
|
** 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
|
2009-08-14 09:30:56 +02:00
|
|
|
** contact the sales department at http://qt.nokia.com/contact.
|
2009-07-13 11:40:54 +02:00
|
|
|
**
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
#include "CheckUndefinedSymbols.h"
|
|
|
|
#include "Overview.h"
|
|
|
|
|
|
|
|
#include <Names.h>
|
|
|
|
#include <Literals.h>
|
|
|
|
#include <Symbols.h>
|
|
|
|
#include <TranslationUnit.h>
|
|
|
|
#include <Scope.h>
|
|
|
|
#include <AST.h>
|
|
|
|
|
2010-05-21 15:44:35 +02:00
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QDebug>
|
2009-07-13 11:40:54 +02:00
|
|
|
|
2010-05-21 15:44:35 +02:00
|
|
|
using namespace CPlusPlus;
|
2009-07-13 11:40:54 +02:00
|
|
|
|
2010-05-21 15:44:35 +02:00
|
|
|
CheckUndefinedSymbols::CheckUndefinedSymbols(Document::Ptr doc, const Snapshot &snapshot)
|
|
|
|
: ASTVisitor(doc->translationUnit()), _context(doc, snapshot)
|
2009-08-26 14:22:00 +02:00
|
|
|
{
|
2010-05-21 15:44:35 +02:00
|
|
|
_fileName = doc->fileName();
|
2009-08-26 14:22:00 +02:00
|
|
|
}
|
|
|
|
|
2010-05-21 15:44:35 +02:00
|
|
|
CheckUndefinedSymbols::~CheckUndefinedSymbols()
|
|
|
|
{ }
|
2009-07-13 11:40:54 +02:00
|
|
|
|
2010-05-21 15:44:35 +02:00
|
|
|
QList<Document::DiagnosticMessage> CheckUndefinedSymbols::operator()(AST *ast)
|
2009-07-13 11:40:54 +02:00
|
|
|
{
|
2010-05-21 15:44:35 +02:00
|
|
|
_diagnosticMessages.clear();
|
|
|
|
accept(ast);
|
|
|
|
return _diagnosticMessages;
|
2009-07-13 11:40:54 +02:00
|
|
|
}
|
|
|
|
|
2010-05-21 15:44:35 +02:00
|
|
|
bool CheckUndefinedSymbols::warning(unsigned line, unsigned column, const QString &text, unsigned length)
|
2009-07-13 11:40:54 +02:00
|
|
|
{
|
2010-05-21 15:44:35 +02:00
|
|
|
Document::DiagnosticMessage m(Document::DiagnosticMessage::Warning, _fileName, line, column, text, length);
|
|
|
|
_diagnosticMessages.append(m);
|
2009-07-13 11:40:54 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-05-21 15:44:35 +02:00
|
|
|
bool CheckUndefinedSymbols::warning(AST *ast, const QString &text)
|
2009-07-13 11:40:54 +02:00
|
|
|
{
|
2010-05-21 15:44:35 +02:00
|
|
|
const Token &firstToken = tokenAt(ast->firstToken());
|
|
|
|
const Token &lastToken = tokenAt(ast->lastToken() - 1);
|
2009-07-13 11:40:54 +02:00
|
|
|
|
2010-05-21 15:44:35 +02:00
|
|
|
const unsigned length = lastToken.end() - firstToken.begin();
|
|
|
|
unsigned line = 1, column = 1;
|
|
|
|
getTokenStartPosition(ast->firstToken(), &line, &column);
|
2009-07-13 11:40:54 +02:00
|
|
|
|
2010-05-21 15:44:35 +02:00
|
|
|
warning(line, column, text, length);
|
|
|
|
return false;
|
2009-08-26 14:22:00 +02:00
|
|
|
}
|
|
|
|
|
2010-05-21 15:44:35 +02:00
|
|
|
bool CheckUndefinedSymbols::visit(UsingDirectiveAST *ast)
|
2009-08-26 14:22:00 +02:00
|
|
|
{
|
2010-05-21 15:44:35 +02:00
|
|
|
checkNamespace(ast->name);
|
|
|
|
return false;
|
2009-08-26 14:22:00 +02:00
|
|
|
}
|
|
|
|
|
2009-07-13 11:40:54 +02:00
|
|
|
bool CheckUndefinedSymbols::visit(SimpleDeclarationAST *ast)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-05-21 15:44:35 +02:00
|
|
|
bool CheckUndefinedSymbols::visit(NamedTypeSpecifierAST *ast)
|
2009-07-13 11:40:54 +02:00
|
|
|
{
|
|
|
|
if (ast->name) {
|
2010-05-21 15:44:35 +02:00
|
|
|
unsigned line, column;
|
|
|
|
getTokenStartPosition(ast->firstToken(), &line, &column);
|
2009-07-13 11:40:54 +02:00
|
|
|
|
2010-05-21 15:44:35 +02:00
|
|
|
Scope *enclosingScope = _context.thisDocument()->scopeAt(line, column);
|
|
|
|
const QList<Symbol *> candidates = _context.lookup(ast->name->name, enclosingScope);
|
2009-10-05 18:02:01 +02:00
|
|
|
|
2010-05-21 15:44:35 +02:00
|
|
|
Symbol *ty = 0;
|
|
|
|
foreach (Symbol *c, candidates) {
|
|
|
|
if (c->isTypedef() || c->isClass() || c->isEnum()
|
|
|
|
|| c->isForwardClassDeclaration() || c->isTypenameArgument())
|
|
|
|
ty = c;
|
2009-10-05 18:02:01 +02:00
|
|
|
}
|
|
|
|
|
2010-05-21 15:44:35 +02:00
|
|
|
if (! ty)
|
|
|
|
warning(ast->name, QCoreApplication::translate("CheckUndefinedSymbols", "Expected a type-name"));
|
2009-10-05 18:02:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2009-11-11 09:32:05 +01:00
|
|
|
|
2010-05-21 15:44:35 +02:00
|
|
|
void CheckUndefinedSymbols::checkNamespace(NameAST *name)
|
2009-11-11 09:32:05 +01:00
|
|
|
{
|
2010-05-21 15:44:35 +02:00
|
|
|
if (! name)
|
|
|
|
return;
|
2010-02-06 14:32:25 +01:00
|
|
|
|
2010-05-21 15:44:35 +02:00
|
|
|
unsigned line, column;
|
|
|
|
getTokenStartPosition(name->firstToken(), &line, &column);
|
2010-02-06 14:32:25 +01:00
|
|
|
|
2010-05-21 15:44:35 +02:00
|
|
|
Scope *enclosingScope = _context.thisDocument()->scopeAt(line, column);
|
|
|
|
if (ClassOrNamespace *b = _context.lookupType(name->name, enclosingScope)) {
|
|
|
|
foreach (Symbol *s, b->symbols()) {
|
|
|
|
if (s->isNamespace())
|
|
|
|
return;
|
2010-02-06 14:32:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-21 15:44:35 +02:00
|
|
|
const unsigned length = tokenAt(name->lastToken() - 1).end() - tokenAt(name->firstToken()).begin();
|
|
|
|
warning(line, column, QCoreApplication::translate("CheckUndefinedSymbols", "Expected a namespace-name"), length);
|
2010-02-06 14:32:25 +01:00
|
|
|
}
|