Skip to content

Commit

Permalink
Fix #13015 (CI: Test typedef-info output)
Browse files Browse the repository at this point in the history
  • Loading branch information
danmar committed Aug 14, 2024
1 parent a25bc07 commit 599fd9f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/tokenize.h
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,17 @@ class CPPCHECKLIB Tokenizer {

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

struct TypedefInfo {
std::string name;
std::string filename;
int lineNumber;
int column;
bool used;
};
std::vector<TypedefInfo> getTypedefInfo() {
return mTypedefInfo;
}

private:
const Token *processFunc(const Token *tok2, bool inOperator) const;
Token *processFunc(Token *tok2, bool inOperator);
Expand Down Expand Up @@ -660,13 +671,6 @@ class CPPCHECKLIB Tokenizer {
/** sizeof information for known types */
std::map<std::string, int> mTypeSize;

struct TypedefInfo {
std::string name;
std::string filename;
int lineNumber;
int column;
bool used;
};
std::vector<TypedefInfo> mTypedefInfo;

std::list<Directive> mDirectives;
Expand Down
24 changes: 24 additions & 0 deletions test/testsimplifytypedef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ class TestSimplifyTypedef : public TestFixture {
TEST_CASE(simplifyTypedefMacro);

TEST_CASE(simplifyTypedefOriginalName);

TEST_CASE(typedefInfo1);
}

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

std::vector<Tokenizer::TypedefInfo> 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;";
Expand Down Expand Up @@ -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)

0 comments on commit 599fd9f

Please sign in to comment.