From ccfd18b96dce8284c392bc8dccbc009f741ad680 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 29 Aug 2021 19:14:53 +0200 Subject: [PATCH] Simplify Typedef: Fixed typedef simplification in expanded tokens --- lib/tokenize.cpp | 2 +- test/testsimplifytypedef.cpp | 43 ++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 0fc1dc14e..9b6719120 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -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 diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index 20b6af4b5..cfbac96fa 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -24,6 +24,10 @@ #include "tokenize.h" #include "tokenlist.h" +#include +#include + + 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 files(1, "test.cpp"); + std::istringstream istr(code); + const simplecpp::TokenList tokens1(istr, files, files[0]); + + // Preprocess.. + simplecpp::TokenList tokens2(files); + std::map 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)