/// <summary> /// Constructs a <see cref="EmbeddedText"/> for embedding the given <see cref="SourceText"/>. /// </summary> /// <param name="filePath">The file path (pre-normalization) to use in the PDB.</param> /// <param name="text">The source text to embed.</param> /// <exception cref="ArgumentNullException"> /// <paramref name="filePath"/> is null. /// <paramref name="text"/> is null. /// </exception> /// <exception cref="ArgumentException"> /// <paramref name="filePath"/> empty. /// <paramref name="text"/> cannot be embedded (see <see cref="SourceText.CanBeEmbedded"/>). /// </exception> public static EmbeddedText FromSource(string filePath, SourceText text) { ValidateFilePath(filePath); if (text == null) { throw new ArgumentNullException(nameof(text)); } if (!text.CanBeEmbedded) { throw new ArgumentException( CodeAnalysisResources.SourceTextCannotBeEmbedded, nameof(text) ); } if (!text.PrecomputedEmbeddedTextBlob.IsDefault) { return(new EmbeddedText( filePath, text.GetChecksum(), text.ChecksumAlgorithm, text.PrecomputedEmbeddedTextBlob )); } return(new EmbeddedText( filePath, text.GetChecksum(), text.ChecksumAlgorithm, CreateBlob(text) )); }
public SourceGeneratedDocumentState WithUpdatedGeneratedContent( SourceText sourceText, SyntaxTree lazySyntaxTree, ParseOptions parseOptions, CancellationToken cancellationToken ) { if ( TryGetText(out var existingText) && Checksum.From(existingText.GetChecksum()) == Checksum.From(sourceText.GetChecksum()) && SyntaxTree.Options.Equals(parseOptions) ) { // We can reuse this instance directly return(this); } return(SourceGeneratedDocumentState.Create( this.HintName, sourceText, lazySyntaxTree, this.Id, this.SourceGenerator, this.LanguageServices, this.solutionServices, cancellationToken )); }
public override byte[] GetChecksum() { if (_checksum == null) { _checksum = _sourceText.GetChecksum().ToArray(); } return(_checksum); }
/// <summary> /// Gets the checksum + algorithm id to use in the PDB. /// </summary> internal Cci.DebugSourceInfo GetDebugSourceInfo() { if (_lazyChecksum.IsDefault) { SourceText text = this.GetText(); _lazyChecksum = text.GetChecksum(); _lazyHashAlgorithm = text.ChecksumAlgorithm; } Debug.Assert(!_lazyChecksum.IsDefault); Debug.Assert(_lazyHashAlgorithm != default(SourceHashAlgorithm)); // NOTE: If this tree is to be embedded, it's debug source info should have // been obtained via EmbeddedText.GetDebugSourceInfo() and not here. return(new Cci.DebugSourceInfo(_lazyChecksum, _lazyHashAlgorithm)); }
public SourceGeneratedDocumentState WithUpdatedGeneratedContent(SourceText sourceText, ParseOptions parseOptions) { if (TryGetText(out var existingText) && Checksum.From(existingText.GetChecksum()) == Checksum.From(sourceText.GetChecksum()) && ParseOptions.Equals(parseOptions)) { // We can reuse this instance directly return(this); } return(Create( Identity, sourceText, ParseOptions, this.LanguageServices, this.solutionServices)); }
private static void VerifyChecksum(SourceText text, ImmutableArray <byte> expectedChecksum) { var actualChecksum = text.GetChecksum(); Assert.Equal <byte>(expectedChecksum, actualChecksum); }
public override byte[] GetChecksum() => _sourceText.GetChecksum().ToArray();
public BoundSourceFile Build() { if (stringBuilder != null) { SourceFile.Content = stringBuilder.ToString(); stringBuilder = null; } SourceText text = SourceText; var info = BoundSourceFile.SourceFile.Info; var checksumKey = GetChecksumKey(text.ChecksumAlgorithm); if (checksumKey != null) { var checksum = text.GetChecksum().ToHex(); info.Properties[checksumKey] = checksum; AnnotateDefinition(0, 0, new DefinitionSymbol() { Id = SymbolId.CreateFromId($"{checksumKey}|{checksum}"), ShortName = checksum, ContainerQualifiedName = checksumKey, ProjectId = BoundSourceFile.ProjectId, ReferenceKind = nameof(ReferenceKind.Definition), Kind = checksumKey, }); } classifications.Sort((cs1, cs2) => cs1.Start.CompareTo(cs2.Start)); references.Sort((cs1, cs2) => cs1.Start.CompareTo(cs2.Start)); BoundSourceFile.Definitions.Sort((cs1, cs2) => cs1.Start.CompareTo(cs2.Start)); ReferenceSpan lastReference = null; foreach (var reference in references) { if (lastReference?.Start == reference.Start) { reference.LineNumber = lastReference.LineNumber; reference.LineSpanStart = lastReference.LineSpanStart; reference.LineSpanText = lastReference.LineSpanText; continue; } var line = text.Lines.GetLineFromPosition(reference.Start); if (lastReference?.LineNumber == line.LineNumber) { reference.LineNumber = line.LineNumber; reference.LineSpanStart = lastReference.LineSpanStart + (reference.Start - lastReference.Start); reference.LineSpanText = lastReference.LineSpanText; continue; } reference.LineNumber = line.LineNumber; reference.LineSpanStart = reference.Start - line.Start; reference.LineSpanText = line.ToString(); reference.Trim(); } foreach (var definitionSpan in BoundSourceFile.Definitions) { definitionSpan.Definition.ProjectId = definitionSpan.Definition.ProjectId ?? ProjectId; var line = text.Lines.GetLineFromPosition(definitionSpan.Start); definitionSpan.LineNumber = line.LineNumber; definitionSpan.LineSpanStart = definitionSpan.Start - line.Start; } return(BoundSourceFile); }