Cache the macros.

This commit is contained in:
Roberto Raggi
2009-09-30 18:52:19 +02:00
parent 06bba1dc78
commit ae43149c97
4 changed files with 34 additions and 46 deletions

View File

@@ -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 &macroName, 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 &macroName,
for (int i = definedMacros.size() - 1; i != -1; --i) { for (int i = definedMacros.size() - 1; i != -1; --i) {
const Macro &macro = definedMacros.at(i); const Macro &macro = 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()) { foreach (const Document::Include &incl, doc->includes())
if (isMacro_helper(macroName, incl.fileName(), processed, done)) updateCache(incl.fileName(), processed);
return true;
else if (*done)
return false;
}
} }
return false;
} }
FastPreprocessor::FastPreprocessor(const Snapshot &snapshot) FastPreprocessor::FastPreprocessor(const Snapshot &snapshot)

View File

@@ -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 &macroName, 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

View File

@@ -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);

View File

@@ -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);