From 5b87a51aa54d4bf68995b2c6afdcaf20ca77e6b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 22 Dec 2009 19:10:33 +0100 Subject: [PATCH] Robert Reif: Fixed #1108 (Only trivial typedefs are tokenized properly) --- lib/tokenize.cpp | 43 ++++++++++++++++++++++++++++++++++++- test/testsimplifytokens.cpp | 37 +++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index ddbffee90..87e46822b 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -400,7 +400,37 @@ void Tokenizer::simplifyTypedef() const char *typeName = 0; bool pointer = false; - if (Token::Match(tok->next(), "%type% %type% ;") || + // pull struct name { ... } out of typedef + if (Token::Match(tok->next(), "struct %type% {")) + { + int structLevel = 1; + Token *tok1 = tok->tokAt(4); + for (; tok1; tok1 = tok1->next()) + { + if (tok1->str() == "}") + { + --structLevel; + if (structLevel == 0) + break; + } + + if (tok1->str() == "{") + ++structLevel; + } + + tok1->insertToken(";"); + tok1 = tok1->next(); + tok1->insertToken("typedef"); + tok1 = tok1->next(); + Token * tok2 = tok1; + tok1->insertToken("struct"); + tok1 = tok1->next(); + tok1->insertToken(tok->strAt(2)); + tok->deleteThis(); + tok = tok2; + } + + if (Token::Match(tok->next(), "%type% *| %type% ;") || Token::Match(tok->next(), "%type% %type% *| %type% ;")) { if (tok->tokAt(3)->str() == ";") @@ -410,6 +440,14 @@ void Tokenizer::simplifyTypedef() typeName = tok->strAt(2); tok = tok->tokAt(3); } + else if (tok->tokAt(2)->str() == "*") + { + pointer = true; + type1 = tok->strAt(1); + type2 = 0; + typeName = tok->strAt(3); + tok = tok->tokAt(4); + } else { pointer = (tok->tokAt(3)->str() == "*"); @@ -616,6 +654,9 @@ bool Tokenizer::tokenize(std::istream &code, const char FileName[]) // Split up variable declarations. simplifyVarDecl(); + // typedef.. + simplifyTypedef(); + // Handle templates.. simplifyTemplates(); diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index b5840c884..76a25b0f7 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -141,6 +141,7 @@ private: TEST_CASE(simplifyTypedef5) TEST_CASE(simplifyTypedef6) TEST_CASE(simplifyTypedef7); + TEST_CASE(simplifyTypedef8); TEST_CASE(reverseArraySyntax) TEST_CASE(simplify_numeric_condition) @@ -2264,6 +2265,42 @@ private: ASSERT_EQUALS(code, tok(code, false)); } + void simplifyTypedef8() + { + const char code[] = "typedef int INT;\n" + "typedef unsigned int UINT;\n" + "typedef int * PINT;\n" + "typedef unsigned int * PUINT;\n" + "typedef struct s S, * PS\n;" + "typedef struct t { int a; } T, *TP;" + "INT ti;\n" + "UINT tui;\n" + "PINT tpi;\n" + "PUINT tpui;" + "S s;\n" + "PS ps;\n" + "T t;\n" + "TP tp;\n"; + + const char expected[] = + "typedef int INT ; " + "typedef unsigned int UINT ; " + "typedef int * PINT ; " + "typedef unsigned int * PUINT ; " + "typedef struct s S ; typedef struct s * PS ; " + "struct t { int a ; } ; typedef struct t T ; typedef struct t * TP ; " + "int ti ; " + "unsigned int tui ; " + "int * tpi ; " + "unsigned int * tpui ; " + "struct s s ; " + "struct s * ps ; " + "struct t t ; " + "struct t * tp ;"; + + ASSERT_EQUALS(expected, tok(code, false)); + } + void reverseArraySyntax() { ASSERT_EQUALS("a [ 13 ]", tok("13[a]"));