Fixed the line retreival for find-usages.

Instead of using the pre-processed source, the original one is used.
This makes a difference when a macro is used in the line, where the pre-
processed source would have a "#gen true" token.

Task-number: QTCREATORBUG-3345
This commit is contained in:
Erik Verbruggen
2011-01-17 14:41:19 +01:00
parent 673f9871ba
commit dea74862d3
3 changed files with 9 additions and 29 deletions

View File

@@ -45,13 +45,14 @@
using namespace CPlusPlus; using namespace CPlusPlus;
FindUsages::FindUsages(Document::Ptr doc, const Snapshot &snapshot) FindUsages::FindUsages(const QByteArray &originalSource, Document::Ptr doc, const Snapshot &snapshot)
: ASTVisitor(doc->translationUnit()), : ASTVisitor(doc->translationUnit()),
_id(0), _id(0),
_declSymbol(0), _declSymbol(0),
_doc(doc), _doc(doc),
_snapshot(snapshot), _snapshot(snapshot),
_context(doc, snapshot), _context(doc, snapshot),
_originalSource(originalSource),
_source(_doc->source()), _source(_doc->source()),
_currentScope(0) _currentScope(0)
{ {
@@ -66,6 +67,7 @@ FindUsages::FindUsages(const LookupContext &context)
_doc(context.thisDocument()), _doc(context.thisDocument()),
_snapshot(context.snapshot()), _snapshot(context.snapshot()),
_context(context), _context(context),
_originalSource(_doc->source()),
_source(_doc->source()), _source(_doc->source()),
_currentScope(0) _currentScope(0)
{ {
@@ -101,27 +103,6 @@ void FindUsages::operator()(Symbol *symbol)
translationUnit(ast->asTranslationUnit()); translationUnit(ast->asTranslationUnit());
} }
QString FindUsages::matchingLine(const Token &tk) const
{
const char *beg = _source.constData();
const char *cp = beg + tk.offset;
for (; cp != beg - 1; --cp) {
if (*cp == '\n')
break;
}
++cp;
const char *lineEnd = cp + 1;
for (; *lineEnd; ++lineEnd) {
if (*lineEnd == '\n')
break;
}
const QString matchingLine = QString::fromUtf8(cp, lineEnd - cp);
return matchingLine;
}
void FindUsages::reportResult(unsigned tokenIndex, const Name *name, Scope *scope) void FindUsages::reportResult(unsigned tokenIndex, const Name *name, Scope *scope)
{ {
if (! (tokenIndex && name != 0)) if (! (tokenIndex && name != 0))
@@ -163,10 +144,9 @@ void FindUsages::reportResult(unsigned tokenIndex)
_processed.insert(tokenIndex); _processed.insert(tokenIndex);
const QString lineText = matchingLine(tk);
unsigned line, col; unsigned line, col;
getTokenStartPosition(tokenIndex, &line, &col); getTokenStartPosition(tokenIndex, &line, &col);
const QString lineText = QString::fromUtf8(_originalSource.split('\n').at(line - 1));
if (col) if (col)
--col; // adjust the column position. --col; // adjust the column position.

View File

@@ -62,7 +62,7 @@ public:
class CPLUSPLUS_EXPORT FindUsages: protected ASTVisitor class CPLUSPLUS_EXPORT FindUsages: protected ASTVisitor
{ {
public: public:
FindUsages(Document::Ptr doc, const Snapshot &snapshot); FindUsages(const QByteArray &originalSource, Document::Ptr doc, const Snapshot &snapshot);
FindUsages(const LookupContext &context); FindUsages(const LookupContext &context);
void operator()(Symbol *symbol); void operator()(Symbol *symbol);
@@ -75,8 +75,6 @@ protected:
Scope *switchScope(Scope *scope); Scope *switchScope(Scope *scope);
QString matchingLine(const Token &tk) const;
void reportResult(unsigned tokenIndex, const Name *name, Scope *scope = 0); void reportResult(unsigned tokenIndex, const Name *name, Scope *scope = 0);
void reportResult(unsigned tokenIndex, const Identifier *id, Scope *scope = 0); void reportResult(unsigned tokenIndex, const Identifier *id, Scope *scope = 0);
void reportResult(unsigned tokenIndex, const QList<LookupItem> &candidates); void reportResult(unsigned tokenIndex, const QList<LookupItem> &candidates);
@@ -295,6 +293,7 @@ private:
Document::Ptr _doc; Document::Ptr _doc;
Snapshot _snapshot; Snapshot _snapshot;
LookupContext _context; LookupContext _context;
QByteArray _originalSource;
QByteArray _source; QByteArray _source;
QList<int> _references; QList<int> _references;
QList<Usage> _usages; QList<Usage> _usages;

View File

@@ -115,11 +115,12 @@ public:
Document::Ptr doc; Document::Ptr doc;
QByteArray source; QByteArray source;
const QString unpreprocessedSource = getSource(fileName, workingCopy);
if (symbolDocument && fileName == symbolDocument->fileName()) if (symbolDocument && fileName == symbolDocument->fileName())
doc = symbolDocument; doc = symbolDocument;
else { else {
source = snapshot.preprocessedCode(getSource(fileName, workingCopy), fileName); source = snapshot.preprocessedCode(unpreprocessedSource, fileName);
doc = snapshot.documentFromSource(source, fileName); doc = snapshot.documentFromSource(source, fileName);
doc->tokenize(); doc->tokenize();
} }
@@ -129,7 +130,7 @@ public:
if (doc != symbolDocument) if (doc != symbolDocument)
doc->check(); doc->check();
FindUsages process(doc, snapshot); FindUsages process(unpreprocessedSource.toUtf8(), doc, snapshot);
process(symbol); process(symbol);
usages = process.usages(); usages = process.usages();