forked from qt-creator/qt-creator
		
	C++: Add utf16 indices to Macro and Document::MacroUse
In most cases we need to work with the utf16 indices. Only in cppfindreferences the byte interface is still needed since there we read in files and work on a QByteArray to save memory. Change-Id: I6ef6a93fc1875a8c9a305c075d51a9ca034c41bb Reviewed-by: Erik Verbruggen <erik.verbruggen@digia.com>
This commit is contained in:
		| @@ -660,8 +660,9 @@ void Dumper::dumpDocuments(const QList<CPlusPlus::Document::Ptr> &documents, boo | ||||
|                 const QString type = use.isFunctionLike() | ||||
|                         ? QLatin1String("function-like") : QLatin1String("object-like"); | ||||
|                 m_out << i4 << "at line " << use.beginLine() << ", " | ||||
|                       << QString::fromUtf8(use.macro().name()) << ", begin=" << use.begin() | ||||
|                       << ", end=" << use.end() << ", " << type << ", args=" | ||||
|                       << use.macro().nameToQString().size() | ||||
|                       << ", begin=" << use.utf16charsBegin() << ", end=" << use.utf16charsEnd() | ||||
|                       << ", " << type << ", args=" | ||||
|                       << use.arguments().size() << "\n"; | ||||
|             } | ||||
|         } | ||||
|   | ||||
| @@ -1751,7 +1751,7 @@ void CppCompletionAssistProcessor::addMacros_helper(const CPlusPlus::Snapshot &s | ||||
|         addMacros_helper(snapshot, i.resolvedFileName(), processed, definedMacros); | ||||
|  | ||||
|     foreach (const Macro ¯o, doc->definedMacros()) { | ||||
|         const QString macroName = QString::fromUtf8(macro.name().constData(), macro.name().length()); | ||||
|         const QString macroName = macro.nameToQString(); | ||||
|         if (!macro.isHidden()) | ||||
|             definedMacros->insert(macroName); | ||||
|         else | ||||
|   | ||||
| @@ -572,10 +572,10 @@ restart_search: | ||||
|                 } | ||||
|  | ||||
|                 if (macro.name() == useMacro.name()) { | ||||
|                     unsigned lineStart; | ||||
|                     const QByteArray &lineSource = matchingLine(use.begin(), source, &lineStart); | ||||
|                     usages.append(Usage(fileName, QString::fromUtf8(lineSource), use.beginLine(), | ||||
|                                         use.begin() - lineStart, useMacro.name().length())); | ||||
|                     unsigned column; | ||||
|                     const QString &lineSource = matchingLine(use.bytesBegin(), source, &column); | ||||
|                     usages.append(Usage(fileName, lineSource, use.beginLine(), column, | ||||
|                                         useMacro.nameToQString().size())); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
| @@ -585,19 +585,26 @@ restart_search: | ||||
|         return usages; | ||||
|     } | ||||
|  | ||||
|     static QByteArray matchingLine(unsigned position, const QByteArray &source, | ||||
|                                    unsigned *lineStart = 0) | ||||
|     static QString matchingLine(unsigned bytesOffsetOfUseStart, const QByteArray &utf8Source, | ||||
|                                 unsigned *columnOfUseStart = 0) | ||||
|     { | ||||
|         int lineBegin = source.lastIndexOf('\n', position) + 1; | ||||
|         int lineEnd = source.indexOf('\n', position); | ||||
|         int lineBegin = utf8Source.lastIndexOf('\n', bytesOffsetOfUseStart) + 1; | ||||
|         int lineEnd = utf8Source.indexOf('\n', bytesOffsetOfUseStart); | ||||
|         if (lineEnd == -1) | ||||
|             lineEnd = source.length(); | ||||
|             lineEnd = utf8Source.length(); | ||||
|  | ||||
|         if (lineStart) | ||||
|             *lineStart = lineBegin; | ||||
|         if (columnOfUseStart) { | ||||
|             *columnOfUseStart = 0; | ||||
|             const char *startOfUse = utf8Source.constData() + bytesOffsetOfUseStart; | ||||
|             QTC_ASSERT(startOfUse < utf8Source.constData() + lineEnd, return QString()); | ||||
|             const char *currentSourceByte = utf8Source.constData() + lineBegin; | ||||
|             unsigned char yychar = *currentSourceByte; | ||||
|             while (currentSourceByte != startOfUse) | ||||
|                 Lexer::yyinp_utf8(currentSourceByte, yychar, *columnOfUseStart); | ||||
|         } | ||||
|  | ||||
|         const QByteArray matchingLine = source.mid(lineBegin, lineEnd - lineBegin); | ||||
|         return matchingLine; | ||||
|         const QByteArray matchingLine = utf8Source.mid(lineBegin, lineEnd - lineBegin); | ||||
|         return QString::fromUtf8(matchingLine, matchingLine.size()); | ||||
|     } | ||||
| }; | ||||
|  | ||||
| @@ -638,7 +645,7 @@ void CppFindReferences::findMacroUses(const Macro ¯o, const QString &replace | ||||
|     Core::SearchResult *search = Core::SearchResultWindow::instance()->startNewSearch( | ||||
|                 tr("C++ Macro Usages:"), | ||||
|                 QString(), | ||||
|                 QString::fromUtf8(macro.name()), | ||||
|                 macro.nameToQString(), | ||||
|                 replace ? Core::SearchResultWindow::SearchAndReplace | ||||
|                         : Core::SearchResultWindow::SearchOnly, | ||||
|                 Core::SearchResultWindow::PreserveCaseDisabled, | ||||
| @@ -661,11 +668,11 @@ void CppFindReferences::findMacroUses(const Macro ¯o, const QString &replace | ||||
|     // add the macro definition itself | ||||
|     { | ||||
|         const QByteArray &source = getSource(macro.fileName(), workingCopy); | ||||
|         unsigned lineStart; | ||||
|         const QByteArray line = FindMacroUsesInFile::matchingLine(macro.offset(), source, | ||||
|                                                                   &lineStart); | ||||
|         search->addResult(macro.fileName(), macro.line(), QString::fromUtf8(line), | ||||
|                           macro.offset() - lineStart, macro.name().length()); | ||||
|         unsigned column; | ||||
|         const QString line = FindMacroUsesInFile::matchingLine(macro.bytesOffset(), source, | ||||
|                                                                &column); | ||||
|         search->addResult(macro.fileName(), macro.line(), line, column, | ||||
|                           macro.nameToQString().length()); | ||||
|     } | ||||
|  | ||||
|     QFuture<Usage> result; | ||||
| @@ -679,7 +686,7 @@ void CppFindReferences::findMacroUses(const Macro ¯o, const QString &replace | ||||
|  | ||||
| void CppFindReferences::renameMacroUses(const Macro ¯o, const QString &replacement) | ||||
| { | ||||
|     const QString textToReplace = replacement.isEmpty() ? QString::fromUtf8(macro.name()) : replacement; | ||||
|     const QString textToReplace = replacement.isEmpty() ? macro.nameToQString() : replacement; | ||||
|     findMacroUses(macro, textToReplace, true); | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -59,15 +59,15 @@ QFuture<TextEditor::HighlightingResult> CppHighlightingSupportInternal::highligh | ||||
|     // Get macro definitions | ||||
|     foreach (const CPlusPlus::Macro& macro, doc->definedMacros()) { | ||||
|         int line, column; | ||||
|         editor()->convertPosition(macro.offset(), &line, &column); | ||||
|         editor()->convertPosition(macro.utf16CharOffset(), &line, &column); | ||||
|         ++column; //Highlighting starts at (column-1) --> compensate here | ||||
|         Result use(line, column, macro.name().size(), MacroUse); | ||||
|         Result use(line, column, macro.nameToQString().size(), MacroUse); | ||||
|         macroUses.append(use); | ||||
|     } | ||||
|  | ||||
|     // Get macro uses | ||||
|     foreach (const Document::MacroUse ¯o, doc->macroUses()) { | ||||
|         const QString name = QString::fromUtf8(macro.macro().name()); | ||||
|         const QString name = macro.macro().nameToQString(); | ||||
|  | ||||
|         //Filter out QtKeywords | ||||
|         if (isQtKeyword(QStringRef(&name))) | ||||
| @@ -86,7 +86,7 @@ QFuture<TextEditor::HighlightingResult> CppHighlightingSupportInternal::highligh | ||||
|             continue; | ||||
|  | ||||
|         int line, column; | ||||
|         editor()->convertPosition(macro.begin(), &line, &column); | ||||
|         editor()->convertPosition(macro.utf16charsBegin(), &line, &column); | ||||
|         ++column; //Highlighting starts at (column-1) --> compensate here | ||||
|         Result use(line, column, name.size(), MacroUse); | ||||
|         macroUses.append(use); | ||||
|   | ||||
| @@ -291,41 +291,51 @@ static inline const Macro revision(const CppModelManagerInterface::WorkingCopy & | ||||
|     return newMacro; | ||||
| } | ||||
|  | ||||
| void CppPreprocessor::passedMacroDefinitionCheck(unsigned offset, unsigned line, const Macro ¯o) | ||||
| void CppPreprocessor::passedMacroDefinitionCheck(unsigned bytesOffset, unsigned utf16charsOffset, | ||||
|                                                  unsigned line, const Macro ¯o) | ||||
| { | ||||
|     if (!m_currentDoc) | ||||
|         return; | ||||
|  | ||||
|     m_currentDoc->addMacroUse(revision(m_workingCopy, macro), offset, macro.name().length(), line, | ||||
|                               QVector<MacroArgumentReference>()); | ||||
|     m_currentDoc->addMacroUse(revision(m_workingCopy, macro), | ||||
|                               bytesOffset, macro.name().length(), | ||||
|                               utf16charsOffset, macro.nameToQString().size(), | ||||
|                               line, QVector<MacroArgumentReference>()); | ||||
| } | ||||
|  | ||||
| void CppPreprocessor::failedMacroDefinitionCheck(unsigned offset, const ByteArrayRef &name) | ||||
| void CppPreprocessor::failedMacroDefinitionCheck(unsigned bytesOffset, unsigned utf16charOffset, | ||||
|                                                  const ByteArrayRef &name) | ||||
| { | ||||
|     if (!m_currentDoc) | ||||
|         return; | ||||
|  | ||||
|     m_currentDoc->addUndefinedMacroUse(QByteArray(name.start(), name.size()), offset); | ||||
|     m_currentDoc->addUndefinedMacroUse(QByteArray(name.start(), name.size()), | ||||
|                                        bytesOffset, utf16charOffset); | ||||
| } | ||||
|  | ||||
| void CppPreprocessor::notifyMacroReference(unsigned offset, unsigned line, const Macro ¯o) | ||||
| void CppPreprocessor::notifyMacroReference(unsigned bytesOffset, unsigned utf16charOffset, | ||||
|                                            unsigned line, const Macro ¯o) | ||||
| { | ||||
|     if (!m_currentDoc) | ||||
|         return; | ||||
|  | ||||
|     m_currentDoc->addMacroUse(revision(m_workingCopy, macro), offset, macro.name().length(), line, | ||||
|                               QVector<MacroArgumentReference>()); | ||||
|     m_currentDoc->addMacroUse(revision(m_workingCopy, macro), | ||||
|                               bytesOffset, macro.name().length(), | ||||
|                               utf16charOffset, macro.nameToQString().size(), | ||||
|                               line, QVector<MacroArgumentReference>()); | ||||
| } | ||||
|  | ||||
| void CppPreprocessor::startExpandingMacro(unsigned offset, unsigned line, | ||||
|                                           const Macro ¯o, | ||||
| void CppPreprocessor::startExpandingMacro(unsigned bytesOffset, unsigned utf16charOffset, | ||||
|                                           unsigned line, const Macro ¯o, | ||||
|                                           const QVector<MacroArgumentReference> &actuals) | ||||
| { | ||||
|     if (!m_currentDoc) | ||||
|         return; | ||||
|  | ||||
|     m_currentDoc->addMacroUse(revision(m_workingCopy, macro), offset, macro.name().length(), line, | ||||
|                               actuals); | ||||
|     m_currentDoc->addMacroUse(revision(m_workingCopy, macro), | ||||
|                               bytesOffset, macro.name().length(), | ||||
|                               utf16charOffset, macro.nameToQString().size(), | ||||
|                               line, actuals); | ||||
| } | ||||
|  | ||||
| void CppPreprocessor::stopExpandingMacro(unsigned, const Macro &) | ||||
| @@ -366,16 +376,16 @@ void CppPreprocessor::mergeEnvironment(Document::Ptr doc) | ||||
|     m_env.addMacros(doc->definedMacros()); | ||||
| } | ||||
|  | ||||
| void CppPreprocessor::startSkippingBlocks(unsigned offset) | ||||
| void CppPreprocessor::startSkippingBlocks(unsigned utf16charsOffset) | ||||
| { | ||||
|     if (m_currentDoc) | ||||
|         m_currentDoc->startSkippingBlocks(offset); | ||||
|         m_currentDoc->startSkippingBlocks(utf16charsOffset); | ||||
| } | ||||
|  | ||||
| void CppPreprocessor::stopSkippingBlocks(unsigned offset) | ||||
| void CppPreprocessor::stopSkippingBlocks(unsigned utf16charsOffset) | ||||
| { | ||||
|     if (m_currentDoc) | ||||
|         m_currentDoc->stopSkippingBlocks(offset); | ||||
|         m_currentDoc->stopSkippingBlocks(utf16charsOffset); | ||||
| } | ||||
|  | ||||
| void CppPreprocessor::sourceNeeded(unsigned line, const QString &fileName, IncludeType type) | ||||
|   | ||||
| @@ -64,19 +64,21 @@ protected: | ||||
|     void mergeEnvironment(CPlusPlus::Document::Ptr doc); | ||||
|  | ||||
|     virtual void macroAdded(const CPlusPlus::Macro ¯o); | ||||
|     virtual void passedMacroDefinitionCheck(unsigned offset, unsigned line, | ||||
|                                             const CPlusPlus::Macro ¯o); | ||||
|     virtual void failedMacroDefinitionCheck(unsigned offset, const CPlusPlus::ByteArrayRef &name); | ||||
|     virtual void notifyMacroReference(unsigned offset, unsigned line, | ||||
|                                       const CPlusPlus::Macro ¯o); | ||||
|     virtual void startExpandingMacro(unsigned offset, | ||||
|     virtual void passedMacroDefinitionCheck(unsigned bytesOffset, unsigned utf16charsOffset, | ||||
|                                             unsigned line, const CPlusPlus::Macro ¯o); | ||||
|     virtual void failedMacroDefinitionCheck(unsigned bytesOffset, unsigned utf16charOffset, | ||||
|                                             const CPlusPlus::ByteArrayRef &name); | ||||
|     virtual void notifyMacroReference(unsigned bytesOffset, unsigned utf16charOffset, | ||||
|                                       unsigned line, const CPlusPlus::Macro ¯o); | ||||
|     virtual void startExpandingMacro(unsigned bytesOffset, | ||||
|                                      unsigned utf16charOffset, | ||||
|                                      unsigned line, | ||||
|                                      const CPlusPlus::Macro ¯o, | ||||
|                                      const QVector<CPlusPlus::MacroArgumentReference> &actuals); | ||||
|     virtual void stopExpandingMacro(unsigned offset, const CPlusPlus::Macro ¯o); | ||||
|     virtual void stopExpandingMacro(unsigned bytesOffset, const CPlusPlus::Macro ¯o); | ||||
|     virtual void markAsIncludeGuard(const QByteArray ¯oName); | ||||
|     virtual void startSkippingBlocks(unsigned offset); | ||||
|     virtual void stopSkippingBlocks(unsigned offset); | ||||
|     virtual void startSkippingBlocks(unsigned utf16charsOffset); | ||||
|     virtual void stopSkippingBlocks(unsigned utf16charsOffset); | ||||
|     virtual void sourceNeeded(unsigned line, const QString &fileName, IncludeType type); | ||||
|  | ||||
| private: | ||||
|   | ||||
| @@ -333,7 +333,7 @@ void CppEditorSupport::onDocumentUpdated(Document::Ptr doc) | ||||
|         QList<BlockRange> ifdefedOutBlocks; | ||||
|         ifdefedOutBlocks.reserve(skippedBlocks.size()); | ||||
|         foreach (const Document::Block &block, skippedBlocks) | ||||
|             ifdefedOutBlocks.append(BlockRange(block.begin(), block.end())); | ||||
|             ifdefedOutBlocks.append(BlockRange(block.utf16charsBegin(), block.utf16charsEnd())); | ||||
|         setIfdefedOutBlocks(ifdefedOutBlocks); | ||||
|     } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user