Clang import; WhileStmt

This commit is contained in:
Daniel Marjamäki 2020-01-08 12:19:48 +01:00
parent 61039023b2
commit ea414e46e1
2 changed files with 29 additions and 0 deletions

View File

@ -39,11 +39,13 @@ static const std::string IfStmt = "IfStmt";
static const std::string ImplicitCastExpr = "ImplicitCastExpr";
static const std::string IntegerLiteral = "IntegerLiteral";
static const std::string MemberExpr = "MemberExpr";
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 UnaryOperator = "UnaryOperator";
static const std::string VarDecl = "VarDecl";
static const std::string WhileStmt = "WhileStmt";
static std::string unquote(const std::string &s)
{
@ -415,6 +417,8 @@ Token *clangastdump::AstNode::createTokens(TokenList *tokenList)
return children[0]->createTokens(tokenList);
if (nodeType == IntegerLiteral)
return addtoken(tokenList, mExtTokens.back());
if (nodeType == NullStmt)
return addtoken(tokenList, ";");
if (nodeType == MemberExpr) {
Token *s = children[0]->createTokens(tokenList);
Token *dot = addtoken(tokenList, ".");
@ -447,6 +451,18 @@ Token *clangastdump::AstNode::createTokens(TokenList *tokenList)
}
if (nodeType == VarDecl)
return createTokensVarDecl(tokenList);
if (nodeType == WhileStmt) {
AstNodePtr cond = children[1];
AstNodePtr body = children[2];
Token *whiletok = addtoken(tokenList, "while");
Token *par1 = addtoken(tokenList, "(");
par1->astOperand1(whiletok);
par1->astOperand2(cond->createTokens(tokenList));
Token *par2 = addtoken(tokenList, ")");
par1->link(par2);
createScope(tokenList, Scope::ScopeType::eWhile, body);
return nullptr;
}
return addtoken(tokenList, "?" + nodeType + "?");
}

View File

@ -37,6 +37,7 @@ private:
TEST_CASE(recordDecl);
TEST_CASE(vardecl1);
TEST_CASE(vardecl2);
TEST_CASE(whileStmt);
}
std::string parse(const char clang[]) {
@ -165,6 +166,18 @@ private:
"a@1 [ 0 ] = 0 ; }",
parse(clang));
}
void whileStmt() {
const char clang[] = "`-FunctionDecl 0x3d45b18 <1.c:1:1, line:3:1> line:1:6 foo 'void ()'\n"
" `-CompoundStmt 0x3d45c48 <col:12, line:3:1>\n"
" `-WhileStmt 0x3d45c28 <line:2:5, col:14>\n"
" |-<<<NULL>>>\n"
" |-IntegerLiteral 0x3d45bf8 <col:12> 'int' 0\n"
" `-NullStmt 0x3d45c18 <col:14>";
ASSERT_EQUALS("void foo ( ) {\n"
"while ( 0 ) { ; } ; }",
parse(clang));
}
};
REGISTER_TEST(TestClangAstDump)