Skip to content

Commit f9a112d

Browse files
dmcppcheckclaude
andcommitted
Add DUI::hideSetExpansion to select the new macro expansion
It is not used yet. The simplecpp option is -hideset-expansion. testrunner runs all tests with both macro expansions; HIDESET_EXPANSION_TODO() skips a test that the new one can not handle yet. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 56acbc2 commit f9a112d

3 files changed

Lines changed: 55 additions & 28 deletions

File tree

‎main.cpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ int main(int argc, char **argv)
153153
found = true;
154154
}
155155
break;
156+
case 'h':
157+
if (std::strcmp(arg, "-hideset-expansion")==0) {
158+
dui.hideSetExpansion = true;
159+
found = true;
160+
}
161+
break;
156162
}
157163
if (!found) {
158164
std::cout << "error: option '" << arg << "' is unknown." << std::endl;
@@ -187,6 +193,7 @@ int main(int argc, char **argv)
187193
std::cout << " -e Output errors only." << std::endl;
188194
std::cout << " -f Fail when errors were encountered (exitcode 1)." << std::endl;
189195
std::cout << " -l Print lines numbers." << std::endl;
196+
std::cout << " -hideset-expansion Use the new macro expansion with hide sets (work in progress)." << std::endl;
190197
return 0;
191198
}
192199

‎simplecpp.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ namespace simplecpp {
277277
std::string std;
278278
bool clearIncludeCache{};
279279
bool removeComments{}; /** remove comment tokens from included files */
280+
bool hideSetExpansion{}; /** use the new macro expansion with hide sets (work in progress) */
280281
};
281282

282283
/** List of tokens. */

‎test.cpp‎

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@ namespace {
4141
}
4242

4343
static Input USE_INPUT = Input::Stringstream;
44+
/** the tests are run with the old macro expansion and with the new one with hide sets */
45+
static bool USE_HIDESET_EXPANSION = false;
4446
static int numberOfFailedAssertions = 0;
47+
static int numberOfHideSetTodos = 0;
48+
49+
/** Skip the rest of the test when it is run with the new macro expansion, which can not handle it yet */
50+
#define HIDESET_EXPANSION_TODO() do { if (USE_HIDESET_EXPANSION) { ++numberOfHideSetTodos; return; } } while (false)
4551

4652
#define ASSERT_EQUALS(expected, actual) (assertEquals((expected), (actual), __LINE__))
4753
#define ASSERT_THROW_EQUALS(stmt, e, expected) do { try { stmt; assertThrowFailed(__LINE__); } catch (const e& ex) { assertEquals((expected), (ex.what()), __LINE__); } } while (false)
@@ -58,12 +64,13 @@ static std::string pprint(const std::string &in)
5864
return ret;
5965
}
6066

61-
static const char* inputString(Input input) {
67+
static std::string inputString(Input input) {
68+
const std::string expansion = USE_HIDESET_EXPANSION ? ", hide-set expansion" : "";
6269
switch (input) {
6370
case Input::Stringstream:
64-
return "Stringstream";
71+
return "Stringstream" + expansion;
6572
case Input::CharBuffer:
66-
return "CharBuffer";
73+
return "CharBuffer" + expansion;
6774
}
6875
return ""; // unreachable - needed for GCC and Visual Studio
6976
}
@@ -125,6 +132,14 @@ static void testcase(const std::string &name, void (*f)(), int argc, char * cons
125132

126133
#define TEST_CASE(F) (testcase(#F, F, argc, argv))
127134

135+
/** simplecpp::preprocess() with the macro expansion selected by USE_HIDESET_EXPANSION */
136+
static void runPreprocess(simplecpp::TokenList &output, const simplecpp::TokenList &rawtokens, std::vector<std::string> &files, simplecpp::FileDataCache &cache, const simplecpp::DUI &dui, simplecpp::OutputList *outputList = nullptr, std::list<simplecpp::MacroUsage> *macroUsage = nullptr, std::list<simplecpp::IfCond> *ifCond = nullptr)
137+
{
138+
simplecpp::DUI dui2(dui);
139+
dui2.hideSetExpansion = USE_HIDESET_EXPANSION;
140+
simplecpp::preprocess(output, rawtokens, files, cache, dui2, outputList, macroUsage, ifCond);
141+
}
142+
128143
static simplecpp::TokenList makeTokenList(const char code[], std::size_t size, std::vector<std::string> &filenames, const std::string &filename=std::string(), const simplecpp::DUI &dui = {}, simplecpp::OutputList *outputList = nullptr)
129144
{
130145
switch (USE_INPUT) {
@@ -184,7 +199,7 @@ static std::string preprocess(const char code[], std::size_t size, const simplec
184199
if (dui.removeComments)
185200
tokens.removeComments();
186201
simplecpp::TokenList tokens2(files);
187-
simplecpp::preprocess(tokens2, tokens, files, cache, dui, outputList, macroUsage, ifCond);
202+
runPreprocess(tokens2, tokens, files, cache, dui, outputList, macroUsage, ifCond);
188203
simplecpp::cleanup(cache);
189204
return tokens2.stringify();
190205
}
@@ -295,7 +310,7 @@ static std::string expandedTokens(const char code[], const simplecpp::DUI &dui =
295310
simplecpp::FileDataCache cache;
296311
simplecpp::TokenList tokens(files);
297312
const simplecpp::TokenList rawtokens = makeTokenList(code, files);
298-
simplecpp::preprocess(tokens, rawtokens, files, cache, dui, nullptr, macroUsage);
313+
runPreprocess(tokens, rawtokens, files, cache, dui, nullptr, macroUsage);
299314
std::string ret;
300315
for (const simplecpp::Token *tok = tokens.cfront(); tok; tok = tok->next) {
301316
if (!ret.empty())
@@ -1719,7 +1734,7 @@ static void error4()
17191734
simplecpp::OutputList outputList;
17201735
simplecpp::TokenList tokens2(files);
17211736
const simplecpp::TokenList rawtoken = makeTokenList(code, sizeof(code)-1,files,"test.c");
1722-
simplecpp::preprocess(tokens2, rawtoken, files, cache, simplecpp::DUI(), &outputList);
1737+
runPreprocess(tokens2, rawtoken, files, cache, simplecpp::DUI(), &outputList);
17231738
ASSERT_EQUALS("file0,1,#error,#error x\n", toString(outputList));
17241739
}
17251740

@@ -1732,7 +1747,7 @@ static void error5()
17321747
simplecpp::OutputList outputList;
17331748
simplecpp::TokenList tokens2(files);
17341749
const simplecpp::TokenList rawtokens = makeTokenList(code, sizeof(code)-1,files,"test.c");
1735-
simplecpp::preprocess(tokens2, rawtokens, files, cache, simplecpp::DUI(), &outputList);
1750+
runPreprocess(tokens2, rawtokens, files, cache, simplecpp::DUI(), &outputList);
17361751
ASSERT_EQUALS("file0,1,#error,#error x\n", toString(outputList));
17371752
}
17381753

@@ -1745,7 +1760,7 @@ static void error6()
17451760
simplecpp::OutputList outputList;
17461761
simplecpp::TokenList tokens2(files);
17471762
const simplecpp::TokenList rawtokens = makeTokenList(code, sizeof(code)-1,files,"test.c");
1748-
simplecpp::preprocess(tokens2, rawtokens, files, cache, simplecpp::DUI(), &outputList);
1763+
runPreprocess(tokens2, rawtokens, files, cache, simplecpp::DUI(), &outputList);
17491764
ASSERT_EQUALS("file0,1,#error,#error \n", toString(outputList));
17501765
}
17511766

@@ -1757,7 +1772,7 @@ static void error7()
17571772
simplecpp::OutputList outputList;
17581773
simplecpp::TokenList tokens2(files);
17591774
const simplecpp::TokenList rawtokens = makeTokenList(code, sizeof(code)-1,files,"test.c");
1760-
simplecpp::preprocess(tokens2, rawtokens, files, cache, simplecpp::DUI(), &outputList);
1775+
runPreprocess(tokens2, rawtokens, files, cache, simplecpp::DUI(), &outputList);
17611776
ASSERT_EQUALS("file0,1,#error,#error blabla\n", toString(outputList));
17621777
}
17631778

@@ -1769,7 +1784,7 @@ static void error8()
17691784
simplecpp::OutputList outputList;
17701785
simplecpp::TokenList tokens2(files);
17711786
const simplecpp::TokenList rawtokens = makeTokenList(code, sizeof(code)-1,files,"test.c");
1772-
simplecpp::preprocess(tokens2, rawtokens, files, cache, simplecpp::DUI(), &outputList);
1787+
runPreprocess(tokens2, rawtokens, files, cache, simplecpp::DUI(), &outputList);
17731788
ASSERT_EQUALS("file0,1,#error,#error blabla\n", toString(outputList));
17741789
}
17751790

@@ -2524,7 +2539,7 @@ static void hashhash_empty_placemarker()
25242539
simplecpp::FileDataCache cache;
25252540
simplecpp::TokenList tokens(files);
25262541
const simplecpp::TokenList rawtokens = makeTokenList(code, files);
2527-
simplecpp::preprocess(tokens, rawtokens, files, cache, simplecpp::DUI());
2542+
runPreprocess(tokens, rawtokens, files, cache, simplecpp::DUI());
25282543
std::string str;
25292544
for (const simplecpp::Token *tok = tokens.cfront(); tok; tok = tok->next)
25302545
str += "[" + tok->str() + "]";
@@ -3604,7 +3619,7 @@ static void missingHeader2()
36043619
const simplecpp::TokenList rawtokens = makeTokenList(code,files);
36053620
simplecpp::DUI dui;
36063621
dui.includePaths.emplace_back(".");
3607-
simplecpp::preprocess(tokens2, rawtokens, files, cache, dui, &outputList);
3622+
runPreprocess(tokens2, rawtokens, files, cache, dui, &outputList);
36083623
ASSERT_EQUALS("", toString(outputList));
36093624
}
36103625

@@ -3638,7 +3653,7 @@ static void nestedInclude()
36383653
simplecpp::TokenList tokens2(files);
36393654
simplecpp::DUI dui;
36403655
dui.includePaths.emplace_back(".");
3641-
simplecpp::preprocess(tokens2, rawtokens, files, cache, dui, &outputList);
3656+
runPreprocess(tokens2, rawtokens, files, cache, dui, &outputList);
36423657

36433658
ASSERT_EQUALS("file0,1,include_nested_too_deeply,#include nested too deeply\n", toString(outputList));
36443659
}
@@ -3656,7 +3671,7 @@ static void systemInclude()
36563671
simplecpp::TokenList tokens2(files);
36573672
simplecpp::DUI dui;
36583673
dui.includePaths.emplace_back("include");
3659-
simplecpp::preprocess(tokens2, rawtokens, files, cache, dui, &outputList);
3674+
runPreprocess(tokens2, rawtokens, files, cache, dui, &outputList);
36603675

36613676
ASSERT_EQUALS("", toString(outputList));
36623677
}
@@ -3698,7 +3713,7 @@ static void circularInclude()
36983713
const simplecpp::TokenList rawtokens = makeTokenList(code, files, "test.cpp");
36993714

37003715
cache = simplecpp::load(rawtokens, filenames, dui, &outputList, std::move(cache));
3701-
simplecpp::preprocess(tokens2, rawtokens, files, cache, dui, &outputList);
3716+
runPreprocess(tokens2, rawtokens, files, cache, dui, &outputList);
37023717
}
37033718

37043719
ASSERT_EQUALS("", toString(outputList));
@@ -3723,7 +3738,7 @@ static void multiline2()
37233738
rawtokens.removeComments();
37243739
simplecpp::FileDataCache cache;
37253740
simplecpp::TokenList tokens2(files);
3726-
simplecpp::preprocess(tokens2, rawtokens, files, cache, simplecpp::DUI());
3741+
runPreprocess(tokens2, rawtokens, files, cache, simplecpp::DUI());
37273742
ASSERT_EQUALS("\n\n1", tokens2.stringify());
37283743
}
37293744

@@ -3738,7 +3753,7 @@ static void multiline3() // #28 - macro with multiline comment
37383753
rawtokens.removeComments();
37393754
simplecpp::FileDataCache cache;
37403755
simplecpp::TokenList tokens2(files);
3741-
simplecpp::preprocess(tokens2, rawtokens, files, cache, simplecpp::DUI());
3756+
runPreprocess(tokens2, rawtokens, files, cache, simplecpp::DUI());
37423757
ASSERT_EQUALS("\n\n1", tokens2.stringify());
37433758
}
37443759

@@ -3754,7 +3769,7 @@ static void multiline4() // #28 - macro with multiline comment
37543769
rawtokens.removeComments();
37553770
simplecpp::FileDataCache cache;
37563771
simplecpp::TokenList tokens2(files);
3757-
simplecpp::preprocess(tokens2, rawtokens, files, cache, simplecpp::DUI());
3772+
runPreprocess(tokens2, rawtokens, files, cache, simplecpp::DUI());
37583773
ASSERT_EQUALS("\n\n\n1", tokens2.stringify());
37593774
}
37603775

@@ -4097,7 +4112,7 @@ static void include3() // #16 - crash when expanding macro from header
40974112
simplecpp::TokenList out(files);
40984113
simplecpp::DUI dui;
40994114
dui.includePaths.emplace_back(".");
4100-
simplecpp::preprocess(out, rawtokens_c, files, cache, dui);
4115+
runPreprocess(out, rawtokens_c, files, cache, dui);
41014116

41024117
ASSERT_EQUALS("\n1234", out.stringify());
41034118
}
@@ -4125,7 +4140,7 @@ static void include4() // #27 - -include
41254140
simplecpp::DUI dui;
41264141
dui.includePaths.emplace_back(".");
41274142
dui.includes.emplace_back("27.h");
4128-
simplecpp::preprocess(out, rawtokens_c, files, cache, dui);
4143+
runPreprocess(out, rawtokens_c, files, cache, dui);
41294144

41304145
ASSERT_EQUALS("123", out.stringify());
41314146
}
@@ -4151,7 +4166,7 @@ static void include5() // #3 - handle #include MACRO
41514166
simplecpp::TokenList out(files);
41524167
simplecpp::DUI dui;
41534168
dui.includePaths.emplace_back(".");
4154-
simplecpp::preprocess(out, rawtokens_c, files, cache, dui);
4169+
runPreprocess(out, rawtokens_c, files, cache, dui);
41554170

41564171
ASSERT_EQUALS("\n#line 1 \"3.h\"\n123", out.stringify());
41574172
}
@@ -4171,7 +4186,7 @@ static void include6() // #57 - incomplete macro #include MACRO(,)
41714186
cache.insert({"57.c", rawtokens});
41724187

41734188
simplecpp::TokenList out(files);
4174-
simplecpp::preprocess(out, rawtokens, files, cache, simplecpp::DUI());
4189+
runPreprocess(out, rawtokens, files, cache, simplecpp::DUI());
41754190
}
41764191

41774192

@@ -4197,7 +4212,7 @@ static void include7() // #include MACRO
41974212
simplecpp::TokenList out(files);
41984213
simplecpp::DUI dui;
41994214
dui.includePaths.emplace_back(".");
4200-
simplecpp::preprocess(out, rawtokens_c, files, cache, dui);
4215+
runPreprocess(out, rawtokens_c, files, cache, dui);
42014216

42024217
ASSERT_EQUALS("\n#line 1 \"3.h\"\n123", out.stringify());
42034218
}
@@ -4236,7 +4251,7 @@ static void include9()
42364251
simplecpp::DUI dui;
42374252
dui.includePaths.emplace_back(".");
42384253
dui.removeComments = true;
4239-
simplecpp::preprocess(out, rawtokens_c, files, cache, dui);
4254+
runPreprocess(out, rawtokens_c, files, cache, dui);
42404255

42414256
ASSERT_EQUALS("\n#line 2 \"1.h\"\nx = 1 ;", out.stringify());
42424257
}
@@ -4292,7 +4307,7 @@ static void include11() // #669 - -include with preprocess()
42924307
simplecpp::DUI dui;
42934308
dui.includes.emplace_back("inc.h");
42944309
dui.includes.emplace_back("missing.h");
4295-
simplecpp::preprocess(out, rawtokens_c, files, cache, dui, &outputList);
4310+
runPreprocess(out, rawtokens_c, files, cache, dui, &outputList);
42964311

42974312
ASSERT_EQUALS("123", out.stringify());
42984313
ASSERT_EQUALS(1, outputList.size());
@@ -4619,7 +4634,7 @@ static void stringify1()
46194634
simplecpp::TokenList out(files);
46204635
simplecpp::DUI dui;
46214636
dui.includePaths.emplace_back(".");
4622-
simplecpp::preprocess(out, rawtokens_c, files, cache, dui);
4637+
runPreprocess(out, rawtokens_c, files, cache, dui);
46234638

46244639
ASSERT_EQUALS("\n#line 1 \"A.h\"\n1\n2\n#line 1 \"A.h\"\n1\n2", out.stringify());
46254640
}
@@ -5214,7 +5229,7 @@ static void preprocess_files()
52145229
ASSERT_EQUALS("", *files.cbegin());
52155230

52165231
simplecpp::FileDataCache cache;
5217-
simplecpp::preprocess(tokens2, tokens, files, cache, simplecpp::DUI(), nullptr);
5232+
runPreprocess(tokens2, tokens, files, cache, simplecpp::DUI(), nullptr);
52185233
ASSERT_EQUALS(1, files.size());
52195234
ASSERT_EQUALS("", *files.cbegin());
52205235
}
@@ -5231,7 +5246,7 @@ static void preprocess_files()
52315246
ASSERT_EQUALS("test.cpp", *files.cbegin());
52325247

52335248
simplecpp::FileDataCache cache;
5234-
simplecpp::preprocess(tokens2, tokens, files, cache, simplecpp::DUI(), nullptr);
5249+
runPreprocess(tokens2, tokens, files, cache, simplecpp::DUI(), nullptr);
52355250
ASSERT_EQUALS(1, files.size());
52365251
ASSERT_EQUALS("test.cpp", *files.cbegin());
52375252
}
@@ -5897,5 +5912,9 @@ int main(int argc, char **argv)
58975912
{
58985913
runTests(argc, argv, Input::Stringstream);
58995914
runTests(argc, argv, Input::CharBuffer);
5915+
USE_HIDESET_EXPANSION = true;
5916+
runTests(argc, argv, Input::Stringstream);
5917+
if (numberOfHideSetTodos > 0)
5918+
std::cout << "Tests skipped with the hide-set expansion: " << numberOfHideSetTodos << std::endl;
59005919
return numberOfFailedAssertions > 0 ? EXIT_FAILURE : EXIT_SUCCESS;
59015920
}

0 commit comments

Comments
 (0)