diff options
| author | Christian Kandeler <[email protected]> | 2020-11-20 12:26:33 +0100 |
|---|---|---|
| committer | Christian Kandeler <[email protected]> | 2020-11-23 09:59:27 +0000 |
| commit | b0dd6b748f68af8b53d5c3fc470e5758ddf969de (patch) | |
| tree | 81f8d6d6a0fa1fd2c590a1e42f74554a658fb253 | |
| parent | fc216b48382b194d88fa9625232cf31ab23a9b61 (diff) | |
clangbackend: Try harder to get the proper cursor
... in cases where it appears that clang_annotateTokens() did not do
what we wanted.
Fixes: QTCREATORBUG-21522
Change-Id: I272061cb6c4b51a5d779ace5b4e06912c0a386e5
Reviewed-by: Christian Stenger <[email protected]>
| -rw-r--r-- | src/tools/clangbackend/source/tokeninfo.cpp | 12 | ||||
| -rw-r--r-- | tests/unit/unittest/data/highlightingmarks.cpp | 9 | ||||
| -rw-r--r-- | tests/unit/unittest/tokenprocessor-test.cpp | 7 |
3 files changed, 27 insertions, 1 deletions
diff --git a/src/tools/clangbackend/source/tokeninfo.cpp b/src/tools/clangbackend/source/tokeninfo.cpp index 2fa0c674b39..f5a3622662c 100644 --- a/src/tools/clangbackend/source/tokeninfo.cpp +++ b/src/tools/clangbackend/source/tokeninfo.cpp @@ -452,8 +452,18 @@ void TokenInfo::identifierKind(const Cursor &cursor, Recursion recursion) case CXCursor_InvalidFile: invalidFileKind(); break; - default: + default: { + // QTCREATORBUG-21522 + // Note: clang_getTokenLocation() does not work. + const SourceLocation loc = m_token->location(); + const CXFile cxFile = clang_getFile(m_token->tu(), loc.filePath().toByteArray()); + const CXSourceLocation cxLoc = clang_getLocation(m_token->tu(), cxFile, loc.line(), + loc.column()); + const CXCursor realCursor = clang_getCursor(m_token->tu(), cxLoc); + if (realCursor != cursor) + identifierKind(realCursor, Recursion::FirstPass); break; + } } } diff --git a/tests/unit/unittest/data/highlightingmarks.cpp b/tests/unit/unittest/data/highlightingmarks.cpp index 346071dea15..a28210012b5 100644 --- a/tests/unit/unittest/data/highlightingmarks.cpp +++ b/tests/unit/unittest/data/highlightingmarks.cpp @@ -736,3 +736,12 @@ void structuredBindingTest() { const int a[] = {1, 2}; const auto [x, y] = a; } + +#define ASSIGN(decl, ptr) do { decl = *ptr; } while (false) +#define ASSIGN2 ASSIGN +void f4() +{ + int *thePointer = 0; + ASSIGN(int i, thePointer); + ASSIGN2(int i, thePointer); +} diff --git a/tests/unit/unittest/tokenprocessor-test.cpp b/tests/unit/unittest/tokenprocessor-test.cpp index b937a777933..a80d90eec43 100644 --- a/tests/unit/unittest/tokenprocessor-test.cpp +++ b/tests/unit/unittest/tokenprocessor-test.cpp @@ -1769,6 +1769,13 @@ TEST_F(TokenProcessor, StructuredBinding) ASSERT_THAT(infos[5], IsHighlightingMark(737u, 20u, 1u, HighlightingType::LocalVariable)); } +TEST_F(TokenProcessor, IndirectMacro) +{ + const auto infos = translationUnit.tokenInfosInRange(sourceRange(746, 32)); + + ASSERT_THAT(infos[5], IsHighlightingMark(746u, 20u, 10u, HighlightingType::LocalVariable)); +} + Data *TokenProcessor::d; void TokenProcessor::SetUpTestCase() |
