diff --git a/lib/clangimport.cpp b/lib/clangimport.cpp index 4addfd7c6..a3c10b725 100644 --- a/lib/clangimport.cpp +++ b/lib/clangimport.cpp @@ -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; } diff --git a/test/testclangimport.cpp b/test/testclangimport.cpp index b5e957e3a..d7048da47 100644 --- a/test/testclangimport.cpp +++ b/test/testclangimport.cpp @@ -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 \n" + " `-IfStmt 0x41d07b8 \n" + " |-ImplicitCastExpr 0x41d0790 'bool' \n" + " | `-IntegerLiteral 0x41d0770 'int' 1\n" + " |-CompoundStmt 0x41d07a8 \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 'const int [3]'\n"