Skip to content

Commit

Permalink
Add call CancellationToken.ThrowIfCancellationRequested
Browse files Browse the repository at this point in the history
  • Loading branch information
kzrnm committed Mar 20, 2021
1 parent a761011 commit 80f49ad
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Source/SourceExpander.Embedder/EmbedderGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public void Execute(GeneratorExecutionContext context)
.FirstOrDefault(a =>
StringComparer.OrdinalIgnoreCase.Compare(Path.GetFileName(a.Path), CONFIG_FILE_NAME) == 0);

context.CancellationToken.ThrowIfCancellationRequested();
EmbedderConfig config;
if (configFile?.GetText(context.CancellationToken) is { } configText)
{
Expand All @@ -63,6 +64,7 @@ public void Execute(GeneratorExecutionContext context)
if (!config.Enabled)
return;

context.CancellationToken.ThrowIfCancellationRequested();
var embeddingContext = new EmbeddingContext(
compilation,
(CSharpParseOptions)context.ParseOptions,
Expand Down
2 changes: 2 additions & 0 deletions Source/SourceExpander.Embedder/EmbeddingResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@ private EmbeddingAssemblyMetadata ParseEmbeddingAssemblyMetadata()
if (!config.Enabled)
return result;
var infos = ResolveFiles();
cancellationToken.ThrowIfCancellationRequested();
if (infos.Length == 0)
return result;
var json = JsonUtil.ToJson(infos);
cancellationToken.ThrowIfCancellationRequested();

result.IsEnabled = true;
result.EmbeddedAllowUnsafe = compilation.Options.AllowUnsafe;
Expand Down
2 changes: 1 addition & 1 deletion Source/SourceExpander.Generator/CompilationExpander.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public CompilationExpander(CSharpCompilation compilation, SourceFileContainer so

public string ExpandCode(SyntaxTree origTree, CancellationToken cancellationToken)
{
var sb = new StringBuilder();
var semanticModel = Compilation.GetSemanticModel(origTree, true);
var origRoot = origTree.GetCompilationUnitRoot(cancellationToken);

Expand All @@ -44,6 +43,7 @@ public string ExpandCode(SyntaxTree origTree, CancellationToken cancellationToke
.Union(requiedFiles.SelectMany(s => s.Usings))
.ToArray();

var sb = new StringBuilder();
foreach (var u in SourceFileInfoUtil.SortUsings(usings))
sb.AppendLine(u);

Expand Down
3 changes: 1 addition & 2 deletions Source/SourceExpander.Generator/EmbeddedLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ void UpdateCompilation()
if (compilationUpdated) return;
compilationUpdated = true;


IEnumerable<SyntaxTree> newTrees;
if (ConcurrentBuild)
newTrees = compilation.SyntaxTrees.AsParallel(cancellationToken)
Expand All @@ -63,7 +62,7 @@ SyntaxTree Rewrited(SyntaxTree tree)
if (!_cacheExpandedCodes.IsDefault) return _cacheExpandedCodes;
if (!config.Enabled) return ImmutableArray<(string filePath, string expandedCode)>.Empty;
UpdateCompilation();

cancellationToken.ThrowIfCancellationRequested();
var expander = new CompilationExpander(compilation, container, config);
if (ConcurrentBuild)
return compilation.SyntaxTrees.AsParallel(cancellationToken)
Expand Down
4 changes: 3 additions & 1 deletion Source/SourceExpander.Generator/ExpandGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public void Execute(GeneratorExecutionContext context)
.FirstOrDefault(a =>
StringComparer.OrdinalIgnoreCase.Compare(Path.GetFileName(a.Path), CONFIG_FILE_NAME) == 0);

context.CancellationToken.ThrowIfCancellationRequested();
ExpandConfig config;
if (configFile?.GetText(context.CancellationToken) is { } configText)
{
Expand All @@ -70,12 +71,13 @@ public void Execute(GeneratorExecutionContext context)
SourceText.From(EmbeddingCore.SourceCodeClassCode, Encoding.UTF8));
}

context.CancellationToken.ThrowIfCancellationRequested();
var loader = new EmbeddedLoader(compilation, opts, new DiagnosticReporter(context), config, context.CancellationToken);
if (loader.IsEmbeddedEmpty)
context.ReportDiagnostic(DiagnosticDescriptors.EXPAND0003_NotFoundEmbedded());

var expandedCode = CreateExpanded(loader.ExpandedCodes());

context.CancellationToken.ThrowIfCancellationRequested();
context.AddSource("SourceExpander.Expanded.cs",
SourceText.From(expandedCode, Encoding.UTF8));
}
Expand Down

0 comments on commit 80f49ad

Please sign in to comment.