ClangCodeModel: Fix detection of #ifdef'ed out blocks with clangd

We were erroneously merging adjacent disabled blocks.

Change-Id: I9f8f588c0362d488a24c044910474815b94efd59
Reviewed-by: David Schulz <david.schulz@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
Christian Kandeler
2022-02-10 10:15:54 +01:00
parent 3bdab2f05e
commit 14ee4654c1
6 changed files with 37 additions and 1 deletions

View File

@@ -2576,7 +2576,8 @@ static QList<BlockRange> cleanupDisabledCode(HighlightingResults &results, const
}
if (wasIfdefedOut && (it + 1 == results.end()
|| (it + 1)->textStyles.mainStyle != C_DISABLED_CODE)) {
|| (it + 1)->textStyles.mainStyle != C_DISABLED_CODE
|| (it + 1)->line != it->line + 1)) {
// The #else or #endif that ends disabled code should not be disabled.
const QTextBlock block = doc->findBlockByNumber(it->line - 1);
ifdefedOutRanges << BlockRange(rangeStartPos, block.position());

View File

@@ -675,6 +675,8 @@ void ClangdTestHighlighting::initTestCase()
{
ClangdTest::initTestCase();
connect(document("highlighting.cpp"), &TextDocument::ifdefedOutBlocksChanged, this,
[this](const QList<BlockRange> &ranges) { m_ifdefedOutBlocks = ranges; });
QTimer timer;
timer.setSingleShot(true);
QEventLoop loop;
@@ -1380,6 +1382,17 @@ void ClangdTestHighlighting::test()
QCOMPARE(result.kind, expectedKind);
}
void ClangdTestHighlighting::testIfdefedOutBlocks()
{
QCOMPARE(m_ifdefedOutBlocks.size(), 3);
QCOMPARE(m_ifdefedOutBlocks.at(0).first(), 12033);
QCOMPARE(m_ifdefedOutBlocks.at(0).last(), 12050);
QCOMPARE(m_ifdefedOutBlocks.at(1).first(), 13351);
QCOMPARE(m_ifdefedOutBlocks.at(1).last(), 13364);
QCOMPARE(m_ifdefedOutBlocks.at(2).first(), 13390);
QCOMPARE(m_ifdefedOutBlocks.at(2).last(), 13402);
}
class Manipulator : public TextDocumentManipulatorInterface
{

View File

@@ -27,6 +27,7 @@
#include <cppeditor/cpptoolstestcase.h>
#include <coreplugin/find/searchresultitem.h>
#include <texteditor/blockrange.h>
#include <texteditor/codeassist/genericproposal.h>
#include <texteditor/semantichighlighter.h>
#include <utils/fileutils.h>
@@ -141,9 +142,11 @@ private slots:
void initTestCase() override;
void test_data();
void test();
void testIfdefedOutBlocks();
private:
TextEditor::HighlightingResults m_results;
QList<TextEditor::BlockRange> m_ifdefedOutBlocks;
};
class ClangdTestCompletion : public ClangdTest

View File

@@ -869,3 +869,14 @@ void constMemberAsFunctionArg()
const Foo &constMember;
};
}
# if 0
#define FOO
#endif
// comment
#if 0
#define BAR
# endif

View File

@@ -532,6 +532,10 @@ void TextDocument::setIfdefedOutBlocks(const QList<BlockRange> &blocks)
if (needUpdate)
documentLayout->requestUpdate();
#ifdef WITH_TESTS
emit ifdefedOutBlocksChanged(blocks);
#endif
}
const ExtraEncodingSettings &TextDocument::extraEncodingSettings() const

View File

@@ -172,6 +172,10 @@ signals:
void fontSettingsChanged();
void markRemoved(TextMark *mark);
#ifdef WITH_TESTS
void ifdefedOutBlocksChanged(const QList<BlockRange> &blocks);
#endif
protected:
virtual void applyFontSettings();