2008-12-02 12:01:29 +01:00
|
|
|
/***************************************************************************
|
|
|
|
**
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
**
|
|
|
|
** Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
|
|
|
|
**
|
|
|
|
** Contact: Qt Software Information (qt-info@nokia.com)
|
|
|
|
**
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
|
|
|
** Non-Open Source Usage
|
|
|
|
**
|
2008-12-02 12:01:29 +01:00
|
|
|
** Licensees may use this file in accordance with the Qt Beta Version
|
|
|
|
** License Agreement, Agreement version 2.2 provided with the Software or,
|
|
|
|
** alternatively, in accordance with the terms contained in a written
|
2008-12-02 14:17:16 +01:00
|
|
|
** agreement between you and Nokia.
|
|
|
|
**
|
|
|
|
** GNU General Public License Usage
|
|
|
|
**
|
2008-12-02 12:01:29 +01:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU General
|
|
|
|
** Public License versions 2.0 or 3.0 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.GPL included in the packaging
|
|
|
|
** of this file. Please review the following information to ensure GNU
|
|
|
|
** General Public Licensing requirements will be met:
|
|
|
|
**
|
|
|
|
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
|
|
|
|
** http://www.gnu.org/copyleft/gpl.html.
|
|
|
|
**
|
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2008-12-02 14:17:16 +01:00
|
|
|
** rights. These rights are described in the Nokia Qt GPL Exception
|
|
|
|
** version 1.2, included in the file GPL_EXCEPTION.txt in this package.
|
|
|
|
**
|
|
|
|
***************************************************************************/
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
#include "CppDocument.h"
|
|
|
|
#include <Control.h>
|
|
|
|
#include <TranslationUnit.h>
|
|
|
|
#include <DiagnosticClient.h>
|
|
|
|
#include <Semantic.h>
|
|
|
|
#include <Literals.h>
|
|
|
|
#include <Symbols.h>
|
|
|
|
#include <AST.h>
|
|
|
|
#include <Scope.h>
|
2008-12-02 14:09:21 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include <QByteArray>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QtDebug>
|
|
|
|
|
|
|
|
using namespace CPlusPlus;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2008-12-02 14:09:21 +01:00
|
|
|
class DocumentDiagnosticClient : public DiagnosticClient
|
|
|
|
{
|
|
|
|
enum { MAX_MESSAGE_COUNT = 10 };
|
|
|
|
|
|
|
|
public:
|
|
|
|
DocumentDiagnosticClient(Document *doc, QList<Document::DiagnosticMessage> *messages)
|
|
|
|
: doc(doc),
|
|
|
|
messages(messages)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
virtual void report(int level,
|
|
|
|
StringLiteral *fileId,
|
|
|
|
unsigned line, unsigned column,
|
|
|
|
const char *format, va_list ap)
|
|
|
|
{
|
|
|
|
if (messages->count() == MAX_MESSAGE_COUNT)
|
|
|
|
return;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2008-12-02 14:09:21 +01:00
|
|
|
const QString fileName = QString::fromUtf8(fileId->chars(), fileId->size());
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2008-12-02 14:09:21 +01:00
|
|
|
if (fileName != doc->fileName())
|
|
|
|
return;
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2008-12-02 14:09:21 +01:00
|
|
|
QString message;
|
|
|
|
message.vsprintf(format, ap);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2008-12-02 14:09:21 +01:00
|
|
|
Document::DiagnosticMessage m(convertLevel(level), doc->fileName(),
|
|
|
|
line, column, message);
|
|
|
|
messages->append(m);
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2008-12-02 14:09:21 +01:00
|
|
|
static int convertLevel(int level) {
|
|
|
|
switch (level) {
|
|
|
|
case Warning: return Document::DiagnosticMessage::Warning;
|
|
|
|
case Error: return Document::DiagnosticMessage::Error;
|
|
|
|
case Fatal: return Document::DiagnosticMessage::Fatal;
|
|
|
|
default: return Document::DiagnosticMessage::Error;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2008-12-02 14:09:21 +01:00
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2008-12-02 14:09:21 +01:00
|
|
|
Document *doc;
|
|
|
|
QList<Document::DiagnosticMessage> *messages;
|
|
|
|
};
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2008-12-02 14:09:21 +01:00
|
|
|
} // anonymous namespace
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
Document::Document(const QString &fileName)
|
|
|
|
: _fileName(fileName),
|
|
|
|
_globalNamespace(0)
|
|
|
|
{
|
|
|
|
_control = new Control();
|
|
|
|
|
|
|
|
_control->setDiagnosticClient(new DocumentDiagnosticClient(this, &_diagnosticMessages));
|
|
|
|
|
|
|
|
const QByteArray localFileName = fileName.toUtf8();
|
|
|
|
StringLiteral *fileId = _control->findOrInsertFileName(localFileName.constData(),
|
|
|
|
localFileName.size());
|
|
|
|
_translationUnit = new TranslationUnit(_control, fileId);
|
|
|
|
_translationUnit->setQtMocRunEnabled(true);
|
|
|
|
(void) _control->switchTranslationUnit(_translationUnit);
|
|
|
|
}
|
|
|
|
|
|
|
|
Document::~Document()
|
|
|
|
{
|
|
|
|
delete _translationUnit;
|
|
|
|
delete _control->diagnosticClient();
|
|
|
|
delete _control;
|
|
|
|
}
|
|
|
|
|
|
|
|
Control *Document::control() const
|
2008-12-02 14:09:21 +01:00
|
|
|
{
|
|
|
|
return _control;
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
QString Document::fileName() const
|
2008-12-02 14:09:21 +01:00
|
|
|
{
|
|
|
|
return _fileName;
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
QStringList Document::includedFiles() const
|
2008-12-02 14:09:21 +01:00
|
|
|
{
|
|
|
|
return _includedFiles;
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
void Document::addIncludeFile(const QString &fileName)
|
2008-12-02 14:09:21 +01:00
|
|
|
{
|
|
|
|
_includedFiles.append(fileName);
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2008-12-08 12:59:33 +01:00
|
|
|
void Document::appendMacro(const Macro ¯o)
|
2008-12-02 14:09:21 +01:00
|
|
|
{
|
2008-12-08 12:59:33 +01:00
|
|
|
_definedMacros.append(macro);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
2008-12-04 12:05:04 +01:00
|
|
|
void Document::addMacroUse(unsigned offset, unsigned length)
|
|
|
|
{
|
|
|
|
_macroUses.append(Block(offset, offset + length));
|
|
|
|
}
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
TranslationUnit *Document::translationUnit() const
|
2008-12-02 14:09:21 +01:00
|
|
|
{
|
|
|
|
return _translationUnit;
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
bool Document::skipFunctionBody() const
|
2008-12-02 14:09:21 +01:00
|
|
|
{
|
|
|
|
return _translationUnit->skipFunctionBody();
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
void Document::setSkipFunctionBody(bool skipFunctionBody)
|
2008-12-02 14:09:21 +01:00
|
|
|
{
|
|
|
|
_translationUnit->setSkipFunctionBody(skipFunctionBody);
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
unsigned Document::globalSymbolCount() const
|
|
|
|
{
|
|
|
|
if (! _globalNamespace)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return _globalNamespace->memberCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
Symbol *Document::globalSymbolAt(unsigned index) const
|
2008-12-02 14:09:21 +01:00
|
|
|
{
|
|
|
|
return _globalNamespace->memberAt(index);
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
Scope *Document::globalSymbols() const
|
|
|
|
{
|
|
|
|
if (! _globalNamespace)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return _globalNamespace->members();
|
|
|
|
}
|
|
|
|
|
|
|
|
Namespace *Document::globalNamespace() const
|
2008-12-02 14:09:21 +01:00
|
|
|
{
|
|
|
|
return _globalNamespace;
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
Symbol *Document::findSymbolAt(unsigned line, unsigned column) const
|
2008-12-02 14:09:21 +01:00
|
|
|
{
|
|
|
|
return findSymbolAt(line, column, globalSymbols());
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
Symbol *Document::findSymbolAt(unsigned line, unsigned column, Scope *scope) const
|
|
|
|
{
|
|
|
|
Symbol *previousSymbol = 0;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < scope->symbolCount(); ++i) {
|
|
|
|
Symbol *symbol = scope->symbolAt(i);
|
|
|
|
if (symbol->line() > line)
|
|
|
|
break;
|
|
|
|
|
|
|
|
previousSymbol = symbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (previousSymbol) {
|
|
|
|
if (ScopedSymbol *scoped = previousSymbol->asScopedSymbol()) {
|
|
|
|
if (Symbol *member = findSymbolAt(line, column, scoped->members()))
|
|
|
|
return member;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return previousSymbol;
|
|
|
|
}
|
|
|
|
|
|
|
|
Document::Ptr Document::create(const QString &fileName)
|
|
|
|
{
|
|
|
|
Document::Ptr doc(new Document(fileName));
|
|
|
|
return doc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Document::setSource(const QByteArray &source)
|
2008-12-02 14:09:21 +01:00
|
|
|
{
|
|
|
|
_translationUnit->setSource(source.constBegin(), source.size());
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
void Document::startSkippingBlocks(unsigned start)
|
2008-12-02 14:09:21 +01:00
|
|
|
{
|
|
|
|
_skippedBlocks.append(Block(start, 0));
|
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
void Document::stopSkippingBlocks(unsigned stop)
|
|
|
|
{
|
|
|
|
unsigned start = _skippedBlocks.back().begin();
|
|
|
|
if (start > stop)
|
|
|
|
_skippedBlocks.removeLast(); // Ignore this block, it's invalid.
|
|
|
|
else
|
|
|
|
_skippedBlocks.back() = Block(start, stop);
|
|
|
|
}
|
|
|
|
|
2008-12-03 14:01:19 +01:00
|
|
|
bool Document::parse(ParseMode mode)
|
2008-12-02 14:09:21 +01:00
|
|
|
{
|
2008-12-03 14:01:19 +01:00
|
|
|
TranslationUnit::ParseMode m = TranslationUnit::ParseTranlationUnit;
|
|
|
|
switch (mode) {
|
|
|
|
case ParseTranlationUnit:
|
|
|
|
m = TranslationUnit::ParseTranlationUnit;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ParseDeclaration:
|
|
|
|
m = TranslationUnit::ParseDeclaration;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ParseExpression:
|
|
|
|
m = TranslationUnit::ParseExpression;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ParseStatement:
|
|
|
|
m = TranslationUnit::ParseStatement;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _translationUnit->parse(m);
|
2008-12-02 14:09:21 +01:00
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
void Document::check()
|
|
|
|
{
|
|
|
|
Q_ASSERT(! _globalNamespace);
|
|
|
|
|
|
|
|
Semantic semantic(_control);
|
|
|
|
|
|
|
|
_globalNamespace = _control->newNamespace(0);
|
|
|
|
Scope *globals = _globalNamespace->members();
|
2008-12-03 14:01:19 +01:00
|
|
|
if (! _translationUnit->ast())
|
|
|
|
return; // nothing to do.
|
|
|
|
|
|
|
|
if (TranslationUnitAST *ast = _translationUnit->ast()->asTranslationUnit()) {
|
2008-12-02 12:01:29 +01:00
|
|
|
for (DeclarationAST *decl = ast->declarations; decl; decl = decl->next) {
|
|
|
|
semantic.check(decl, globals);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Document::releaseTranslationUnit()
|
2008-12-02 14:09:21 +01:00
|
|
|
{
|
|
|
|
_translationUnit->release();
|
|
|
|
}
|