Clang import: IfStmt

This commit is contained in:
Daniel Marjamäki 2020-01-23 16:18:39 +01:00
parent ce77db2b1b
commit fc813cef2a
2 changed files with 28 additions and 7 deletions

View File

@ -779,9 +779,18 @@ Token *clangimport::AstNode::createTokens(TokenList *tokenList)
return nullptr;
}
if (nodeType == IfStmt) {
AstNodePtr cond = children[children.size() - 3];
AstNodePtr then = children[children.size() - 2];
AstNodePtr else_ = children[children.size() - 1];
AstNodePtr cond;
AstNodePtr thenCode;
AstNodePtr elseCode;
if (children.size() == 2) {
cond = children[children.size() - 2];
thenCode = children[children.size() - 1];
} else {
cond = children[children.size() - 3];
thenCode = children[children.size() - 2];
elseCode = children[children.size() - 1];
}
Token *iftok = addtoken(tokenList, "if");
Token *par1 = addtoken(tokenList, "(");
par1->astOperand1(iftok);
@ -789,10 +798,10 @@ Token *clangimport::AstNode::createTokens(TokenList *tokenList)
Token *par2 = addtoken(tokenList, ")");
par1->link(par2);
par2->link(par1);
createScope(tokenList, Scope::ScopeType::eIf, then, iftok);
if (else_) {
else_->addtoken(tokenList, "else");
createScope(tokenList, Scope::ScopeType::eElse, else_, tokenList->back());
createScope(tokenList, Scope::ScopeType::eIf, thenCode, iftok);
if (elseCode) {
elseCode->addtoken(tokenList, "else");
createScope(tokenList, Scope::ScopeType::eElse, elseCode, tokenList->back());
}
return nullptr;
}

View File

@ -69,6 +69,7 @@ private:
TEST_CASE(functionTemplateDecl2);
TEST_CASE(initListExpr);
TEST_CASE(ifelse);
TEST_CASE(ifStmt);
TEST_CASE(labelStmt);
TEST_CASE(memberExpr);
TEST_CASE(namespaceDecl);
@ -603,6 +604,17 @@ private:
"else { } }", parse(clang));
}
void ifStmt() {
// Clang 8 in cygwin
const char clang[] = "`-FunctionDecl 0x41d0690 <2.cpp:1:1, col:24> col:6 foo 'void ()'\n"
" `-CompoundStmt 0x41d07f0 <col:12, col:24>\n"
" `-IfStmt 0x41d07b8 <col:14, col:22>\n"
" |-ImplicitCastExpr 0x41d0790 <col:18> 'bool' <IntegralToBoolean>\n"
" | `-IntegerLiteral 0x41d0770 <col:18> 'int' 1\n"
" |-CompoundStmt 0x41d07a8 <col:21, col:22>\n";
ASSERT_EQUALS("void foo ( ) { if ( 1 ) { } }", parse(clang));
}
void initListExpr() {
const char clang[] = "|-VarDecl 0x397c680 <1.cpp:2:1, col:26> col:11 used ints 'const int [3]' cinit\n"
"| `-InitListExpr 0x397c7d8 <col:20, col:26> 'const int [3]'\n"