Skip to content

Instantly share code, notes, and snippets.

@wilzbach
Created February 20, 2017 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wilzbach/0cb7f0bff0d786683065ad3150f0dbe3 to your computer and use it in GitHub Desktop.
Save wilzbach/0cb7f0bff0d786683065ad3150f0dbe3 to your computer and use it in GitHub Desktop.
Set possibly immutable variables in Phobos to const
  1. Enable the DScanner check

In the .dscanner.ini, set the immutable check to enable:

could_be_immutable_check="enabled"
  1. Generate a file with all Dscanner warnings

make -f posix style | tail -n+16 

Instead of tail you can also manually cut of the lines in the beginning)

  1. Fix the warnings

rdmd --compiler=../dmd/src/dmd <path-to-replacement-script>
  1. Reset the replacements

With git, it's as simple as:

git checkout std
#!/usr/bin/env rdmd
void main(string[] args)
{
import std.algorithm, std.array, std.ascii, std.conv, std.file, std.getopt,
std.path, std.range, std.regex, std.stdio, std.string;
string inFileList = "list";
string repoDir = getcwd;
getopt(args, "listfile|l", &inFileList,
"repodir|d", &repoDir);
struct Entry
{
size_t line;
size_t column;
}
Entry[][string] replacements;
auto re = regex(`(.+)\(([0-9]+):([0-9]+)\)`);
// sortedness from dscanner is expected
foreach (entry; File(inFileList).byLineCopy)
{
auto m = entry.matchFirst(re);
if (m.length < 3)
continue;
const key = m[1];
replacements[key] ~= Entry(m[2].to!size_t, m[3].to!size_t);
}
foreach (key, values; replacements.byPair)
{
auto tmpFile = tempDir ~ "/" ~ key.replace("/", "_");
auto outFile = File(tmpFile, "w");
auto destFile = repoDir.buildPath(key);
writeln("open: ", key);
auto lines = File(destFile).byLineCopy.array;
scope(exit) {
// dump file
foreach (line; lines)
outFile.writeln(line);
outFile.flush;
outFile.flush;
tmpFile.copy(key);
tmpFile.remove;
}
foreach (entry; values)
{
auto line = entry.line - 1; // dscanner lines start with 1
// try to avoid false positives at all costs
if (lines[line].canFind("="))
{
auto tmp = lines[line].splitter("=");
lines[line] = tmp.front.replace(" auto ", " const ") ~ "=" ~ tmp.dropOne.join("=");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment