From 681953ae70a7a1af845eaad619ef34292746bf04 Mon Sep 17 00:00:00 2001 From: calum Date: Wed, 31 Oct 2018 11:56:47 +0000 Subject: [PATCH] C#: Reduce logging output and write arguments to separate files. Fix missing response file. --- .../Semmle.Extraction.CSharp/Analyser.cs | 39 ++++--------------- .../CompilerVersion.cs | 2 +- .../Semmle.Extraction.CSharp/Extractor.cs | 22 ++++++----- csharp/extractor/Semmle.Extraction/Message.cs | 2 + .../Semmle.Util/CommandLineExtensions.cs | 26 +++++++++++++ 5 files changed, 50 insertions(+), 41 deletions(-) create mode 100644 csharp/extractor/Semmle.Util/CommandLineExtensions.cs diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Analyser.cs b/csharp/extractor/Semmle.Extraction.CSharp/Analyser.cs index df2d709dc5e..2cbf843a545 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Analyser.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Analyser.cs @@ -399,14 +399,6 @@ namespace Semmle.Extraction.CSharp /// public int TotalErrors => CompilationErrors + ExtractorErrors; - void AppendQuoted(StringBuilder sb, string s) - { - if (s.IndexOf(' ') != -1) - sb.Append('\"').Append(s).Append('\"'); - else - sb.Append(s); - } - /// /// Logs detailed information about this invocation, /// in the event that errors were detected. @@ -414,36 +406,21 @@ namespace Semmle.Extraction.CSharp /// The arguments passed to Roslyn. public void LogDiagnostics(string[] roslynArgs) { - Logger.Log(Severity.Info, " Current working directory: {0}", Directory.GetCurrentDirectory()); Logger.Log(Severity.Info, " Extractor: {0}", Environment.GetCommandLineArgs().First()); if (extractor != null) Logger.Log(Severity.Info, " Extractor version: {0}", extractor.Version); - var sb = new StringBuilder(); - sb.Append(" Expanded command line: "); - bool first = true; - foreach (var arg in Environment.GetCommandLineArgs().Skip(1)) - { - if (arg[0] == '@') - { - foreach (var line in File.ReadAllLines(arg.Substring(1))) - { - if (first) first = false; - else sb.Append(" "); - sb.Append(line); - } - } - else - { - if (first) first = false; - else sb.Append(" "); - AppendQuoted(sb, arg); - } - } - Logger.Log(Severity.Info, sb.ToString()); + + Logger.Log(Severity.Info, " Current working directory: {0}", Directory.GetCurrentDirectory()); if (roslynArgs != null) Logger.Log(Severity.Info, $" Arguments to Roslyn: {string.Join(' ', roslynArgs)}"); + // Create a new file in the log folder. + var argsFile = Path.Combine(Extractor.GetCSharpLogDirectory(), $"csharp.{Path.GetRandomFileName()}.txt"); + + if (roslynArgs.ArchiveCommandLine(argsFile)) + Logger.Log(Severity.Info, $" Arguments have been written to {argsFile}"); + foreach (var error in FilteredDiagnostics) { Logger.Log(Severity.Error, " Compilation error: {0}", error); diff --git a/csharp/extractor/Semmle.Extraction.CSharp/CompilerVersion.cs b/csharp/extractor/Semmle.Extraction.CSharp/CompilerVersion.cs index 6f92b639e1a..c3905272655 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/CompilerVersion.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/CompilerVersion.cs @@ -124,7 +124,7 @@ namespace Semmle.Extraction.CSharp /// Modified list of arguments. static IEnumerable AddDefaultResponse(string responseFile, IEnumerable args) { - return SuppressDefaultResponseFile(args) && File.Exists(responseFile) ? + return SuppressDefaultResponseFile(args) || !File.Exists(responseFile) ? args : new[] { "@" + responseFile }.Concat(args); } diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Extractor.cs b/csharp/extractor/Semmle.Extraction.CSharp/Extractor.cs index 83f2d39eda9..b1b9d4bbda6 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Extractor.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Extractor.cs @@ -34,8 +34,11 @@ namespace Semmle.Extraction.CSharp public void Analysed(int item, int total, string source, string output, TimeSpan time, AnalysisAction action) { - Logger.Log(Severity.Info, " {0} -> {1} ({2})", source, output, - action == AnalysisAction.Extracted ? time.ToString() : action == AnalysisAction.Excluded ? "excluded" : "up to date"); + if (action != AnalysisAction.UpToDate) + { + Logger.Log(Severity.Info, " {0} ({2})", source, output, + action == AnalysisAction.Extracted ? time.ToString() : action == AnalysisAction.Excluded ? "excluded" : "up to date"); + } } public void MissingNamespace(string @namespace) { } @@ -361,27 +364,28 @@ namespace Semmle.Extraction.CSharp /// /// Gets the path to the `csharp.log` file written to by the C# extractor. /// - public static string GetCSharpLogPath() + public static string GetCSharpLogPath() => + Path.Combine(GetCSharpLogDirectory(), "csharp.log"); + + public static string GetCSharpLogDirectory() { string snapshot = Environment.GetEnvironmentVariable("ODASA_SNAPSHOT"); string buildErrorDir = Environment.GetEnvironmentVariable("ODASA_BUILD_ERROR_DIR"); string traps = Environment.GetEnvironmentVariable("TRAP_FOLDER"); - string output = "csharp.log"; if (!string.IsNullOrEmpty(snapshot)) { - snapshot = Path.Combine(snapshot, "log"); - return Path.Combine(snapshot, output); + return Path.Combine(snapshot, "log"); } if (!string.IsNullOrEmpty(buildErrorDir)) { // Used by `qltest` - return Path.Combine(buildErrorDir, output); + return buildErrorDir; } if (!string.IsNullOrEmpty(traps)) { - return Path.Combine(traps, output); + return traps; } - return output; + return Directory.GetCurrentDirectory(); } } } diff --git a/csharp/extractor/Semmle.Extraction/Message.cs b/csharp/extractor/Semmle.Extraction/Message.cs index 696583aab3b..4ca4b011ff2 100644 --- a/csharp/extractor/Semmle.Extraction/Message.cs +++ b/csharp/extractor/Semmle.Extraction/Message.cs @@ -14,5 +14,7 @@ namespace Semmle.Extraction public ISymbol symbol; public SyntaxNode node; public Exception exception; + + public override string ToString() => message; } } diff --git a/csharp/extractor/Semmle.Util/CommandLineExtensions.cs b/csharp/extractor/Semmle.Util/CommandLineExtensions.cs new file mode 100644 index 00000000000..95ea9dc03df --- /dev/null +++ b/csharp/extractor/Semmle.Util/CommandLineExtensions.cs @@ -0,0 +1,26 @@ +using System.Collections.Generic; +using System.IO; +using System.Linq; + +namespace Semmle.Util +{ + public static class CommandLineExtensions + { + /// + /// Archives the first "@" argument in a list of command line arguments. + /// Subsequent "@" arguments are ignored. + /// + /// The raw command line arguments. + /// The full filename to write to. + /// True iff the file was written. + public static bool ArchiveCommandLine(this IEnumerable commandLineArguments, string filename) + { + foreach (var arg in commandLineArguments.Where(arg => arg[0] == '@').Select(arg => arg.Substring(1))) + { + File.Copy(arg, filename, true); + return true; + } + return false; + } + } +}