-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement support for configurable generator options. (#458)
- Loading branch information
1 parent
ada198f
commit 9649d43
Showing
13 changed files
with
274 additions
and
49 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
...tGenerator/DllImportGenerator.IntegrationTests/DllImportGenerator.IntegrationTests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
DllImportGenerator/DllImportGenerator.UnitTests/DllImportGeneratorOptionsProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.Interop; | ||
|
||
namespace DllImportGenerator.UnitTests | ||
{ | ||
/// <summary> | ||
/// An implementation of <see cref="AnalyzerConfigOptionsProvider"/> that provides configuration in code | ||
/// of the options supported by the DllImportGenerator source generator. Used for testing various configurations. | ||
/// </summary> | ||
internal class DllImportGeneratorOptionsProvider : AnalyzerConfigOptionsProvider | ||
{ | ||
public DllImportGeneratorOptionsProvider(bool useMarshalType, bool generateForwarders) | ||
{ | ||
GlobalOptions = new GlobalGeneratorOptions(useMarshalType, generateForwarders); | ||
} | ||
|
||
public override AnalyzerConfigOptions GlobalOptions { get; } | ||
|
||
public override AnalyzerConfigOptions GetOptions(SyntaxTree tree) | ||
{ | ||
return EmptyOptions.Instance; | ||
} | ||
|
||
public override AnalyzerConfigOptions GetOptions(AdditionalText textFile) | ||
{ | ||
return EmptyOptions.Instance; | ||
} | ||
|
||
private class GlobalGeneratorOptions : AnalyzerConfigOptions | ||
{ | ||
private readonly bool _useMarshalType = false; | ||
private readonly bool _generateForwarders = false; | ||
public GlobalGeneratorOptions(bool useMarshalType, bool generateForwarders) | ||
{ | ||
_useMarshalType = useMarshalType; | ||
_generateForwarders = generateForwarders; | ||
} | ||
|
||
public override bool TryGetValue(string key, [NotNullWhen(true)] out string? value) | ||
{ | ||
switch (key) | ||
{ | ||
case OptionsHelper.UseMarshalTypeOption: | ||
value = _useMarshalType.ToString(); | ||
return true; | ||
|
||
case OptionsHelper.GenerateForwardersOption: | ||
value = _generateForwarders.ToString(); | ||
return true; | ||
|
||
default: | ||
value = null; | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
private class EmptyOptions : AnalyzerConfigOptions | ||
{ | ||
public override bool TryGetValue(string key, [NotNullWhen(true)] out string? value) | ||
{ | ||
value = null; | ||
return false; | ||
} | ||
|
||
public static AnalyzerConfigOptions Instance = new EmptyOptions(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.