Clang: Properly handle Q_PROPERTY in TokenInfo

Transform Q_PROPERTY into unique AST node.
Mark different parts with types and search for parent
in FullTokenInfos.

Change-Id: Iaa1ec0c73d34773edf5605d3682bd6a290d195de
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
This commit is contained in:
Ivan Donchevskii
2018-01-26 15:12:38 +01:00
parent b5f63a76b2
commit 66c7629814
15 changed files with 235 additions and 27 deletions

View File

@@ -26,6 +26,9 @@
#include "clangstring.h"
#include "cursor.h"
#include "fulltokeninfo.h"
#include "sourcerange.h"
#include <utils/predicates.h>
namespace ClangBackEnd {
@@ -72,6 +75,73 @@ void FullTokenInfo::updateTypeSpelling(const Cursor &cursor, bool functionLike)
+ (hasSpaceAfterReturnType ? 1 : 0));
}
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
|| kind == CXCursor_ClassDecl || kind == CXCursor_CXXMethod) {
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;
}
// CXCursor_CXXMethod case. This is Q_PROPERTY_MAGIC_FUNCTION
parentSpelling = Cursor(parent).type().spelling();
return CXChildVisit_Break;
}
}
return CXChildVisit_Continue;
});
return parentSpelling;
}
static Utf8String getPropertyType(const char *const lineContents, uint propertyPosition)
{
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()
{
CXSourceLocation cxLocation(clang_getTokenLocation(m_cxTranslationUnit, *m_cxToken));
const SourceLocation location(m_cxTranslationUnit, cxLocation);
m_extraInfo.semanticParentTypeSpelling = propertyParentSpelling(m_cxTranslationUnit,
location.filePath(),
line(),
column());
m_extraInfo.declaration = true;
m_extraInfo.definition = true;
#if defined(CINDEX_VERSION_HAS_GETFILECONTENTS_BACKPORTED) || CINDEX_VERSION_MINOR >= 47
// Extract property type from the source code
CXFile cxFile;
uint offset;
clang_getFileLocation(cxLocation, &cxFile, nullptr, nullptr, &offset);
const uint propertyPosition = column() - 1;
const char *const contents = clang_getFileContents(m_cxTranslationUnit, cxFile, nullptr);
const char *const lineContents = &contents[offset - propertyPosition];
m_extraInfo.typeSpelling = getPropertyType(lineContents, propertyPosition);
#endif
}
void FullTokenInfo::identifierKind(const Cursor &cursor, Recursion recursion)
{
updateTypeSpelling(cursor);
@@ -79,6 +149,8 @@ void FullTokenInfo::identifierKind(const Cursor &cursor, Recursion recursion)
TokenInfo::identifierKind(cursor, recursion);
m_extraInfo.identifier = (cursor.kind() != CXCursor_PreprocessingDirective);
if (types().mainHighlightingType == HighlightingType::QtProperty)
updatePropertyData();
}
void FullTokenInfo::referencedTypeKind(const Cursor &cursor)
@@ -138,8 +210,6 @@ void FullTokenInfo::memberReferenceKind(const Cursor &cursor)
void FullTokenInfo::evaluate()
{
TokenInfo::evaluate();
m_extraInfo.token = ClangString(clang_getTokenSpelling(m_cxTranslationUnit, *m_cxToken));
auto cxTokenKind = clang_getTokenKind(*m_cxToken);
@@ -148,6 +218,8 @@ void FullTokenInfo::evaluate()
m_extraInfo.definition = m_originalCursor.isDefinition();
}
m_extraInfo.includeDirectivePath = (m_originalCursor.kind() == CXCursor_InclusionDirective);
TokenInfo::evaluate();
}
} // namespace ClangBackEnd

View File

@@ -48,6 +48,7 @@ protected:
void memberReferenceKind(const Cursor &cursor) override;
private:
void updateTypeSpelling(const Cursor &cursor, bool functionLike = false);
void updatePropertyData();
ExtraInfo m_extraInfo;
};

View File

@@ -40,6 +40,7 @@ class SourceLocation
friend class SourceRange;
friend class TranslationUnit;
friend class Cursor;
friend class FullTokenInfo;
friend bool operator==(const SourceLocation &first, const SourceLocation &second);
public:

View File

@@ -69,10 +69,13 @@ bool SourceRange::contains(unsigned line, unsigned column) const
const SourceLocation start_ = start();
const SourceLocation end_ = end();
return start_.line() <= line
&& start_.column() <= column
&& line <= end_.line()
&& column <= end_.column();
if (line < start_.line() || line > end_.line())
return false;
if (line == start_.line() && column < start_.column())
return false;
if (line == end_.line() && column > end_.column())
return false;
return true;
}
SourceRangeContainer SourceRange::toSourceRangeContainer() const

View File

@@ -32,7 +32,7 @@
#include <utils/qtcfallthrough.h>
#include <QDebug>
#include <array>
namespace ClangBackEnd {
@@ -421,6 +421,9 @@ void TokenInfo::identifierKind(const Cursor &cursor, Recursion recursion)
case CXCursor_LabelStmt:
m_types.mainHighlightingType = HighlightingType::Label;
break;
case CXCursor_InvalidFile:
invalidFileKind();
break;
default:
break;
}
@@ -466,10 +469,15 @@ HighlightingType TokenInfo::punctuationKind(const Cursor &cursor)
HighlightingType highlightingType = HighlightingType::Invalid;
switch (cursor.kind()) {
case CXCursor_DeclRefExpr: highlightingType = operatorKind(cursor); break;
case CXCursor_DeclRefExpr:
highlightingType = operatorKind(cursor);
break;
case CXCursor_Constructor:
case CXCursor_CallExpr: collectOutputArguments(cursor); break;
default: break;
case CXCursor_CallExpr:
collectOutputArguments(cursor);
break;
default:
break;
}
if (isOutputArgument())
@@ -478,6 +486,75 @@ HighlightingType TokenInfo::punctuationKind(const Cursor &cursor)
return highlightingType;
}
enum class PropertyPart
{
None,
Type,
Property,
Keyword,
FunctionOrPrimitiveType
};
static PropertyPart propertyPart(CXTranslationUnit tu, CXToken *token)
{
static constexpr const char *propertyKeywords[]
= {"READ", "WRITE", "MEMBER", "RESET", "NOTIFY", "REVISION", "DESIGNABLE",
"SCRIPTABLE", "STORED", "USER", "CONSTANT", "FINAL"
};
CXSourceLocation location = clang_getTokenLocation(tu, *token);
// If current token is inside Q_PROPERTY then the cursor from token's position will be
// the whole Q_PROPERTY macro cursor.
Cursor possibleQPropertyCursor = clang_getCursor(tu, location);
if (!(possibleQPropertyCursor.spelling() == "Q_PROPERTY"))
return PropertyPart::None;
const ClangString currentToken = clang_getTokenSpelling(tu, *token);
if (std::find(std::begin(propertyKeywords), std::end(propertyKeywords), currentToken)
!= std::end(propertyKeywords)) {
return PropertyPart::Keyword;
}
const ClangString nextToken = clang_getTokenSpelling(tu, *(token + 1));
const ClangString previousToken = clang_getTokenSpelling(tu, *(token - 1));
if (std::find(std::begin(propertyKeywords), std::end(propertyKeywords), nextToken)
!= std::end(propertyKeywords)) {
if (std::find(std::begin(propertyKeywords), std::end(propertyKeywords), previousToken)
== std::end(propertyKeywords)) {
return PropertyPart::Property;
}
return PropertyPart::FunctionOrPrimitiveType;
}
if (std::find(std::begin(propertyKeywords), std::end(propertyKeywords), previousToken)
!= std::end(propertyKeywords)) {
return PropertyPart::FunctionOrPrimitiveType;
}
return PropertyPart::Type;
}
void TokenInfo::invalidFileKind()
{
const PropertyPart propPart = propertyPart(m_cxTranslationUnit, m_cxToken);
switch (propPart) {
case PropertyPart::None:
case PropertyPart::Keyword:
m_types.mainHighlightingType = HighlightingType::Invalid;
return;
case PropertyPart::Property:
m_types.mainHighlightingType = HighlightingType::QtProperty;
return;
case PropertyPart::Type:
m_types.mainHighlightingType = HighlightingType::Type;
return;
case PropertyPart::FunctionOrPrimitiveType:
m_types.mainHighlightingType = HighlightingType::Function;
return;
}
}
static HighlightingType highlightingTypeForKeyword(CXTranslationUnit cxTranslationUnit,
CXToken *cxToken,
const Cursor &cursor)

View File

@@ -94,6 +94,7 @@ protected:
virtual void memberReferenceKind(const Cursor &cursor);
virtual HighlightingType punctuationKind(const Cursor &cursor);
virtual void typeKind(const Cursor &cursor);
virtual void invalidFileKind();
Cursor m_originalCursor;
CXToken *m_cxToken;