Commit c3cc7cf4 authored by Christian Kamm's avatar Christian Kamm

Enhance data stored for macros and macro uses.

In preparation for finding macro uses.

* Macro: add offset and length
* MacroUse: add line
* Document: add convenience functions for finding a macro definition, use
  or undefined use at a given location.

Reviewed-by: Erik Verbruggen
parent 6c27faf0
......@@ -194,9 +194,10 @@ void Document::appendMacro(const Macro &macro)
}
void Document::addMacroUse(const Macro &macro, unsigned offset, unsigned length,
unsigned beginLine,
const QVector<MacroArgumentReference> &actuals, bool inCondition)
{
MacroUse use(macro, offset, offset + length);
MacroUse use(macro, offset, offset + length, beginLine);
use.setInCondition(inCondition);
foreach (const MacroArgumentReference &actual, actuals) {
......@@ -330,6 +331,33 @@ Symbol *Document::findSymbolAt(unsigned line, unsigned column, Scope *scope) con
return previousSymbol;
}
const Macro *Document::findMacroDefinitionAt(unsigned line) const
{
foreach (const Macro &macro, _definedMacros) {
if (macro.line() == line)
return &macro;
}
return 0;
}
const Document::MacroUse *Document::findMacroUseAt(unsigned offset) const
{
foreach (const Document::MacroUse &use, _macroUses) {
if (use.contains(offset))
return &use;
}
return 0;
}
const Document::UndefinedMacroUse *Document::findUndefinedMacroUseAt(unsigned offset) const
{
foreach (const Document::UndefinedMacroUse &use, _undefinedMacroUses) {
if (use.contains(offset))
return &use;
}
return 0;
}
Document::Ptr Document::create(const QString &fileName)
{
Document::Ptr doc(new Document(fileName));
......
......@@ -73,7 +73,8 @@ public:
void appendMacro(const Macro &macro);
void addMacroUse(const Macro &macro, unsigned offset, unsigned length,
const QVector<MacroArgumentReference> &range, bool inCondition);
unsigned beginLine, const QVector<MacroArgumentReference> &range,
bool inCondition);
void addUndefinedMacroUse(const QByteArray &name, unsigned offset);
Control *control() const;
......@@ -234,14 +235,15 @@ public:
Macro _macro;
QVector<Block> _arguments;
bool _inCondition;
unsigned _beginLine;
public:
inline MacroUse(const Macro &macro,
unsigned begin = 0,
unsigned end = 0)
unsigned begin, unsigned end, unsigned beginLine)
: Block(begin, end),
_macro(macro),
_inCondition(false)
_inCondition(false),
_beginLine(beginLine)
{ }
const Macro &macro() const
......@@ -256,6 +258,9 @@ public:
bool isInCondition() const
{ return _inCondition; }
unsigned beginLine() const
{ return _beginLine; }
private:
void setArguments(const QVector<Block> &arguments)
{ _arguments = arguments; }
......@@ -275,7 +280,7 @@ public:
public:
inline UndefinedMacroUse(
const QByteArray &name,
unsigned begin = 0)
unsigned begin)
: Block(begin, begin + name.length()),
_name(name)
{ }
......@@ -298,6 +303,10 @@ public:
QList<UndefinedMacroUse> undefinedMacroUses() const
{ return _undefinedMacroUses; }
const Macro *findMacroDefinitionAt(unsigned line) const;
const MacroUse *findMacroUseAt(unsigned offset) const;
const UndefinedMacroUse *findUndefinedMacroUseAt(unsigned offset) const;
private:
Symbol *findSymbolAt(unsigned line, unsigned column, Scope *scope) const;
......
......@@ -54,6 +54,8 @@ Macro::Macro()
: _next(0),
_hashcode(0),
_line(0),
_offset(0),
_length(0),
_state(0)
{ }
......
......@@ -92,6 +92,18 @@ public:
void setLine(unsigned line)
{ _line = line; }
unsigned offset() const
{ return _offset; }
void setOffset(unsigned offset)
{ _offset = offset; }
unsigned length() const
{ return _length; }
void setLength(unsigned length)
{ _length = length; }
bool isHidden() const
{ return f._hidden; }
......@@ -129,6 +141,8 @@ private:
QVector<QByteArray> _formals;
QString _fileName;
unsigned _line;
unsigned _offset;
unsigned _length;
union
{
......
......@@ -1205,6 +1205,8 @@ void Preprocessor::processDefine(TokenIterator firstToken, TokenIterator lastTok
macro.setFileName(env->currentFile);
macro.setLine(env->currentLine);
macro.setName(tokenText(*tk));
macro.setOffset(firstToken->offset);
macro.setLength(endOfToken(lastToken[- 1]) - startOfToken(*firstToken));
++tk; // skip T_IDENTIFIER
if (tk->is(T_LPAREN) && ! tk->f.whitespace) {
......
......@@ -1363,15 +1363,14 @@ CPPEditor::Link CPPEditor::findLinkAt(const QTextCursor &cursor,
}
} else {
// Handle macro uses
foreach (const Document::MacroUse use, doc->macroUses()) {
if (use.contains(endOfName - 1)) {
const Macro &macro = use.macro();
link.fileName = macro.fileName();
link.line = macro.line();
link.pos = use.begin();
link.length = use.end() - use.begin();
return link;
}
const Document::MacroUse *use = doc->findMacroUseAt(endOfName - 1);
if (use) {
const Macro &macro = use->macro();
link.fileName = macro.fileName();
link.line = macro.line();
link.pos = use->begin();
link.length = use->end() - use->begin();
return link;
}
}
......
......@@ -455,7 +455,7 @@ void CppPreprocessor::passedMacroDefinitionCheck(unsigned offset, const Macro &m
if (! m_currentDoc)
return;
m_currentDoc->addMacroUse(macro, offset, macro.name().length(),
m_currentDoc->addMacroUse(macro, offset, macro.name().length(), env.currentLine,
QVector<MacroArgumentReference>(), true);
}
......@@ -477,7 +477,8 @@ void CppPreprocessor::startExpandingMacro(unsigned offset,
return;
//qDebug() << "start expanding:" << macro.name() << "text:" << originalText;
m_currentDoc->addMacroUse(macro, offset, originalText.length(), actuals, inCondition);
m_currentDoc->addMacroUse(macro, offset, originalText.length(), env.currentLine,
actuals, inCondition);
}
void CppPreprocessor::stopExpandingMacro(unsigned, const Macro &)
......
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