Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Use @safe and @trusted #840

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion src/dscanner/analysis/base.d
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected:
override void visit(const T structDec)
{
inAggregate = true;
structDec.accept(this);
() @trusted { structDec.accept(this); } ();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this accept call can generally be @trusted, this should probably stay @system

inAggregate = false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/dscanner/analysis/enumarrayliteral.d
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ final class EnumArrayLiteralCheck : BaseAnalyzer
~ part.identifier.text ~ " = [ ...' instead.");
}
}
autoDec.accept(this);
() @trusted { autoDec.accept(this); } ();
}
}
2 changes: 1 addition & 1 deletion src/dscanner/analysis/redundant_attributes.d
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ unittest
assertAnalyzerWarnings(q{
unittest
{
@safe:

@safe void foo();
@system
{
Expand Down
5 changes: 2 additions & 3 deletions src/dscanner/analysis/undocumented.d
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ private:
{
assert(isProtection(p));
}
body
do
{
stack[$ - 1].protection = p;
}
Expand All @@ -275,7 +275,7 @@ private:
{
assert(isProtection(p));
}
body
do
{
stack ~= ProtectionInfo(p, false);
}
Expand Down Expand Up @@ -349,4 +349,3 @@ unittest

stderr.writeln("Unittest for UndocumentedDeclarationCheck passed.");
}

2 changes: 1 addition & 1 deletion src/dscanner/etags.d
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ version = UseModuleContext;
* tagAll = if set, tag private/package declaration too
* fileNames = tags will be generated from these files
*/
void printEtags(File output, bool tagAll, string[] fileNames)
void printEtags(File output, bool tagAll, string[] fileNames) @trusted
{
LexerConfig config;
StringCache cache = StringCache(StringCache.defaultBucketCount);
Expand Down
14 changes: 12 additions & 2 deletions src/dscanner/highlighter.d
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ import std.stdio;
import std.array;
import dparse.lexer;

@safe:

// http://ethanschoonover.com/solarized
void highlight(R)(ref R tokens, string fileName)
{
() @trusted {
stdout.writeln(q"[
<!DOCTYPE html>
<html>
Expand All @@ -31,6 +34,7 @@ html { background-color: #fdf6e3; color: #002b36; }
.cons { color: #859900; font-weight: bold; }
</style>
<pre>]");
} ();

while (!tokens.empty)
{
Expand All @@ -57,17 +61,23 @@ html { background-color: #fdf6e3; color: #002b36; }
// Stupid Windows automatically does a LF → CRLF, so
// CRLF → CRCRLF, which is obviously wrong.
// Strip out the CR characters here to avoid this.
() @trusted {
stdout.write(t.text.replace("<", "&lt;").replace("\r", ""));
} ();
}
else
() @trusted {
stdout.write(t.text.replace("<", "&lt;"));
} ();
}

}
stdout.writeln("</pre>\n</body></html>");
() @trusted {
stdout.writeln("</pre>\n</body></html>");
} ();
}

void writeSpan(string cssClass, string value)
void writeSpan(string cssClass, string value) @trusted
{
version (Windows)
stdout.write(`<span class="`, cssClass, `">`, value.replace("&",
Expand Down
76 changes: 44 additions & 32 deletions src/dscanner/main.d
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ import inifiled;

import dsymbol.modulecache;

@safe:

version (unittest)
void main()
void main() @trusted
{
}
else
int main(string[] args)
int main(string[] args) @trusted
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the point of this change?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't main be @safe instead of @trusted?

{
bool sloc;
bool highlight;
Expand Down Expand Up @@ -74,48 +76,55 @@ else
try
{
// dfmt off
() @trusted {
getopt(args, std.getopt.config.caseSensitive,
"sloc|l", &sloc,
"highlight", &highlight,
"ctags|c", &ctags,
"help|h", &help,
"etags|e", &etags,
"etagsAll", &etagsAll,
"tokenCount|t", &tokenCount,
"syntaxCheck|s", &syntaxCheck,
"ast|xml", &ast,
"imports|i", &imports,
"recursiveImports", &recursiveImports,
"outline|o", &outline,
"tokenDump", &tokenDump,
"styleCheck|S", &styleCheck,
"defaultConfig", &defaultConfig,
"declaration|d", &symbolName,
"config", &configLocation,
"report", &report,
"reportFormat", &reportFormat,
"reportFile", &reportFile,
"I", &importPaths,
"version", &printVersion,
"muffinButton", &muffin,
"explore", &explore,
"skipTests", &skipTests,
"errorFormat|f", &errorFormat);
"sloc|l", &sloc,
"highlight", &highlight,
"ctags|c", &ctags,
"help|h", &help,
"etags|e", &etags,
"etagsAll", &etagsAll,
"tokenCount|t", &tokenCount,
"syntaxCheck|s", &syntaxCheck,
"ast|xml", &ast,
"imports|i", &imports,
"recursiveImports", &recursiveImports,
"outline|o", &outline,
"tokenDump", &tokenDump,
"styleCheck|S", &styleCheck,
"defaultConfig", &defaultConfig,
"declaration|d", &symbolName,
"config", &configLocation,
"report", &report,
"reportFormat", &reportFormat,
"reportFile", &reportFile,
"I", &importPaths,
"version", &printVersion,
"muffinButton", &muffin,
"explore", &explore,
"skipTests", &skipTests,
"errorFormat|f", &errorFormat);
} ();
//dfmt on
}
catch (ConvException e)
{
stderr.writeln(e.msg);
() @trusted {
stderr.writeln(e.msg);
} ();
return 1;
}
catch (GetOptException e)
{
stderr.writeln(e.msg);
() @trusted {
stderr.writeln(e.msg);
} ();
return 1;
}

if (muffin)
{
() @trusted {
stdout.writeln(` ___________
__(#*O 0** @%*)__
_(%*o#*O%*0 #O#%##@)_
Expand All @@ -125,13 +134,16 @@ else
|I|I|I|I|I|I|I|I|I|I|
|I|I|I|I|I|I|I|I|I|I|
|I|I|I|I|I|I|I|I|I|I|`);
} ();
return 0;
}

if (explore)
{
() @trusted {
stdout.writeln("D-Scanner: Scanning...");
stderr.writeln("D-Scanner: No new astronomical objects discovered.");
} ();
return 1;
}

Expand All @@ -153,7 +165,7 @@ else
const(string[]) absImportPaths = importPaths.map!(a => a.absolutePath()
.buildNormalizedPath()).array();

auto alloc = scoped!(dsymbol.modulecache.ASTAllocator)();
scope alloc = () @trusted { return new dsymbol.modulecache.ASTAllocator(); } ();
auto moduleCache = ModuleCache(alloc);

if (absImportPaths.length)
Expand Down Expand Up @@ -328,7 +340,7 @@ else
return 0;
}

void printHelp(string programName)
void printHelp(string programName) @trusted
{
stderr.writefln(`
Usage: %s <options>
Expand Down
8 changes: 5 additions & 3 deletions src/dscanner/reports.d
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import std.array : split, array, Appender, appender;
import dscanner.analysis.base : Message, MessageSet;
import dscanner.analysis.stats_collector;

@safe:

class DScannerJsonReporter
{
struct Issue
Expand Down Expand Up @@ -146,7 +148,7 @@ class SonarQubeGenericIssueDataReporter
{
_issues ~= toIssues(messageSet);
}

void addMessage(Message message, bool isError = false)
{
_issues ~= toIssue(message, isError);
Expand Down Expand Up @@ -185,7 +187,7 @@ class SonarQubeGenericIssueDataReporter
}

private static Issue toIssue(Message message, bool isError = false)
{
{
// dfmt off
Issue issue = {
engineId: "dscanner",
Expand Down Expand Up @@ -242,4 +244,4 @@ class SonarQubeGenericIssueDataReporter
}
}
}
}
}
2 changes: 2 additions & 0 deletions src/dscanner/stats.d
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import std.stdio;
import std.algorithm;
import dparse.lexer;

@safe:

pure nothrow bool isLineOfCode(IdType t)
{
switch (t)
Expand Down
6 changes: 4 additions & 2 deletions src/dscanner/symbol_finder.d
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import std.stdio;
import std.file : isFile;
import std.functional : toDelegate;

@safe:

void findDeclarationOf(File output, string symbolName, string[] fileNames)
{
findDeclarationOf((string fileName, size_t line, size_t column)
Expand All @@ -30,7 +32,7 @@ alias OutputHandler = void delegate(string fileName, size_t line, size_t column)
/// output = Callback which gets called when a declaration is found
/// symbolName = Symbol name to search for
/// fileNames = An array of file names which might contain stdin to read from stdin
void findDeclarationOf(scope OutputHandler output, string symbolName, string[] fileNames)
void findDeclarationOf(scope OutputHandler output, string symbolName, string[] fileNames) @trusted
{
import std.array : uninitializedArray, array;
import std.conv : to;
Expand Down Expand Up @@ -136,7 +138,7 @@ class FinderVisitor : ASTVisitor
{
if (t.name.text == symbolName)
output(fileName, t.name.line, t.name.column);
t.accept(this);
() @trusted { t.accept(this); } ();
}
}

Expand Down
18 changes: 11 additions & 7 deletions src/dscanner/utils.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import std.encoding : BOM, BOMSeq, EncodingException, getBOM;
import std.format : format;
import std.file : exists, read;

@safe:

private void processBOM(ref ubyte[] sourceCode, string fname)
{
enum spec = "D-Scanner does not support %s-encoded files (%s)";
Expand Down Expand Up @@ -54,7 +56,7 @@ ubyte[] readStdin()
ubyte[4096] buf;
while (true)
{
auto b = stdin.rawRead(buf);
auto b = () @trusted { return stdin.rawRead(buf); } ();
if (b.length == 0)
break;
sourceCode.put(b);
Expand All @@ -70,12 +72,12 @@ ubyte[] readFile(string fileName)
return readStdin();
if (!exists(fileName))
{
stderr.writefln("%s does not exist", fileName);
() @trusted { stderr.writefln("%s does not exist", fileName); } ();
return [];
}
File f = File(fileName);
ubyte[] sourceCode;
sourceCode = cast(ubyte[]) fileName.read();
() @trusted { sourceCode = cast(ubyte[]) fileName.read(); } ();
sourceCode.processBOM(fileName);
return sourceCode;
}
Expand Down Expand Up @@ -104,13 +106,15 @@ string[] expandArgs(string[] args)
if (arg == "stdin" || isFileSafe(arg))
rVal ~= arg;
else
() @trusted {
foreach (item; dirEntries(arg, SpanMode.breadth).map!(a => a.name))
{
{
if (isFileSafe(item) && (item.endsWith(`.d`) || item.endsWith(`.di`)) && !item.canFind(dirSeparator ~ '.'))
rVal ~= item;
rVal ~= item;
else
continue;
}
continue;
}
} ();
}
return rVal;
}
Expand Down