From 066e43cce3f9fade5f152a8bde3e046d0bc4d49f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 13 Jan 2020 12:44:11 +0100 Subject: [PATCH] Clang import; InitListExpr --- lib/clangimport.cpp | 18 +++++++++++++++++- test/testclangimport.cpp | 10 ++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/clangimport.cpp b/lib/clangimport.cpp index 85d307b0e..01cd665bd 100644 --- a/lib/clangimport.cpp +++ b/lib/clangimport.cpp @@ -56,6 +56,7 @@ static const std::string FunctionDecl = "FunctionDecl"; static const std::string FunctionTemplateDecl = "FunctionTemplateDecl"; static const std::string IfStmt = "IfStmt"; static const std::string ImplicitCastExpr = "ImplicitCastExpr"; +static const std::string InitListExpr = "InitListExpr"; static const std::string IntegerLiteral = "IntegerLiteral"; static const std::string MemberExpr = "MemberExpr"; static const std::string NamespaceDecl = "NamespaceDecl"; @@ -387,7 +388,7 @@ const Scope *clangimport::AstNode::getNestedInScope(TokenList *tokenList) { if (!tokenList->back()) return &mData->mSymbolDatabase->scopeList.front(); - if (tokenList->back()->str() == "}") + if (tokenList->back()->str() == "}" && !tokenList->back()->link()->astParent()) return tokenList->back()->scope()->nestedIn; return tokenList->back()->scope(); } @@ -652,6 +653,21 @@ Token *clangimport::AstNode::createTokens(TokenList *tokenList) setValueType(expr); return expr; } + if (nodeType == InitListExpr) { + const Scope *scope = tokenList->back()->scope(); + Token *start = addtoken(tokenList, "{"); + start->scope(scope); + for (AstNodePtr child: children) { + if (tokenList->back()->str() != "{") + addtoken(tokenList, ","); + child->createTokens(tokenList); + } + Token *end = addtoken(tokenList, "}"); + end->scope(scope); + start->link(end); + end->link(start); + return start; + } if (nodeType == IntegerLiteral) return addtoken(tokenList, mExtTokens.back()); if (nodeType == NullStmt) diff --git a/test/testclangimport.cpp b/test/testclangimport.cpp index dfcb65e16..64ae9ecea 100644 --- a/test/testclangimport.cpp +++ b/test/testclangimport.cpp @@ -57,6 +57,7 @@ private: TEST_CASE(funcdecl4); TEST_CASE(functionTemplateDecl1); TEST_CASE(functionTemplateDecl2); + TEST_CASE(initListExpr); TEST_CASE(ifelse); TEST_CASE(memberExpr); TEST_CASE(namespaceDecl); @@ -429,6 +430,15 @@ private: "else { } }", 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" + "| |-IntegerLiteral 0x397c720 'int' 1\n" + "| |-IntegerLiteral 0x397c740 'int' 2\n" + "| `-IntegerLiteral 0x397c760 'int' 3"; + ASSERT_EQUALS("const int [3] ints@1 = { 1 , 2 , 3 } ;", parse(clang)); + } + void memberExpr() { // C code: // struct S { int x };