2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2009-06-03 15:51:37 +02:00
|
|
|
**
|
2013-01-28 17:12:19 +01:00
|
|
|
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
2012-10-02 09:12:39 +02:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2009-06-03 15:51:37 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2009-06-03 15:51:37 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
2009-06-03 15:51:37 +02:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2009-06-03 15:51:37 +02:00
|
|
|
|
|
|
|
|
#include "FastPreprocessor.h"
|
2013-03-27 18:54:03 +01:00
|
|
|
|
|
|
|
|
#include <cplusplus/Literals.h>
|
|
|
|
|
#include <cplusplus/TranslationUnit.h>
|
|
|
|
|
|
2012-12-19 18:14:28 +01:00
|
|
|
#include <QDir>
|
2009-06-03 15:51:37 +02:00
|
|
|
|
|
|
|
|
using namespace CPlusPlus;
|
|
|
|
|
|
|
|
|
|
FastPreprocessor::FastPreprocessor(const Snapshot &snapshot)
|
2012-11-28 09:52:19 +01:00
|
|
|
: _snapshot(snapshot)
|
|
|
|
|
, _preproc(this, &_env)
|
2009-06-03 15:51:37 +02:00
|
|
|
{ }
|
|
|
|
|
|
2013-08-19 15:47:51 +02:00
|
|
|
QByteArray FastPreprocessor::run(Document::Ptr newDoc, const QByteArray &source)
|
2009-06-03 15:51:37 +02:00
|
|
|
{
|
2012-10-11 16:16:01 +02:00
|
|
|
std::swap(newDoc, _currentDoc);
|
|
|
|
|
const QString fileName = _currentDoc->fileName();
|
2012-08-06 15:28:00 +02:00
|
|
|
_preproc.setExpandFunctionlikeMacros(false);
|
2011-08-23 14:05:47 +02:00
|
|
|
_preproc.setKeepComments(true);
|
2009-07-04 20:54:31 +02:00
|
|
|
|
2009-12-07 10:54:27 +01:00
|
|
|
if (Document::Ptr doc = _snapshot.document(fileName)) {
|
2009-06-29 09:59:55 +02:00
|
|
|
_merged.insert(fileName);
|
|
|
|
|
|
2012-11-28 09:52:19 +01:00
|
|
|
mergeEnvironment(Preprocessor::configurationFileName);
|
2013-07-25 11:21:31 +02:00
|
|
|
foreach (const Document::Include &i, doc->resolvedIncludes())
|
2013-06-06 09:35:40 +02:00
|
|
|
mergeEnvironment(i.resolvedFileName());
|
2009-06-29 09:59:55 +02:00
|
|
|
}
|
|
|
|
|
|
2012-04-19 10:35:49 +02:00
|
|
|
const QByteArray preprocessed = _preproc.run(fileName, source);
|
2012-03-30 13:01:32 +02:00
|
|
|
// qDebug("FastPreprocessor::run for %s produced [[%s]]", fileName.toUtf8().constData(), preprocessed.constData());
|
2012-10-11 16:16:01 +02:00
|
|
|
std::swap(newDoc, _currentDoc);
|
2009-06-03 15:51:37 +02:00
|
|
|
return preprocessed;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-06 09:35:40 +02:00
|
|
|
void FastPreprocessor::sourceNeeded(unsigned line, const QString &fileName,
|
|
|
|
|
IncludeType mode)
|
2012-10-11 16:16:01 +02:00
|
|
|
{
|
|
|
|
|
Q_ASSERT(_currentDoc);
|
2012-12-19 18:14:28 +01:00
|
|
|
// CHECKME: Is that cleanName needed?
|
|
|
|
|
QString cleanName = QDir::cleanPath(fileName);
|
2013-06-06 09:35:40 +02:00
|
|
|
_currentDoc->addIncludeFile(Document::Include(fileName, cleanName, line, mode));
|
2012-10-11 16:16:01 +02:00
|
|
|
mergeEnvironment(fileName);
|
|
|
|
|
}
|
2009-06-29 09:59:55 +02:00
|
|
|
|
2009-06-03 15:51:37 +02:00
|
|
|
void FastPreprocessor::mergeEnvironment(const QString &fileName)
|
|
|
|
|
{
|
|
|
|
|
if (! _merged.contains(fileName)) {
|
|
|
|
|
_merged.insert(fileName);
|
|
|
|
|
|
2009-12-07 10:54:27 +01:00
|
|
|
if (Document::Ptr doc = _snapshot.document(fileName)) {
|
2013-07-25 11:21:31 +02:00
|
|
|
foreach (const Document::Include &i, doc->resolvedIncludes())
|
2013-06-06 09:35:40 +02:00
|
|
|
mergeEnvironment(i.resolvedFileName());
|
2009-06-03 15:51:37 +02:00
|
|
|
|
|
|
|
|
_env.addMacros(doc->definedMacros());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-10-11 16:16:01 +02:00
|
|
|
|
|
|
|
|
void FastPreprocessor::macroAdded(const Macro ¯o)
|
|
|
|
|
{
|
|
|
|
|
Q_ASSERT(_currentDoc);
|
|
|
|
|
|
|
|
|
|
_currentDoc->appendMacro(macro);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const Macro revision(const Snapshot &s, const Macro &m)
|
|
|
|
|
{
|
|
|
|
|
if (Document::Ptr d = s.document(m.fileName())) {
|
|
|
|
|
Macro newMacro(m);
|
|
|
|
|
newMacro.setFileRevision(d->revision());
|
|
|
|
|
return newMacro;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return m;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FastPreprocessor::passedMacroDefinitionCheck(unsigned offset, unsigned line, const Macro ¯o)
|
|
|
|
|
{
|
|
|
|
|
Q_ASSERT(_currentDoc);
|
|
|
|
|
|
|
|
|
|
_currentDoc->addMacroUse(revision(_snapshot, macro),
|
|
|
|
|
offset, macro.name().length(), line,
|
|
|
|
|
QVector<MacroArgumentReference>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FastPreprocessor::failedMacroDefinitionCheck(unsigned offset, const ByteArrayRef &name)
|
|
|
|
|
{
|
|
|
|
|
Q_ASSERT(_currentDoc);
|
|
|
|
|
|
|
|
|
|
_currentDoc->addUndefinedMacroUse(QByteArray(name.start(), name.size()), offset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FastPreprocessor::notifyMacroReference(unsigned offset, unsigned line, const Macro ¯o)
|
|
|
|
|
{
|
|
|
|
|
Q_ASSERT(_currentDoc);
|
|
|
|
|
|
|
|
|
|
_currentDoc->addMacroUse(revision(_snapshot, macro),
|
|
|
|
|
offset, macro.name().length(), line,
|
|
|
|
|
QVector<MacroArgumentReference>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FastPreprocessor::startExpandingMacro(unsigned offset, unsigned line,
|
|
|
|
|
const Macro ¯o,
|
|
|
|
|
const QVector<MacroArgumentReference> &actuals)
|
|
|
|
|
{
|
|
|
|
|
Q_ASSERT(_currentDoc);
|
|
|
|
|
|
|
|
|
|
_currentDoc->addMacroUse(revision(_snapshot, macro),
|
|
|
|
|
offset, macro.name().length(), line, actuals);
|
|
|
|
|
}
|