From f307c55eac8c8b1a2b24966d34cb0610203fa6ae Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Fri, 16 Apr 2021 15:38:52 +0200 Subject: [PATCH] ClangBackEnd: Fix mis-detection of operator< Inside the body of a function template, clang reports a different cursor type for operators in some cases. Make sure we don't mistake such operator< or operator> as opening or closing a template, respectively. Fixes: QTCREATORBUG-25596 Change-Id: Ifc357e199ea13e44ac501613639e667c333d48e3 Reviewed-by: David Schulz --- src/tools/clangbackend/source/tokeninfo.cpp | 1 + tests/unit/unittest/data/highlightingmarks.cpp | 17 +++++++++++++++++ tests/unit/unittest/tokenprocessor-test.cpp | 6 ++++++ 3 files changed, 24 insertions(+) diff --git a/src/tools/clangbackend/source/tokeninfo.cpp b/src/tools/clangbackend/source/tokeninfo.cpp index 8f63de41874..8e380b639c7 100644 --- a/src/tools/clangbackend/source/tokeninfo.cpp +++ b/src/tools/clangbackend/source/tokeninfo.cpp @@ -599,6 +599,7 @@ void TokenInfo::punctuationOrOperatorKind() if (m_types.mainHighlightingType == HighlightingType::Punctuation && m_types.mixinHighlightingTypes.empty() + && kind != CXCursor_OverloadedDeclRef && kind != CXCursor_InclusionDirective && kind != CXCursor_PreprocessingDirective) { const ClangString spelling = m_token->spelling(); diff --git a/tests/unit/unittest/data/highlightingmarks.cpp b/tests/unit/unittest/data/highlightingmarks.cpp index 8221afc29f6..f4c9aa74bbb 100644 --- a/tests/unit/unittest/data/highlightingmarks.cpp +++ b/tests/unit/unittest/data/highlightingmarks.cpp @@ -771,3 +771,20 @@ void func(T v) { } static std::vector> pv; + +template +struct vecn +{ + T v[S]; +}; + +template +static inline constexpr vecn operator<(vecn a, vecn b) +{ + vecn x = vecn{}; + for(long i = 0; i < S; ++i) + { + x[i] = a[i] < b[i]; + } + return x; +} diff --git a/tests/unit/unittest/tokenprocessor-test.cpp b/tests/unit/unittest/tokenprocessor-test.cpp index 562f852abed..b7ebeba692e 100644 --- a/tests/unit/unittest/tokenprocessor-test.cpp +++ b/tests/unit/unittest/tokenprocessor-test.cpp @@ -1801,6 +1801,12 @@ TEST_F(TokenProcessor, NestedTemplate) HighlightingType::DoubleAngleBracketClose)); } +TEST_F(TokenProcessor, OperatorInTemplate) +{ + const auto infos = translationUnit.tokenInfosInRange(sourceRange(787, 28)); + ASSERT_THAT(infos[9], HasOnlyType(HighlightingType::Punctuation)); +} + Data *TokenProcessor::d; void TokenProcessor::SetUpTestCase()