2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2009-06-03 15:51:37 +02:00
|
|
|
**
|
2016-01-15 14:58:39 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
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
|
2016-01-15 14:58:39 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2009-06-03 15:51:37 +02:00
|
|
|
**
|
2016-01-15 14:58:39 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2010-12-17 16:01:08 +01:00
|
|
|
**
|
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)
|
2015-01-13 14:12:17 +01:00
|
|
|
, _addIncludesToCurrentDoc(false)
|
2009-06-03 15:51:37 +02:00
|
|
|
{ }
|
|
|
|
|
|
2016-03-04 12:43:06 +01:00
|
|
|
QByteArray FastPreprocessor::run(Document::Ptr newDoc,
|
|
|
|
|
const QByteArray &source,
|
|
|
|
|
bool mergeDefinedMacrosOfDocument)
|
2009-06-03 15:51:37 +02:00
|
|
|
{
|
2012-10-11 16:16:01 +02:00
|
|
|
std::swap(newDoc, _currentDoc);
|
2015-01-13 14:12:17 +01:00
|
|
|
_addIncludesToCurrentDoc = _currentDoc->resolvedIncludes().isEmpty()
|
|
|
|
|
&& _currentDoc->unresolvedIncludes().isEmpty();
|
2012-10-11 16:16:01 +02:00
|
|
|
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);
|
|
|
|
|
|
2013-10-01 14:48:31 +02:00
|
|
|
for (Snapshot::const_iterator i = _snapshot.begin(), ei = _snapshot.end(); i != ei; ++i) {
|
2014-11-27 12:11:46 +01:00
|
|
|
if (isInjectedFile(i.key().toString()))
|
|
|
|
|
mergeEnvironment(i.key().toString());
|
2013-10-01 14:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
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());
|
2016-03-04 12:43:06 +01:00
|
|
|
|
|
|
|
|
if (mergeDefinedMacrosOfDocument)
|
|
|
|
|
_env.addMacros(_currentDoc->definedMacros());
|
2009-06-29 09:59:55 +02:00
|
|
|
}
|
|
|
|
|
|
2013-12-12 21:36:01 +01: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;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-16 11:25:15 +02:00
|
|
|
void FastPreprocessor::sourceNeeded(unsigned line, const QString &fileName, IncludeType mode,
|
|
|
|
|
const QStringList &initialIncludes)
|
2012-10-11 16:16:01 +02:00
|
|
|
{
|
2014-07-16 11:25:15 +02:00
|
|
|
Q_UNUSED(initialIncludes)
|
2012-10-11 16:16:01 +02:00
|
|
|
Q_ASSERT(_currentDoc);
|
2015-01-13 14:12:17 +01:00
|
|
|
if (_addIncludesToCurrentDoc) {
|
|
|
|
|
// CHECKME: Is that cleanName needed?
|
|
|
|
|
const QString cleanName = QDir::cleanPath(fileName);
|
|
|
|
|
_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;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-09 10:04:13 -04:00
|
|
|
void FastPreprocessor::passedMacroDefinitionCheck(unsigned bytesOffset, unsigned utf16charsOffset,
|
|
|
|
|
unsigned line, const Macro ¯o)
|
2012-10-11 16:16:01 +02:00
|
|
|
{
|
|
|
|
|
Q_ASSERT(_currentDoc);
|
|
|
|
|
|
|
|
|
|
_currentDoc->addMacroUse(revision(_snapshot, macro),
|
2014-05-09 10:04:13 -04:00
|
|
|
bytesOffset, macro.name().size(),
|
|
|
|
|
utf16charsOffset, macro.nameToQString().size(),
|
|
|
|
|
line, QVector<MacroArgumentReference>());
|
2012-10-11 16:16:01 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-09 10:04:13 -04:00
|
|
|
void FastPreprocessor::failedMacroDefinitionCheck(unsigned bytesOffset, unsigned utf16charsOffset,
|
|
|
|
|
const ByteArrayRef &name)
|
2012-10-11 16:16:01 +02:00
|
|
|
{
|
|
|
|
|
Q_ASSERT(_currentDoc);
|
|
|
|
|
|
2014-05-09 10:04:13 -04:00
|
|
|
_currentDoc->addUndefinedMacroUse(QByteArray(name.start(), name.size()),
|
|
|
|
|
bytesOffset, utf16charsOffset);
|
2012-10-11 16:16:01 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-09 10:04:13 -04:00
|
|
|
void FastPreprocessor::notifyMacroReference(unsigned bytesOffset, unsigned utf16charsOffset,
|
|
|
|
|
unsigned line, const Macro ¯o)
|
2012-10-11 16:16:01 +02:00
|
|
|
{
|
|
|
|
|
Q_ASSERT(_currentDoc);
|
|
|
|
|
|
|
|
|
|
_currentDoc->addMacroUse(revision(_snapshot, macro),
|
2014-05-09 10:04:13 -04:00
|
|
|
bytesOffset, macro.name().size(),
|
|
|
|
|
utf16charsOffset, macro.nameToQString().size(),
|
|
|
|
|
line, QVector<MacroArgumentReference>());
|
2012-10-11 16:16:01 +02:00
|
|
|
}
|
|
|
|
|
|
2014-05-09 10:04:13 -04:00
|
|
|
void FastPreprocessor::startExpandingMacro(unsigned bytesOffset, unsigned utf16charsOffset,
|
|
|
|
|
unsigned line, const Macro ¯o,
|
|
|
|
|
const QVector<MacroArgumentReference> &actuals)
|
2012-10-11 16:16:01 +02:00
|
|
|
{
|
|
|
|
|
Q_ASSERT(_currentDoc);
|
|
|
|
|
|
|
|
|
|
_currentDoc->addMacroUse(revision(_snapshot, macro),
|
2014-05-09 10:04:13 -04:00
|
|
|
bytesOffset, macro.name().size(),
|
|
|
|
|
utf16charsOffset, macro.nameToQString().size(),
|
|
|
|
|
line, actuals);
|
2012-10-11 16:16:01 +02:00
|
|
|
}
|
2014-10-07 17:20:25 +02:00
|
|
|
|
|
|
|
|
void FastPreprocessor::markAsIncludeGuard(const QByteArray ¯oName)
|
|
|
|
|
{
|
|
|
|
|
if (!_currentDoc)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
_currentDoc->setIncludeGuardMacroName(macroName);
|
|
|
|
|
}
|