internal ExternalFileLocation( string filePath, TextSpan sourceSpan, LinePositionSpan lineSpan ) { _sourceSpan = sourceSpan; _lineSpan = new FileLinePositionSpan(filePath, lineSpan); }
protected void WriteRegion(FileLinePositionSpan span) { // Note that SARIF lines and columns are 1-based, but FileLinePositionSpan is 0-based _writer.WriteObjectStart("region"); _writer.Write("startLine", span.StartLinePosition.Line + 1); _writer.Write("startColumn", span.StartLinePosition.Character + 1); _writer.Write("endLine", span.EndLinePosition.Line + 1); _writer.Write("endColumn", span.EndLinePosition.Character + 1); _writer.WriteObjectEnd(); // region }
private Value GetSpanInfoValue(FileLinePositionSpan lineSpan) { var builder = ArrayBuilder <KeyValuePair <string, Value> > .GetInstance(); builder.Add(CreateSimpleKeyValuePair(WellKnownStrings.LocationSpanStartLine, lineSpan.StartLinePosition.Line)); builder.Add(CreateSimpleKeyValuePair(WellKnownStrings.LocationSpanStartColumn, lineSpan.StartLinePosition.Character)); builder.Add(CreateSimpleKeyValuePair(WellKnownStrings.LocationSpanEndLine, lineSpan.EndLinePosition.Line)); builder.Add(CreateSimpleKeyValuePair(WellKnownStrings.LocationSpanEndColumn, lineSpan.EndLinePosition.Character)); return(Value.Create(builder.ToImmutableAndFree(), this)); }
/// <summary> /// Returns a path for particular location in source that is presented to the user. /// </summary> /// <remarks> /// Used for implementation of <see cref="System.Runtime.CompilerServices.CallerFilePathAttribute"/> /// or for embedding source paths in error messages. /// /// Unlike Dev12 we do account for #line and #ExternalSource directives when determining value for /// <see cref="System.Runtime.CompilerServices.CallerFilePathAttribute"/>. /// </remarks> internal string GetDisplayPath(TextSpan span, SourceReferenceResolver resolver) { FileLinePositionSpan mappedSpan = GetMappedLineSpan(span); if (resolver == null || mappedSpan.Path.IsEmpty()) { return(mappedSpan.Path); } return(resolver.NormalizePath(mappedSpan.Path, baseFilePath: mappedSpan.HasMappedPath?FilePath: null) ?? mappedSpan.Path); }
private Value GetSpanInfoValue(FileLinePositionSpan lineSpan) { // Note that SARIF region lines and columns are specified to be 1-based, but FileLinePositionSpan.Line and Character are 0-based. var builder = ArrayBuilder <KeyValuePair <string, Value> > .GetInstance(); builder.Add(CreateSimpleKeyValuePair(WellKnownStrings.LocationSpanStartLine, lineSpan.StartLinePosition.Line + 1)); builder.Add(CreateSimpleKeyValuePair(WellKnownStrings.LocationSpanStartColumn, lineSpan.StartLinePosition.Character + 1)); builder.Add(CreateSimpleKeyValuePair(WellKnownStrings.LocationSpanEndLine, lineSpan.EndLinePosition.Line + 1)); builder.Add(CreateSimpleKeyValuePair(WellKnownStrings.LocationSpanEndColumn, lineSpan.EndLinePosition.Character + 1)); return(Value.Create(builder.ToImmutableAndFree(), this)); }
public override FileLinePositionSpan GetMappedLineSpan() { // If there's no syntax tree (e.g. because we're binding speculatively), // then just return an invalid span. if (_syntaxTree == null) { FileLinePositionSpan result = default(FileLinePositionSpan); Debug.Assert(!result.IsValid); return(result); } return(_syntaxTree.GetMappedLineSpan(_span)); }
protected virtual string GetDebuggerDisplay() { string result = this.GetType().Name; FileLinePositionSpan pos = GetLineSpan(); if (pos.Path != null) { // user-visible line and column counts are 1-based, but internally are 0-based. result += "(" + pos.Path + "@" + (pos.StartLinePosition.Line + 1) + ":" + (pos.StartLinePosition.Character + 1) + ")"; } return(result); }
protected override void WritePhysicalLocation(Location location) { Debug.Assert(HasPath(location)); FileLinePositionSpan span = location.GetLineSpan(); _writer.WriteObjectStart(); _writer.Write("uri", GetUri(span.Path)); WriteRegion(span); _writer.WriteObjectEnd(); }
protected override void WritePhysicalLocation(Location diagnosticLocation) { Debug.Assert(HasPath(diagnosticLocation)); FileLinePositionSpan span = diagnosticLocation.GetLineSpan(); _writer.WriteObjectStart(); // physicalLocation _writer.WriteObjectStart("artifactLocation"); _writer.Write("uri", GetUri(span.Path)); _writer.WriteObjectEnd(); // artifactLocation WriteRegion(span); _writer.WriteObjectEnd(); }
/// <summary> /// Formats the <see cref="Diagnostic"/> message using the optional <see cref="IFormatProvider"/>. /// </summary> /// <param name="diagnostic">The diagnostic.</param> /// <param name="formatter">The formatter; or null to use the default formatter.</param> /// <returns>The formatted message.</returns> public virtual string Format(Diagnostic diagnostic, IFormatProvider formatter = null) { if (diagnostic == null) { throw new ArgumentNullException(nameof(diagnostic)); } CultureInfo culture = formatter as CultureInfo; switch (diagnostic.Location.Kind) { case LocationKind.SourceFile: case LocationKind.XmlFile: case LocationKind.ExternalFile: FileLinePositionSpan span = diagnostic.Location.GetLineSpan(); FileLinePositionSpan mappedSpan = diagnostic.Location.GetMappedLineSpan(); if (!span.IsValid || !mappedSpan.IsValid) { goto default; } string path, basePath; if (mappedSpan.HasMappedPath) { path = mappedSpan.Path; basePath = span.Path; } else { path = span.Path; basePath = null; } return(string.Format(formatter, "{0}{1}: {2}: {3}", FormatSourcePath(path, basePath, formatter), FormatSourceSpan(mappedSpan.Span, formatter), GetMessagePrefix(diagnostic), diagnostic.GetMessage(culture))); default: return(string.Format(formatter, "{0}: {1}", GetMessagePrefix(diagnostic), diagnostic.GetMessage(culture))); } }
private void WritePhysicalLocation(Location location) { Debug.Assert(HasPath(location)); FileLinePositionSpan span = location.GetLineSpan(); _writer.WriteObjectStart(); _writer.Write("uri", GetUri(span.Path)); // Note that SARIF lines and columns are 1-based, but FileLinePositionSpan is 0-based _writer.WriteObjectStart("region"); _writer.Write("startLine", span.StartLinePosition.Line + 1); _writer.Write("startColumn", span.StartLinePosition.Character + 1); _writer.Write("endLine", span.EndLinePosition.Line + 1); _writer.Write("endColumn", span.EndLinePosition.Character + 1); _writer.WriteObjectEnd(); // region _writer.WriteObjectEnd(); }
internal XSharpSourceLocation(XSharpParserRuleContext context) { var start = context.Start as XSharpToken; var stop = context.Stop as XSharpToken; if (start.SourceSymbol != null) { start = start.SourceSymbol; } if (stop.SourceSymbol != null) { stop = stop.SourceSymbol; } if (stop.Type == XSharpLexer.Eof) { var cu = context.CompilationUnit; if (cu != null) { var stream = cu.XTokens as BufferedTokenStream; var tokens = stream.GetTokens(); var last = tokens.Count - 1; if (tokens[last].Type == XSharpLexer.Eof && last > 0) { stop = (XSharpToken)tokens[last - 1]; } } } var width = stop.StopIndex - start.StartIndex; var lp1 = new LinePosition(start.Line - 1, start.Column); var lp2 = new LinePosition(stop.Line - 1, stop.Column + stop.Text.Length - 1); _context = context; _sourceSpan = new TextSpan(start.StartIndex, width); _lineSpan = new FileLinePositionSpan(context.SourceFileName, new LinePositionSpan(lp1, lp2)); }
public void Deconstruct(out LinePositionSpan span, out FileLinePositionSpan mappedSpan) { span = Span; mappedSpan = MappedSpan; }
private void Dg_SelectionChanged(object sender, EventArgs e) { if (dg.SelectedRows.Count <= 0) { return; } int i = dg.SelectedRows[0].Index; DataGridViewRow v = dg.Rows[i]; if (ef == null) { return; } if (v.Tag == null) { return; } if (v.Tag.GetType() == typeof(Microsoft.Build.Framework.BuildErrorEventArgs)) { Microsoft.Build.Framework.BuildErrorEventArgs b = v.Tag as Microsoft.Build.Framework.BuildErrorEventArgs; string project = b.ProjectFile; string file = Path.GetDirectoryName(b.ProjectFile) + "\\" + b.File; int c = b.LineNumber; int p = b.ColumnNumber; int es = b.EndLineNumber; int ep = b.EndColumnNumber; AsyncCallback callBack = new AsyncCallback(ProcessInformation); workerDisplayLine wde = DisplayLine; wde.BeginInvoke(file, c, p, 100, callBack, "null"); // ef.OpenFileLine(file, c.ToString(), p); } else if (v.Tag.GetType() == typeof(Microsoft.Build.Framework.BuildWarningEventArgs)) { Microsoft.Build.Framework.BuildWarningEventArgs b = v.Tag as Microsoft.Build.Framework.BuildWarningEventArgs; string project = b.ProjectFile; string file = Path.GetDirectoryName(b.ProjectFile) + "\\" + b.File; int c = b.LineNumber; int p = b.ColumnNumber; int es = b.EndLineNumber; int ep = b.EndColumnNumber; AsyncCallback callBack = new AsyncCallback(ProcessInformation); workerDisplayLine wde = DisplayLine; wde.BeginInvoke(file, c, p, 100, callBack, "null"); //ef.OpenFileLine(file, c.ToString(), p); } else //if (v.Tag.GetType() == typeof(Diagnostic)) { Diagnostic b = v.Tag as Diagnostic; if (b != null) { //string project = b.vp.FileName; if (b.Location == Microsoft.CodeAnalysis.Location.None) { return; } if (b.Location.SourceTree == null) { return; } string file = b.Location.SourceTree.FilePath; Microsoft.CodeAnalysis.FileLinePositionSpan c = b.Location.GetLineSpan(); int ps = c.StartLinePosition.Line; int cs = 0; int es = c.StartLinePosition.Character; int start = b.Location.SourceSpan.Start; int length = b.Location.SourceSpan.Length; AsyncCallback callBack = new AsyncCallback(ProcessInformation); workerDisplayLine wde = DisplayLine; wde.BeginInvoke(file, start, length, 100, callBack, "null"); //ef.OpenFileLine(file, c.ToString(), p); } } }
public LineMapping(LinePositionSpan span, int?characterOffset, FileLinePositionSpan mappedSpan) { Span = span; CharacterOffset = characterOffset; MappedSpan = mappedSpan; }
public LineMapping(LinePositionSpan span, FileLinePositionSpan mappedSpan) { Span = span; MappedSpan = mappedSpan; }
public SoftmakeAll.SDK.OperationResult <System.Text.Json.JsonElement> CompileTo(System.IO.Stream Stream) { if (Stream == null) { throw new System.Exception(System.String.Format(SoftmakeAll.SDK.NetReflector.Compiler.NullOrEmptyMessage, "Stream")); } if (System.String.IsNullOrWhiteSpace(this.OutputFileName)) { throw new System.Exception(System.String.Format(SoftmakeAll.SDK.NetReflector.Compiler.NullOrEmptyMessage, "OutputFileName")); } if ((this.CodeFiles == null) || (!(this.CodeFiles.Any()))) { throw new System.Exception(System.String.Format(SoftmakeAll.SDK.NetReflector.Compiler.NullOrEmptyMessage, "Code")); } System.Collections.Generic.List <Microsoft.CodeAnalysis.SyntaxTree> SyntaxTrees = new System.Collections.Generic.List <Microsoft.CodeAnalysis.SyntaxTree>(); foreach (System.Collections.Generic.KeyValuePair <System.String, System.String> CodeFile in this.CodeFiles.Where(c => (!(System.String.IsNullOrWhiteSpace(c.Value))))) { SyntaxTrees.Add(Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree.ParseText(Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree.ParseText(CodeFile.Value.Trim()).GetRoot().NormalizeWhitespace(new System.String(' ', this.IndentSize), System.Environment.NewLine, false).ToFullString(), null, CodeFile.Key)); } if (!(SyntaxTrees.Any())) { throw new System.Exception(System.String.Format(SoftmakeAll.SDK.NetReflector.Compiler.NullOrEmptyMessage, "Code")); } SoftmakeAll.SDK.OperationResult <System.Text.Json.JsonElement> CompileResult = new SoftmakeAll.SDK.OperationResult <System.Text.Json.JsonElement>(); System.Collections.Generic.List <Microsoft.CodeAnalysis.MetadataReference> CurrentReferences = new System.Collections.Generic.List <Microsoft.CodeAnalysis.MetadataReference>(); if (!(System.String.IsNullOrWhiteSpace(this.DefaultReferencesDirectoryPath))) { if (!(System.IO.Directory.Exists(this.DefaultReferencesDirectoryPath))) { throw new System.IO.DirectoryNotFoundException(); } else { foreach (System.String ReferencedFile in System.IO.Directory.GetFiles(this.DefaultReferencesDirectoryPath, "*.*", System.IO.SearchOption.AllDirectories)) { CurrentReferences.Add(Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(ReferencedFile)); } } } if ((this.AdditionalReferences != null) && (this.AdditionalReferences.Any())) { foreach (System.String ReferencedFile in this.AdditionalReferences) { CurrentReferences.Add(Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(ReferencedFile)); } } Microsoft.CodeAnalysis.CSharp.CSharpCompilationOptions CSharpCompilationOptions = new Microsoft.CodeAnalysis.CSharp.CSharpCompilationOptions ( optimizationLevel: Microsoft.CodeAnalysis.OptimizationLevel.Release, outputKind: (Microsoft.CodeAnalysis.OutputKind) this.OutputKind, platform: (Microsoft.CodeAnalysis.Platform) this.Platform ); System.IO.FileInfo OutputFileNameInfo = new System.IO.FileInfo(this.OutputFileName); System.String AssemblyName = OutputFileNameInfo.Name.Substring(0, OutputFileNameInfo.Name.Length - OutputFileNameInfo.Extension.Length); Microsoft.CodeAnalysis.CSharp.CSharpCompilation CSharpCompilation = Microsoft.CodeAnalysis.CSharp.CSharpCompilation.Create(AssemblyName, SyntaxTrees, CurrentReferences, CSharpCompilationOptions); Microsoft.CodeAnalysis.Emit.EmitResult EmitResult = CSharpCompilation.Emit(Stream); if (EmitResult.Diagnostics.Length > 0) { System.Collections.Generic.List <System.Text.Json.JsonElement> Diagnostics = new System.Collections.Generic.List <System.Text.Json.JsonElement>(); foreach (Microsoft.CodeAnalysis.Diagnostic Diagnostic in EmitResult.Diagnostics) { if ((this.OmitCompilationHiddenSeverity) && (Diagnostic.Descriptor.DefaultSeverity == Microsoft.CodeAnalysis.DiagnosticSeverity.Hidden)) { continue; } if ((this.OmitCompilationInfoSeverity) && (Diagnostic.Descriptor.DefaultSeverity == Microsoft.CodeAnalysis.DiagnosticSeverity.Info)) { continue; } if ((this.OmitCompilationWarningSeverity) && (Diagnostic.Descriptor.DefaultSeverity == Microsoft.CodeAnalysis.DiagnosticSeverity.Warning)) { continue; } System.String ID = Diagnostic.Descriptor.Id; if (ID == "CS8019") // CS8019 = using directives { continue; } System.String Severity = Diagnostic.Descriptor.DefaultSeverity.ToString(); System.String Category = Diagnostic.Descriptor.Category; System.String Message = Diagnostic.GetMessage(); System.Text.Json.JsonElement Location = new System.Text.Json.JsonElement(); if ((Diagnostic.Location != null) && (Diagnostic.Location.SourceSpan != null) && (!(Diagnostic.Location.SourceSpan.IsEmpty))) { Microsoft.CodeAnalysis.FileLinePositionSpan FileLinePositionSpan = Diagnostic.Location.GetMappedLineSpan(); System.Nullable <System.Int32> Line = null; System.Nullable <System.Int32> Character = null; if (FileLinePositionSpan.IsValid) { Line = FileLinePositionSpan.StartLinePosition.Line; Character = FileLinePositionSpan.StartLinePosition.Character; } Location = new { Diagnostic.Location.SourceTree.FilePath, Line, Character, Code = Diagnostic.Location.SourceTree.ToString().Substring(Diagnostic.Location.SourceSpan.Start, Diagnostic.Location.SourceSpan.End - Diagnostic.Location.SourceSpan.Start) }.ToJsonElement(); } Diagnostics.Add(new { Severity, Category, ID, Message, Location }.ToJsonElement()); } CompileResult.Data = Diagnostics.ToJsonElement(); } CompileResult.ExitCode = System.Convert.ToInt16((!(EmitResult.Success)) ? -1 : CompileResult.Data.IsValid() ? 1 : 0); return(CompileResult); }
public ExternalFileLocation(string filePath, TextSpan sourceSpan, LinePositionSpan lineSpan) { this.filePath = filePath; this.sourceSpan = sourceSpan; this.lineSpan = new FileLinePositionSpan(filePath, lineSpan); }