Commit a450c133 authored by Francois Ferrand's avatar Francois Ferrand Committed by Erik Verbruggen

CppEditor: Fix go to definition of macros.

- Go to macro definition only on macro name (ie not in parameters).
- Prefer macro definition over expanded code definition, as the
  preprocessor is executed first: when trying to go to definition, the
  user sees the macro, not the expanded code.

Task-number: QTCREATORBUG-2240
Task-number: QTCREATORBUG-6175
Task-number: QTCREATORBUG-6848
Task-number: QTCREATORBUG-7008
Task-number: QTCREATORBUG-7009

Change-Id: I819c763524e79b20518c26a46a99a3a3b0a131bd
Reviewed-by: default avatarAndre Hartmann <aha_1980@gmx.de>
Reviewed-by: Orgad Shaneh's avatarOrgad Shaneh <orgads@gmail.com>
Reviewed-by: default avatarErik Verbruggen <erik.verbruggen@nokia.com>
parent 0cf5044d
......@@ -473,7 +473,7 @@ const Macro *Document::findMacroDefinitionAt(unsigned line) const
const Document::MacroUse *Document::findMacroUseAt(unsigned offset) const
{
foreach (const Document::MacroUse &use, _macroUses) {
if (use.contains(offset))
if (use.contains(offset) && (offset < use.begin() + use.macro().name().length()))
return &use;
}
return 0;
......@@ -482,7 +482,7 @@ const Document::MacroUse *Document::findMacroUseAt(unsigned offset) const
const Document::UndefinedMacroUse *Document::findUndefinedMacroUseAt(unsigned offset) const
{
foreach (const Document::UndefinedMacroUse &use, _undefinedMacroUses) {
if (use.contains(offset))
if (use.contains(offset) && (offset < use.begin() + use.name().length()))
return &use;
}
return 0;
......
......@@ -707,11 +707,14 @@ const Macro *CPPEditorWidget::findCanonicalMacro(const QTextCursor &cursor, Docu
int line, col;
convertPosition(cursor.position(), &line, &col);
if (const Macro *macro = doc->findMacroDefinitionAt(line))
if (const Macro *macro = doc->findMacroDefinitionAt(line)) {
QTextCursor macroCursor = cursor;
const QByteArray name = identifierUnderCursor(&macroCursor).toLatin1();
if (macro->name() == name)
return macro;
if (const Document::MacroUse *use = doc->findMacroUseAt(cursor.position()))
} else if (const Document::MacroUse *use = doc->findMacroUseAt(cursor.position())) {
return &use->macro();
}
return 0;
}
......@@ -722,12 +725,13 @@ void CPPEditorWidget::findUsages()
info.snapshot = CppModelManagerInterface::instance()->snapshot();
info.snapshot.insert(info.doc);
if (const Macro *macro = findCanonicalMacro(textCursor(), info.doc)) {
m_modelManager->findMacroUsages(*macro);
} else {
CanonicalSymbol cs(this, info);
Symbol *canonicalSymbol = cs(textCursor());
if (canonicalSymbol) {
if (canonicalSymbol)
m_modelManager->findUsages(canonicalSymbol, cs.context());
} else if (const Macro *macro = findCanonicalMacro(textCursor(), info.doc)) {
m_modelManager->findMacroUsages(*macro);
}
}
......@@ -1381,6 +1385,25 @@ CPPEditorWidget::Link CPPEditorWidget::findLinkAt(const QTextCursor &cursor,
tc.setPosition(endOfToken);
}
// Handle macro uses
const Macro *macro = doc->findMacroDefinitionAt(line);
if (macro) {
QTextCursor macroCursor = cursor;
const QByteArray name = identifierUnderCursor(&macroCursor).toLatin1();
if (macro->name() == name)
return link; //already on definition!
} else {
const Document::MacroUse *use = doc->findMacroUseAt(endOfToken - 1);
if (use && use->macro().fileName() != QLatin1String("<configuration>")) {
const Macro &macro = use->macro();
link.fileName = macro.fileName();
link.line = macro.line();
link.begin = use->begin();
link.end = use->end();
return link;
}
}
// Find the last symbol up to the cursor position
Scope *scope = doc->scopeAt(line, column);
if (!scope)
......
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