diff --git a/src/analysis/run.d b/src/analysis/run.d index 32f1cdb..650c89e 100644 --- a/src/analysis/run.d +++ b/src/analysis/run.d @@ -66,6 +66,8 @@ import dsymbol.conversion.first; import dsymbol.conversion.second; import dsymbol.modulecache : ModuleCache; +import readers; + bool first = true; private alias ASTAllocator = CAllocatorImpl!( @@ -112,11 +114,10 @@ void generateReport(string[] fileNames, const StaticAnalysisConfig config, ulong lineOfCodeCount; foreach (fileName; fileNames) { - File f = File(fileName); - if (f.size == 0) + auto code = fileName == "stdin" ? readStdin() : readFile(fileName); + // Skip files that could not be read and continue with the rest + if (code.length == 0) continue; - auto code = uninitializedArray!(ubyte[])(to!size_t(f.size)); - f.rawRead(code); RollbackAllocator r; const(Token)[] tokens; const Module m = parseModule(fileName, code, &r, cache, true, tokens, &lineOfCodeCount); @@ -151,11 +152,10 @@ bool analyze(string[] fileNames, const StaticAnalysisConfig config, bool hasErrors = false; foreach (fileName; fileNames) { - File f = File(fileName); - if (f.size == 0) + auto code = fileName == "stdin" ? readStdin() : readFile(fileName); + // Skip files that could not be read and continue with the rest + if (code.length == 0) continue; - auto code = uninitializedArray!(ubyte[])(to!size_t(f.size)); - f.rawRead(code); RollbackAllocator r; uint errorCount = 0; uint warningCount = 0; diff --git a/src/main.d b/src/main.d index a012522..60adad9 100644 --- a/src/main.d +++ b/src/main.d @@ -29,6 +29,7 @@ import symbol_finder; import analysis.run; import analysis.config; import dscanner_version; +import readers; import inifiled; @@ -160,6 +161,8 @@ else if (report) styleCheck = true; + immutable usingStdin = args.length == 1; + StringCache cache = StringCache(StringCache.defaultBucketCount); if (defaultConfig) { @@ -171,7 +174,6 @@ else } else if (tokenDump || highlight) { - immutable bool usingStdin = args.length == 1; ubyte[] bytes = usingStdin ? readStdin() : readFile(args[1]); LexerConfig config; config.stringBehavior = StringBehavior.source; @@ -221,11 +223,10 @@ else } else if (syntaxCheck) { - return .syntaxCheck(expandArgs(args), cache, moduleCache) ? 1 : 0; + return .syntaxCheck(usingStdin ? ["stdin"] : expandArgs(args), cache, moduleCache) ? 1 : 0; } else { - bool usingStdin = args.length == 1; if (sloc || tokenCount) { if (usingStdin) @@ -330,35 +331,6 @@ string[] expandArgs(string[] args) return rVal; } -ubyte[] readStdin() -{ - auto sourceCode = appender!(ubyte[])(); - ubyte[4096] buf; - while (true) - { - auto b = stdin.rawRead(buf); - if (b.length == 0) - break; - sourceCode.put(b); - } - return sourceCode.data; -} - -ubyte[] readFile(string fileName) -{ - if (!exists(fileName)) - { - stderr.writefln("%s does not exist", fileName); - return []; - } - File f = File(fileName); - if (f.size == 0) - return []; - ubyte[] sourceCode = uninitializedArray!(ubyte[])(to!size_t(f.size)); - f.rawRead(sourceCode); - return sourceCode; -} - void printHelp(string programName) { stderr.writefln(` diff --git a/src/readers.d b/src/readers.d new file mode 100644 index 0000000..c96838d --- /dev/null +++ b/src/readers.d @@ -0,0 +1,35 @@ +module readers; + +import std.array : appender, uninitializedArray; +import std.stdio : stdin, stderr, File; +import std.conv : to; +import std.file : exists; + +ubyte[] readStdin() +{ + auto sourceCode = appender!(ubyte[])(); + ubyte[4096] buf; + while (true) + { + auto b = stdin.rawRead(buf); + if (b.length == 0) + break; + sourceCode.put(b); + } + return sourceCode.data; +} + +ubyte[] readFile(string fileName) +{ + if (!exists(fileName)) + { + stderr.writefln("%s does not exist", fileName); + return []; + } + File f = File(fileName); + if (f.size == 0) + return []; + ubyte[] sourceCode = uninitializedArray!(ubyte[])(to!size_t(f.size)); + f.rawRead(sourceCode); + return sourceCode; +}