From 199b243bca769049095b72bf5af7e95add1ecff6 Mon Sep 17 00:00:00 2001 From: Andre Hartmann Date: Mon, 8 Oct 2012 18:18:28 +0200 Subject: C++ Macro Usages: Fix search result encoding and highlighting. If the result lines contain non-Latin1 characters (e.g. in comments), the output is garbled. Using QByteArrays with toUtf8() and fromUtf8() doesn't seem to be an alternative, as in this case the macro.offset() is no longer valid. So it seems to be the best to use QString for the result lines. Task-number: QTCREATORBUG-7122 Change-Id: I57128c9c9f3eb182f079e305e97e9c5ac0a1bc61 Reviewed-by: Erik Verbruggen --- src/plugins/cpptools/cppfindreferences.cpp | 41 ++++++++++-------------------- 1 file changed, 13 insertions(+), 28 deletions(-) (limited to 'src/plugins/cpptools/cppfindreferences.cpp') diff --git a/src/plugins/cpptools/cppfindreferences.cpp b/src/plugins/cpptools/cppfindreferences.cpp index 83605ef106..4741aa7dff 100644 --- a/src/plugins/cpptools/cppfindreferences.cpp +++ b/src/plugins/cpptools/cppfindreferences.cpp @@ -569,7 +569,7 @@ public: return usages; const Document::Ptr &doc = snapshot.document(fileName); - QByteArray source; + QString source; foreach (const Document::MacroUse &use, doc->macroUses()) { const Macro &useMacro = use.macro(); @@ -577,7 +577,7 @@ public: && useMacro.fileName() == macro.fileName()) { if (source.isEmpty()) - source = getSource(fileName, workingCopy).toLatin1(); // ### FIXME: Encoding? + source = getSource(fileName, workingCopy); unsigned lineStart; const QString &lineSource = matchingLine(use.begin(), source, &lineStart); @@ -592,29 +592,18 @@ public: } // ### FIXME: Pretty close to FindUsages::matchingLine. - static QString matchingLine(unsigned position, const QByteArray &source, + static QString matchingLine(unsigned position, const QString &source, unsigned *lineStart = 0) { - const char *beg = source.constData(); - const char *start = beg + position; - for (; start != beg - 1; --start) { - if (*start == '\n') - break; - } - - ++start; - - const char *end = start + 1; - for (; *end; ++end) { - if (*end == '\n') - break; - } + int lineBegin = source.lastIndexOf(QLatin1Char('\n'), position) + 1; + int lineEnd = source.indexOf(QLatin1Char('\n'), position); + if (lineEnd == -1) + lineEnd = source.length(); if (lineStart) - *lineStart = start - beg; + *lineStart = lineBegin; - // ### FIXME: Encoding? - const QString matchingLine = QString::fromUtf8(start, end - start); + const QString matchingLine = source.mid(lineBegin, lineEnd - lineBegin); return matchingLine; } }; @@ -678,15 +667,11 @@ void CppFindReferences::findMacroUses(const Macro ¯o, const QString &replace // add the macro definition itself { - // ### FIXME: Encoding? - const QByteArray &source = getSource(macro.fileName(), workingCopy).toLatin1(); - int lineBegin = source.lastIndexOf('\n', macro.offset()) + 1; - int lineEnd = source.indexOf('\n', macro.offset()); - if (lineEnd == -1) - lineEnd = source.length(); - const QByteArray line = source.mid(lineBegin, lineEnd - lineBegin); + const QString &source = getSource(macro.fileName(), workingCopy); + unsigned lineStart; + const QString line = FindMacroUsesInFile::matchingLine(macro.offset(), source, &lineStart); search->addResult(macro.fileName(), macro.line(), line, - line.indexOf(macro.name()), macro.name().length()); + macro.offset() - lineStart, macro.name().length()); } QFuture result; -- cgit v1.2.1 From f3faef5a1e345b02dfca0b8c682e5e36e318849f Mon Sep 17 00:00:00 2001 From: Erik Verbruggen Date: Thu, 11 Oct 2012 16:16:01 +0200 Subject: C++: Fix outdated macro usage info in documents. Record revisions of documents in macro definitions and usages. Then, when searching for usages, check the revision of the documents against the revision of the macros. If they are out-of-sync, repreprocess the documents to get up-to-date info. Task-number: QTCREATORBUG-7872 Change-Id: I846bb52ec660024728ab117a9fb7e43382a50e63 Reviewed-by: Christian Stenger --- src/plugins/cpptools/cppfindreferences.cpp | 31 +++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) (limited to 'src/plugins/cpptools/cppfindreferences.cpp') diff --git a/src/plugins/cpptools/cppfindreferences.cpp b/src/plugins/cpptools/cppfindreferences.cpp index 4741aa7dff..9be53c0c76 100644 --- a/src/plugins/cpptools/cppfindreferences.cpp +++ b/src/plugins/cpptools/cppfindreferences.cpp @@ -128,14 +128,12 @@ public: return usages; // skip this document, it's not using symbolId. } Document::Ptr doc; - QByteArray source; const QString unpreprocessedSource = getSource(fileName, workingCopy); if (symbolDocument && fileName == symbolDocument->fileName()) { doc = symbolDocument; } else { - source = snapshot.preprocessedCode(unpreprocessedSource, fileName); - doc = snapshot.documentFromSource(source, fileName); + doc = snapshot.preprocessedDocument(unpreprocessedSource, fileName); doc->tokenize(); } @@ -458,10 +456,8 @@ bool CppFindReferences::findSymbol(CppFindReferencesParameters *parameters, Document::Ptr newSymbolDocument = snapshot.document(symbolFile); // document is not parsed and has no bindings yet, do it QString source = getSource(newSymbolDocument->fileName(), _modelManager->workingCopy()); - const QByteArray &preprocessedCode = - snapshot.preprocessedCode(source, newSymbolDocument->fileName()); Document::Ptr doc = - snapshot.documentFromSource(preprocessedCode, newSymbolDocument->fileName()); + snapshot.preprocessedDocument(source, newSymbolDocument->fileName()); doc->check(); // construct id of old symbol @@ -563,19 +559,29 @@ public: QList operator()(const QString &fileName) { QList usages; + Document::Ptr doc = snapshot.document(fileName); + QString source; + +_Lrestart: if (future->isPaused()) future->waitForResume(); if (future->isCanceled()) return usages; - const Document::Ptr &doc = snapshot.document(fileName); - QString source; - + usages.clear(); foreach (const Document::MacroUse &use, doc->macroUses()) { const Macro &useMacro = use.macro(); - if (useMacro.line() == macro.line() - && useMacro.fileName() == macro.fileName()) - { + + if (useMacro.fileName() == macro.fileName()) { // Check if this is a match, but possibly against an outdated document. + if (macro.fileRevision() > useMacro.fileRevision()) { + // yes, it is outdated, so re-preprocess and start from scratch for this file. + source = getSource(fileName, workingCopy).toLatin1(); + doc = snapshot.preprocessedDocument(source, fileName); + goto _Lrestart; + } + } + + if (useMacro.fileName() == macro.fileName() && macro.name() == useMacro.name()) { if (source.isEmpty()) source = getSource(fileName, workingCopy); @@ -625,7 +631,6 @@ static void findMacroUses_helper(QFutureInterface &future, files.removeDuplicates(); future.setProgressRange(0, files.size()); - FindMacroUsesInFile process(workingCopy, snapshot, macro, &future); UpdateUI reduce(&future); // This thread waits for blockingMappedReduced to finish, so reduce the pool's used thread count -- cgit v1.2.1 From 1b90d999a5cdf227d3df07ebb0ba6589475776f8 Mon Sep 17 00:00:00 2001 From: Erik Verbruggen Date: Mon, 15 Oct 2012 10:48:30 +0200 Subject: C++: removed invalid FIXME. Change-Id: I05d17e5384bcc75e97c5d901b65a582516a5d321 Reviewed-by: David Schulz --- src/plugins/cpptools/cppfindreferences.cpp | 1 - 1 file changed, 1 deletion(-) (limited to 'src/plugins/cpptools/cppfindreferences.cpp') diff --git a/src/plugins/cpptools/cppfindreferences.cpp b/src/plugins/cpptools/cppfindreferences.cpp index 9be53c0c76..6779fe2c9d 100644 --- a/src/plugins/cpptools/cppfindreferences.cpp +++ b/src/plugins/cpptools/cppfindreferences.cpp @@ -597,7 +597,6 @@ _Lrestart: return usages; } - // ### FIXME: Pretty close to FindUsages::matchingLine. static QString matchingLine(unsigned position, const QString &source, unsigned *lineStart = 0) { -- cgit v1.2.1