2015-11-17 13:33:31 +01:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2016-01-15 14:57:14 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2015-11-17 13:33:31 +01:00
|
|
|
**
|
|
|
|
|
** 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
|
2016-01-15 14:57:14 +01:00
|
|
|
** 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.
|
2015-11-17 13:33:31 +01:00
|
|
|
**
|
2016-01-15 14:57:14 +01:00
|
|
|
** 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.
|
2015-11-17 13:33:31 +01:00
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2015-11-18 17:07:44 +01:00
|
|
|
#include <highlightingmarkcontainer.h>
|
|
|
|
|
|
2015-11-17 13:33:31 +01:00
|
|
|
#include "clangstring.h"
|
|
|
|
|
#include "cursor.h"
|
2016-02-29 13:09:39 +01:00
|
|
|
#include "highlightingmark.h"
|
2015-11-17 13:33:31 +01:00
|
|
|
#include "sourcelocation.h"
|
|
|
|
|
#include "sourcerange.h"
|
|
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
#include <ostream>
|
|
|
|
|
|
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
|
|
namespace ClangBackEnd {
|
|
|
|
|
|
2016-02-29 13:09:39 +01:00
|
|
|
HighlightingMark::HighlightingMark(const CXCursor &cxCursor,
|
2015-11-17 13:33:31 +01:00
|
|
|
CXToken *cxToken,
|
|
|
|
|
CXTranslationUnit cxTranslationUnit)
|
|
|
|
|
{
|
|
|
|
|
const SourceRange sourceRange = clang_getTokenExtent(cxTranslationUnit, *cxToken);
|
|
|
|
|
const auto start = sourceRange.start();
|
|
|
|
|
const auto end = sourceRange.end();
|
|
|
|
|
|
|
|
|
|
originalCursor = cxCursor;
|
|
|
|
|
line = start.line();
|
|
|
|
|
column = start.column();
|
|
|
|
|
length = end.offset() - start.offset();
|
2016-03-03 13:02:29 +01:00
|
|
|
collectKinds(cxToken, originalCursor);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HighlightingMark::HighlightingMark(uint line, uint column, uint length, HighlightingTypes types)
|
|
|
|
|
: line(line),
|
|
|
|
|
column(column),
|
|
|
|
|
length(length),
|
|
|
|
|
types(types)
|
|
|
|
|
{
|
2015-11-17 13:33:31 +01:00
|
|
|
}
|
|
|
|
|
|
2016-02-29 13:09:39 +01:00
|
|
|
HighlightingMark::HighlightingMark(uint line, uint column, uint length, HighlightingType type)
|
2015-11-17 13:33:31 +01:00
|
|
|
: line(line),
|
|
|
|
|
column(column),
|
|
|
|
|
length(length),
|
2016-03-03 13:02:29 +01:00
|
|
|
types(HighlightingTypes())
|
|
|
|
|
{
|
|
|
|
|
types.mainHighlightingType = type;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool HighlightingMark::hasInvalidMainType() const
|
|
|
|
|
{
|
|
|
|
|
return types.mainHighlightingType == HighlightingType::Invalid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool HighlightingMark::hasMainType(HighlightingType type) const
|
2015-11-17 13:33:31 +01:00
|
|
|
{
|
2016-03-03 13:02:29 +01:00
|
|
|
return types.mainHighlightingType == type;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool HighlightingMark::hasMixinType(HighlightingType type) const
|
|
|
|
|
{
|
|
|
|
|
auto found = std::find(types.mixinHighlightingTypes.begin(),
|
|
|
|
|
types.mixinHighlightingTypes.end(),
|
|
|
|
|
type);
|
|
|
|
|
|
|
|
|
|
return found != types.mixinHighlightingTypes.end();
|
2015-11-17 13:33:31 +01:00
|
|
|
}
|
|
|
|
|
|
2016-03-03 13:02:29 +01:00
|
|
|
bool HighlightingMark::hasOnlyType(HighlightingType type) const
|
2015-11-17 13:33:31 +01:00
|
|
|
{
|
2016-03-03 13:02:29 +01:00
|
|
|
return types.mixinHighlightingTypes.size() == 0 && hasMainType(type);
|
2015-11-17 13:33:31 +01:00
|
|
|
}
|
|
|
|
|
|
2016-02-29 13:09:39 +01:00
|
|
|
bool HighlightingMark::hasFunctionArguments() const
|
2015-11-17 13:33:31 +01:00
|
|
|
{
|
|
|
|
|
return originalCursor.argumentCount() > 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-29 13:09:39 +01:00
|
|
|
HighlightingMark::operator HighlightingMarkContainer() const
|
2015-11-18 17:07:44 +01:00
|
|
|
{
|
2016-03-03 13:02:29 +01:00
|
|
|
return HighlightingMarkContainer(line, column, length, types);
|
2015-11-18 17:07:44 +01:00
|
|
|
}
|
|
|
|
|
|
2015-11-17 13:33:31 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
bool isFinalFunction(const Cursor &cursor)
|
|
|
|
|
{
|
|
|
|
|
auto referencedCursor = cursor.referenced();
|
|
|
|
|
if (referencedCursor.hasFinalFunctionAttribute())
|
|
|
|
|
return true;
|
2016-03-03 13:02:29 +01:00
|
|
|
else
|
|
|
|
|
return false;
|
2015-11-17 13:33:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool isFunctionInFinalClass(const Cursor &cursor)
|
|
|
|
|
{
|
|
|
|
|
auto functionBase = cursor.functionBaseDeclaration();
|
|
|
|
|
if (functionBase.isValid() && functionBase.hasFinalClassAttribute())
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-03 13:02:29 +01:00
|
|
|
void HighlightingMark::memberReferenceKind(const Cursor &cursor)
|
2015-11-17 13:33:31 +01:00
|
|
|
{
|
|
|
|
|
if (cursor.isDynamicCall()) {
|
|
|
|
|
if (isFinalFunction(cursor) || isFunctionInFinalClass(cursor))
|
2016-03-03 13:02:29 +01:00
|
|
|
types.mainHighlightingType = HighlightingType::Function;
|
2015-11-17 13:33:31 +01:00
|
|
|
else
|
2016-03-03 13:02:29 +01:00
|
|
|
types.mainHighlightingType = HighlightingType::VirtualFunction;
|
|
|
|
|
} else {
|
|
|
|
|
identifierKind(cursor.referenced(), Recursion::RecursivePass);
|
2015-11-17 13:33:31 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-03 13:02:29 +01:00
|
|
|
void HighlightingMark::referencedTypeKind(const Cursor &cursor)
|
2015-11-17 13:33:31 +01:00
|
|
|
{
|
|
|
|
|
const Cursor referencedCursor = cursor.referenced();
|
|
|
|
|
|
|
|
|
|
switch (referencedCursor.kind()) {
|
|
|
|
|
case CXCursor_ClassDecl:
|
|
|
|
|
case CXCursor_StructDecl:
|
|
|
|
|
case CXCursor_UnionDecl:
|
2015-12-02 20:57:53 +01:00
|
|
|
case CXCursor_TypedefDecl:
|
2015-11-17 13:33:31 +01:00
|
|
|
case CXCursor_TemplateTypeParameter:
|
2015-12-07 14:27:38 +01:00
|
|
|
case CXCursor_TypeAliasDecl:
|
2016-03-03 13:02:29 +01:00
|
|
|
case CXCursor_EnumDecl: types.mainHighlightingType = HighlightingType::Type; break;
|
|
|
|
|
default: types.mainHighlightingType = HighlightingType::Invalid; break;
|
2015-11-17 13:33:31 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-03 13:02:29 +01:00
|
|
|
void HighlightingMark::variableKind(const Cursor &cursor)
|
2015-11-17 13:33:31 +01:00
|
|
|
{
|
|
|
|
|
if (cursor.isLocalVariable())
|
2016-03-03 13:02:29 +01:00
|
|
|
types.mainHighlightingType = HighlightingType::LocalVariable;
|
2015-11-17 13:33:31 +01:00
|
|
|
else
|
2016-03-03 13:02:29 +01:00
|
|
|
types.mainHighlightingType = HighlightingType::GlobalVariable;
|
2015-11-17 13:33:31 +01:00
|
|
|
}
|
|
|
|
|
|
2016-02-29 13:09:39 +01:00
|
|
|
bool HighlightingMark::isVirtualMethodDeclarationOrDefinition(const Cursor &cursor) const
|
2015-11-17 13:33:31 +01:00
|
|
|
{
|
|
|
|
|
return cursor.isVirtualMethod()
|
|
|
|
|
&& (originalCursor.isDeclaration() || originalCursor.isDefinition());
|
|
|
|
|
}
|
|
|
|
|
namespace {
|
|
|
|
|
bool isNotFinalFunction(const Cursor &cursor)
|
|
|
|
|
{
|
|
|
|
|
return !cursor.hasFinalFunctionAttribute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2016-02-29 13:09:39 +01:00
|
|
|
bool HighlightingMark::isRealDynamicCall(const Cursor &cursor) const
|
2015-11-17 13:33:31 +01:00
|
|
|
{
|
|
|
|
|
return originalCursor.isDynamicCall() && isNotFinalFunction(cursor);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-03 13:02:29 +01:00
|
|
|
void HighlightingMark::addExtraTypeIfFirstPass(HighlightingType type,
|
|
|
|
|
Recursion recursion)
|
|
|
|
|
{
|
|
|
|
|
if (recursion == Recursion::FirstPass)
|
|
|
|
|
types.mixinHighlightingTypes.push_back(type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HighlightingMark::functionKind(const Cursor &cursor, Recursion recursion)
|
2015-11-17 13:33:31 +01:00
|
|
|
{
|
|
|
|
|
if (isRealDynamicCall(cursor) || isVirtualMethodDeclarationOrDefinition(cursor))
|
2016-03-03 13:02:29 +01:00
|
|
|
types.mainHighlightingType = HighlightingType::VirtualFunction;
|
2015-11-17 13:33:31 +01:00
|
|
|
else
|
2016-03-03 13:02:29 +01:00
|
|
|
types.mainHighlightingType = HighlightingType::Function;
|
|
|
|
|
|
|
|
|
|
addExtraTypeIfFirstPass(HighlightingType::Declaration, recursion);
|
2015-11-17 13:33:31 +01:00
|
|
|
}
|
|
|
|
|
|
2016-03-03 13:02:29 +01:00
|
|
|
void HighlightingMark::identifierKind(const Cursor &cursor, Recursion recursion)
|
2015-11-17 13:33:31 +01:00
|
|
|
{
|
|
|
|
|
switch (cursor.kind()) {
|
|
|
|
|
case CXCursor_Destructor:
|
|
|
|
|
case CXCursor_Constructor:
|
|
|
|
|
case CXCursor_FunctionDecl:
|
|
|
|
|
case CXCursor_CallExpr:
|
2016-03-03 13:02:29 +01:00
|
|
|
case CXCursor_CXXMethod: functionKind(cursor, recursion); break;
|
2015-11-17 13:33:31 +01:00
|
|
|
case CXCursor_NonTypeTemplateParameter:
|
2016-03-03 13:02:29 +01:00
|
|
|
case CXCursor_ParmDecl: types.mainHighlightingType = HighlightingType::LocalVariable; break;
|
|
|
|
|
case CXCursor_VarDecl: variableKind(cursor); break;
|
|
|
|
|
case CXCursor_DeclRefExpr: identifierKind(cursor.referenced(), Recursion::RecursivePass); break;
|
|
|
|
|
case CXCursor_MemberRefExpr: memberReferenceKind(cursor); break;
|
2015-11-17 13:33:31 +01:00
|
|
|
case CXCursor_FieldDecl:
|
|
|
|
|
case CXCursor_MemberRef:
|
|
|
|
|
case CXCursor_ObjCIvarDecl:
|
|
|
|
|
case CXCursor_ObjCPropertyDecl:
|
|
|
|
|
case CXCursor_ObjCClassMethodDecl:
|
|
|
|
|
case CXCursor_ObjCInstanceMethodDecl:
|
|
|
|
|
case CXCursor_ObjCSynthesizeDecl:
|
2016-03-03 13:02:29 +01:00
|
|
|
case CXCursor_ObjCDynamicDecl: types.mainHighlightingType = HighlightingType::Field; break;
|
|
|
|
|
case CXCursor_TypeRef: referencedTypeKind(cursor); break;
|
2015-11-17 13:33:31 +01:00
|
|
|
case CXCursor_ClassDecl:
|
|
|
|
|
case CXCursor_TemplateTypeParameter:
|
|
|
|
|
case CXCursor_TemplateTemplateParameter:
|
|
|
|
|
case CXCursor_UnionDecl:
|
|
|
|
|
case CXCursor_StructDecl:
|
2015-12-09 15:16:55 +01:00
|
|
|
case CXCursor_OverloadedDeclRef:
|
2015-11-17 13:33:31 +01:00
|
|
|
case CXCursor_TemplateRef:
|
|
|
|
|
case CXCursor_Namespace:
|
|
|
|
|
case CXCursor_NamespaceRef:
|
|
|
|
|
case CXCursor_NamespaceAlias:
|
|
|
|
|
case CXCursor_TypeAliasDecl:
|
2015-12-02 20:57:53 +01:00
|
|
|
case CXCursor_TypedefDecl:
|
2015-11-17 13:33:31 +01:00
|
|
|
case CXCursor_ClassTemplate:
|
2015-12-07 14:27:38 +01:00
|
|
|
case CXCursor_EnumDecl:
|
2015-11-17 13:33:31 +01:00
|
|
|
case CXCursor_CXXStaticCastExpr:
|
|
|
|
|
case CXCursor_CXXReinterpretCastExpr:
|
|
|
|
|
case CXCursor_ObjCCategoryDecl:
|
|
|
|
|
case CXCursor_ObjCCategoryImplDecl:
|
|
|
|
|
case CXCursor_ObjCImplementationDecl:
|
|
|
|
|
case CXCursor_ObjCInterfaceDecl:
|
|
|
|
|
case CXCursor_ObjCProtocolDecl:
|
|
|
|
|
case CXCursor_ObjCProtocolRef:
|
|
|
|
|
case CXCursor_ObjCClassRef:
|
2016-03-03 13:02:29 +01:00
|
|
|
case CXCursor_ObjCSuperClassRef: types.mainHighlightingType = HighlightingType::Type; break;
|
|
|
|
|
case CXCursor_FunctionTemplate: types.mainHighlightingType = HighlightingType::Function; break;
|
|
|
|
|
case CXCursor_EnumConstantDecl: types.mainHighlightingType = HighlightingType::Enumeration; break;
|
|
|
|
|
case CXCursor_PreprocessingDirective: types.mainHighlightingType = HighlightingType::Preprocessor; break;
|
|
|
|
|
case CXCursor_MacroExpansion: types.mainHighlightingType = HighlightingType::PreprocessorExpansion; break;
|
|
|
|
|
case CXCursor_MacroDefinition: types.mainHighlightingType = HighlightingType::PreprocessorDefinition; break;
|
|
|
|
|
case CXCursor_InclusionDirective: types.mainHighlightingType = HighlightingType::StringLiteral; break;
|
2015-11-17 13:33:31 +01:00
|
|
|
case CXCursor_LabelRef:
|
2016-03-03 13:02:29 +01:00
|
|
|
case CXCursor_LabelStmt: types.mainHighlightingType = HighlightingType::Label; break;
|
|
|
|
|
default: break;
|
2015-11-17 13:33:31 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
HighlightingType literalKind(const Cursor &cursor)
|
|
|
|
|
{
|
|
|
|
|
switch (cursor.kind()) {
|
|
|
|
|
case CXCursor_CharacterLiteral:
|
|
|
|
|
case CXCursor_StringLiteral:
|
|
|
|
|
case CXCursor_ObjCStringLiteral: return HighlightingType::StringLiteral;
|
|
|
|
|
case CXCursor_IntegerLiteral:
|
|
|
|
|
case CXCursor_ImaginaryLiteral:
|
|
|
|
|
case CXCursor_FloatingLiteral: return HighlightingType::NumberLiteral;
|
|
|
|
|
default: return HighlightingType::Invalid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Q_UNREACHABLE();
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-18 19:09:38 +01:00
|
|
|
bool hasOperatorName(const char *operatorString)
|
|
|
|
|
{
|
|
|
|
|
return std::strncmp(operatorString, "operator", 8) == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HighlightingType operatorKind(const Cursor &cursor)
|
|
|
|
|
{
|
|
|
|
|
if (hasOperatorName(cursor.spelling().cString()))
|
|
|
|
|
return HighlightingType::Operator;
|
|
|
|
|
else
|
|
|
|
|
return HighlightingType::Invalid;
|
|
|
|
|
}
|
2015-11-17 13:33:31 +01:00
|
|
|
|
|
|
|
|
HighlightingType punctationKind(const Cursor &cursor)
|
|
|
|
|
{
|
|
|
|
|
switch (cursor.kind()) {
|
2015-11-18 19:09:38 +01:00
|
|
|
case CXCursor_DeclRefExpr: return operatorKind(cursor);
|
2015-11-17 13:33:31 +01:00
|
|
|
default: return HighlightingType::Invalid;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2016-03-03 13:02:29 +01:00
|
|
|
void HighlightingMark::collectKinds(CXToken *cxToken, const Cursor &cursor)
|
2015-11-17 13:33:31 +01:00
|
|
|
{
|
|
|
|
|
auto cxTokenKind = clang_getTokenKind(*cxToken);
|
|
|
|
|
|
2016-03-07 14:28:33 +01:00
|
|
|
types = HighlightingTypes();
|
2016-03-03 13:02:29 +01:00
|
|
|
|
2015-11-17 13:33:31 +01:00
|
|
|
switch (cxTokenKind) {
|
2016-03-03 13:02:29 +01:00
|
|
|
case CXToken_Keyword: types.mainHighlightingType = HighlightingType::Keyword; break;
|
|
|
|
|
case CXToken_Punctuation: types.mainHighlightingType = punctationKind(cursor); break;
|
|
|
|
|
case CXToken_Identifier: identifierKind(cursor, Recursion::FirstPass); break;
|
|
|
|
|
case CXToken_Comment: types.mainHighlightingType = HighlightingType::Comment; break;
|
|
|
|
|
case CXToken_Literal: types.mainHighlightingType = literalKind(cursor); break;
|
2015-11-17 13:33:31 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-29 13:09:39 +01:00
|
|
|
void PrintTo(const HighlightingMark &information, ::std::ostream *os)
|
2015-11-17 13:33:31 +01:00
|
|
|
{
|
|
|
|
|
*os << "type: ";
|
2016-03-03 13:02:29 +01:00
|
|
|
PrintTo(information.types, os);
|
2015-11-17 13:33:31 +01:00
|
|
|
*os << " line: " << information.line
|
|
|
|
|
<< " column: " << information.column
|
|
|
|
|
<< " length: " << information.length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace ClangBackEnd
|