Clang: Add field prefix for HighlightingMark

Change-Id: I6cbd091f65ed931fbc31707c09b832ad782de03f
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
This commit is contained in:
Marco Bubke
2017-07-20 16:43:16 +02:00
parent 05a77a39a1
commit edbb656f2d
2 changed files with 84 additions and 84 deletions

View File

@@ -43,69 +43,69 @@ HighlightingMark::HighlightingMark(const CXCursor &cxCursor,
CXToken *cxToken, CXToken *cxToken,
CXTranslationUnit cxTranslationUnit, CXTranslationUnit cxTranslationUnit,
std::vector<CXSourceRange> &currentOutputArgumentRanges) std::vector<CXSourceRange> &currentOutputArgumentRanges)
: currentOutputArgumentRanges(&currentOutputArgumentRanges), : m_currentOutputArgumentRanges(&currentOutputArgumentRanges),
originalCursor(cxCursor) m_originalCursor(cxCursor)
{ {
const SourceRange sourceRange = clang_getTokenExtent(cxTranslationUnit, *cxToken); const SourceRange sourceRange = clang_getTokenExtent(cxTranslationUnit, *cxToken);
const auto start = sourceRange.start(); const auto start = sourceRange.start();
const auto end = sourceRange.end(); const auto end = sourceRange.end();
line = start.line(); m_line = start.line();
column = start.column(); m_column = start.column();
offset = start.offset(); m_offset = start.offset();
length = end.offset() - start.offset(); m_length = end.offset() - start.offset();
collectKinds(cxTranslationUnit, cxToken, originalCursor); collectKinds(cxTranslationUnit, cxToken, m_originalCursor);
} }
HighlightingMark::HighlightingMark(uint line, uint column, uint length, HighlightingTypes types) HighlightingMark::HighlightingMark(uint line, uint column, uint length, HighlightingTypes types)
: line(line), : m_line(line),
column(column), m_column(column),
length(length), m_length(length),
types(types) m_types(types)
{ {
} }
HighlightingMark::HighlightingMark(uint line, uint column, uint length, HighlightingType type) HighlightingMark::HighlightingMark(uint line, uint column, uint length, HighlightingType type)
: line(line), : m_line(line),
column(column), m_column(column),
length(length), m_length(length),
types(HighlightingTypes()) m_types(HighlightingTypes())
{ {
types.mainHighlightingType = type; m_types.mainHighlightingType = type;
} }
bool HighlightingMark::hasInvalidMainType() const bool HighlightingMark::hasInvalidMainType() const
{ {
return types.mainHighlightingType == HighlightingType::Invalid; return m_types.mainHighlightingType == HighlightingType::Invalid;
} }
bool HighlightingMark::hasMainType(HighlightingType type) const bool HighlightingMark::hasMainType(HighlightingType type) const
{ {
return types.mainHighlightingType == type; return m_types.mainHighlightingType == type;
} }
bool HighlightingMark::hasMixinType(HighlightingType type) const bool HighlightingMark::hasMixinType(HighlightingType type) const
{ {
auto found = std::find(types.mixinHighlightingTypes.begin(), auto found = std::find(m_types.mixinHighlightingTypes.begin(),
types.mixinHighlightingTypes.end(), m_types.mixinHighlightingTypes.end(),
type); type);
return found != types.mixinHighlightingTypes.end(); return found != m_types.mixinHighlightingTypes.end();
} }
bool HighlightingMark::hasOnlyType(HighlightingType type) const bool HighlightingMark::hasOnlyType(HighlightingType type) const
{ {
return types.mixinHighlightingTypes.size() == 0 && hasMainType(type); return m_types.mixinHighlightingTypes.size() == 0 && hasMainType(type);
} }
bool HighlightingMark::hasFunctionArguments() const bool HighlightingMark::hasFunctionArguments() const
{ {
return originalCursor.argumentCount() > 0; return m_originalCursor.argumentCount() > 0;
} }
HighlightingMark::operator HighlightingMarkContainer() const HighlightingMark::operator HighlightingMarkContainer() const
{ {
return HighlightingMarkContainer(line, column, length, types); return HighlightingMarkContainer(m_line, m_column, m_length, m_types);
} }
namespace { namespace {
@@ -133,9 +133,9 @@ void HighlightingMark::memberReferenceKind(const Cursor &cursor)
{ {
if (cursor.isDynamicCall()) { if (cursor.isDynamicCall()) {
if (isFinalFunction(cursor) || isFunctionInFinalClass(cursor)) if (isFinalFunction(cursor) || isFunctionInFinalClass(cursor))
types.mainHighlightingType = HighlightingType::Function; m_types.mainHighlightingType = HighlightingType::Function;
else else
types.mainHighlightingType = HighlightingType::VirtualFunction; m_types.mainHighlightingType = HighlightingType::VirtualFunction;
} else { } else {
identifierKind(cursor.referenced(), Recursion::RecursivePass); identifierKind(cursor.referenced(), Recursion::RecursivePass);
} }
@@ -152,47 +152,47 @@ void HighlightingMark::referencedTypeKind(const Cursor &cursor)
case CXCursor_TypedefDecl: case CXCursor_TypedefDecl:
case CXCursor_TemplateTypeParameter: case CXCursor_TemplateTypeParameter:
case CXCursor_TypeAliasDecl: case CXCursor_TypeAliasDecl:
case CXCursor_EnumDecl: types.mainHighlightingType = HighlightingType::Type; break; case CXCursor_EnumDecl: m_types.mainHighlightingType = HighlightingType::Type; break;
default: types.mainHighlightingType = HighlightingType::Invalid; break; default: m_types.mainHighlightingType = HighlightingType::Invalid; break;
} }
} }
void HighlightingMark::overloadedDeclRefKind(const Cursor &cursor) void HighlightingMark::overloadedDeclRefKind(const Cursor &cursor)
{ {
types.mainHighlightingType = HighlightingType::Function; m_types.mainHighlightingType = HighlightingType::Function;
// CLANG-UPGRADE-CHECK: Workaround still needed? // CLANG-UPGRADE-CHECK: Workaround still needed?
// Workaround https://bugs.llvm.org//show_bug.cgi?id=33256 - SomeType in // Workaround https://bugs.llvm.org//show_bug.cgi?id=33256 - SomeType in
// "using N::SomeType" is mistakenly considered as a CXCursor_OverloadedDeclRef. // "using N::SomeType" is mistakenly considered as a CXCursor_OverloadedDeclRef.
if (cursor.overloadedDeclarationsCount() >= 1 if (cursor.overloadedDeclarationsCount() >= 1
&& cursor.overloadedDeclaration(0).kind() != CXCursor_FunctionDecl) { && cursor.overloadedDeclaration(0).kind() != CXCursor_FunctionDecl) {
types.mainHighlightingType = HighlightingType::Type; m_types.mainHighlightingType = HighlightingType::Type;
} }
} }
void HighlightingMark::variableKind(const Cursor &cursor) void HighlightingMark::variableKind(const Cursor &cursor)
{ {
if (cursor.isLocalVariable()) if (cursor.isLocalVariable())
types.mainHighlightingType = HighlightingType::LocalVariable; m_types.mainHighlightingType = HighlightingType::LocalVariable;
else else
types.mainHighlightingType = HighlightingType::GlobalVariable; m_types.mainHighlightingType = HighlightingType::GlobalVariable;
if (isOutputArgument()) if (isOutputArgument())
types.mixinHighlightingTypes.push_back(HighlightingType::OutputArgument); m_types.mixinHighlightingTypes.push_back(HighlightingType::OutputArgument);
} }
void HighlightingMark::fieldKind(const Cursor &) void HighlightingMark::fieldKind(const Cursor &)
{ {
types.mainHighlightingType = HighlightingType::Field; m_types.mainHighlightingType = HighlightingType::Field;
if (isOutputArgument()) if (isOutputArgument())
types.mixinHighlightingTypes.push_back(HighlightingType::OutputArgument); m_types.mixinHighlightingTypes.push_back(HighlightingType::OutputArgument);
} }
bool HighlightingMark::isVirtualMethodDeclarationOrDefinition(const Cursor &cursor) const bool HighlightingMark::isVirtualMethodDeclarationOrDefinition(const Cursor &cursor) const
{ {
return cursor.isVirtualMethod() return cursor.isVirtualMethod()
&& (originalCursor.isDeclaration() || originalCursor.isDefinition()); && (m_originalCursor.isDeclaration() || m_originalCursor.isDefinition());
} }
namespace { namespace {
bool isNotFinalFunction(const Cursor &cursor) bool isNotFinalFunction(const Cursor &cursor)
@@ -203,37 +203,37 @@ bool isNotFinalFunction(const Cursor &cursor)
} }
bool HighlightingMark::isRealDynamicCall(const Cursor &cursor) const bool HighlightingMark::isRealDynamicCall(const Cursor &cursor) const
{ {
return originalCursor.isDynamicCall() && isNotFinalFunction(cursor); return m_originalCursor.isDynamicCall() && isNotFinalFunction(cursor);
} }
void HighlightingMark::addExtraTypeIfFirstPass(HighlightingType type, void HighlightingMark::addExtraTypeIfFirstPass(HighlightingType type,
Recursion recursion) Recursion recursion)
{ {
if (recursion == Recursion::FirstPass) if (recursion == Recursion::FirstPass)
types.mixinHighlightingTypes.push_back(type); m_types.mixinHighlightingTypes.push_back(type);
} }
bool HighlightingMark::isArgumentInCurrentOutputArgumentLocations() const bool HighlightingMark::isArgumentInCurrentOutputArgumentLocations() const
{ {
auto originalSourceLocation = originalCursor.cxSourceLocation(); auto originalSourceLocation = m_originalCursor.cxSourceLocation();
const auto isNotSameOutputArgument = [&] (const CXSourceRange &currentSourceRange) { const auto isNotSameOutputArgument = [&] (const CXSourceRange &currentSourceRange) {
return originalSourceLocation.int_data >= currentSourceRange.begin_int_data return originalSourceLocation.int_data >= currentSourceRange.begin_int_data
&& originalSourceLocation.int_data <= currentSourceRange.end_int_data; && originalSourceLocation.int_data <= currentSourceRange.end_int_data;
}; };
auto found = std::find_if(currentOutputArgumentRanges->begin(), auto found = std::find_if(m_currentOutputArgumentRanges->begin(),
currentOutputArgumentRanges->end(), m_currentOutputArgumentRanges->end(),
isNotSameOutputArgument); isNotSameOutputArgument);
bool isOutputArgument = found != currentOutputArgumentRanges->end(); bool isOutputArgument = found != m_currentOutputArgumentRanges->end();
return isOutputArgument; return isOutputArgument;
} }
bool HighlightingMark::isOutputArgument() const bool HighlightingMark::isOutputArgument() const
{ {
if (currentOutputArgumentRanges->empty()) if (m_currentOutputArgumentRanges->empty())
return false; return false;
return isArgumentInCurrentOutputArgumentLocations(); return isArgumentInCurrentOutputArgumentLocations();
@@ -241,7 +241,7 @@ bool HighlightingMark::isOutputArgument() const
void HighlightingMark::collectOutputArguments(const Cursor &cursor) void HighlightingMark::collectOutputArguments(const Cursor &cursor)
{ {
cursor.collectOutputArgumentRangesTo(*currentOutputArgumentRanges); cursor.collectOutputArgumentRangesTo(*m_currentOutputArgumentRanges);
filterOutPreviousOutputArguments(); filterOutPreviousOutputArguments();
} }
@@ -262,22 +262,22 @@ uint getStart(CXSourceRange cxSourceRange)
void HighlightingMark::filterOutPreviousOutputArguments() void HighlightingMark::filterOutPreviousOutputArguments()
{ {
auto isAfterLocation = [this] (CXSourceRange outputRange) { auto isAfterLocation = [this] (CXSourceRange outputRange) {
return getStart(outputRange) > offset; return getStart(outputRange) > m_offset;
}; };
auto precedingBegin = std::partition(currentOutputArgumentRanges->begin(), auto precedingBegin = std::partition(m_currentOutputArgumentRanges->begin(),
currentOutputArgumentRanges->end(), m_currentOutputArgumentRanges->end(),
isAfterLocation); isAfterLocation);
currentOutputArgumentRanges->erase(precedingBegin, currentOutputArgumentRanges->end()); m_currentOutputArgumentRanges->erase(precedingBegin, m_currentOutputArgumentRanges->end());
} }
void HighlightingMark::functionKind(const Cursor &cursor, Recursion recursion) void HighlightingMark::functionKind(const Cursor &cursor, Recursion recursion)
{ {
if (isRealDynamicCall(cursor) || isVirtualMethodDeclarationOrDefinition(cursor)) if (isRealDynamicCall(cursor) || isVirtualMethodDeclarationOrDefinition(cursor))
types.mainHighlightingType = HighlightingType::VirtualFunction; m_types.mainHighlightingType = HighlightingType::VirtualFunction;
else else
types.mainHighlightingType = HighlightingType::Function; m_types.mainHighlightingType = HighlightingType::Function;
addExtraTypeIfFirstPass(HighlightingType::Declaration, recursion); addExtraTypeIfFirstPass(HighlightingType::Declaration, recursion);
} }
@@ -291,7 +291,7 @@ void HighlightingMark::identifierKind(const Cursor &cursor, Recursion recursion)
case CXCursor_CallExpr: case CXCursor_CallExpr:
case CXCursor_CXXMethod: functionKind(cursor, recursion); break; case CXCursor_CXXMethod: functionKind(cursor, recursion); break;
case CXCursor_NonTypeTemplateParameter: case CXCursor_NonTypeTemplateParameter:
case CXCursor_CompoundStmt: types.mainHighlightingType = HighlightingType::LocalVariable; break; case CXCursor_CompoundStmt: m_types.mainHighlightingType = HighlightingType::LocalVariable; break;
case CXCursor_ParmDecl: case CXCursor_ParmDecl:
case CXCursor_VarDecl: variableKind(cursor); break; case CXCursor_VarDecl: variableKind(cursor); break;
case CXCursor_DeclRefExpr: identifierKind(cursor.referenced(), Recursion::RecursivePass); break; case CXCursor_DeclRefExpr: identifierKind(cursor.referenced(), Recursion::RecursivePass); break;
@@ -303,7 +303,7 @@ void HighlightingMark::identifierKind(const Cursor &cursor, Recursion recursion)
case CXCursor_ObjCClassMethodDecl: case CXCursor_ObjCClassMethodDecl:
case CXCursor_ObjCInstanceMethodDecl: case CXCursor_ObjCInstanceMethodDecl:
case CXCursor_ObjCSynthesizeDecl: case CXCursor_ObjCSynthesizeDecl:
case CXCursor_ObjCDynamicDecl: types.mainHighlightingType = HighlightingType::Field; break; case CXCursor_ObjCDynamicDecl: m_types.mainHighlightingType = HighlightingType::Field; break;
case CXCursor_TypeRef: referencedTypeKind(cursor); break; case CXCursor_TypeRef: referencedTypeKind(cursor); break;
case CXCursor_ClassDecl: case CXCursor_ClassDecl:
case CXCursor_ClassTemplatePartialSpecialization: case CXCursor_ClassTemplatePartialSpecialization:
@@ -328,16 +328,16 @@ void HighlightingMark::identifierKind(const Cursor &cursor, Recursion recursion)
case CXCursor_ObjCProtocolDecl: case CXCursor_ObjCProtocolDecl:
case CXCursor_ObjCProtocolRef: case CXCursor_ObjCProtocolRef:
case CXCursor_ObjCClassRef: case CXCursor_ObjCClassRef:
case CXCursor_ObjCSuperClassRef: types.mainHighlightingType = HighlightingType::Type; break; case CXCursor_ObjCSuperClassRef: m_types.mainHighlightingType = HighlightingType::Type; break;
case CXCursor_OverloadedDeclRef: overloadedDeclRefKind(cursor); break; case CXCursor_OverloadedDeclRef: overloadedDeclRefKind(cursor); break;
case CXCursor_FunctionTemplate: types.mainHighlightingType = HighlightingType::Function; break; case CXCursor_FunctionTemplate: m_types.mainHighlightingType = HighlightingType::Function; break;
case CXCursor_EnumConstantDecl: types.mainHighlightingType = HighlightingType::Enumeration; break; case CXCursor_EnumConstantDecl: m_types.mainHighlightingType = HighlightingType::Enumeration; break;
case CXCursor_PreprocessingDirective: types.mainHighlightingType = HighlightingType::Preprocessor; break; case CXCursor_PreprocessingDirective: m_types.mainHighlightingType = HighlightingType::Preprocessor; break;
case CXCursor_MacroExpansion: types.mainHighlightingType = HighlightingType::PreprocessorExpansion; break; case CXCursor_MacroExpansion: m_types.mainHighlightingType = HighlightingType::PreprocessorExpansion; break;
case CXCursor_MacroDefinition: types.mainHighlightingType = HighlightingType::PreprocessorDefinition; break; case CXCursor_MacroDefinition: m_types.mainHighlightingType = HighlightingType::PreprocessorDefinition; break;
case CXCursor_InclusionDirective: types.mainHighlightingType = HighlightingType::StringLiteral; break; case CXCursor_InclusionDirective: m_types.mainHighlightingType = HighlightingType::StringLiteral; break;
case CXCursor_LabelRef: case CXCursor_LabelRef:
case CXCursor_LabelStmt: types.mainHighlightingType = HighlightingType::Label; break; case CXCursor_LabelStmt: m_types.mainHighlightingType = HighlightingType::Label; break;
default: break; default: break;
} }
} }
@@ -422,23 +422,23 @@ void HighlightingMark::collectKinds(CXTranslationUnit cxTranslationUnit,
{ {
auto cxTokenKind = clang_getTokenKind(*cxToken); auto cxTokenKind = clang_getTokenKind(*cxToken);
types = HighlightingTypes(); m_types = HighlightingTypes();
switch (cxTokenKind) { switch (cxTokenKind) {
case CXToken_Keyword: types.mainHighlightingType = highlightingTypeForKeyword(cxTranslationUnit, cxToken, originalCursor); break; case CXToken_Keyword: m_types.mainHighlightingType = highlightingTypeForKeyword(cxTranslationUnit, cxToken, m_originalCursor); break;
case CXToken_Punctuation: types.mainHighlightingType = punctuationKind(cursor); break; case CXToken_Punctuation: m_types.mainHighlightingType = punctuationKind(cursor); break;
case CXToken_Identifier: identifierKind(cursor, Recursion::FirstPass); break; case CXToken_Identifier: identifierKind(cursor, Recursion::FirstPass); break;
case CXToken_Comment: types.mainHighlightingType = HighlightingType::Comment; break; case CXToken_Comment: m_types.mainHighlightingType = HighlightingType::Comment; break;
case CXToken_Literal: types.mainHighlightingType = literalKind(cursor); break; case CXToken_Literal: m_types.mainHighlightingType = literalKind(cursor); break;
} }
} }
std::ostream &operator<<(std::ostream &os, const HighlightingMark& highlightingMark) std::ostream &operator<<(std::ostream &os, const HighlightingMark& highlightingMark)
{ {
os << "(type: " << highlightingMark.types << ", " os << "(type: " << highlightingMark.m_types << ", "
<< " line: " << highlightingMark.line << ", " << " line: " << highlightingMark.m_line << ", "
<< " column: " << highlightingMark.column << ", " << " column: " << highlightingMark.m_column << ", "
<< " length: " << highlightingMark.length << " length: " << highlightingMark.m_length
<< ")"; << ")";
return os; return os;

View File

@@ -47,9 +47,9 @@ public:
HighlightingMark(const CXCursor &cxCursor, HighlightingMark(const CXCursor &cxCursor,
CXToken *cxToken, CXToken *cxToken,
CXTranslationUnit cxTranslationUnit, CXTranslationUnit cxTranslationUnit,
std::vector<CXSourceRange> &currentOutputArgumentRanges); std::vector<CXSourceRange> &m_currentOutputArgumentRanges);
HighlightingMark(uint line, uint column, uint length, HighlightingTypes types); HighlightingMark(uint m_line, uint m_column, uint m_length, HighlightingTypes m_types);
HighlightingMark(uint line, uint column, uint length, HighlightingType type); HighlightingMark(uint m_line, uint m_column, uint m_length, HighlightingType type);
bool hasInvalidMainType() const; bool hasInvalidMainType() const;
bool hasMainType(HighlightingType type) const; bool hasMainType(HighlightingType type) const;
@@ -80,22 +80,22 @@ private:
friend std::ostream &operator<<(std::ostream &os, const HighlightingMark& highlightingMark); friend std::ostream &operator<<(std::ostream &os, const HighlightingMark& highlightingMark);
private: private:
std::vector<CXSourceRange> *currentOutputArgumentRanges = nullptr; std::vector<CXSourceRange> *m_currentOutputArgumentRanges = nullptr;
Cursor originalCursor; Cursor m_originalCursor;
uint line; uint m_line;
uint column; uint m_column;
uint length; uint m_length;
uint offset = 0; uint m_offset = 0;
HighlightingTypes types; HighlightingTypes m_types;
}; };
inline bool operator==(const HighlightingMark &first, const HighlightingMark &second) inline bool operator==(const HighlightingMark &first, const HighlightingMark &second)
{ {
return first.line == second.line return first.m_line == second.m_line
&& first.column == second.column && first.m_column == second.m_column
&& first.length == second.length && first.m_length == second.m_length
&& first.types == second.types; && first.m_types == second.m_types;
} }
} // namespace ClangBackEnd } // namespace ClangBackEnd