Skip to content

Commit 8a92e99

Browse files
authored
Fix #14988: Add relative path to preprocessor dump (#8812)
1 parent ab753cd commit 8a92e99

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

lib/preprocessor.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,10 +1067,10 @@ void Preprocessor::dump(std::ostream &out) const
10671067
for (const simplecpp::MacroUsage &macroUsage: mMacroUsage) {
10681068
out << " <macro"
10691069
<< " name=\"" << macroUsage.macroName << "\""
1070-
<< " file=\"" << ErrorLogger::toxml(mTokens.file(macroUsage.macroLocation)) << "\""
1070+
<< " file=\"" << ErrorLogger::toxml(mSettings.relativePaths ? Path::getRelativePath(mTokens.file(macroUsage.macroLocation), mSettings.basePaths) : mTokens.file(macroUsage.macroLocation)) << "\""
10711071
<< " line=\"" << macroUsage.macroLocation.line << "\""
10721072
<< " column=\"" << macroUsage.macroLocation.col << "\""
1073-
<< " usefile=\"" << ErrorLogger::toxml(mTokens.file(macroUsage.useLocation)) << "\""
1073+
<< " usefile=\"" << ErrorLogger::toxml(mSettings.relativePaths ? Path::getRelativePath(mTokens.file(macroUsage.useLocation), mSettings.basePaths) : mTokens.file(macroUsage.useLocation)) << "\""
10741074
<< " useline=\"" << macroUsage.useLocation.line << "\""
10751075
<< " usecolumn=\"" << macroUsage.useLocation.col << "\""
10761076
<< " is-known-value=\"" << bool_to_string(macroUsage.macroValueKnown) << "\""
@@ -1083,7 +1083,7 @@ void Preprocessor::dump(std::ostream &out) const
10831083
out << " <simplecpp-if-cond>" << std::endl;
10841084
for (const simplecpp::IfCond &ifCond: mIfCond) {
10851085
out << " <if-cond"
1086-
<< " file=\"" << ErrorLogger::toxml(mTokens.file(ifCond.location)) << "\""
1086+
<< " file=\"" << ErrorLogger::toxml(mSettings.relativePaths ? Path::getRelativePath(mTokens.file(ifCond.location), mSettings.basePaths) : mTokens.file(ifCond.location)) << "\""
10871087
<< " line=\"" << ifCond.location.line << "\""
10881088
<< " column=\"" << ifCond.location.col << "\""
10891089
<< " E=\"" << ErrorLogger::toxml(ifCond.E) << "\""

test/testpreprocessor.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,8 @@ class TestPreprocessor : public TestFixture {
386386
TEST_CASE(writeLocations);
387387

388388
TEST_CASE(pragmaAsm);
389+
390+
TEST_CASE(dumpPreprocessor);
389391
}
390392

391393
template<size_t size>
@@ -3204,6 +3206,30 @@ class TestPreprocessor : public TestFixture {
32043206
const char code[] = "#pragma asm\n";
32053207
ASSERT_THROW_INTERNAL(getcodeforcfg(settingsDefault, *this, code, "", "test.cpp"), InternalError::SYNTAX);
32063208
}
3209+
3210+
void dumpPreprocessor() {
3211+
const char code[] = "#if M == 0\n"
3212+
"#endif\n;\n";
3213+
std::vector<std::string> files;
3214+
simplecpp::OutputList outputList;
3215+
3216+
Settings settings;
3217+
settings.relativePaths = true;
3218+
settings.basePaths.emplace_back("/some/path");
3219+
3220+
simplecpp::TokenList tokens1(code, files, "/some/path/test.cpp", {}, &outputList);
3221+
Preprocessor preprocessor(tokens1, settings, *this, Standards::Language::CPP);
3222+
(void)preprocessor.preprocess("", files, outputList);
3223+
(void)preprocessor.reportOutput(outputList, true);
3224+
3225+
std::ostringstream ostr;
3226+
preprocessor.dump(ostr);
3227+
ASSERT_EQUALS(
3228+
" <simplecpp-if-cond>\n"
3229+
" <if-cond file=\"test.cpp\" line=\"1\" column=\"2\" E=\"M == 0\" result=\"1\"/>\n"
3230+
" </simplecpp-if-cond>\n",
3231+
ostr.str());
3232+
}
32073233
};
32083234

32093235
REGISTER_TEST(TestPreprocessor)

0 commit comments

Comments
 (0)