Skip to content

Commit

Permalink
Fix danmar#6690: Add isFunctionPointer to typeDef-info
Browse files Browse the repository at this point in the history
  • Loading branch information
swasti16 committed Aug 16, 2024
1 parent 9d9cf75 commit d2e30dc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
10 changes: 10 additions & 0 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,10 @@ namespace {
return mEndToken;
}

Token* nameToken() const {
return mNameToken;
}

private:
static bool isCast(const Token* tok) {
if (Token::Match(tok, "( %name% ) (|%name%|%num%"))
Expand Down Expand Up @@ -1145,6 +1149,7 @@ void Tokenizer::simplifyTypedef()
typedefInfo.lineNumber = typedefToken->linenr();
typedefInfo.column = typedefToken->column();
typedefInfo.used = t.second.isUsed();
typedefInfo.isFunctionPointer = Token::Match(t.second.nameToken(), "%name% ) (");
mTypedefInfo.push_back(std::move(typedefInfo));

t.second.removeDeclaration();
Expand Down Expand Up @@ -1645,6 +1650,7 @@ void Tokenizer::simplifyTypedefCpp()
typedefInfo.lineNumber = typeName->linenr();
typedefInfo.column = typeName->column();
typedefInfo.used = false;
typedefInfo.isFunctionPointer = Token::Match(typeName, "%name% ) (");
mTypedefInfo.push_back(std::move(typedefInfo));

while (!done) {
Expand Down Expand Up @@ -6194,6 +6200,10 @@ std::string Tokenizer::dumpTypedefInfo() const
outs += std::to_string(typedefInfo.used?1:0);
outs += "\"";

outs += " isFunctionPointer=\"";
outs += std::to_string(typedefInfo.isFunctionPointer);
outs += "\"";

outs += "/>";
outs += '\n';
}
Expand Down
1 change: 1 addition & 0 deletions lib/tokenize.h
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,7 @@ class CPPCHECKLIB Tokenizer {
int lineNumber;
int column;
bool used;
bool isFunctionPointer;
};
std::vector<TypedefInfo> mTypedefInfo;

Expand Down
17 changes: 16 additions & 1 deletion test/testsimplifytypedef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ class TestSimplifyTypedef : public TestFixture {
TEST_CASE(simplifyTypedefOriginalName);

TEST_CASE(typedefInfo1);

TEST_CASE(typedefInfo2);
}

#define tok(...) tok_(__FILE__, __LINE__, __VA_ARGS__)
Expand Down Expand Up @@ -4402,10 +4404,23 @@ class TestSimplifyTypedef : public TestFixture {
void typedefInfo1() {
const std::string xml = dumpTypedefInfo("typedef int A;\nA x;");
ASSERT_EQUALS(" <typedef-info>\n"
" <info name=\"A\" file=\"file.c\" line=\"1\" column=\"1\" used=\"1\"/>\n"
" <info name=\"A\" file=\"file.c\" line=\"1\" column=\"1\" used=\"1\" isFunctionPointer=\"0\"/>\n"
" </typedef-info>\n",
xml);
}

void typedefInfo2() {
const std::string xml = dumpTypedefInfo("typedef signed short int16_t;\n"
"typedef void ( *fp16 )( int16_t n );\n"
"void R_11_1 ( void ){\n"
" typedef fp16 ( *pfp16 ) ( void );\n"
"}\n");
ASSERT_EQUALS(" <typedef-info>\n"
" <info name=\"fp16\" file=\"file.c\" line=\"2\" column=\"1\" used=\"1\" isFunctionPointer=\"1\"/>\n"
" <info name=\"int16_t\" file=\"file.c\" line=\"1\" column=\"1\" used=\"1\" isFunctionPointer=\"0\"/>\n"
" <info name=\"pfp16\" file=\"file.c\" line=\"4\" column=\"20\" used=\"0\" isFunctionPointer=\"1\"/>\n"
" </typedef-info>\n",xml);
}
};

REGISTER_TEST(TestSimplifyTypedef)

0 comments on commit d2e30dc

Please sign in to comment.