2018-01-19 09:18:57 +01:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2018 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
|
**
|
|
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "clangstring.h"
|
2018-08-07 10:05:31 +02:00
|
|
|
#include "clangtooltipinfocollector.h"
|
2018-01-19 09:18:57 +01:00
|
|
|
#include "cursor.h"
|
|
|
|
|
#include "fulltokeninfo.h"
|
2018-01-26 15:12:38 +01:00
|
|
|
#include "sourcerange.h"
|
2018-04-06 14:07:35 +02:00
|
|
|
#include "tokenprocessor.h"
|
2018-01-26 15:12:38 +01:00
|
|
|
|
|
|
|
|
#include <utils/predicates.h>
|
2018-01-19 09:18:57 +01:00
|
|
|
|
|
|
|
|
namespace ClangBackEnd {
|
|
|
|
|
|
|
|
|
|
FullTokenInfo::FullTokenInfo(const CXCursor &cxCursor,
|
|
|
|
|
CXToken *cxToken,
|
|
|
|
|
CXTranslationUnit cxTranslationUnit,
|
|
|
|
|
std::vector<CXSourceRange> ¤tOutputArgumentRanges)
|
|
|
|
|
: TokenInfo(cxCursor, cxToken, cxTranslationUnit, currentOutputArgumentRanges)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FullTokenInfo::operator TokenInfoContainer() const
|
|
|
|
|
{
|
2018-04-06 14:07:35 +02:00
|
|
|
return TokenInfoContainer(line(), column(), length(), m_types, m_extraInfo);
|
2018-01-19 09:18:57 +01:00
|
|
|
}
|
|
|
|
|
|
2018-08-07 10:05:31 +02:00
|
|
|
static Utf8String fullyQualifiedType(const Cursor &cursor) {
|
|
|
|
|
Utf8String prefix;
|
2018-08-09 14:52:58 +02:00
|
|
|
if (cursor.kind() == CXCursor_ClassTemplate || cursor.kind() == CXCursor_Namespace) {
|
|
|
|
|
if (cursor.unifiedSymbolResolution() == "c:@aN")
|
|
|
|
|
return Utf8String::fromUtf8("(anonymous)");
|
2018-08-07 10:05:31 +02:00
|
|
|
return qualificationPrefix(cursor) + cursor.displayName();
|
2018-08-09 14:52:58 +02:00
|
|
|
}
|
2018-08-07 10:05:31 +02:00
|
|
|
return cursor.type().canonical().spelling();
|
2018-01-19 09:18:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FullTokenInfo::updateTypeSpelling(const Cursor &cursor, bool functionLike)
|
|
|
|
|
{
|
|
|
|
|
m_extraInfo.semanticParentTypeSpelling = fullyQualifiedType(cursor.semanticParent());
|
2018-08-07 10:05:31 +02:00
|
|
|
if (!functionLike) {
|
|
|
|
|
m_extraInfo.typeSpelling = fullyQualifiedType(cursor);
|
2018-01-19 09:18:57 +01:00
|
|
|
return;
|
2018-08-07 10:05:31 +02:00
|
|
|
}
|
|
|
|
|
|
2018-04-19 14:59:22 +02:00
|
|
|
m_extraInfo.token = cursor.displayName();
|
|
|
|
|
// On the client side full type is typeSpelling + token.
|
2018-08-07 10:05:31 +02:00
|
|
|
m_extraInfo.typeSpelling = cursor.type().resultType().utf8Spelling();
|
2018-01-19 09:18:57 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-26 15:12:38 +01:00
|
|
|
static Utf8String propertyParentSpelling(CXTranslationUnit cxTranslationUnit,
|
|
|
|
|
const Utf8String &filePath,
|
|
|
|
|
uint line, uint column)
|
|
|
|
|
{
|
|
|
|
|
// Q_PROPERTY expands into QPropertyMagicFunction which can be found as a child of
|
|
|
|
|
// the containing class.
|
|
|
|
|
Cursor tuCursor = clang_getTranslationUnitCursor(cxTranslationUnit);
|
|
|
|
|
Utf8String parentSpelling;
|
|
|
|
|
tuCursor.visit([&filePath, line, column, &parentSpelling](CXCursor cxCursor, CXCursor parent) {
|
|
|
|
|
const CXCursorKind kind = clang_getCursorKind(cxCursor);
|
|
|
|
|
if (kind == CXCursor_Namespace || kind == CXCursor_StructDecl
|
2018-02-21 09:02:15 +01:00
|
|
|
|| kind == CXCursor_ClassDecl || kind == CXCursor_StaticAssert) {
|
2018-01-26 15:12:38 +01:00
|
|
|
Cursor cursor(cxCursor);
|
|
|
|
|
const SourceRange range = cursor.sourceRange();
|
|
|
|
|
if (range.start().filePath() != filePath)
|
|
|
|
|
return CXChildVisit_Continue;
|
|
|
|
|
if (range.contains(line, column)) {
|
|
|
|
|
if (kind == CXCursor_Namespace || kind == CXCursor_StructDecl
|
|
|
|
|
|| kind == CXCursor_ClassDecl) {
|
|
|
|
|
return CXChildVisit_Recurse;
|
|
|
|
|
}
|
2018-02-21 09:02:15 +01:00
|
|
|
// CXCursor_StaticAssert case. This is Q_PROPERTY static_assert
|
2018-01-26 15:12:38 +01:00
|
|
|
parentSpelling = Cursor(parent).type().spelling();
|
|
|
|
|
return CXChildVisit_Break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return CXChildVisit_Continue;
|
|
|
|
|
});
|
|
|
|
|
return parentSpelling;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-20 12:08:36 +03:00
|
|
|
static Utf8String getPropertyType(const CXSourceLocation &cxLocation,
|
|
|
|
|
CXTranslationUnit cxTranslationUnit,
|
|
|
|
|
uint propertyPosition)
|
2018-01-26 15:12:38 +01:00
|
|
|
{
|
2018-02-20 12:08:36 +03:00
|
|
|
// Extract property type from the source code
|
|
|
|
|
CXFile cxFile;
|
|
|
|
|
uint offset;
|
|
|
|
|
clang_getFileLocation(cxLocation, &cxFile, nullptr, nullptr, &offset);
|
|
|
|
|
const char *const contents = clang_getFileContents(cxTranslationUnit, cxFile, nullptr);
|
|
|
|
|
const char *const lineContents = &contents[offset - propertyPosition];
|
|
|
|
|
|
2018-01-26 15:12:38 +01:00
|
|
|
const char *typeStart = std::strstr(lineContents, "Q_PROPERTY") + 10;
|
|
|
|
|
typeStart += std::strspn(typeStart, "( \t\n\r");
|
|
|
|
|
if (typeStart - lineContents >= propertyPosition)
|
|
|
|
|
return Utf8String();
|
|
|
|
|
auto typeEnd = std::find_if(std::reverse_iterator<const char*>(lineContents + propertyPosition),
|
|
|
|
|
std::reverse_iterator<const char*>(typeStart),
|
|
|
|
|
Utils::unequalTo(' '));
|
|
|
|
|
|
|
|
|
|
return Utf8String(typeStart, static_cast<int>(&(*typeEnd) + 1 - typeStart));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FullTokenInfo::updatePropertyData()
|
|
|
|
|
{
|
2018-02-07 13:59:06 +01:00
|
|
|
CXSourceRange cxRange(clang_getTokenExtent(m_cxTranslationUnit, *m_cxToken));
|
|
|
|
|
const SourceRange range(m_cxTranslationUnit, cxRange);
|
2018-01-26 15:12:38 +01:00
|
|
|
m_extraInfo.semanticParentTypeSpelling = propertyParentSpelling(m_cxTranslationUnit,
|
2018-02-07 13:59:06 +01:00
|
|
|
range.start().filePath(),
|
2018-01-26 15:12:38 +01:00
|
|
|
line(),
|
|
|
|
|
column());
|
2018-02-07 13:59:06 +01:00
|
|
|
m_extraInfo.cursorRange = range;
|
2018-01-26 15:12:38 +01:00
|
|
|
m_extraInfo.declaration = true;
|
|
|
|
|
m_extraInfo.definition = true;
|
2018-02-20 12:08:36 +03:00
|
|
|
m_extraInfo.typeSpelling = getPropertyType(clang_getRangeStart(cxRange),
|
|
|
|
|
m_cxTranslationUnit,
|
|
|
|
|
column() - 1);
|
2018-01-26 15:12:38 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-19 09:18:57 +01:00
|
|
|
void FullTokenInfo::identifierKind(const Cursor &cursor, Recursion recursion)
|
|
|
|
|
{
|
|
|
|
|
updateTypeSpelling(cursor);
|
|
|
|
|
|
|
|
|
|
TokenInfo::identifierKind(cursor, recursion);
|
|
|
|
|
|
|
|
|
|
m_extraInfo.identifier = (cursor.kind() != CXCursor_PreprocessingDirective);
|
2018-02-07 13:59:06 +01:00
|
|
|
|
2018-04-06 14:07:35 +02:00
|
|
|
if (m_types.mainHighlightingType == HighlightingType::QtProperty)
|
2018-01-26 15:12:38 +01:00
|
|
|
updatePropertyData();
|
2018-02-07 13:59:06 +01:00
|
|
|
else
|
|
|
|
|
m_extraInfo.cursorRange = cursor.sourceRange();
|
2018-01-19 09:18:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FullTokenInfo::referencedTypeKind(const Cursor &cursor)
|
|
|
|
|
{
|
|
|
|
|
updateTypeSpelling(cursor.referenced());
|
|
|
|
|
|
|
|
|
|
TokenInfo::referencedTypeKind(cursor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FullTokenInfo::functionKind(const Cursor &cursor, Recursion recursion)
|
|
|
|
|
{
|
|
|
|
|
updateTypeSpelling(cursor, true);
|
|
|
|
|
|
|
|
|
|
TokenInfo::functionKind(cursor, recursion);
|
|
|
|
|
|
|
|
|
|
m_extraInfo.accessSpecifier = cursor.accessSpecifier();
|
|
|
|
|
m_extraInfo.storageClass = cursor.storageClass();
|
|
|
|
|
|
|
|
|
|
bool isSignal = false;
|
|
|
|
|
bool isSlot = false;
|
|
|
|
|
m_originalCursor.visit([&isSignal, &isSlot](CXCursor cursor, CXCursor) {
|
|
|
|
|
Cursor cur(cursor);
|
|
|
|
|
ClangString spelling = cur.spelling();
|
|
|
|
|
if (spelling == "qt_signal")
|
|
|
|
|
isSignal = true;
|
|
|
|
|
else if (spelling == "qt_slot")
|
|
|
|
|
isSlot = true;
|
|
|
|
|
return CXChildVisit_Break;
|
|
|
|
|
});
|
|
|
|
|
m_extraInfo.signal = isSignal;
|
|
|
|
|
m_extraInfo.slot = isSlot;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FullTokenInfo::variableKind(const Cursor &cursor)
|
|
|
|
|
{
|
|
|
|
|
TokenInfo::variableKind(cursor);
|
|
|
|
|
|
|
|
|
|
m_extraInfo.storageClass = cursor.storageClass();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FullTokenInfo::fieldKind(const Cursor &cursor)
|
|
|
|
|
{
|
|
|
|
|
TokenInfo::fieldKind(cursor);
|
|
|
|
|
|
|
|
|
|
m_extraInfo.accessSpecifier = cursor.accessSpecifier();
|
|
|
|
|
m_extraInfo.storageClass = cursor.storageClass();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FullTokenInfo::memberReferenceKind(const Cursor &cursor)
|
|
|
|
|
{
|
|
|
|
|
TokenInfo::memberReferenceKind(cursor);
|
|
|
|
|
if (cursor.isDynamicCall()) {
|
|
|
|
|
m_extraInfo.storageClass = cursor.storageClass();
|
|
|
|
|
m_extraInfo.accessSpecifier = cursor.accessSpecifier();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-05 15:29:10 +01:00
|
|
|
void FullTokenInfo::keywordKind()
|
2018-04-06 14:07:35 +02:00
|
|
|
{
|
2016-11-05 15:29:10 +01:00
|
|
|
TokenInfo::keywordKind();
|
2018-04-06 14:07:35 +02:00
|
|
|
|
2016-11-05 15:29:10 +01:00
|
|
|
CXCursorKind cursorKind = m_originalCursor.kind();
|
2018-04-06 14:07:35 +02:00
|
|
|
bool anonymous = false;
|
2016-11-05 15:29:10 +01:00
|
|
|
if (clang_Cursor_isAnonymous(m_originalCursor.cx())) {
|
2018-04-06 14:07:35 +02:00
|
|
|
anonymous = true;
|
|
|
|
|
} else {
|
2016-11-05 15:29:10 +01:00
|
|
|
const Utf8String type = fullyQualifiedType(m_originalCursor);
|
2018-04-06 14:07:35 +02:00
|
|
|
if (type.endsWith(Utf8StringLiteral(")"))
|
|
|
|
|
&& static_cast<const QByteArray &>(type).indexOf("(anonymous") >= 0) {
|
|
|
|
|
anonymous = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (anonymous) {
|
|
|
|
|
if (cursorKind == CXCursor_EnumDecl)
|
|
|
|
|
m_types.mixinHighlightingTypes.push_back(HighlightingType::Enum);
|
|
|
|
|
else if (cursorKind == CXCursor_ClassDecl)
|
|
|
|
|
m_types.mixinHighlightingTypes.push_back(HighlightingType::Class);
|
|
|
|
|
else if (cursorKind == CXCursor_StructDecl)
|
|
|
|
|
m_types.mixinHighlightingTypes.push_back(HighlightingType::Struct);
|
|
|
|
|
else if (cursorKind == CXCursor_Namespace)
|
|
|
|
|
m_types.mixinHighlightingTypes.push_back(HighlightingType::Namespace);
|
|
|
|
|
m_extraInfo.declaration = m_extraInfo.definition = true;
|
|
|
|
|
m_extraInfo.token = Utf8StringLiteral("anonymous");
|
2016-11-05 15:29:10 +01:00
|
|
|
updateTypeSpelling(m_originalCursor);
|
|
|
|
|
m_extraInfo.cursorRange = m_originalCursor.sourceRange();
|
2018-04-06 14:07:35 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-05 15:29:10 +01:00
|
|
|
void FullTokenInfo::overloadedOperatorKind()
|
|
|
|
|
{
|
|
|
|
|
TokenInfo::overloadedOperatorKind();
|
|
|
|
|
|
2018-08-22 17:25:12 +02:00
|
|
|
if (!m_types.mixinHighlightingTypes.contains(HighlightingType::OverloadedOperator))
|
2016-11-05 15:29:10 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Overloaded operator
|
|
|
|
|
m_extraInfo.identifier = true;
|
|
|
|
|
if (!m_originalCursor.isDeclaration())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Overloaded operator declaration
|
|
|
|
|
m_extraInfo.declaration = true;
|
|
|
|
|
m_extraInfo.definition = m_originalCursor.isDefinition();
|
|
|
|
|
|
|
|
|
|
updateTypeSpelling(m_originalCursor, true);
|
|
|
|
|
m_extraInfo.cursorRange = m_originalCursor.sourceRange();
|
|
|
|
|
m_extraInfo.accessSpecifier = m_originalCursor.accessSpecifier();
|
|
|
|
|
m_extraInfo.storageClass = m_originalCursor.storageClass();
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-19 09:18:57 +01:00
|
|
|
void FullTokenInfo::evaluate()
|
|
|
|
|
{
|
|
|
|
|
m_extraInfo.token = ClangString(clang_getTokenSpelling(m_cxTranslationUnit, *m_cxToken));
|
|
|
|
|
|
|
|
|
|
auto cxTokenKind = clang_getTokenKind(*m_cxToken);
|
|
|
|
|
if (cxTokenKind == CXToken_Identifier) {
|
|
|
|
|
m_extraInfo.declaration = m_originalCursor.isDeclaration();
|
|
|
|
|
m_extraInfo.definition = m_originalCursor.isDefinition();
|
|
|
|
|
}
|
|
|
|
|
m_extraInfo.includeDirectivePath = (m_originalCursor.kind() == CXCursor_InclusionDirective);
|
2018-01-26 15:12:38 +01:00
|
|
|
|
|
|
|
|
TokenInfo::evaluate();
|
2018-01-19 09:18:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace ClangBackEnd
|