From f51048e03be780eac18af5b6943b00aede6090dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 8 Jan 2020 14:25:09 +0100 Subject: [PATCH] Clang Import; TypedefDecl --- lib/clangastdump.cpp | 16 ++++++++++++---- test/testclangastdump.cpp | 23 +++++++++++++++++++++++ 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/lib/clangastdump.cpp b/lib/clangastdump.cpp index ed6e3debf..18c15b4eb 100644 --- a/lib/clangastdump.cpp +++ b/lib/clangastdump.cpp @@ -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(); diff --git a/test/testclangastdump.cpp b/test/testclangastdump.cpp index 5bde68134..89c2ddb36 100644 --- a/test/testclangastdump.cpp +++ b/test/testclangastdump.cpp @@ -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 <> implicit __int128_t '__int128'\n" + "| `-BuiltinType 0x2d5fe80 '__int128'"; + ASSERT_EQUALS("typedef __int128 __int128_t ;", parse(clang)); + } + + void typedefDecl2() { + const char clang[] = "|-TypedefDecl 0x2d604a8 <> 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 <> 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 'int' 1\n"