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