Commit 310d50fd authored by Roberto Raggi's avatar Roberto Raggi

Use Macros.

parent 94236391
......@@ -138,19 +138,9 @@ void Document::addIncludeFile(const QString &fileName)
_includedFiles.append(fileName);
}
QByteArray Document::definedMacros() const
void Document::appendMacro(const Macro &macro)
{
return _definedMacros;
}
void Document::appendMacro(const QByteArray &macroName, const QByteArray &text)
{
int index = macroName.indexOf('(');
if (index == -1)
_macroNames.insert(macroName);
else
_macroNames.insert(macroName.left(index));
_definedMacros += text;
_definedMacros.append(macro);
}
void Document::addMacroUse(unsigned offset, unsigned length)
......@@ -251,11 +241,6 @@ void Document::stopSkippingBlocks(unsigned stop)
_skippedBlocks.back() = Block(start, stop);
}
QSet<QByteArray> Document::macroNames() const
{
return _macroNames;
}
bool Document::parse(ParseMode mode)
{
TranslationUnit::ParseMode m = TranslationUnit::ParseTranlationUnit;
......
......@@ -36,6 +36,8 @@
#include <CPlusPlusForwardDeclarations.h>
#include "pp-macro.h"
#include <QByteArray>
#include <QList>
#include <QSet>
......@@ -45,6 +47,8 @@
namespace CPlusPlus {
class Macro;
class CPLUSPLUS_EXPORT Document
{
Document(const Document &other);
......@@ -63,10 +67,7 @@ public:
QStringList includedFiles() const;
void addIncludeFile(const QString &fileName);
QByteArray definedMacros() const;
QSet<QByteArray> macroNames() const;
void appendMacro(const QByteArray &macroName, const QByteArray &text);
void appendMacro(const Macro &macro);
void addMacroUse(unsigned offset, unsigned length);
......@@ -81,6 +82,9 @@ public:
Scope *globalSymbols() const; // ### deprecate?
Namespace *globalNamespace() const;
QList<Macro> definedMacros() const
{ return _definedMacros; }
Symbol *findSymbolAt(unsigned line, unsigned column) const;
void setSource(const QByteArray &source);
......@@ -191,8 +195,7 @@ private:
TranslationUnit *_translationUnit;
Namespace *_globalNamespace;
QList<DiagnosticMessage> _diagnosticMessages;
QByteArray _definedMacros;
QSet<QByteArray> _macroNames;
QList<Macro> _definedMacros;
QList<Block> _skippedBlocks;
QList<Block> _macroUses;
};
......
......@@ -53,20 +53,22 @@
#ifndef PP_CCTYPE_H
#define PP_CCTYPE_H
#include <CPlusPlusForwardDeclarations.h>
#include <cctype>
namespace CPlusPlus {
inline bool pp_isalpha (int __ch)
inline bool CPLUSPLUS_EXPORT pp_isalpha (int __ch)
{ return std::isalpha ((unsigned char) __ch) != 0; }
inline bool pp_isalnum (int __ch)
inline bool CPLUSPLUS_EXPORT pp_isalnum (int __ch)
{ return std::isalnum ((unsigned char) __ch) != 0; }
inline bool pp_isdigit (int __ch)
inline bool CPLUSPLUS_EXPORT pp_isdigit (int __ch)
{ return std::isdigit ((unsigned char) __ch) != 0; }
inline bool pp_isspace (int __ch)
inline bool CPLUSPLUS_EXPORT pp_isspace (int __ch)
{ return std::isspace ((unsigned char) __ch) != 0; }
} // namespace CPlusPlus
......
......@@ -34,6 +34,8 @@
#ifndef PP_CLIENT_H
#define PP_CLIENT_H
#include <CPlusPlusForwardDeclarations.h>
#include <QByteArray>
#include <QString>
#include <QFile>
......@@ -42,7 +44,7 @@ namespace CPlusPlus {
class Macro;
class Client
class CPLUSPLUS_EXPORT Client
{
Client(const Client &other);
void operator=(const Client &other);
......@@ -60,7 +62,7 @@ public:
virtual ~Client()
{ }
virtual void macroAdded(const QByteArray &macroId, const QByteArray &text) = 0;
virtual void macroAdded(const Macro &macro) = 0;
virtual void sourceNeeded(QString &fileName, IncludeType mode) = 0; // ### FIX the signature.
virtual void startExpandingMacro(unsigned offset,
......
......@@ -906,16 +906,8 @@ void pp::processDefine(TokenIterator firstToken, TokenIterator lastToken)
env.bind(macro);
QByteArray macroText;
macroText.reserve(64);
macroText += "#define ";
macroText += macroId;
macroText += ' ';
macroText += macro.definition;
macroText += '\n';
client->macroAdded(macroId, macroText);
if (client)
client->macroAdded(macro);
}
void pp::processIf(TokenIterator firstToken, TokenIterator lastToken)
......@@ -1019,13 +1011,10 @@ void pp::processUndef(TokenIterator firstToken, TokenIterator lastToken)
if (tk->is(T_IDENTIFIER)) {
const QByteArray macroName = tokenText(*tk);
env.remove(macroName);
const Macro *macro = env.remove(macroName);
QByteArray macroText;
macroText += "#undef ";
macroText += macroName;
macroText += '\n';
client->macroAdded(macroName, macroText);
if (client && macro)
client->macroAdded(*macro);
}
}
......
......@@ -134,7 +134,7 @@ namespace CPlusPlus {
#undef PP_DEFINE_BIN_OP
};
class pp
class CPLUSPLUS_EXPORT pp
{
Client *client;
Environment &env;
......
......@@ -115,12 +115,12 @@ Macro *Environment::bind(const Macro &__macro)
return m;
}
void Environment::remove (const QByteArray &name)
Macro *Environment::remove (const QByteArray &name)
{
Macro macro;
macro.name = name;
macro.hidden = true;
bind(macro);
return bind(macro);
}
bool Environment::isBuiltinMacro(const QByteArray &s) const
......
......@@ -53,14 +53,16 @@
#ifndef PP_ENVIRONMENT_H
#define PP_ENVIRONMENT_H
#include "CPlusPlusForwardDeclarations.h"
#include <QVector>
#include <QByteArray>
namespace CPlusPlus {
struct Macro;
class Macro;
class Environment
class CPLUSPLUS_EXPORT Environment
{
public:
Environment();
......@@ -70,7 +72,7 @@ public:
Macro *macroAt(unsigned index) const;
Macro *bind(const Macro &macro);
void remove(const QByteArray &name);
Macro *remove(const QByteArray &name);
Macro *resolve(const QByteArray &name) const;
bool isBuiltinMacro(const QByteArray &name) const;
......
......@@ -53,42 +53,43 @@
#ifndef PP_MACRO_H
#define PP_MACRO_H
#include <CPlusPlusForwardDeclarations.h>
#include <QByteArray>
#include <QVector>
namespace CPlusPlus {
struct Macro
class CPLUSPLUS_EXPORT Macro
{
public:
QByteArray name;
QByteArray definition;
QVector<QByteArray> formals;
QByteArray fileName;
int line;
Macro *next;
unsigned hashcode;
union
{
QByteArray name;
QByteArray definition;
QVector<QByteArray> formals;
QByteArray fileName;
int line;
int lines;
Macro *next;
unsigned hashcode;
unsigned state;
union
struct
{
unsigned state;
struct
{
unsigned hidden: 1;
unsigned function_like: 1;
unsigned variadics: 1;
};
unsigned hidden: 1;
unsigned function_like: 1;
unsigned variadics: 1;
};
};
inline Macro():
inline Macro():
line(0),
lines(0),
next(0),
hashcode(0),
state(0)
{ }
};
{ }
};
} // namespace CPlusPlus
......
......@@ -699,7 +699,9 @@ void CppCodeCompletion::addMacros(const LookupContext &context)
continue;
processed.insert(fn);
if (Document::Ptr doc = context.document(fn)) {
macroNames += doc->macroNames();
foreach (const Macro macro, doc->definedMacros()) {
macroNames.insert(macro.name);
}
todo += doc->includedFiles();
}
}
......
......@@ -31,7 +31,7 @@
**
***************************************************************************/
#include "pp.h"
#include <cplusplus/pp.h>
#include "cppmodelmanager.h"
#include "cpphoverhandler.h"
......@@ -129,8 +129,7 @@ protected:
void mergeEnvironment(CPlusPlus::Document::Ptr doc);
void mergeEnvironment(CPlusPlus::Document::Ptr doc, QSet<QString> *processed);
virtual void macroAdded(const QByteArray &macroName,
const QByteArray &macroText);
virtual void macroAdded(const Macro &macro);
virtual void startExpandingMacro(unsigned offset,
const Macro &macro,
const QByteArray &originalText);
......@@ -295,12 +294,12 @@ QByteArray CppPreprocessor::tryIncludeFile(QString &fileName, IncludeType type)
return QByteArray();
}
void CppPreprocessor::macroAdded(const QByteArray &macroName, const QByteArray &macroText)
void CppPreprocessor::macroAdded(const Macro &macro)
{
if (! m_currentDoc)
return;
m_currentDoc->appendMacro(macroName, macroText);
m_currentDoc->appendMacro(macro);
}
void CppPreprocessor::startExpandingMacro(unsigned offset,
......@@ -340,14 +339,13 @@ void CppPreprocessor::mergeEnvironment(Document::Ptr doc, QSet<QString> *process
processed->insert(fn);
foreach (QString includedFile, doc->includedFiles())
foreach (QString includedFile, doc->includedFiles()) {
mergeEnvironment(m_documents.value(includedFile), processed);
}
const QByteArray macros = doc->definedMacros();
QByteArray localFileName = doc->fileName().toUtf8();
QByteArray dummy;
m_proc(localFileName, macros, &dummy);
foreach (const Macro macro, doc->definedMacros()) {
env.bind(macro);
}
}
void CppPreprocessor::startSkippingBlocks(unsigned offset)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment