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

Use Macros.

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