Skip to content

Commit 599fd9f

Browse files
committed
Fix #13015 (CI: Test typedef-info output)
1 parent a25bc07 commit 599fd9f

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

lib/tokenize.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,17 @@ class CPPCHECKLIB Tokenizer {
627627

628628
void setDirectives(std::list<Directive> directives);
629629

630+
struct TypedefInfo {
631+
std::string name;
632+
std::string filename;
633+
int lineNumber;
634+
int column;
635+
bool used;
636+
};
637+
std::vector<TypedefInfo> getTypedefInfo() {
638+
return mTypedefInfo;
639+
}
640+
630641
private:
631642
const Token *processFunc(const Token *tok2, bool inOperator) const;
632643
Token *processFunc(Token *tok2, bool inOperator);
@@ -660,13 +671,6 @@ class CPPCHECKLIB Tokenizer {
660671
/** sizeof information for known types */
661672
std::map<std::string, int> mTypeSize;
662673

663-
struct TypedefInfo {
664-
std::string name;
665-
std::string filename;
666-
int lineNumber;
667-
int column;
668-
bool used;
669-
};
670674
std::vector<TypedefInfo> mTypedefInfo;
671675

672676
std::list<Directive> mDirectives;

test/testsimplifytypedef.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ class TestSimplifyTypedef : public TestFixture {
241241
TEST_CASE(simplifyTypedefMacro);
242242

243243
TEST_CASE(simplifyTypedefOriginalName);
244+
245+
TEST_CASE(typedefInfo1);
244246
}
245247

246248
#define tok(...) tok_(__FILE__, __LINE__, __VA_ARGS__)
@@ -307,6 +309,22 @@ class TestSimplifyTypedef : public TestFixture {
307309
return tokenizer.tokens()->stringifyList(nullptr, false);
308310
}
309311

312+
std::vector<Tokenizer::TypedefInfo> getTypedefInfo(const char code[]) {
313+
Tokenizer tokenizer(settings1, *this);
314+
315+
std::istringstream istr(code);
316+
if (!tokenizer.list.createTokens(istr, "file.c"))
317+
return {};
318+
tokenizer.createLinks();
319+
tokenizer.simplifyTypedef();
320+
try {
321+
tokenizer.validate();
322+
} catch (const InternalError&) {
323+
return {};
324+
}
325+
return tokenizer.getTypedefInfo();
326+
}
327+
310328
void c1() {
311329
const char code[] = "typedef int t;\n"
312330
"t x;";
@@ -4380,6 +4398,12 @@ class TestSimplifyTypedef : public TestFixture {
43804398
token = Token::findsimplematch(endOfTypeDef, "*", tokenizer.list.back());
43814399
ASSERT_EQUALS("rFunctionPointer_fp", token->originalName());
43824400
}
4401+
4402+
void typedefInfo1() {
4403+
const auto& t = getTypedefInfo("typedef int A;\nA x;");
4404+
ASSERT_EQUALS(1, t.size());
4405+
ASSERT_EQUALS("A", t[0].name);
4406+
}
43834407
};
43844408

43854409
REGISTER_TEST(TestSimplifyTypedef)

0 commit comments

Comments
 (0)