Clang Import; TypedefDecl

This commit is contained in:
Daniel Marjamäki 2020-01-08 14:25:09 +01:00
parent e6ee29fd11
commit f51048e03b
2 changed files with 35 additions and 4 deletions

View File

@ -45,6 +45,7 @@ static const std::string NullStmt = "NullStmt";
static const std::string ParmVarDecl = "ParmVarDecl";
static const std::string RecordDecl = "RecordDecl";
static const std::string ReturnStmt = "ReturnStmt";
static const std::string TypedefDecl = "TypedefDecl";
static const std::string UnaryOperator = "UnaryOperator";
static const std::string VarDecl = "VarDecl";
static const std::string WhileStmt = "WhileStmt";
@ -145,7 +146,7 @@ namespace clangastdump {
void createTokens1(TokenList *tokenList) {
setLocations(tokenList, 0, 1, 1);
createTokens(tokenList);
if (nodeType == VarDecl || nodeType == RecordDecl)
if (nodeType == VarDecl || nodeType == RecordDecl || nodeType == TypedefDecl)
addtoken(tokenList, ";");
}
private:
@ -175,12 +176,14 @@ std::string clangastdump::AstNode::getSpelling() const
std::string clangastdump::AstNode::getType() const
{
if (nodeType == DeclRefExpr)
return unquote(mExtTokens.back());
if (nodeType == BinaryOperator)
return unquote(mExtTokens[mExtTokens.size() - 2]);
if (nodeType == DeclRefExpr)
return unquote(mExtTokens.back());
if (nodeType == IntegerLiteral)
return unquote(mExtTokens[mExtTokens.size() - 2]);
if (nodeType == TypedefDecl)
return unquote(mExtTokens.back());
return "";
}
@ -450,6 +453,11 @@ Token *clangastdump::AstNode::createTokens(TokenList *tokenList)
tok1->astOperand1(children[0]->createTokens(tokenList));
return tok1;
}
if (nodeType == TypedefDecl) {
addtoken(tokenList, "typedef");
addTypeTokens(tokenList, getType());
return addtoken(tokenList, getSpelling());
}
if (nodeType == UnaryOperator) {
Token *unop = addtoken(tokenList, unquote(mExtTokens.back()));
unop->astOperand1(children[0]->createTokens(tokenList));
@ -521,7 +529,7 @@ void clangastdump::parseClangAstDump(Tokenizer *tokenizer, std::istream &f)
const std::string nodeType = line.substr(pos1+1, pos2 - pos1 - 1);
const std::string ext = line.substr(pos2);
if (pos1 == 1 && endsWith(nodeType, "Decl", 4) && nodeType != "TypedefDecl") {
if (pos1 == 1 && endsWith(nodeType, "Decl", 4)) {
if (!tree.empty())
tree[0]->createTokens1(tokenList);
tree.clear();

View File

@ -37,6 +37,9 @@ private:
TEST_CASE(ifelse);
TEST_CASE(memberExpr);
TEST_CASE(recordDecl);
TEST_CASE(typedefDecl1);
TEST_CASE(typedefDecl2);
TEST_CASE(typedefDecl3);
TEST_CASE(vardecl1);
TEST_CASE(vardecl2);
TEST_CASE(vardecl3);
@ -159,6 +162,26 @@ private:
parse(clang));
}
void typedefDecl1() {
const char clang[] = "|-TypedefDecl 0x2d60180 <<invalid sloc>> <invalid sloc> implicit __int128_t '__int128'\n"
"| `-BuiltinType 0x2d5fe80 '__int128'";
ASSERT_EQUALS("typedef __int128 __int128_t ;", parse(clang));
}
void typedefDecl2() {
const char clang[] = "|-TypedefDecl 0x2d604a8 <<invalid sloc>> <invalid sloc> implicit __NSConstantString 'struct __NSConstantString_tag'\n"
"| `-RecordType 0x2d602c0 'struct __NSConstantString_tag'\n"
"| `-Record 0x2d60238 '__NSConstantString_tag'";
ASSERT_EQUALS("typedef struct __NSConstantString_tag __NSConstantString ;", parse(clang));
}
void typedefDecl3() {
const char clang[] = "|-TypedefDecl 0x2d60540 <<invalid sloc>> <invalid sloc> implicit __builtin_ms_va_list 'char *'\n"
"| `-PointerType 0x2d60500 'char *'\n"
"| `-BuiltinType 0x2d5f980 'char'";
ASSERT_EQUALS("typedef char * __builtin_ms_va_list ;", parse(clang));
}
void vardecl1() {
const char clang[] = "|-VarDecl 0x32b8aa0 <1.c:1:1, col:9> col:5 used a 'int' cinit\n"
"| `-IntegerLiteral 0x32b8b40 <col:9> 'int' 1\n"