forked from qt-creator/qt-creator
clangbackend: Another workaround for libclang cursor weirdness
Sometimes, clang_getCursor() erroneously returns an invalid cursor, in which case we may be able to retrieve the real one via clang_annotateTokens(). This is basically the reverse of the issue we recently had, where we had to call clang_getCursor() to get information missing from clang_annotateTokens(). Fixes: QTCREATORBUG-21194 Change-Id: Ie8c4767bca03bd1b27e3ef1f906378fc1d436b90 Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
@@ -71,19 +71,6 @@ static SourceRangeContainer extractMatchingTokenRange(const Cursor &cursor,
|
||||
return SourceRangeContainer();
|
||||
}
|
||||
|
||||
static int getTokenIndex(CXTranslationUnit tu, const Tokens &tokens, uint line, uint column)
|
||||
{
|
||||
int tokenIndex = -1;
|
||||
for (int i = tokens.size() - 1; i >= 0; --i) {
|
||||
const SourceRange range(tu, tokens[i].extent());
|
||||
if (range.contains(line, column)) {
|
||||
tokenIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return tokenIndex;
|
||||
}
|
||||
|
||||
FollowSymbolResult FollowSymbol::followSymbol(CXTranslationUnit tu,
|
||||
const Cursor &fullCursor,
|
||||
uint line,
|
||||
@@ -100,7 +87,7 @@ FollowSymbolResult FollowSymbol::followSymbol(CXTranslationUnit tu,
|
||||
return SourceRangeContainer();
|
||||
|
||||
std::vector<Cursor> cursors = tokens.annotate();
|
||||
int tokenIndex = getTokenIndex(tu, tokens, line, column);
|
||||
const int tokenIndex = tokens.getTokenIndex(tu, line, column);
|
||||
QTC_ASSERT(tokenIndex >= 0, return SourceRangeContainer());
|
||||
|
||||
const Utf8String tokenSpelling = tokens[tokenIndex].spelling();
|
||||
|
@@ -512,9 +512,25 @@ Utf8String ToolTipInfoCollector::lineRange(const Utf8String &filePath,
|
||||
|
||||
ToolTipInfo ToolTipInfoCollector::collect(uint line, uint column) const
|
||||
{
|
||||
const Cursor cursor = clang_getCursor(m_cxTranslationUnit, toCXSourceLocation(line, column));
|
||||
Cursor cursor = clang_getCursor(m_cxTranslationUnit, toCXSourceLocation(line, column));
|
||||
if (!cursor.isValid()) { // QTCREATORBUG-21194
|
||||
Tokens tokens(Cursor(clang_getTranslationUnitCursor(m_cxTranslationUnit)).sourceRange());
|
||||
if (!tokens.size())
|
||||
return {};
|
||||
|
||||
// TODO: Only annotate the tokens up until the location we are interested in?
|
||||
// Same goes for FollowSymbol.
|
||||
const std::vector<Cursor> cursors = tokens.annotate();
|
||||
|
||||
const int tokenIndex = tokens.getTokenIndex(m_cxTranslationUnit, line, column);
|
||||
QTC_ASSERT(tokenIndex >= 0, return {});
|
||||
const Utf8String tokenSpelling = tokens[tokenIndex].spelling();
|
||||
if (tokenSpelling.isEmpty())
|
||||
return {};
|
||||
cursor = cursors[tokenIndex];
|
||||
}
|
||||
if (!cursor.isValid())
|
||||
return ToolTipInfo(); // E.g. cursor on ifdeffed out range
|
||||
return {};
|
||||
|
||||
const Cursor referenced = referencedCursor(cursor);
|
||||
QTC_CHECK(referenced.isValid());
|
||||
|
@@ -202,6 +202,19 @@ std::vector<Token>::iterator Tokens::end()
|
||||
return m_tokens.end();
|
||||
}
|
||||
|
||||
int Tokens::getTokenIndex(CXTranslationUnit tu, uint line, uint column) const
|
||||
{
|
||||
int tokenIndex = -1;
|
||||
for (int i = size() - 1; i >= 0; --i) {
|
||||
const SourceRange range(tu, (*this)[i].extent());
|
||||
if (range.contains(line, column)) {
|
||||
tokenIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return tokenIndex;
|
||||
}
|
||||
|
||||
Tokens::~Tokens()
|
||||
{
|
||||
if (m_tokens.empty())
|
||||
|
@@ -25,6 +25,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <utils/smallstringfwd.h>
|
||||
|
||||
#include <clang-c/Index.h>
|
||||
|
||||
#include <vector>
|
||||
@@ -81,6 +83,8 @@ public:
|
||||
std::vector<Token>::iterator begin();
|
||||
std::vector<Token>::iterator end();
|
||||
|
||||
int getTokenIndex(CXTranslationUnit tu, uint line, uint column) const;
|
||||
|
||||
CXTranslationUnit tu() const { return m_cxTranslationUnit; }
|
||||
private:
|
||||
CXTranslationUnit m_cxTranslationUnit;
|
||||
|
Reference in New Issue
Block a user