clangbackend: Enhance support for attributes

Instead of assuming an attribute is always just a single
identifier enhance this to support attributes with arguments,
with namespaces and attribute lists as well.

Change-Id: I4cbae33e6b70e29705404dfba952eaab56f507e9
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
Christian Stenger
2020-11-12 12:47:57 +01:00
parent 21b03918b7
commit 2d5a6818f2

View File

@@ -100,7 +100,7 @@ std::vector<Cursor> Tokens::annotate() const
// TODO: Investigate whether we can fix this in libclang itself. // TODO: Investigate whether we can fix this in libclang itself.
for (int i = 1; i < int(m_tokens.size()) - 2; ++i) { for (int i = 1; i < int(m_tokens.size()) - 2; ++i) {
const Token &tok = m_tokens.at(i); const Token &tok = m_tokens.at(i);
if (tok.kind() != CXToken_Identifier) if (tok.kind() != CXToken_Identifier && tok.kind() != CXToken_Keyword)
continue; continue;
const Token &prevTok = m_tokens.at(i - 1); const Token &prevTok = m_tokens.at(i - 1);
const Token &nextTok = m_tokens.at(i + 1); const Token &nextTok = m_tokens.at(i + 1);
@@ -126,25 +126,41 @@ std::vector<Cursor> Tokens::annotate() const
} }
// QTCREATORBUG-24636, QTCREATORBUG-24650 // QTCREATORBUG-24636, QTCREATORBUG-24650
if (i >= 2 && i < int(m_tokens.size()) - 3) { if (i >= 2) {
QList<int> setToNull;
const Token &prevPrevTok = m_tokens.at(i - 2); const Token &prevPrevTok = m_tokens.at(i - 2);
const Token &nextNextTok = m_tokens.at(i + 2);
if (prevTok.kind() == CXToken_Punctuation && prevTok.spelling() == "[" if (prevTok.kind() == CXToken_Punctuation && prevTok.spelling() == "["
&& prevPrevTok.kind() == CXToken_Punctuation && prevPrevTok.spelling() == "[" && prevPrevTok.kind() == CXToken_Punctuation && prevPrevTok.spelling() == "[") {
&& nextTok.kind() == CXToken_Punctuation && nextTok.spelling() == "]"
&& nextNextTok.kind() == CXToken_Punctuation && nextNextTok.spelling() == "]") { int endOfAttrList = i + 2; // assume the first possible end of attribute declaration
for (int j = i + 3; j < int(m_tokens.size()); ++j) { while (endOfAttrList < int(m_tokens.size())) {
const Token &last = m_tokens.at(endOfAttrList);
const Token &secondLast = m_tokens.at(endOfAttrList - 1);
const Token &current = m_tokens.at(endOfAttrList - 2);
if (current.kind() == CXToken_Identifier || current.kind() == CXToken_Keyword)
setToNull.append(endOfAttrList - 2);
if (last.kind() == CXToken_Punctuation && last.spelling() == "]"
&& secondLast.kind() == CXToken_Punctuation
&& secondLast.spelling() == "]") {
break;
}
++endOfAttrList;
}
for (int index : qAsConst(setToNull)) {
for (int j = index + 3; j < int(m_tokens.size()); ++j) {
if (cxCursors[j] == cxCursors[j - 1]) if (cxCursors[j] == cxCursors[j - 1])
continue; continue;
if (cxCursors[j].kind == CXCursor_FunctionDecl if (cxCursors[j].kind == CXCursor_FunctionDecl
|| cxCursors[j].kind == CXCursor_ParmDecl) { || cxCursors[j].kind == CXCursor_ParmDecl) {
cxCursors[i] = clang_getNullCursor(); cxCursors[index] = clang_getNullCursor();
} }
break; break;
} }
} }
} }
} }
}
cursors.reserve(cxCursors.size()); cursors.reserve(cxCursors.size());
for (const CXCursor &cxCursor : cxCursors) for (const CXCursor &cxCursor : cxCursors)