Implemented a simple(fast?) strategy to resolve macro references.

This commit is contained in:
Roberto Raggi
2009-09-30 17:15:31 +02:00
parent e50d60ac97
commit 9a21143384
5 changed files with 104 additions and 4 deletions

View File

@@ -28,9 +28,71 @@
**************************************************************************/
#include "FastPreprocessor.h"
#include <Literals.h>
#include <TranslationUnit.h>
using namespace CPlusPlus;
FastMacroResolver::FastMacroResolver(const Snapshot &snapshot)
: _snapshot(snapshot)
{ }
bool FastMacroResolver::isMacro(TranslationUnit *unit, unsigned tokenIndex) const
{
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;
}
bool FastMacroResolver::isMacro_helper(const QByteArray &macroName,
const QString &fileName,
QSet<QString> *processed,
bool *done) const
{
if (processed->contains(fileName))
return false;
processed->insert(fileName);
if (Document::Ptr doc = _snapshot.value(fileName)) {
const QList<Macro> definedMacros = doc->definedMacros();
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;
}
}
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)
: _snapshot(snapshot),
_preproc(this, &_env)

View File

@@ -34,11 +34,30 @@
#include "CppDocument.h"
#include "pp.h"
#include <Control.h>
#include <QtCore/QSet>
#include <QtCore/QString>
namespace CPlusPlus {
class CPLUSPLUS_EXPORT FastMacroResolver: public MacroResolver
{
public:
FastMacroResolver(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;
private:
Snapshot _snapshot;
};
class CPLUSPLUS_EXPORT FastPreprocessor: public Client
{
Environment _env;