From 9c2248254e993247e082b9c627423ec4dbe9c913 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Fri, 19 Mar 2010 16:17:25 +0100 Subject: [PATCH] Here is a patch that adds support for derived structs and fixes a bug that missed some nested structs. (#1493) --- lib/tokenize.cpp | 14 +++++++++---- test/testsimplifytokens.cpp | 12 +++++++++++ test/testtokenize.cpp | 40 +++++++++++++++++++++++++++---------- 3 files changed, 51 insertions(+), 15 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 84cdd1a36..ea6263988 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -6813,12 +6813,15 @@ void Tokenizer::simplifyStructDecl() for (Token *tok = _tokens; tok; tok = tok->next()) { // check for named struct/union - if (Token::Match(tok, "struct|union %type% {")) + if (Token::Match(tok, "struct|union %type% :|{")) { Token *type = tok->next(); Token *next = tok->tokAt(2); - tok = tok->tokAt(2)->link(); + while (next->str() != "{") + next = next->next(); + + tok = next->link(); // check for named type if (Token::Match(tok->next(), "*|&| %type% ,|;|[")) @@ -6826,8 +6829,9 @@ void Tokenizer::simplifyStructDecl() tok->insertToken(";"); tok = tok->next(); tok->insertToken(type->str().c_str()); - tok = next; } + + tok = next; } // check for anonymous struct/union @@ -6837,6 +6841,7 @@ void Tokenizer::simplifyStructDecl() tok = tok->next()->link(); + // check for named type if (Token::Match(tok->next(), "*|&| %type% ,|;|[")) { std::string name; @@ -6848,8 +6853,9 @@ void Tokenizer::simplifyStructDecl() tok->insertToken(";"); tok = tok->next(); tok->insertToken(name.c_str()); - tok = tok1->next(); } + + tok = tok1->next(); } } } diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index d84584033..667f0dbae 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -4221,6 +4221,18 @@ private: const char expected[] = "union ABC { int i ; float f ; } ; ABC abc ;"; ASSERT_EQUALS(expected, tok(code, false)); } + + { + const char code[] = "struct ABC { struct {} def; };"; + const char expected[] = "struct ABC { struct Anonymous0 { } ; Anonymous0 def ; } ;"; + ASSERT_EQUALS(expected, tok(code, false)); + } + + { + const char code[] = "struct ABC : public XYZ { struct {} def; };"; + const char expected[] = "struct ABC : public XYZ { struct Anonymous0 { } ; Anonymous0 def ; } ;"; + ASSERT_EQUALS(expected, tok(code, false)); + } } }; diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 2aef71966..9e35460e9 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -1514,19 +1514,37 @@ private: void varid15() { - const std::string actual = tokenizeDebugListing( - "struct S {\n" - " struct T {\n" - " } t;\n" - "} s;"); + { + const std::string actual = tokenizeDebugListing( + "struct S {\n" + " struct T {\n" + " } t;\n" + "} s;"); - const std::string expected("\n\n##file 0\n" - "1: struct S {\n" - "2: struct T {\n" - "3: } ; T t@1 ;\n" - "4: } ; S s@2 ;\n"); + const std::string expected("\n\n##file 0\n" + "1: struct S {\n" + "2: struct T {\n" + "3: } ; T t@1 ;\n" + "4: } ; S s@2 ;\n"); - ASSERT_EQUALS(expected, actual); + ASSERT_EQUALS(expected, actual); + } + + { + const std::string actual = tokenizeDebugListing( + "struct S {\n" + " struct T {\n" + " } t;\n" + "};"); + + const std::string expected("\n\n##file 0\n" + "1: struct S {\n" + "2: struct T {\n" + "3: } ; T t@1 ;\n" + "4: } ;\n"); + + ASSERT_EQUALS(expected, actual); + } } void varidStl()