Skip to content

Commit 88ce1c8

Browse files
Parsing tools: added StripPreprocessorDirectives function
1 parent 04b09f9 commit 88ce1c8

File tree

2 files changed

+168
-0
lines changed

2 files changed

+168
-0
lines changed

Common/interface/ParsingTools.hpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#include <cstring>
3333
#include <sstream>
3434
#include <limits>
35+
#include <vector>
36+
#include <algorithm>
3537

3638
#include "../../Primitives/interface/BasicTypes.h"
3739
#include "../../Primitives/interface/FlagEnum.h"
@@ -1225,6 +1227,54 @@ inline int GetArrayIndex(const std::string& Var, std::string& NameWithoutBracket
12251227
return GetArrayIndex(Var.begin(), Var.end(), NameWithoutBrackets);
12261228
}
12271229

1230+
1231+
/// Strips all preprocessor directives from the source string.
1232+
inline void StripPreprocessorDirectives(std::string& Source, const std::vector<std::string>& Directives)
1233+
{
1234+
if (Directives.empty())
1235+
return;
1236+
1237+
auto Pos = Source.begin();
1238+
while (Pos != Source.end())
1239+
{
1240+
// // Comment
1241+
// ^
1242+
Pos = SkipDelimitersAndComments(Pos, Source.end());
1243+
if (Pos == Source.end())
1244+
break;
1245+
1246+
if (*Pos == '#')
1247+
{
1248+
const auto DirectiveStart = Pos;
1249+
// # version 450
1250+
// ^
1251+
1252+
++Pos;
1253+
if (Pos == Source.end())
1254+
break;
1255+
1256+
Pos = SkipDelimiters(Pos, Source.end(), " \t");
1257+
// # version 450
1258+
// ^
1259+
1260+
if (Pos == Source.end())
1261+
break;
1262+
1263+
const std::string DirectiveIdentifier{Pos, SkipIdentifier(Pos, Source.end())};
1264+
if (!DirectiveIdentifier.empty() && std::find(Directives.begin(), Directives.end(), DirectiveIdentifier) != Directives.end())
1265+
{
1266+
const auto LineEnd = SkipLine(Pos, Source.end(), false);
1267+
1268+
Pos = Source.erase(DirectiveStart, LineEnd);
1269+
if (Pos == Source.end())
1270+
break;
1271+
}
1272+
}
1273+
1274+
Pos = SkipLine(Pos, Source.end(), true);
1275+
}
1276+
}
1277+
12281278
} // namespace Parsing
12291279

12301280
} // namespace Diligent

Tests/DiligentCoreTest/src/Common/ParsingToolsTest.cpp

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,4 +1442,122 @@ TEST(Common_ParsingTools, GetArrayIndex)
14421442
Test("xy7[12 ", 3, INT_MIN);
14431443
}
14441444

1445+
TEST(Common_ParsingTools, StripPreprocessorDirectives)
1446+
{
1447+
auto Test = [](std::string Source, const std::string& RefSource, const std::vector<std::string>& Directives) {
1448+
StripPreprocessorDirectives(Source, Directives);
1449+
EXPECT_STREQ(Source.c_str(), RefSource.c_str());
1450+
};
1451+
1452+
Test("", "", {{"version"}});
1453+
Test(" ", " ", {{"version"}});
1454+
Test("// Comment", "// Comment", {{"version"}});
1455+
Test("/* Comment */", "/* Comment */", {{"version"}});
1456+
Test("#", "#", {{"version"}});
1457+
Test("#\n", "#\n", {{"version"}});
1458+
Test("#\nversion", "#\nversion", {{"version"}});
1459+
Test("#\n#version XYZ", "#\n", {{"version"}});
1460+
Test("# ", "# ", {{"version"}});
1461+
Test("# \n", "# \n", {{"version"}});
1462+
Test("#extension XYZ", "#extension XYZ", {{"version"}});
1463+
Test("// #version XYZ", "// #version XYZ", {{"version"}});
1464+
1465+
Test(R"(
1466+
void main()
1467+
{
1468+
})",
1469+
R"(
1470+
void main()
1471+
{
1472+
})",
1473+
{{"version"}});
1474+
1475+
1476+
Test(R"(
1477+
#version 450
1478+
void main()
1479+
{
1480+
})",
1481+
R"(
1482+
1483+
void main()
1484+
{
1485+
})",
1486+
{{"version"}});
1487+
1488+
1489+
Test(R"(
1490+
// Comment
1491+
# version 450
1492+
void main()
1493+
{
1494+
})",
1495+
R"(
1496+
// Comment
1497+
1498+
void main()
1499+
{
1500+
})",
1501+
{{"version"}});
1502+
1503+
1504+
Test(R"(
1505+
/* Comment */ # version 450
1506+
void main()
1507+
{
1508+
})",
1509+
R"(
1510+
/* Comment */
1511+
void main()
1512+
{
1513+
})",
1514+
{{"version"}});
1515+
1516+
1517+
Test(R"(
1518+
/* # version 450 */
1519+
void main()
1520+
{
1521+
})",
1522+
R"(
1523+
/* # version 450 */
1524+
void main()
1525+
{
1526+
})",
1527+
{{"version"}});
1528+
1529+
1530+
Test(R"(
1531+
#version 450
1532+
void main()
1533+
{
1534+
# error Error Message
1535+
})",
1536+
R"(
1537+
#version 450
1538+
void main()
1539+
{
1540+
1541+
})",
1542+
{{"error"}});
1543+
1544+
Test(R"(#version 450
1545+
#extension GL_ARB_separate_shader_objects : enable
1546+
#ifndef GL_ARB_shader_draw_parameters
1547+
#error GL_ARB_shader_draw_parameters is not supported.
1548+
#endif
1549+
void main()
1550+
{
1551+
})",
1552+
R"(
1553+
1554+
#ifndef GL_ARB_shader_draw_parameters
1555+
1556+
#endif
1557+
void main()
1558+
{
1559+
})",
1560+
{{"version"}, {"extension"}, {"error"}});
1561+
}
1562+
14451563
} // namespace

0 commit comments

Comments
 (0)