From 4094274723125498aaa321b383ce7a948f01688d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 1 Nov 2020 11:33:01 +0100 Subject: [PATCH] TestClangImport: Fix tests --- lib/clangimport.cpp | 29 ++++++++------- test/testclangimport.cpp | 78 ++++++++++++++-------------------------- 2 files changed, 40 insertions(+), 67 deletions(-) diff --git a/lib/clangimport.cpp b/lib/clangimport.cpp index 0909252b7..6454294d6 100644 --- a/lib/clangimport.cpp +++ b/lib/clangimport.cpp @@ -1186,22 +1186,21 @@ void clangimport::AstNode::createTokensForCXXRecord(TokenList *tokenList) className = mExtTokens[mExtTokens.size() - 2]; className += getTemplateParameters(); /*Token *nameToken =*/ addtoken(tokenList, className); - if (!isDefinition()) { - addtoken(tokenList, ";"); - return; + if (isDefinition()) { + std::vector children2; + for (AstNodePtr child: children) { + if (child->nodeType == CXXConstructorDecl || + child->nodeType == CXXDestructorDecl || + child->nodeType == CXXMethodDecl || + child->nodeType == FieldDecl) + children2.push_back(child); + } + Scope *scope = createScope(tokenList, isStruct ? Scope::ScopeType::eStruct : Scope::ScopeType::eClass, children2, classToken); + scope->className = className; + mData->mSymbolDatabase->typeList.push_back(Type(classToken, scope, classToken->scope())); + scope->definedType = &mData->mSymbolDatabase->typeList.back(); } - std::vector children2; - for (AstNodePtr child: children) { - if (child->nodeType == CXXConstructorDecl || - child->nodeType == CXXDestructorDecl || - child->nodeType == CXXMethodDecl || - child->nodeType == FieldDecl) - children2.push_back(child); - } - Scope *scope = createScope(tokenList, isStruct ? Scope::ScopeType::eStruct : Scope::ScopeType::eClass, children2, classToken); - scope->className = className; - mData->mSymbolDatabase->typeList.push_back(Type(classToken, scope, classToken->scope())); - scope->definedType = &mData->mSymbolDatabase->typeList.back(); + addtoken(tokenList, ";"); } Token * clangimport::AstNode::createTokensVarDecl(TokenList *tokenList) diff --git a/test/testclangimport.cpp b/test/testclangimport.cpp index af0f48264..f460fb14b 100644 --- a/test/testclangimport.cpp +++ b/test/testclangimport.cpp @@ -60,6 +60,7 @@ private: TEST_CASE(cxxNullPtrLiteralExpr); TEST_CASE(cxxOperatorCallExpr); TEST_CASE(cxxRecordDecl1); + TEST_CASE(cxxRecordDecl2); TEST_CASE(cxxStaticCastExpr1); TEST_CASE(cxxStaticCastExpr2); TEST_CASE(cxxStdInitializerListExpr); @@ -115,7 +116,7 @@ private: Tokenizer tokenizer(&settings, this); std::istringstream istr(clang); clangimport::parseClangAstDump(&tokenizer, istr); - return tokenizer.tokens()->stringifyList(true, false, false, true, false); + return tokenizer.tokens()->stringifyList(true, false, false, false, false); } void breakStmt() { @@ -164,14 +165,7 @@ private: " | |-DeclRefExpr 0x2444d68 'int' lvalue ParmVar 0x2444aa0 'x' 'int'\n" " | `-IntegerLiteral 0x2444d90 'int' 123\n" " `-BreakStmt 0x2444dd8 "; - ASSERT_EQUALS("void foo ( int x@1 ) {\n" - "switch ( x@1 ) {\n" - "case 16 :\n" - "case 32 :\n" - "x@1 = 123 ;\n" - "\n" - "\n" - "break ; } }", parse(clang)); + ASSERT_EQUALS("void foo ( int x@1 ) { switch ( x@1 ) { case 16 : case 32 : x@1 = 123 ; break ; } }", parse(clang)); } void characterLiteral() { @@ -192,7 +186,7 @@ private: " |-CXXRecordDecl 0x274c758 col:7 implicit class C\n" " `-CXXMethodDecl 0x274c870 col:16 foo 'void ()'\n" " `-CompoundStmt 0x274c930 "; - ASSERT_EQUALS("class C { void foo ( ) { } }", parse(clang)); + ASSERT_EQUALS("class C { void foo ( ) { } } ;", parse(clang)); } void classTemplateDecl1() { @@ -253,7 +247,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 ; } default ( ) { } noexcept-unevaluated ( const C & ) ; noexcept-unevaluated ( C && ) ; } ;", parse(clang)); } void conditionalExpr() { @@ -360,7 +354,7 @@ private: const char clang[] = "`-CXXRecordDecl 0x8ecd60 <1.cpp:1:1, line:4:1> line:1:8 struct S definition\n" " `-CXXDestructorDecl 0x8ed088 col:3 ~S 'void () noexcept'\n" " `-CompoundStmt 0x8ed1a8 "; - ASSERT_EQUALS("struct S\n\n{ ~S ( ) { } }", parse(clang)); + ASSERT_EQUALS("struct S { ~S ( ) { } } ;", parse(clang)); } void cxxForRangeStmt1() { @@ -394,8 +388,7 @@ private: " | `-ImplicitCastExpr 0x4281040 'const char *':'const char *' \n" " | `-DeclRefExpr 0x4281018 'const char *':'const char *' lvalue Var 0x4280ca8 '__begin1' 'const char *':'const char *'\n" " `-CompoundStmt 0x42810e0 "; - ASSERT_EQUALS("void foo ( ) {\n" - "for ( char c1@1 : hello ) { } }", + ASSERT_EQUALS("void foo ( ) { for ( char c1@1 : hello ) { } }", parse(clang)); } @@ -449,8 +442,7 @@ private: " | `-CXXFunctionalCastExpr 0x1592b40 'MyVar':'MyVar' functional cast to MyVar \n" " | `-CXXConstructExpr 0x15929f0 'MyVar':'MyVar' 'void (int)'\n" " | `-IntegerLiteral 0x1570248 'int' 5\n"; - ASSERT_EQUALS("int main ( int argc@1 , char ** argv@2 ) {\n" - "MyVar setCode@3 = MyVar ( 5 ) ; }", + ASSERT_EQUALS("int main ( int argc@1 , char ** argv@2 ) { MyVar setCode@3 = MyVar ( 5 ) ; }", parse(clang)); } @@ -498,11 +490,7 @@ private: "| `-CXXMethodDecl 0x21ccc68 col:6 foo 'void ()'\n" "`-CXXMethodDecl 0x21ccd60 parent 0x21cca40 prev 0x21ccc68 col:12 foo 'void ()'\n" " `-CompoundStmt 0x21cce50 "; - ASSERT_EQUALS("class Fred\n" - "{ void foo ( ) ; }\n" - "\n" - "\n" - "void foo ( ) { }", parse(clang)); + ASSERT_EQUALS("class Fred { void foo ( ) ; } ; void foo ( ) { }", parse(clang)); } void cxxNewExpr1() { @@ -512,8 +500,7 @@ private: " `-CXXNewExpr 0x3a97e68 'int *' array Function 0x3a978c0 'operator new[]' 'void *(unsigned long)'\n" " `-ImplicitCastExpr 0x3a97e18 'unsigned long' \n" " `-IntegerLiteral 0x3a97de0 'int' 100"; - ASSERT_EQUALS("int * i@1 = new int ;\n" - "int * j@2 = new int [ 100 ] ;", + ASSERT_EQUALS("int * i@1 = new int ; int * j@2 = new int [ 100 ] ;", parse(clang)); } @@ -523,8 +510,7 @@ private: "| `-ReturnStmt 0x5c4308 \n" "| `-CXXNewExpr 0x5c42c8 'S *' Function 0x59a378 'operator new' 'void *(unsigned long)'\n" "| `-CXXConstructExpr 0x5c42a0 'S' 'void () noexcept'"; - ASSERT_EQUALS("struct S * f ( ) {\n" - "return new S ( ) ; }", + ASSERT_EQUALS("struct S * f ( ) { return new S ( ) ; }", parse(clang)); } @@ -554,6 +540,11 @@ private: ASSERT_EQUALS("class Foo ;", parse(clang)); } + void cxxRecordDecl2() { + const char clang[] = "`-CXXRecordDecl 0x34cc5f8 <1.cpp:2:1, col:7> col:7 struct Foo definition"; + ASSERT_EQUALS("struct Foo { } ;", parse(clang)); + } + void cxxStaticCastExpr1() { const char clang[] = "`-VarDecl 0x2e0e650 col:5 a 'int' cinit\n" " `-CXXStaticCastExpr 0x2e0e728 'int' static_cast \n" @@ -601,7 +592,7 @@ private: " `-DefaultStmt 0x1847830 \n" " `-ReturnStmt 0x1847820 \n" " `-IntegerLiteral 0x1847800 'int' 1"; - ASSERT_EQUALS("int foo ( int rc@1 ) {\n\nswitch ( rc@1 ) {\ndefault : return 1 ; } }", parse(clang)); + ASSERT_EQUALS("int foo ( int rc@1 ) { switch ( rc@1 ) { default : return 1 ; } }", parse(clang)); } void doStmt() { @@ -661,8 +652,7 @@ private: " | `-DeclRefExpr 0x24b2d28 'int' lvalue ParmVar 0x24b2ae0 'x' 'int'\n" " `-ImplicitCastExpr 0x24b2d90 'int' \n" " `-DeclRefExpr 0x24b2d50 'int' lvalue ParmVar 0x24b2b58 'y' 'int'"; - ASSERT_EQUALS("int foo ( int x@1 , int y@2 ) {\n\n" - "return x@1 / y@2 ; }", parse(clang)); + ASSERT_EQUALS("int foo ( int x@1 , int y@2 ) { return x@1 / y@2 ; }", parse(clang)); } void funcdecl3() { @@ -711,8 +701,7 @@ private: " |-ImplicitCastExpr 0x333afc8 'int (*)(int)' \n" " | `-DeclRefExpr 0x333af00 'int (int)' lvalue Function 0x333ae00 'foo' 'int (int)' (FunctionTemplate 0x333a860 'foo')\n" " `-IntegerLiteral 0x333ab48 'int' 1"; - ASSERT_EQUALS("int foo ( int t@1 ) { return t@1 + 1 ; }\n" - "int bar ( ) { foo ( 1 ) ; }", parse(clang)); + ASSERT_EQUALS("int foo ( int t@1 ) { return t@1 + 1 ; } int bar ( ) { foo ( 1 ) ; }", parse(clang)); } void ifelse() { @@ -728,9 +717,7 @@ private: " | `-IntegerLiteral 0x2637cb8 'int' 10\n" " |-CompoundStmt 0x2637d18 \n" " `-CompoundStmt 0x2637d28 "; - ASSERT_EQUALS("int foo ( int x@1 ) {\n" - "if ( x@1 > 10 ) { }\n" - "else { } }", parse(clang)); + ASSERT_EQUALS("int foo ( int x@1 ) { if ( x@1 > 10 ) { } else { } }", parse(clang)); } void ifStmt() { @@ -774,8 +761,7 @@ private: " `-ImplicitCastExpr 0x2441e40 'int' \n" " `-MemberExpr 0x2441e08 'int' lvalue .x 0x2441b48\n" " `-DeclRefExpr 0x2441de0 'struct S':'struct S' lvalue ParmVar 0x2441be8 's' 'struct S':'struct S'"; - ASSERT_EQUALS("struct S { int x@1 ; } ;\n" - "int foo ( struct S s@2 ) { return s@2 . x@1 ; }", + ASSERT_EQUALS("struct S { int x@1 ; } ; int foo ( struct S s@2 ) { return s@2 . x@1 ; }", parse(clang)); } @@ -790,9 +776,7 @@ private: const char clang[] = "`-RecordDecl 0x354eac8 <1.c:1:1, line:4:1> line:1:8 struct S definition\n" " |-FieldDecl 0x354eb88 col:7 x 'int'\n" " `-FieldDecl 0x354ebe8 col:7 y 'int'"; - ASSERT_EQUALS("struct S\n" - "{ int x@1 ;\n" - "int y@2 ; } ;", + ASSERT_EQUALS("struct S { int x@1 ; int y@2 ; } ;", parse(clang)); } @@ -868,8 +852,7 @@ private: " `-ImplicitCastExpr 0x32b8c00 'int' \n" " `-DeclRefExpr 0x32b8bd8 'int' lvalue Var 0x32b8aa0 'a' 'int'"; - ASSERT_EQUALS("int a@1 = 1 ;\n" - "int b@2 = a@1 ;", + ASSERT_EQUALS("int a@1 = 1 ; int b@2 = a@1 ;", parse(clang)); } @@ -884,11 +867,7 @@ private: " | `-IntegerLiteral 0x3873d00 'int' 0\n" " `-IntegerLiteral 0x3873d88 'int' 0\n"; - ASSERT_EQUALS("int [10] a@1 ;\n" // <- TODO - "\n" - "void foo ( ) {\n" - "\n" - "a@1 [ 0 ] = 0 ; }", + ASSERT_EQUALS("int [10] a@1 ; void foo ( ) { a@1 [ 0 ] = 0 ; }", parse(clang)); } @@ -920,8 +899,7 @@ private: " |-<<>>\n" " |-IntegerLiteral 0x3d45bf8 'int' 0\n" " `-NullStmt 0x3d45c18 "; - ASSERT_EQUALS("void foo ( ) {\n" - "while ( 0 ) { ; } }", + ASSERT_EQUALS("void foo ( ) { while ( 0 ) { ; } }", parse(clang)); } @@ -957,11 +935,7 @@ private: "`-VarDecl 0x29ad898 col:9 x 'ns::abc':'ns::abc' cinit\n" " `-DeclRefExpr 0x29ad998 'ns::abc' EnumConstant 0x29ad7b0 'c' 'ns::abc'\n"; - ASSERT_EQUALS("namespace ns\n" - "{ enum abc { a , b , c } }\n" - "\n" - "\n" - "ns::abc x@1 = c ;", parse(clang)); + ASSERT_EQUALS("namespace ns { enum abc { a , b , c } } ns::abc x@1 = c ;", parse(clang)); GET_SYMBOL_DB(clang);