diff --git a/lib/tokenize.h b/lib/tokenize.h index 5043c67d73bb..1f5d2abdd6d2 100644 --- a/lib/tokenize.h +++ b/lib/tokenize.h @@ -627,6 +627,17 @@ class CPPCHECKLIB Tokenizer { void setDirectives(std::list directives); + struct TypedefInfo { + std::string name; + std::string filename; + int lineNumber; + int column; + bool used; + }; + std::vector getTypedefInfo() { + return mTypedefInfo; + } + private: const Token *processFunc(const Token *tok2, bool inOperator) const; Token *processFunc(Token *tok2, bool inOperator); @@ -660,13 +671,6 @@ class CPPCHECKLIB Tokenizer { /** sizeof information for known types */ std::map mTypeSize; - struct TypedefInfo { - std::string name; - std::string filename; - int lineNumber; - int column; - bool used; - }; std::vector mTypedefInfo; std::list mDirectives; diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index 7c1555000f5b..aecd30bab771 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -241,6 +241,8 @@ class TestSimplifyTypedef : public TestFixture { TEST_CASE(simplifyTypedefMacro); TEST_CASE(simplifyTypedefOriginalName); + + TEST_CASE(typedefInfo1); } #define tok(...) tok_(__FILE__, __LINE__, __VA_ARGS__) @@ -307,6 +309,22 @@ class TestSimplifyTypedef : public TestFixture { return tokenizer.tokens()->stringifyList(nullptr, false); } + std::vector getTypedefInfo(const char code[]) { + Tokenizer tokenizer(settings1, *this); + + std::istringstream istr(code); + if (!tokenizer.list.createTokens(istr, "file.c")) + return {}; + tokenizer.createLinks(); + tokenizer.simplifyTypedef(); + try { + tokenizer.validate(); + } catch (const InternalError&) { + return {}; + } + return tokenizer.getTypedefInfo(); + } + void c1() { const char code[] = "typedef int t;\n" "t x;"; @@ -4380,6 +4398,12 @@ class TestSimplifyTypedef : public TestFixture { token = Token::findsimplematch(endOfTypeDef, "*", tokenizer.list.back()); ASSERT_EQUALS("rFunctionPointer_fp", token->originalName()); } + + void typedefInfo1() { + const auto& t = getTypedefInfo("typedef int A;\nA x;"); + ASSERT_EQUALS(1, t.size()); + ASSERT_EQUALS("A", t[0].name); + } }; REGISTER_TEST(TestSimplifyTypedef)