Skip to content

Commit

Permalink
splitter: Don't try to reduce module names in D source code
Browse files Browse the repository at this point in the history
Avoid reducing "import std.stdio;" to "import std;".
  • Loading branch information
CyberShadow committed May 2, 2023
1 parent 5b65ade commit d0af4a5
Show file tree
Hide file tree
Showing 65 changed files with 2,601 additions and 1,231 deletions.
45 changes: 45 additions & 0 deletions splitter.d
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import std.string;
import std.traits;
import std.stdio : stderr;
import std.typecons;
import std.utf : byChar;

import polyhash;

Expand Down Expand Up @@ -935,6 +936,49 @@ struct DSplitter
}
}

// Join together module names. We should not attempt to reduce "import std.stdio" to "import std" (or "import stdio").
static void postProcessImports(ref Entity[] entities)
{
if (entities.length && entities[0].head.strip == "import" && !entities[0].children.length && !entities[0].tail.length)
foreach (entity; entities[1 .. $])
{
static void visit(Entity entity)
{
static bool isValidModuleName(string s) { return s.byChar.all!(c => isWordChar(c) || isWhite(c) || c == '.'); }
static bool canBeMerged(Entity entity)
{
return
isValidModuleName(entity.head) &&
entity.children.all!(child => canBeMerged(child)) &&
isValidModuleName(entity.tail);
}

if (canBeMerged(entity))
{
auto root = entity;
// Link all ancestors to the root, and in reverse, therefore making them inextricable.
void link(Entity entity)
{
entity.dependents ~= EntityRef(root);
// root.dependents ~= EntityRef(entity);
foreach (child; entity.children)
link(child);
}
foreach (child; entity.children)
link(child);
}
else
{
foreach (child; entity.children)
visit(child);
}
}

foreach (child; entity.children)
visit(child);
}
}

static void postProcessDependency(ref Entity[] entities)
{
if (entities.length < 2)
Expand Down Expand Up @@ -1155,6 +1199,7 @@ struct DSplitter
postProcessRecursive(e.children);

postProcessSimplify(entities);
postProcessImports(entities);
postProcessTemplates(entities);
postProcessDependency(entities);
postProcessBlockKeywords(entities);
Expand Down
12 changes: 6 additions & 6 deletions tests/args/progress.txt
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
[90.4%] Remove [000010000] => No
[94.2%] Remove [000000110] => No
[95.0%] Remove [000000101] => No
[95.4%] Remove [000000100] => No
[95.4%] Remove [000000100] => No (cached)
============= Depth 10 =============
[ 6.7%] Remove [0010111010] => No
[11.2%] Remove [0010110101] => No
Expand Down Expand Up @@ -402,7 +402,7 @@
[88.3%] Remove [000010000] => No
[92.8%] Remove [000000110] => No
[93.9%] Remove [000000101] => No
[94.4%] Remove [000000100] => No
[94.4%] Remove [000000100] => No (cached)
============= Depth 10 =============
[ 7.6%] Remove [0010111010] => No
[12.7%] Remove [0010110101] => No
Expand Down Expand Up @@ -560,7 +560,7 @@
[85.2%] Unwrap [0000101] => No
[87.2%] Remove [0000100] => No
[90.8%] Remove [0000010] => No
[92.8%] Remove [0000001] => No
[92.8%] Remove [0000001] => No (cached)
[95.9%] Remove [0000000] => No
============= Depth 8 =============
[ 1.0%] Remove [00111010] => No (cached)
Expand Down Expand Up @@ -597,8 +597,8 @@
[87.7%] Remove [00001000] => No
[90.8%] Remove [00000101] => No
[91.3%] Remove [00000100] => No
[92.8%] Remove [00000011] => No
[93.8%] Remove [00000010] => No
[92.8%] Remove [00000011] => No (cached)
[93.8%] Remove [00000010] => No (cached)
============= Depth 9 =============
[ 2.0%] Remove [001110000] => No (cached)
[ 4.5%] Remove [001100000] => No (cached)
Expand Down Expand Up @@ -720,4 +720,4 @@
============= Depth 15 =============
[12.7%] Remove [001011010100000] => No (cached)
[17.3%] Remove [001011000100000] => No (cached)
Done in 505 tests
Done in 500 tests
10 changes: 5 additions & 5 deletions tests/args/src.dump
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
[ "import " /* import */ ]
[
[
[ "std" ]
[ "." /* . */ ] => 10
]
[ "std" ] =8
[ "." /* . */ ] =9 => 10
] => 8 9
[
[ "stdio" ]
] =10
[ "stdio" ] =11
] =10 => 11
]
";\n\n"
]
Expand Down
15 changes: 13 additions & 2 deletions tests/args/src.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,34 @@
{
"children": [
{
"dependents": [
"7"
],
"head": "std"
},
{
"dependents": [
"7"
],
"label": "9",
"tail": "."
}
]
],
"label": "7"
},
{
"children": [
{
"dependents": [
"10"
],
"head": "stdio"
}
],
"dependents": [
"9"
]
],
"label": "10"
}
]
}
Expand Down
14 changes: 7 additions & 7 deletions tests/constructs/progress.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
[95.1%] Remove [000101] => No
[95.1%] Unwrap [000101] => No (cached)
[95.3%] Remove [000100] => No
[96.7%] Remove [000011] => No
[96.7%] Remove [000011] => No (cached)
[97.2%] Remove [000010] => No
============= Depth 7 =============
[ 0.8%] Remove [0001111] => No
Expand All @@ -37,7 +37,7 @@
[95.6%] Remove [0001000] => No
[96.7%] Remove [0000110] => No
[97.2%] Remove [0000101] => No
[97.5%] Remove [0000100] => No
[97.5%] Remove [0000100] => No (cached)
============= Depth 8 =============
[ 1.0%] Remove [00011111] => No
[ 4.6%] Remove [00011110] => No
Expand Down Expand Up @@ -118,17 +118,17 @@
[71.8%] Remove [000101] => No
[71.8%] Unwrap [000101] => No (cached)
[73.4%] Remove [000100] => No
[81.2%] Remove [000011] => No
[81.2%] Remove [000011] => No (cached)
[84.3%] Remove [000010] => No
============= Depth 7 =============
[ 4.6%] Remove [0001111] => No
[39.0%] Remove [0001110] => No (cached)
[51.5%] Remove [0001100] => No (cached)
[73.4%] Remove [0001001] => No
[75.0%] Remove [0001000] => No
[81.2%] Remove [0000110] => No
[84.3%] Remove [0000101] => No
[85.9%] Remove [0000100] => No
[81.2%] Remove [0000110] => No (cached)
[84.3%] Remove [0000101] => No (cached)
[85.9%] Remove [0000100] => No (cached)
============= Depth 8 =============
[ 4.6%] Remove [00011111] => No
[20.3%] Remove [00011110] => No
Expand Down Expand Up @@ -173,4 +173,4 @@
[39.0%] Remove [0001110000000] => No (cached)
[56.2%] Remove [0001100000001] => No (cached)
[57.8%] Remove [0001100000000] => No (cached)
Done in 94 tests
Done in 88 tests
10 changes: 5 additions & 5 deletions tests/constructs/src.dump
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
[ "import " /* import */ ]
[
[
[ "std" ]
[ "." /* . */ ] => 10
]
[ "std" ] =8
[ "." /* . */ ] =9 => 10
] => 8 9
[
[ "stdio" ]
] =10
[ "stdio" ] =11
] =10 => 11
]
";\n\n"
]
Expand Down
15 changes: 13 additions & 2 deletions tests/constructs/src.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,34 @@
{
"children": [
{
"dependents": [
"7"
],
"head": "std"
},
{
"dependents": [
"7"
],
"label": "9",
"tail": "."
}
]
],
"label": "7"
},
{
"children": [
{
"dependents": [
"10"
],
"head": "stdio"
}
],
"dependents": [
"9"
]
],
"label": "10"
}
]
}
Expand Down
10 changes: 5 additions & 5 deletions tests/empty/src.dump
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
[ "import " /* import */ ]
[
[
[ "std" ]
[ "." /* . */ ] => 22
]
[ "std" ] =20
[ "." /* . */ ] =21 => 22
] => 20 21
[
[ "stdio" ]
] =22
[ "stdio" ] =23
] =22 => 23
]
";\n"
]
Expand Down
15 changes: 13 additions & 2 deletions tests/empty/src.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,34 @@
{
"children": [
{
"dependents": [
"19"
],
"head": "std"
},
{
"dependents": [
"19"
],
"label": "21",
"tail": "."
}
]
],
"label": "19"
},
{
"children": [
{
"dependents": [
"22"
],
"head": "stdio"
}
],
"dependents": [
"21"
]
],
"label": "22"
}
]
}
Expand Down
10 changes: 5 additions & 5 deletions tests/emptyset/src.dump
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
[ "import " /* import */ ]
[
[
[ "std" ]
[ "." /* . */ ] => 22
]
[ "std" ] =20
[ "." /* . */ ] =21 => 22
] => 20 21
[
[ "stdio" ]
] =22
[ "stdio" ] =23
] =22 => 23
]
";\n"
]
Expand Down
15 changes: 13 additions & 2 deletions tests/emptyset/src.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,34 @@
{
"children": [
{
"dependents": [
"19"
],
"head": "std"
},
{
"dependents": [
"19"
],
"label": "21",
"tail": "."
}
]
],
"label": "19"
},
{
"children": [
{
"dependents": [
"22"
],
"head": "stdio"
}
],
"dependents": [
"21"
]
],
"label": "22"
}
]
}
Expand Down
12 changes: 6 additions & 6 deletions tests/imports-only/progress.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
============= Depth 11 =============
[45.4%] Remove [00001000000] => No (cached)
============= Depth 12 =============
[45.4%] Remove [000010000001] => No
[51.5%] Remove [000010000000] => No
[45.4%] Remove [000010000001] => No (cached)
[51.5%] Remove [000010000000] => No (cached)
============= Depth 13 =============
[45.4%] Remove [0000100000010] => No
[51.5%] Remove [0000100000001] => No
[54.5%] Remove [0000100000000] => No
[45.4%] Remove [0000100000010] => No (cached)
[51.5%] Remove [0000100000001] => No (cached)
[54.5%] Remove [0000100000000] => No (cached)
############### ITERATION 1 ################
============= Depth 4 =============
[45.4%] Remove [0000] => No (cached)
Expand All @@ -51,4 +51,4 @@
[45.4%] Remove [0000100000010] => No (cached)
[51.5%] Remove [0000100000001] => No (cached)
[54.5%] Remove [0000100000000] => No (cached)
Done in 15 tests
Done in 10 tests
Loading

0 comments on commit d0af4a5

Please sign in to comment.