forked from qt-creator/qt-creator
Cache the macros.
This commit is contained in:
@@ -30,39 +30,40 @@
|
|||||||
#include "FastPreprocessor.h"
|
#include "FastPreprocessor.h"
|
||||||
#include <Literals.h>
|
#include <Literals.h>
|
||||||
#include <TranslationUnit.h>
|
#include <TranslationUnit.h>
|
||||||
|
#include <QtCore/QDebug>
|
||||||
|
|
||||||
using namespace CPlusPlus;
|
using namespace CPlusPlus;
|
||||||
|
|
||||||
FastMacroResolver::FastMacroResolver(const Snapshot &snapshot)
|
TranslationUnit *_previousUnit;
|
||||||
: _snapshot(snapshot)
|
FastMacroResolver::FastMacroResolver(TranslationUnit *unit, const Snapshot &snapshot)
|
||||||
{ }
|
: _unit(unit), _snapshot(snapshot)
|
||||||
|
{
|
||||||
|
const QString fileName = QString::fromUtf8(unit->fileName(), unit->fileNameLength());
|
||||||
|
|
||||||
|
QSet<QString> processed;
|
||||||
|
updateCache(fileName, &processed);
|
||||||
|
}
|
||||||
|
|
||||||
bool FastMacroResolver::isMacro(TranslationUnit *unit, unsigned tokenIndex) const
|
bool FastMacroResolver::isMacro(TranslationUnit *unit, unsigned tokenIndex) const
|
||||||
{
|
{
|
||||||
|
if (unit != _unit){
|
||||||
|
qWarning() << Q_FUNC_INFO << "unexpected translation unit:" << unit->fileName();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
const Token &tk = unit->tokenAt(tokenIndex);
|
const Token &tk = unit->tokenAt(tokenIndex);
|
||||||
if (tk.isNot(T_IDENTIFIER))
|
if (tk.isNot(T_IDENTIFIER))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
Identifier *id = tk.identifier;
|
Identifier *id = tk.identifier;
|
||||||
const QByteArray macroName = QByteArray::fromRawData(id->chars(), id->size());
|
const QByteArray macroName = QByteArray::fromRawData(id->chars(), id->size());
|
||||||
const QString fileName = QString::fromUtf8(unit->fileName(), unit->fileNameLength());
|
return _cachedMacros.contains(macroName);
|
||||||
|
|
||||||
bool done = false;
|
|
||||||
QSet<QString> processed;
|
|
||||||
|
|
||||||
if (isMacro_helper(macroName, fileName, &processed, &done))
|
|
||||||
return true;
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool FastMacroResolver::isMacro_helper(const QByteArray ¯oName,
|
void FastMacroResolver::updateCache(const QString &fileName, QSet<QString> *processed)
|
||||||
const QString &fileName,
|
|
||||||
QSet<QString> *processed,
|
|
||||||
bool *done) const
|
|
||||||
{
|
{
|
||||||
if (processed->contains(fileName))
|
if (processed->contains(fileName))
|
||||||
return false;
|
return;
|
||||||
|
|
||||||
processed->insert(fileName);
|
processed->insert(fileName);
|
||||||
|
|
||||||
@@ -72,25 +73,15 @@ bool FastMacroResolver::isMacro_helper(const QByteArray ¯oName,
|
|||||||
for (int i = definedMacros.size() - 1; i != -1; --i) {
|
for (int i = definedMacros.size() - 1; i != -1; --i) {
|
||||||
const Macro ¯o = definedMacros.at(i);
|
const Macro ¯o = definedMacros.at(i);
|
||||||
|
|
||||||
if (macro.name() == macroName) { // ### handle line numbers.
|
if (macro.isHidden())
|
||||||
if (macro.isHidden()) {
|
_cachedMacros.remove(macro.name());
|
||||||
*done = true;
|
else
|
||||||
return false;
|
_cachedMacros.insert(macro.name());
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
foreach (const Document::Include &incl, doc->includes())
|
||||||
|
updateCache(incl.fileName(), processed);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
foreach (const Document::Include &incl, doc->includes()) {
|
|
||||||
if (isMacro_helper(macroName, incl.fileName(), processed, done))
|
|
||||||
return true;
|
|
||||||
else if (*done)
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
FastPreprocessor::FastPreprocessor(const Snapshot &snapshot)
|
FastPreprocessor::FastPreprocessor(const Snapshot &snapshot)
|
||||||
|
|||||||
@@ -44,18 +44,17 @@ namespace CPlusPlus {
|
|||||||
class CPLUSPLUS_EXPORT FastMacroResolver: public MacroResolver
|
class CPLUSPLUS_EXPORT FastMacroResolver: public MacroResolver
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
FastMacroResolver(const Snapshot &snapshot);
|
FastMacroResolver(TranslationUnit *unit, const Snapshot &snapshot);
|
||||||
|
|
||||||
virtual bool isMacro(TranslationUnit *unit, unsigned tokenIndex) const;
|
virtual bool isMacro(TranslationUnit *unit, unsigned tokenIndex) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool isMacro_helper(const QByteArray ¯oName,
|
void updateCache(const QString &fileName, QSet<QString> *processed);
|
||||||
const QString &fileName,
|
|
||||||
QSet<QString> *processed,
|
|
||||||
bool *done) const;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
TranslationUnit *_unit;
|
||||||
Snapshot _snapshot;
|
Snapshot _snapshot;
|
||||||
|
QSet<QByteArray> _cachedMacros;
|
||||||
};
|
};
|
||||||
|
|
||||||
class CPLUSPLUS_EXPORT FastPreprocessor: public Client
|
class CPLUSPLUS_EXPORT FastPreprocessor: public Client
|
||||||
|
|||||||
@@ -2069,7 +2069,7 @@ SemanticInfo SemanticHighlighter::semanticInfo(const Source &source)
|
|||||||
snapshot = source.snapshot;
|
snapshot = source.snapshot;
|
||||||
doc = source.snapshot.documentFromSource(preprocessedCode, source.fileName);
|
doc = source.snapshot.documentFromSource(preprocessedCode, source.fileName);
|
||||||
|
|
||||||
FastMacroResolver fastMacroResolver(snapshot);
|
FastMacroResolver fastMacroResolver(doc->translationUnit(), snapshot);
|
||||||
doc->control()->setMacroResolver(&fastMacroResolver);
|
doc->control()->setMacroResolver(&fastMacroResolver);
|
||||||
doc->check();
|
doc->check();
|
||||||
doc->control()->setMacroResolver(0);
|
doc->control()->setMacroResolver(0);
|
||||||
|
|||||||
@@ -171,7 +171,8 @@ protected:
|
|||||||
unsigned line, column;
|
unsigned line, column;
|
||||||
getTokenStartPosition(ast->firstToken(), &line, &column);
|
getTokenStartPosition(ast->firstToken(), &line, &column);
|
||||||
Symbol *lastVisibleSymbol = _doc->findSymbolAt(line, column);
|
Symbol *lastVisibleSymbol = _doc->findSymbolAt(line, column);
|
||||||
return LookupContext(lastVisibleSymbol, _exprDoc, _doc, _snapshot);
|
LookupContext ctx(lastVisibleSymbol, _exprDoc, _doc, _snapshot);
|
||||||
|
return ctx;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ensureNameIsValid(NameAST *ast)
|
void ensureNameIsValid(NameAST *ast)
|
||||||
@@ -232,7 +233,7 @@ protected:
|
|||||||
const unsigned end = tokenAt(endToken).end();
|
const unsigned end = tokenAt(endToken).end();
|
||||||
|
|
||||||
const QString expression = _source.mid(begin, end - begin);
|
const QString expression = _source.mid(begin, end - begin);
|
||||||
// qDebug() << "*** expression:" << expression;
|
// qDebug() << "*** check expression:" << expression;
|
||||||
|
|
||||||
TypeOfExpression typeofExpression;
|
TypeOfExpression typeofExpression;
|
||||||
typeofExpression.setSnapshot(_snapshot);
|
typeofExpression.setSnapshot(_snapshot);
|
||||||
@@ -426,16 +427,12 @@ static void find_helper(QFutureInterface<Core::Utils::FileSearchResult> &future,
|
|||||||
|
|
||||||
future.setProgressRange(0, files.size());
|
future.setProgressRange(0, files.size());
|
||||||
|
|
||||||
FastMacroResolver fastMacroResolver(snapshot);
|
|
||||||
|
|
||||||
for (int i = 0; i < files.size(); ++i) {
|
for (int i = 0; i < files.size(); ++i) {
|
||||||
const QString &fileName = files.at(i);
|
const QString &fileName = files.at(i);
|
||||||
future.setProgressValueAndText(i, QFileInfo(fileName).fileName());
|
future.setProgressValueAndText(i, QFileInfo(fileName).fileName());
|
||||||
|
|
||||||
Document::Ptr previousDoc = snapshot.value(fileName);
|
if (Document::Ptr previousDoc = snapshot.value(fileName)) {
|
||||||
if (previousDoc) {
|
|
||||||
Control *control = previousDoc->control();
|
Control *control = previousDoc->control();
|
||||||
previousDoc->control();
|
|
||||||
Identifier *id = control->findIdentifier(symbolId->chars(), symbolId->size());
|
Identifier *id = control->findIdentifier(symbolId->chars(), symbolId->size());
|
||||||
if (! id)
|
if (! id)
|
||||||
continue; // skip this document, it's not using symbolId.
|
continue; // skip this document, it's not using symbolId.
|
||||||
@@ -462,6 +459,7 @@ static void find_helper(QFutureInterface<Core::Utils::FileSearchResult> &future,
|
|||||||
TranslationUnit *unit = doc->translationUnit();
|
TranslationUnit *unit = doc->translationUnit();
|
||||||
Control *control = doc->control();
|
Control *control = doc->control();
|
||||||
|
|
||||||
|
FastMacroResolver fastMacroResolver(unit, snapshot);
|
||||||
control->setMacroResolver(&fastMacroResolver);
|
control->setMacroResolver(&fastMacroResolver);
|
||||||
doc->parse();
|
doc->parse();
|
||||||
control->setMacroResolver(0);
|
control->setMacroResolver(0);
|
||||||
|
|||||||
Reference in New Issue
Block a user