示例#1
0
        public void WithoutError()
        {
            const string output   = "";
            var          messages = RustcMessageJsonParser.Parse(output).ToList();

            Assert.AreEqual(0, messages.Count);
        }
示例#2
0
        public void FileNotFound()
        {
            const string output = "{\"message\":\"couldn't read \\\"main2.rs\\\": file" +
                                  " not found. (os error 2)\",\"code\":null,\"level\":\"error" +
                                  "\",\"spans\":[],\"children\":[],\"rendered\":null}\n";
            var messages = RustcMessageJsonParser.Parse(output).ToList();

            Assert.AreEqual(1, messages.Count);
            Assert.AreEqual(RustcMessageType.Error, messages[0].GetLevelAsEnum());
        }
示例#3
0
        public void NoMain()
        {
            const string output = "{\"message\":\"main function not found\",\"code\":null" +
                                  ",\"level\":\"error\",\"spans\":[],\"children\":[],\"rendered\":null" +
                                  "}\n{\"message\":\"aborting due to previous error\",\"code\":null,\"level" +
                                  "\":\"error\",\"spans\":[],\"children\":[],\"rendered\":null}\n";
            var messages = RustcMessageJsonParser.Parse(output).ToList();

            Assert.AreEqual(2, messages.Count);
        }
示例#4
0
        public void WithWarning()
        {
            const string output = "{\"message\":\"unused variable: `x`, #[warn" +
                                  "(unused_variables)] on by default\",\"code\":null,\"level\":\"war" +
                                  "ning\",\"spans\":[{\"file_name\":\"main.rs\",\"byte_start\":498,\"by" +
                                  "te_end\":499,\"line_start\":12,\"line_end\":12,\"column_start\":9,\"c" +
                                  "olumn_end\":10,\"is_primary\":true,\"text\":[{\"text\":\"    let x " +
                                  "= 42;\\r\",\"highlight_start\":9,\"highlight_end\":10}],\"label\":nu" +
                                  "ll,\"suggested_replacement\":null,\"expansion\":null}],\"chil" +
                                  "dren\":[],\"rendered\":null}\n";
            var messages = RustcMessageJsonParser.Parse(output).ToList();

            Assert.AreEqual(1, messages.Count);
            Assert.AreEqual(RustcMessageType.Warning, messages[0].GetLevelAsEnum());

            var span = messages[0].GetPrimarySpan();

            Assert.NotNull(span);
            Assert.True(span.is_primary);
        }
示例#5
0
        private bool ExecuteInner()
        {
            var useJsonErrorFormat = rustcVersion.Major >= 1 && rustcVersion.Minor >= 12;

            StringBuilder sb = new StringBuilder();

            if (ConfigFlags.Length > 0)
            {
                sb.AppendFormat(" --cfg {0}", String.Join(",", ConfigFlags));
            }
            if (AdditionalLibPaths.Length > 0)
            {
                sb.AppendFormat(" -L {0}", String.Join(",", AdditionalLibPaths));
            }
            if (CrateType.Length > 0)
            {
                sb.AppendFormat(" --crate-type {0}", String.Join(",", CrateType));
            }
            if (Emit.Length > 0)
            {
                sb.AppendFormat(" --emit {0}", String.Join(",", Emit));
            }
            if (!String.IsNullOrWhiteSpace(CrateName))
            {
                sb.AppendFormat(" --crate-name {0}", CrateName);
            }
            if (DebugInfo)
            {
                sb.AppendFormat(" -g");
            }
            if (OutputFile != null)
            {
                sb.AppendFormat(" -o {0}", OutputFile);
            }
            if (optimizationLevel.HasValue)
            {
                sb.AppendFormat(" -C opt-level={0}", Shared.OptimizationLevelExtension.Parse(OptimizationLevel.ToString()).ToBuildString());
            }
            if (OutputDirectory != null)
            {
                sb.AppendFormat(" --out-dir {0}", OutputDirectory);
            }
            if (test.HasValue && test.Value)
            {
                sb.Append(" --test");
            }
            if (TargetTriple != null && !String.Equals(TargetTriple, Shared.Environment.DefaultTarget, StringComparison.OrdinalIgnoreCase))
            {
                sb.AppendFormat(" --target {0}", TargetTriple);
            }
            if (LintsAsWarnings.Length > 0)
            {
                sb.AppendFormat(" -W {0}", String.Join(",", LintsAsWarnings));
            }
            if (LintsAsAllowed.Length > 0)
            {
                sb.AppendFormat(" -A {0}", String.Join(",", LintsAsAllowed));
            }
            if (LintsAsDenied.Length > 0)
            {
                sb.AppendFormat(" -D {0}", String.Join(",", LintsAsDenied));
            }
            if (LintsAsForbidden.Length > 0)
            {
                sb.AppendFormat(" -F {0}", String.Join(",", LintsAsForbidden));
            }
            if (lto.HasValue && lto.Value)
            {
                sb.AppendFormat(" -C lto");
            }
            if (CodegenOptions != null)
            {
                sb.AppendFormat(" -C {0}", CodegenOptions);
            }
            if (useJsonErrorFormat)
            {
                sb.Append(" --error-format=json");
            }
            sb.AppendFormat(" {0}", Input);

            var process = CreateProcess(sb.ToString());

            Log.LogCommandLine(String.Join(" ", process.StartInfo.FileName, process.StartInfo.Arguments));
            try
            {
                StringBuilder error = new StringBuilder();

                using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
                {
                    process.ErrorDataReceived += (sender, e) =>
                    {
                        if (e.Data == null)
                        {
                            errorWaitHandle.Set();
                        }
                        else
                        {
                            error.AppendLine(e.Data);
                        }
                    };

                    process.Start();
                    process.BeginErrorReadLine();
                    process.WaitForExit();
                    errorWaitHandle.WaitOne();
                }

                string errorOutput = error.ToString();
                // We found some warning or errors in the output, print them out
                IEnumerable <RustcMessageHuman> messagesHuman = null;
                IEnumerable <RustcMessageJson>  messageJson   = null;
                bool haveAnyMessages = false;

                if (useJsonErrorFormat)
                {
                    messageJson = RustcMessageJsonParser.Parse(errorOutput);
                    foreach (var msg in messageJson)
                    {
                        LogRustcMessage(msg, WorkingDirectory, Log);
                        haveAnyMessages = true;
                    }
                }
                else
                {
                    messagesHuman = RustcMessageHumanParser.Parse(errorOutput);
                    foreach (var msg in messagesHuman)
                    {
                        LogRustcMessage(msg);
                        haveAnyMessages = true;
                    }
                }

                // rustc failed but we couldn't sniff anything from stderr
                // this could be an internal compiler error or a missing main() function (there are probably more errors without spans)
                if (process.ExitCode != 0 && !haveAnyMessages)
                {
                    // FIXME: This automatically sets the file to VisualRust.Rust.targets. Is there a way to set no file instead?
                    this.Log.LogError(errorOutput);
                    return(false);
                }
                return(process.ExitCode == 0);
            }
            catch (Exception ex)
            {
                Log.LogErrorFromException(ex, true);
                return(false);
            }
        }