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 <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 &macroName,
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 &macroName,
for (int i = definedMacros.size() - 1; i != -1; --i) {
const Macro &macro = 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)

View File

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