From e35dfd592f63dcda0a2cdc26687e84c96ab07783 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Fri, 30 Oct 2020 12:28:37 +0100 Subject: [PATCH] clangbackend: Fix highlighting for #include directives These were falsely categorized as string literals. No one ever noticed, because highlighting information for string literals was ignored until baf25e4cdb. Change-Id: Ib59fde04359aecb6da3995de1a9febbbf41b12c8 Reviewed-by: David Schulz --- src/tools/clangbackend/source/tokeninfo.cpp | 5 ++++- tests/unit/unittest/tokenprocessor-test.cpp | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/tools/clangbackend/source/tokeninfo.cpp b/src/tools/clangbackend/source/tokeninfo.cpp index 6b2e56c8ac1..2fa0c674b39 100644 --- a/src/tools/clangbackend/source/tokeninfo.cpp +++ b/src/tools/clangbackend/source/tokeninfo.cpp @@ -440,7 +440,10 @@ void TokenInfo::identifierKind(const Cursor &cursor, Recursion recursion) m_types.mainHighlightingType = HighlightingType::PreprocessorDefinition; break; case CXCursor_InclusionDirective: - m_types.mainHighlightingType = HighlightingType::StringLiteral; + // Included files are sometimes reported as strings and sometimes as + // include directives, depending on various circumstances. + m_types.mainHighlightingType = m_token->spelling() == "include" + ? HighlightingType::Preprocessor : HighlightingType::StringLiteral; break; case CXCursor_LabelRef: case CXCursor_LabelStmt: diff --git a/tests/unit/unittest/tokenprocessor-test.cpp b/tests/unit/unittest/tokenprocessor-test.cpp index 62a84af59d0..b937a777933 100644 --- a/tests/unit/unittest/tokenprocessor-test.cpp +++ b/tests/unit/unittest/tokenprocessor-test.cpp @@ -928,7 +928,8 @@ TEST_F(TokenProcessor, PreprocessorInclusionDirective) { const auto infos = translationUnit.tokenInfosInRange(sourceRange(239, 18)); - ASSERT_THAT(infos[1], HasOnlyType(HighlightingType::StringLiteral)); + ASSERT_THAT(infos[1], HasOnlyType(HighlightingType::Preprocessor)); + ASSERT_THAT(infos[2], HasOnlyType(HighlightingType::StringLiteral)); } TEST_F(TokenProcessor, GotoLabelStatement) @@ -1080,6 +1081,7 @@ TEST_F(TokenProcessor, PreprocessorInclusionDirectiveWithAngleBrackets ) { const auto infos = translationUnit.tokenInfosInRange(sourceRange(289, 38)); + ASSERT_THAT(infos[1], HasOnlyType(HighlightingType::Preprocessor)); ASSERT_THAT(infos[3], HasOnlyType(HighlightingType::StringLiteral)); }