data: polished location parsing in clang to capture exact type and variable name locations.
This commit is contained in:
@@ -113,7 +113,7 @@ void Storage::onFunctionParsed(
|
||||
|
||||
Node* node = m_graph.createNodeHierarchyWithDistinctSignature(
|
||||
Node::NODE_FUNCTION, fullName,
|
||||
ParserClient::functionSignatureStr(returnType.type, fullName, parameters, false)
|
||||
ParserClient::functionSignatureStr(returnType.dataType, fullName, parameters, false)
|
||||
);
|
||||
addTokenLocation(node, location);
|
||||
|
||||
@@ -134,7 +134,7 @@ void Storage::onMethodParsed(
|
||||
|
||||
Node* node = m_graph.createNodeHierarchyWithDistinctSignature(
|
||||
Node::NODE_METHOD, fullName,
|
||||
ParserClient::functionSignatureStr(returnType.type, fullName, parameters, isConst)
|
||||
ParserClient::functionSignatureStr(returnType.dataType, fullName, parameters, isConst)
|
||||
);
|
||||
|
||||
if (isConst)
|
||||
@@ -492,7 +492,7 @@ Edge* Storage::addTypeEdge(Node* node, Edge::EdgeType edgeType, const ParseTypeU
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Edge* edge = addTypeEdge(node, edgeType, typeUsage.type);
|
||||
Edge* edge = addTypeEdge(node, edgeType, typeUsage.dataType);
|
||||
|
||||
addTokenLocation(edge, typeUsage.location);
|
||||
return edge;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "data/parser/ParseTypeUsage.h"
|
||||
|
||||
ParseTypeUsage::ParseTypeUsage(const ParseLocation& location, const DataType& type)
|
||||
ParseTypeUsage::ParseTypeUsage(const ParseLocation& location, const DataType& dataType)
|
||||
: location(location)
|
||||
, type(type)
|
||||
, dataType(dataType)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -6,10 +6,10 @@
|
||||
|
||||
struct ParseTypeUsage
|
||||
{
|
||||
ParseTypeUsage(const ParseLocation& location, const DataType& type);
|
||||
ParseTypeUsage(const ParseLocation& location, const DataType& dataType);
|
||||
|
||||
const ParseLocation location;
|
||||
const DataType type;
|
||||
const DataType dataType;
|
||||
};
|
||||
|
||||
#endif // PARSE_TYPE_USAGE_H
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "data/parser/ParseVariable.h"
|
||||
|
||||
ParseVariable::ParseVariable(const DataType& type, const std::string& fullName, bool isStatic)
|
||||
ParseVariable::ParseVariable(const ParseTypeUsage& type, const std::string& fullName, bool isStatic)
|
||||
: type(type)
|
||||
, fullName(fullName)
|
||||
, isStatic(isStatic)
|
||||
|
||||
@@ -3,13 +3,13 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "data/type/DataType.h"
|
||||
#include "data/parser/ParseTypeUsage.h"
|
||||
|
||||
struct ParseVariable
|
||||
{
|
||||
ParseVariable(const DataType& type, const std::string& fullName, bool isStatic);
|
||||
ParseVariable(const ParseTypeUsage& type, const std::string& fullName, bool isStatic);
|
||||
|
||||
const DataType type;
|
||||
const ParseTypeUsage type;
|
||||
const std::string fullName;
|
||||
const bool isStatic;
|
||||
};
|
||||
|
||||
@@ -63,7 +63,7 @@ std::string ParserClient::addLocationSuffix(const std::string& str, const ParseL
|
||||
|
||||
std::string ParserClient::variableStr(const ParseVariable& variable)
|
||||
{
|
||||
std::string str = variable.type.getFullTypeName() + " " + variable.fullName;
|
||||
std::string str = variable.type.dataType.getFullTypeName() + " " + variable.fullName;
|
||||
return addStaticPrefix(str, variable.isStatic);
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ std::string ParserClient::parameterStr(const std::vector<ParseTypeUsage> paramet
|
||||
std::string str = "(";
|
||||
for (size_t i = 0; i < parameters.size(); i++)
|
||||
{
|
||||
str += parameters[i].type.getFullTypeName();
|
||||
str += parameters[i].dataType.getFullTypeName();
|
||||
if (i < parameters.size() - 1)
|
||||
{
|
||||
str += ", ";
|
||||
|
||||
@@ -90,7 +90,7 @@ bool ASTVisitor::VisitVarDecl(clang::VarDecl* declaration)
|
||||
if (access == clang::AS_none)
|
||||
{
|
||||
m_client->onGlobalVariableParsed(
|
||||
getParseLocation(declaration->getSourceRange()),
|
||||
getParseLocationForNamedDecl(declaration),
|
||||
getParseVariable(declaration)
|
||||
);
|
||||
|
||||
@@ -103,7 +103,7 @@ bool ASTVisitor::VisitVarDecl(clang::VarDecl* declaration)
|
||||
else
|
||||
{
|
||||
m_client->onFieldParsed(
|
||||
getParseLocation(declaration->getSourceRange()),
|
||||
getParseLocationForNamedDecl(declaration),
|
||||
getParseVariable(declaration),
|
||||
convertAccessType(declaration->getAccess())
|
||||
);
|
||||
@@ -118,7 +118,7 @@ bool ASTVisitor::VisitFieldDecl(clang::FieldDecl* declaration)
|
||||
if (hasValidLocation(declaration))
|
||||
{
|
||||
m_client->onFieldParsed(
|
||||
getParseLocation(declaration->getSourceRange()),
|
||||
getParseLocationForNamedDecl(declaration),
|
||||
getParseVariable(declaration),
|
||||
convertAccessType(declaration->getAccess())
|
||||
);
|
||||
@@ -280,18 +280,10 @@ void ASTVisitor::VisitCXXConstructExprInDeclBody(clang::NamedDecl* decl, clang::
|
||||
|
||||
void ASTVisitor::VisitFieldUsageExprInDeclBody(clang::NamedDecl* decl, clang::MemberExpr* expr)
|
||||
{
|
||||
const clang::SourceManager& sourceManager = m_context->getSourceManager();
|
||||
const clang::PresumedLoc& presumedBegin = sourceManager.getPresumedLoc(expr->getSourceRange().getBegin());
|
||||
const clang::PresumedLoc& presumedEnd = sourceManager.getPresumedLoc(expr->getSourceRange().getEnd());
|
||||
const std::string exprName = expr->getMemberNameInfo().getAsString();
|
||||
ParseLocation parseLocation = getParseLocation(expr->getSourceRange());
|
||||
|
||||
ParseLocation parseLocation(
|
||||
presumedBegin.getFilename(),
|
||||
presumedBegin.getLine(),
|
||||
presumedBegin.getColumn(),
|
||||
presumedEnd.getLine(),
|
||||
presumedEnd.getColumn() + exprName.size() - 1
|
||||
);
|
||||
const std::string exprName = expr->getMemberNameInfo().getAsString();
|
||||
parseLocation.endColumnNumber += exprName.size() - 1;
|
||||
|
||||
m_client->onFieldUsageParsed(
|
||||
parseLocation,
|
||||
@@ -302,18 +294,10 @@ void ASTVisitor::VisitFieldUsageExprInDeclBody(clang::NamedDecl* decl, clang::Me
|
||||
|
||||
void ASTVisitor::VisitGlobalVariableUsageExprInDeclBody(clang::NamedDecl* decl, clang::DeclRefExpr* expr)
|
||||
{
|
||||
const clang::SourceManager& sourceManager = m_context->getSourceManager();
|
||||
const clang::PresumedLoc& presumedBegin = sourceManager.getPresumedLoc(expr->getSourceRange().getBegin());
|
||||
const clang::PresumedLoc& presumedEnd = sourceManager.getPresumedLoc(expr->getSourceRange().getEnd());
|
||||
const std::string exprName = expr->getNameInfo().getAsString();
|
||||
ParseLocation parseLocation = getParseLocation(expr->getSourceRange());
|
||||
|
||||
ParseLocation parseLocation(
|
||||
presumedBegin.getFilename(),
|
||||
presumedBegin.getLine(),
|
||||
presumedBegin.getColumn(),
|
||||
presumedEnd.getLine(),
|
||||
presumedEnd.getColumn() + exprName.size() - 1
|
||||
);
|
||||
const std::string exprName = expr->getNameInfo().getAsString();
|
||||
parseLocation.endColumnNumber += exprName.size() - 1;
|
||||
|
||||
m_client->onGlobalVariableUsageParsed(
|
||||
parseLocation,
|
||||
@@ -336,7 +320,6 @@ ParseLocation ASTVisitor::getParseLocation(const clang::SourceRange& sourceRange
|
||||
}
|
||||
|
||||
const clang::SourceManager& sourceManager = m_context->getSourceManager();
|
||||
|
||||
const clang::PresumedLoc& presumedBegin = sourceManager.getPresumedLoc(sourceRange.getBegin());
|
||||
const clang::PresumedLoc& presumedEnd = sourceManager.getPresumedLoc(sourceRange.getEnd());
|
||||
|
||||
@@ -349,15 +332,21 @@ ParseLocation ASTVisitor::getParseLocation(const clang::SourceRange& sourceRange
|
||||
);
|
||||
}
|
||||
|
||||
ParseTypeUsage ASTVisitor::getParseTypeUsage(clang::ValueDecl* declaration) const
|
||||
ParseLocation ASTVisitor::getParseLocationForNamedDecl(clang::NamedDecl* decl) const
|
||||
{
|
||||
return ParseTypeUsage(
|
||||
getParseLocation(declaration->getSourceRange()),
|
||||
utility::qualTypeToDataType(declaration->getType())
|
||||
const clang::SourceManager& sourceManager = m_context->getSourceManager();
|
||||
const clang::PresumedLoc& presumedBegin = sourceManager.getPresumedLoc(decl->getLocation());
|
||||
|
||||
return ParseLocation(
|
||||
presumedBegin.getFilename(),
|
||||
presumedBegin.getLine(),
|
||||
presumedBegin.getColumn(),
|
||||
presumedBegin.getLine(),
|
||||
presumedBegin.getColumn() + decl->getNameAsString().size() - 1
|
||||
);
|
||||
}
|
||||
|
||||
ParseVariable ASTVisitor::getParseVariable(clang::ValueDecl* declaration) const
|
||||
ParseVariable ASTVisitor::getParseVariable(clang::DeclaratorDecl* declaration) const
|
||||
{
|
||||
bool isStatic = false;
|
||||
if (clang::isa<clang::VarDecl>(declaration))
|
||||
@@ -367,34 +356,47 @@ ParseVariable ASTVisitor::getParseVariable(clang::ValueDecl* declaration) const
|
||||
}
|
||||
|
||||
return ParseVariable(
|
||||
utility::qualTypeToDataType(declaration->getType()),
|
||||
getParseTypeUsage(declaration),
|
||||
declaration->getQualifiedNameAsString(),
|
||||
isStatic
|
||||
);
|
||||
}
|
||||
|
||||
ParseTypeUsage ASTVisitor::getParseTypeUsage(clang::DeclaratorDecl* declaration) const
|
||||
{
|
||||
clang::TypeLoc loc = declaration->getTypeSourceInfo()->getTypeLoc();
|
||||
|
||||
while (loc.getNextTypeLoc())
|
||||
{
|
||||
loc = loc.getNextTypeLoc();
|
||||
}
|
||||
|
||||
ParseLocation parseLocation = getParseLocation(loc.getSourceRange());
|
||||
DataType dataType = utility::qualTypeToDataType(declaration->getType());
|
||||
|
||||
parseLocation.endColumnNumber += dataType.getRawTypeName().size() - 1;
|
||||
|
||||
return ParseTypeUsage(parseLocation, dataType);
|
||||
}
|
||||
|
||||
ParseTypeUsage ASTVisitor::getParseTypeUsageOfReturnType(clang::FunctionDecl* declaration) const
|
||||
{
|
||||
// TODO: use FunctionDecl::getReturnTypeSourceRange() in newer clang version
|
||||
clang::SourceRange range;
|
||||
const clang::TypeSourceInfo *TSI = declaration->getTypeSourceInfo();
|
||||
if (TSI)
|
||||
const clang::FunctionTypeLoc FTL = TSI->getTypeLoc().IgnoreParens().getAs<clang::FunctionTypeLoc>();
|
||||
clang::TypeLoc loc = FTL.getReturnLoc();
|
||||
|
||||
while (loc.getNextTypeLoc())
|
||||
{
|
||||
clang::FunctionTypeLoc FTL = TSI->getTypeLoc().IgnoreParens().getAs<clang::FunctionTypeLoc>();
|
||||
if (FTL)
|
||||
{
|
||||
// Skip self-referential return types.
|
||||
range = clang::SourceRange(
|
||||
FTL.getReturnLoc().getLocStart(),
|
||||
declaration->getNameInfo().getLocStart().getLocWithOffset(-2)
|
||||
);
|
||||
}
|
||||
loc = loc.getNextTypeLoc();
|
||||
}
|
||||
|
||||
return ParseTypeUsage(
|
||||
getParseLocation(range),
|
||||
utility::qualTypeToDataType(declaration->getReturnType())
|
||||
);
|
||||
ParseLocation parseLocation = getParseLocation(loc.getSourceRange());
|
||||
DataType dataType = utility::qualTypeToDataType(declaration->getReturnType());
|
||||
|
||||
parseLocation.endColumnNumber += dataType.getRawTypeName().size() - 1;
|
||||
|
||||
return ParseTypeUsage(parseLocation, dataType);
|
||||
}
|
||||
|
||||
std::vector<ParseTypeUsage> ASTVisitor::getParameters(clang::FunctionDecl* declaration) const
|
||||
|
||||
@@ -47,8 +47,9 @@ public:
|
||||
private:
|
||||
bool hasValidLocation(const clang::Decl* declaration) const;
|
||||
ParseLocation getParseLocation(const clang::SourceRange& sourceRange) const;
|
||||
ParseTypeUsage getParseTypeUsage(clang::ValueDecl* declaration) const;
|
||||
ParseVariable getParseVariable(clang::ValueDecl* declaration) const;
|
||||
ParseLocation getParseLocationForNamedDecl(clang::NamedDecl* decl) const;
|
||||
ParseVariable getParseVariable(clang::DeclaratorDecl* declaration) const;
|
||||
ParseTypeUsage getParseTypeUsage(clang::DeclaratorDecl* declaration) const;
|
||||
ParseTypeUsage getParseTypeUsageOfReturnType(clang::FunctionDecl* declaration) const;
|
||||
std::vector<ParseTypeUsage> getParameters(clang::FunctionDecl* declaration) const;
|
||||
DataType qualTypeToDataType(clang::QualType qualType);
|
||||
|
||||
Reference in New Issue
Block a user