From ff1203de9ec11fb70792bd601fa4c56f6ae85447 Mon Sep 17 00:00:00 2001 From: Nikolai Kosjar Date: Mon, 2 Jun 2014 16:06:26 -0400 Subject: [PATCH] C++: Fix Macrouse::utf16charsBegin() This caused displaced highlighting of macro uses after #if constructs. MacroUse::utf16charBegin() was based on the last "continuation token", which was wrong. Change-Id: I89983d82fcf804ba853c04a59a7533c489785d05 Reviewed-by: Erik Verbruggen --- src/libs/cplusplus/pp-engine.cpp | 3 ++- src/plugins/cpptools/cpppreprocessor_test.cpp | 22 +++++++++++++++++++ src/plugins/cpptools/cpptoolsplugin.h | 1 + 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/libs/cplusplus/pp-engine.cpp b/src/libs/cplusplus/pp-engine.cpp index fec9dd28067..4681ef65a9f 100644 --- a/src/libs/cplusplus/pp-engine.cpp +++ b/src/libs/cplusplus/pp-engine.cpp @@ -1788,6 +1788,7 @@ QByteArray Preprocessor::expand(PPToken *tk, PPToken *lastConditionToken) { unsigned line = tk->lineno; unsigned bytesBegin = tk->bytesBegin(); + unsigned utf16charsBegin = tk->utf16charsBegin(); PPToken lastTk; while (isContinuationToken(*tk)) { lastTk = *tk; @@ -1801,7 +1802,7 @@ QByteArray Preprocessor::expand(PPToken *tk, PPToken *lastConditionToken) QByteArray result; result.reserve(256); preprocess(m_state.m_currentFileName, condition, &result, 0, true, false, true, - bytesBegin, tk->utf16charsBegin(), line); + bytesBegin, utf16charsBegin, line); result.squeeze(); // qDebug("*** Condition after: [%s]", result.constData()); diff --git a/src/plugins/cpptools/cpppreprocessor_test.cpp b/src/plugins/cpptools/cpppreprocessor_test.cpp index 61a6c4a7b5c..398c6946877 100644 --- a/src/plugins/cpptools/cpppreprocessor_test.cpp +++ b/src/plugins/cpptools/cpppreprocessor_test.cpp @@ -180,3 +180,25 @@ void CppToolsPlugin::test_cpppreprocessor_includes_allDiagnostics() QCOMPARE(document->unresolvedIncludes().size(), 3); QCOMPARE(document->diagnosticMessages().size(), 3); } + +void CppToolsPlugin::test_cpppreprocessor_macroUses() +{ + QByteArray source = + "#define SOMEDEFINE 1\n" + "#if SOMEDEFINE == 1\n" + " int someNumber;\n" + "#endif\n" + ; + + SourcePreprocessor processor; + Document::Ptr document = processor.run(source); + QVERIFY(document); + const QList macroUses = document->macroUses(); + QCOMPARE(macroUses.size(), 1); + const Document::MacroUse macroUse = macroUses.at(0); + QCOMPARE(macroUse.bytesBegin(), 25U); + QCOMPARE(macroUse.bytesEnd(), 35U); + QCOMPARE(macroUse.utf16charsBegin(), 25U); + QCOMPARE(macroUse.utf16charsEnd(), 35U); + QCOMPARE(macroUse.beginLine(), 2U); +} diff --git a/src/plugins/cpptools/cpptoolsplugin.h b/src/plugins/cpptools/cpptoolsplugin.h index 00a8a05567c..b87f4485a5d 100644 --- a/src/plugins/cpptools/cpptoolsplugin.h +++ b/src/plugins/cpptools/cpptoolsplugin.h @@ -128,6 +128,7 @@ private slots: void test_cpppreprocessor_includes_resolvedUnresolved(); void test_cpppreprocessor_includes_cyclic(); void test_cpppreprocessor_includes_allDiagnostics(); + void test_cpppreprocessor_macroUses(); void test_functionutils_virtualFunctions(); void test_functionutils_virtualFunctions_data();