data: polished location parsing in clang to capture exact type and variable name locations.

This commit is contained in:
Eberhard Graether
2014-07-26 18:34:02 +02:00
parent 4bf459fa99
commit eeacb92b55
9 changed files with 84 additions and 81 deletions
+3 -3
View File
@@ -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;
+2 -2
View File
@@ -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)
{
}
+2 -2
View File
@@ -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 -1
View File
@@ -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 -3
View File
@@ -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;
};
+2 -2
View File
@@ -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 += ", ";
+49 -47
View File
@@ -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
+3 -2
View File
@@ -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);
+19 -19
View File
@@ -125,10 +125,10 @@ public:
);
TS_ASSERT_EQUALS(client->globalVariables.size(), 4);
TS_ASSERT_EQUALS(client->globalVariables[0], "int x <1:1 1:5>");
TS_ASSERT_EQUALS(client->globalVariables[1], "int const y <2:1 2:15>");
TS_ASSERT_EQUALS(client->globalVariables[2], "static int z <3:1 3:12>");
TS_ASSERT_EQUALS(client->globalVariables[3], "A * b <5:1 5:4>");
TS_ASSERT_EQUALS(client->globalVariables[0], "int x <1:5 1:5>");
TS_ASSERT_EQUALS(client->globalVariables[1], "int const y <2:11 2:11>");
TS_ASSERT_EQUALS(client->globalVariables[2], "static int z <3:12 3:12>");
TS_ASSERT_EQUALS(client->globalVariables[3], "A * b <5:4 5:4>");
}
void test_cxx_parser_finds_variable_definitions_in_namespace_scope()
@@ -143,8 +143,8 @@ public:
);
TS_ASSERT_EQUALS(client->globalVariables.size(), 2);
TS_ASSERT_EQUALS(client->globalVariables[0], "int n::x <2:2 2:6>");
TS_ASSERT_EQUALS(client->globalVariables[1], "n::A * n::b <4:2 4:5>");
TS_ASSERT_EQUALS(client->globalVariables[0], "int n::x <2:6 2:6>");
TS_ASSERT_EQUALS(client->globalVariables[1], "n::A * n::b <4:5 4:5>");
}
void test_cxx_parser_finds_field_in_nested_class()
@@ -162,7 +162,7 @@ public:
);
TS_ASSERT_EQUALS(client->fields.size(), 1);
TS_ASSERT_EQUALS(client->fields[0], "private static int const B::C::amount <7:3 7:20>");
TS_ASSERT_EQUALS(client->fields[0], "private static int const B::C::amount <7:20 7:25>");
}
void test_cxx_parser_finds_fields_in_class_with_access_type()
@@ -182,10 +182,10 @@ public:
);
TS_ASSERT_EQUALS(client->fields.size(), 4);
TS_ASSERT_EQUALS(client->fields[0], "private int A::a <3:2 3:6>");
TS_ASSERT_EQUALS(client->fields[1], "public int A::b <5:2 5:6>");
TS_ASSERT_EQUALS(client->fields[2], "protected static int A::c <6:2 6:13>");
TS_ASSERT_EQUALS(client->fields[3], "private int const A::d <8:2 8:12>");
TS_ASSERT_EQUALS(client->fields[0], "private int A::a <3:6 3:6>");
TS_ASSERT_EQUALS(client->fields[1], "public int A::b <5:6 5:6>");
TS_ASSERT_EQUALS(client->fields[2], "protected static int A::c <6:13 6:13>");
TS_ASSERT_EQUALS(client->fields[3], "private int const A::d <8:12 8:12>");
}
void test_cxx_parser_finds_function_in_global_namespace()
@@ -751,7 +751,7 @@ public:
TS_ASSERT_EQUALS(client->typeUses.size(), 2);
TS_ASSERT_EQUALS(client->typeUses[0], "int <1:1 1:3>");
TS_ASSERT_EQUALS(client->typeUses[1], "float <1:10 1:16>");
TS_ASSERT_EQUALS(client->typeUses[1], "float <1:10 1:14>");
}
void test_cxx_parser_finds_parameter_type_uses_in_constructor()
@@ -764,10 +764,10 @@ public:
);
TS_ASSERT_EQUALS(client->typeUses.size(), 4);
TS_ASSERT_EQUALS(client->typeUses[0], "int <3:4 3:8>");
TS_ASSERT_EQUALS(client->typeUses[1], "_Bool <3:11 3:16>");
TS_ASSERT_EQUALS(client->typeUses[2], "float <3:19 3:25>");
TS_ASSERT_EQUALS(client->typeUses[3], "int <3:28 3:32>");
TS_ASSERT_EQUALS(client->typeUses[0], "int <3:4 3:6>");
TS_ASSERT_EQUALS(client->typeUses[1], "_Bool <3:11 3:15>");
TS_ASSERT_EQUALS(client->typeUses[2], "float <3:19 3:23>");
TS_ASSERT_EQUALS(client->typeUses[3], "int <3:28 3:30>");
}
void test_cxx_parser_parses_multiple_files()
@@ -829,7 +829,7 @@ private:
const ParseLocation& location, const std::string& fullName, const ParseTypeUsage& returnType,
const std::vector<ParseTypeUsage>& parameters
){
std::string str = functionStr(returnType.type, fullName, parameters, false);
std::string str = functionStr(returnType.dataType, fullName, parameters, false);
functions.push_back(addLocationSuffix(str, location));
addTypeUse(returnType);
@@ -845,7 +845,7 @@ private:
bool isConst, bool isStatic
)
{
std::string str = functionStr(returnType.type, fullName, parameters, isConst);
std::string str = functionStr(returnType.dataType, fullName, parameters, isConst);
str = addStaticPrefix(addAbstractionPrefix(str, abstraction), isStatic);
str = addAccessPrefix(str, access);
str = addLocationSuffix(str, location);
@@ -918,7 +918,7 @@ private:
{
if (use.location.isValid())
{
typeUses.push_back(addLocationSuffix(use.type.getFullTypeName(), use.location));
typeUses.push_back(addLocationSuffix(use.dataType.getFullTypeName(), use.location));
}
}
};