From 87323b33bd11943c5a41f830890b4b185ade50a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 8 Nov 2020 01:17:34 +0100 Subject: [PATCH] Clang import: Fixed problems when handling CXXConstructorDecl --- lib/clangimport.cpp | 9 +++++++-- test/testclangimport.cpp | 13 ++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/clangimport.cpp b/lib/clangimport.cpp index 812bf10d0..f4ef07c33 100644 --- a/lib/clangimport.cpp +++ b/lib/clangimport.cpp @@ -320,7 +320,7 @@ std::string clangimport::AstNode::getSpelling() const } int typeIndex = mExtTokens.size() - 1; - if (nodeType == FunctionDecl) { + if (nodeType == FunctionDecl || nodeType == CXXConstructorDecl) { while (typeIndex >= 0 && mExtTokens[typeIndex][0] != '\'') typeIndex--; if (typeIndex <= 0) @@ -1111,7 +1111,7 @@ Token * clangimport::AstNode::createTokensCall(TokenList *tokenList) void clangimport::AstNode::createTokensFunctionDecl(TokenList *tokenList) { const bool prev = (std::find(mExtTokens.begin(), mExtTokens.end(), "prev") != mExtTokens.end()); - const bool hasBody = mFile == 0 && !children.empty() && children.back()->nodeType == CompoundStmt; + const bool hasBody = !children.empty() && children.back()->nodeType == CompoundStmt; const bool isStatic = (std::find(mExtTokens.begin(), mExtTokens.end(), "static") != mExtTokens.end()); const bool isInline = (std::find(mExtTokens.begin(), mExtTokens.end(), "inline") != mExtTokens.end()); @@ -1207,6 +1207,11 @@ void clangimport::AstNode::createTokensFunctionDecl(TokenList *tokenList) bodyStart->link(bodyEnd); bodyEnd->link(bodyStart); } else { + if (nodeType == CXXConstructorDecl && (std::find(mExtTokens.begin(), mExtTokens.end(), "default") != mExtTokens.end())) { + addtoken(tokenList, "="); + addtoken(tokenList, "default"); + } + addtoken(tokenList, ";"); } } diff --git a/test/testclangimport.cpp b/test/testclangimport.cpp index eeca5ce34..49ed2010a 100644 --- a/test/testclangimport.cpp +++ b/test/testclangimport.cpp @@ -42,7 +42,8 @@ private: TEST_CASE(continueStmt); TEST_CASE(cstyleCastExpr); TEST_CASE(cxxBoolLiteralExpr); - TEST_CASE(cxxConstructorDecl); + TEST_CASE(cxxConstructorDecl1); + TEST_CASE(cxxConstructorDecl2); TEST_CASE(cxxConstructExpr1); TEST_CASE(cxxConstructExpr2); TEST_CASE(cxxConstructExpr3); @@ -250,7 +251,7 @@ private: "| | `-ParmVarDecl 0x247c790 col:25 'const C &'\n" "| `-CXXConstructorDecl 0x247c828 col:25 implicit constexpr C 'void (C &&)' inline default trivial noexcept-unevaluated 0x247c828\n" "| `-ParmVarDecl 0x247c960 col:25 'C &&'\n"; - ASSERT_EQUALS("class C { int foo ( ) { return 0 ; } default ( ) { } noexcept-unevaluated ( const C & ) ; noexcept-unevaluated ( C && ) ; } ;", parse(clang)); + ASSERT_EQUALS("class C { int foo ( ) { return 0 ; } C ( ) { } C ( const C & ) = default ; C ( C && ) = default ; } ;", parse(clang)); } void conditionalExpr() { @@ -297,7 +298,7 @@ private: ASSERT_EQUALS("bool x@1 = true ;", parse(clang)); } - void cxxConstructorDecl() { + void cxxConstructorDecl1() { const char clang[] = "|-CXXConstructorDecl 0x428e890 col:11 C 'void ()'\n" "| `-CompoundStmt 0x428ea58 \n" "| `-BinaryOperator 0x428ea30 'int' lvalue '='\n" @@ -308,6 +309,12 @@ private: ASSERT_EQUALS("C ( ) { this . x@1 = 0 ; } int x@1", parse(clang)); } + void cxxConstructorDecl2() { + const char clang[] = "`-CXXConstructorDecl 0x1c208c0 col:11 implicit constexpr basic_string 'void (std::basic_string &&)' inline default trivial noexcept-unevaluated 0x1c208c0\n" + " `-ParmVarDecl 0x1c209f0 col:11 'std::basic_string &&'"; + ASSERT_EQUALS("basic_string ( std :: basic_string && ) = default ;", parse(clang)); + } + void cxxConstructExpr1() { const char clang[] = "`-FunctionDecl 0x2dd7940 col:5 f 'Foo (Foo)'\n" " |-ParmVarDecl 0x2dd7880 col:11 used foo 'Foo'\n"