data: Parsing Typedefs with ParseTypeUsage
With this change typedefs have two TokenLocations, one for the TypeName and one for the UnderlyingType. When the typedef is used with a variable the typedef is referenced now and not the underlying type.
This commit is contained in:
@@ -39,9 +39,9 @@ void Storage::logLocations() const
|
||||
|
||||
|
||||
void Storage::onTypedefParsed(
|
||||
const ParseLocation& location, const std::string& fullName, const DataType& underlyingType, AccessType access
|
||||
const ParseLocation& location, const std::string& fullName, const ParseTypeUsage& underlyingType, AccessType access
|
||||
){
|
||||
log("typedef", fullName + " -> " + underlyingType.getFullTypeName(), location);
|
||||
log("typedef", fullName + " -> " + underlyingType.dataType.getFullTypeName(), location);
|
||||
|
||||
Node* node = m_graph.createNodeHierarchy(Node::NODE_TYPEDEF, fullName);
|
||||
addAccess(node, access);
|
||||
|
||||
@@ -27,7 +27,7 @@ public:
|
||||
|
||||
// ParserClient implementation
|
||||
virtual void onTypedefParsed(
|
||||
const ParseLocation& location, const std::string& fullName, const DataType& underlyingType,
|
||||
const ParseLocation& location, const std::string& fullName, const ParseTypeUsage& underlyingType,
|
||||
AccessType access
|
||||
);
|
||||
virtual void onClassParsed(
|
||||
|
||||
@@ -52,7 +52,7 @@ public:
|
||||
virtual ~ParserClient();
|
||||
|
||||
virtual void onTypedefParsed(
|
||||
const ParseLocation& location, const std::string& fullName, const DataType& underlyingType,
|
||||
const ParseLocation& location, const std::string& fullName, const ParseTypeUsage& underlyingType,
|
||||
AccessType access) = 0;
|
||||
virtual void onClassParsed(
|
||||
const ParseLocation& location, const std::string& fullName, AccessType access,
|
||||
|
||||
@@ -22,14 +22,14 @@ bool ASTVisitor::VisitStmt(const clang::Stmt* statement)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ASTVisitor::VisitTypedefDecl(const clang::TypedefDecl* declaration)
|
||||
bool ASTVisitor::VisitTypedefDecl(clang::TypedefDecl* declaration)
|
||||
{
|
||||
if (hasValidLocation(declaration))
|
||||
{
|
||||
m_client->onTypedefParsed(
|
||||
getParseLocation(declaration->getSourceRange()),
|
||||
getParseLocationForNamedDecl(declaration),
|
||||
declaration->getQualifiedNameAsString(),
|
||||
utility::qualTypeToDataType(declaration->getUnderlyingType()),
|
||||
getParseTypeUsage(declaration->getTypeSourceInfo()->getTypeLoc(), declaration->getUnderlyingType()),
|
||||
convertAccessType(declaration->getAccess())
|
||||
);
|
||||
}
|
||||
@@ -382,23 +382,21 @@ ParseVariable ASTVisitor::getParseVariable(clang::DeclaratorDecl* declaration) c
|
||||
}
|
||||
|
||||
return ParseVariable(
|
||||
getParseTypeUsage(declaration),
|
||||
getParseTypeUsage(declaration->getTypeSourceInfo()->getTypeLoc(), declaration->getType()),
|
||||
declaration->getQualifiedNameAsString(),
|
||||
isStatic
|
||||
);
|
||||
}
|
||||
|
||||
ParseTypeUsage ASTVisitor::getParseTypeUsage(clang::DeclaratorDecl* declaration) const
|
||||
ParseTypeUsage ASTVisitor::getParseTypeUsage(clang::TypeLoc typeLoc, const clang::QualType& type) const
|
||||
{
|
||||
clang::TypeLoc loc = declaration->getTypeSourceInfo()->getTypeLoc();
|
||||
|
||||
while (loc.getNextTypeLoc())
|
||||
while (typeLoc.getNextTypeLoc())
|
||||
{
|
||||
loc = loc.getNextTypeLoc();
|
||||
typeLoc = typeLoc.getNextTypeLoc();
|
||||
}
|
||||
|
||||
ParseLocation parseLocation = getParseLocation(loc.getSourceRange());
|
||||
DataType dataType = utility::qualTypeToDataType(declaration->getType());
|
||||
ParseLocation parseLocation = getParseLocation(typeLoc.getSourceRange());
|
||||
DataType dataType = utility::qualTypeToDataType(type);
|
||||
|
||||
parseLocation.endColumnNumber += dataType.getRawTypeName().size() - 1;
|
||||
|
||||
@@ -410,19 +408,8 @@ ParseTypeUsage ASTVisitor::getParseTypeUsageOfReturnType(clang::FunctionDecl* de
|
||||
// TODO: use FunctionDecl::getReturnTypeSourceRange() in newer clang version
|
||||
const clang::TypeSourceInfo *TSI = declaration->getTypeSourceInfo();
|
||||
const clang::FunctionTypeLoc FTL = TSI->getTypeLoc().IgnoreParens().getAs<clang::FunctionTypeLoc>();
|
||||
clang::TypeLoc loc = FTL.getReturnLoc();
|
||||
|
||||
while (loc.getNextTypeLoc())
|
||||
{
|
||||
loc = loc.getNextTypeLoc();
|
||||
}
|
||||
|
||||
ParseLocation parseLocation = getParseLocation(loc.getSourceRange());
|
||||
DataType dataType = utility::qualTypeToDataType(declaration->getReturnType());
|
||||
|
||||
parseLocation.endColumnNumber += dataType.getRawTypeName().size() - 1;
|
||||
|
||||
return ParseTypeUsage(parseLocation, dataType);
|
||||
return getParseTypeUsage(FTL.getReturnLoc(), declaration->getReturnType());
|
||||
}
|
||||
|
||||
std::vector<ParseTypeUsage> ASTVisitor::getParameters(clang::FunctionDecl* declaration) const
|
||||
@@ -431,7 +418,8 @@ std::vector<ParseTypeUsage> ASTVisitor::getParameters(clang::FunctionDecl* decla
|
||||
|
||||
for (unsigned i = 0; i < declaration->getNumParams(); i++)
|
||||
{
|
||||
parameters.push_back(getParseTypeUsage(declaration->getParamDecl(i)));
|
||||
clang::ParmVarDecl* paramDecl = declaration->getParamDecl(i);
|
||||
parameters.push_back(getParseTypeUsage(paramDecl->getTypeSourceInfo()->getTypeLoc(), paramDecl->getType()));
|
||||
}
|
||||
|
||||
return parameters;
|
||||
|
||||
@@ -27,7 +27,7 @@ public:
|
||||
// RecursiveASTVisitor implementation
|
||||
virtual bool VisitStmt(const clang::Stmt* statement); // avoid visiting
|
||||
|
||||
virtual bool VisitTypedefDecl(const clang::TypedefDecl* declaration); // typedefs
|
||||
virtual bool VisitTypedefDecl(clang::TypedefDecl* declaration); // typedefs
|
||||
virtual bool VisitCXXRecordDecl(clang::CXXRecordDecl* declaration); // structs, classes and inheritance
|
||||
virtual bool VisitVarDecl(clang::VarDecl* declaration); // global variables and static fields
|
||||
virtual bool VisitFieldDecl(clang::FieldDecl* declaration); // fields
|
||||
@@ -51,7 +51,7 @@ private:
|
||||
ParseLocation getParseLocationOfFunctionBody(clang::FunctionDecl* decl) const;
|
||||
ParseLocation getParseLocationOfRecordBody(clang::CXXRecordDecl* decl) const;
|
||||
ParseVariable getParseVariable(clang::DeclaratorDecl* declaration) const;
|
||||
ParseTypeUsage getParseTypeUsage(clang::DeclaratorDecl* declaration) const;
|
||||
ParseTypeUsage getParseTypeUsage(clang::TypeLoc typeLoc, const clang::QualType& type) const;
|
||||
ParseTypeUsage getParseTypeUsageOfReturnType(clang::FunctionDecl* declaration) const;
|
||||
std::vector<ParseTypeUsage> getParameters(clang::FunctionDecl* declaration) const;
|
||||
DataType qualTypeToDataType(clang::QualType qualType);
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "data/type/modifier/DataTypeModifierReference.h"
|
||||
#include "data/type/DataTypeModifierStack.h"
|
||||
#include "data/type/DataTypeQualifierList.h"
|
||||
#include "utility/utilityString.h"
|
||||
|
||||
namespace utility
|
||||
{
|
||||
@@ -18,11 +19,18 @@ namespace utility
|
||||
while (true)
|
||||
{
|
||||
const clang::Type* type = qualType.getTypePtr();
|
||||
if (type->isPointerType())
|
||||
if (type->getAs<clang::TypedefType>())
|
||||
{
|
||||
typeName = utility::substrAfter(qualType.getAsString(), ' ');
|
||||
break;
|
||||
}
|
||||
else if (type->isPointerType())
|
||||
{
|
||||
std::shared_ptr<DataTypeModifier> modifier = std::make_shared<DataTypeModifierPointer>();
|
||||
if (qualType.isConstQualified())
|
||||
{
|
||||
modifier->addQualifier(DataTypeQualifierList::QUALIFIER_CONST);
|
||||
}
|
||||
modifierStack.push(modifier);
|
||||
|
||||
qualType = type->getPointeeType();
|
||||
@@ -31,7 +39,9 @@ namespace utility
|
||||
{
|
||||
std::shared_ptr<DataTypeModifier> modifier = std::make_shared<DataTypeModifierArray>();
|
||||
if (qualType.isConstQualified())
|
||||
{
|
||||
modifier->addQualifier(DataTypeQualifierList::QUALIFIER_CONST);
|
||||
}
|
||||
modifierStack.push(modifier);
|
||||
|
||||
qualType = clang::dyn_cast<clang::ArrayType>(type)->getElementType();
|
||||
@@ -46,21 +56,10 @@ namespace utility
|
||||
}
|
||||
else if (type->isStructureOrClassType() || type->isEnumeralType())
|
||||
{
|
||||
typeName = qualType.getAsString();
|
||||
|
||||
// we are working on the string here to not lose the namespace information stored in the name.
|
||||
size_t nameStartPosition = typeName.find(' ');
|
||||
if (nameStartPosition != typeName.npos)
|
||||
{
|
||||
nameStartPosition += 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
nameStartPosition = 0;
|
||||
}
|
||||
typeName = typeName.substr(nameStartPosition, typeName.size());
|
||||
typeName = utility::substrAfter(qualType.getAsString(), ' ');
|
||||
|
||||
//m_typeName = qualType.getBaseTypeIdentifier()->getName(); // this one does not keep namespace information.
|
||||
// typeName = qualType.getBaseTypeIdentifier()->getName(); // this one does not keep namespace information.
|
||||
break;
|
||||
}
|
||||
else
|
||||
@@ -71,7 +70,9 @@ namespace utility
|
||||
}
|
||||
|
||||
if (qualType.isConstQualified())
|
||||
{
|
||||
qualifierList.addQualifier(DataTypeQualifierList::QUALIFIER_CONST);
|
||||
}
|
||||
|
||||
return DataType(typeName, qualifierList, modifierStack);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,16 @@
|
||||
|
||||
namespace utility
|
||||
{
|
||||
std::string substrAfter(const std::string& str, char delimiter)
|
||||
{
|
||||
size_t pos = str.find(delimiter);
|
||||
if (pos != std::string::npos)
|
||||
{
|
||||
return str.substr(pos + 1, str.size());
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
bool isPrefix(const std::string& prefix, const std::string& text)
|
||||
{
|
||||
typedef std::pair<std::string::const_iterator, std::string::const_iterator> ResType;
|
||||
|
||||
@@ -12,6 +12,8 @@ namespace utility
|
||||
template<typename ContainerType = std::vector<std::string>>
|
||||
ContainerType split(const std::string& str, const std::string& delimiter);
|
||||
|
||||
std::string substrAfter(const std::string& str, char delimiter);
|
||||
|
||||
bool isPrefix(const std::string& prefix, const std::string& text);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user