Simplify Typedef: Fixed typedef simplification in expanded tokens

This commit is contained in:
Daniel Marjamäki 2021-08-29 19:14:53 +02:00
parent 19fea629c6
commit ccfd18b96d
2 changed files with 44 additions and 1 deletions

View File

@ -1168,7 +1168,7 @@ void Tokenizer::simplifyTypedef()
}
// check for typedef that can be substituted
else if (tok2->isNameOnly() &&
else if ((tok2->isNameOnly() || (tok2->isName() && tok2->isExpandedMacro())) &&
(Token::simpleMatch(tok2, pattern.c_str(), pattern.size()) ||
(inMemberFunc && tok2->str() == typeName->str()))) {
// member function class variables don't need qualification

View File

@ -24,6 +24,10 @@
#include "tokenize.h"
#include "tokenlist.h"
#include <sstream>
#include <simplecpp.h>
struct InternalError;
@ -190,6 +194,8 @@ private:
TEST_CASE(simplifyTypedefFunction10); // #5191
TEST_CASE(simplifyTypedefShadow); // #4445 - shadow variable
TEST_CASE(simplifyTypedefMacro);
}
std::string tok(const char code[], bool simplify = true, Settings::PlatformType type = Settings::Native, bool debugwarnings = true) {
@ -219,6 +225,31 @@ private:
return tokenizer.tokens()->stringifyList(nullptr, false);
}
std::string simplifyTypedefP(const char code[]) {
// Clear the error buffer..
errout.str("");
// Raw tokens..
std::vector<std::string> files(1, "test.cpp");
std::istringstream istr(code);
const simplecpp::TokenList tokens1(istr, files, files[0]);
// Preprocess..
simplecpp::TokenList tokens2(files);
std::map<std::string, simplecpp::TokenList*> filedata;
simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI());
// Tokenize..
Tokenizer tokenizer(&settings0, this);
tokenizer.createTokens(std::move(tokens2));
tokenizer.createLinks();
tokenizer.simplifyTypedef();
return tokenizer.tokens()->stringifyList(nullptr, false);
}
void checkSimplifyTypedef(const char code[]) {
errout.str("");
// Tokenize..
@ -3562,6 +3593,18 @@ private:
ASSERT_EQUALS("struct xyz { int x ; } ; void f ( ) { int abc ; int xyz ; }",
tok(code,false));
}
void simplifyTypedefMacro() {
const char code[] = "typedef uint32_t index_t;\n"
"\n"
"#define NO_SEGMENT ((index_t)12)\n"
"\n"
"void foo(index_t prev_segment) {\n"
" if(prev_segment==NO_SEGMENT) {}\n" // <- test that index_t is replaced with uint32_t in the expanded tokens
"}";
ASSERT_EQUALS("void foo ( uint32_t prev_segment ) { if ( prev_segment == ( ( uint32_t ) 12 ) ) { } }",
simplifyTypedefP(code));
}
};
REGISTER_TEST(TestSimplifyTypedef)