Skip to content

Commit

Permalink
Annotate nullability in Bind.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nihlus committed Jul 10, 2018
1 parent b0d5ea0 commit f536011
Show file tree
Hide file tree
Showing 28 changed files with 607 additions and 367 deletions.
4 changes: 4 additions & 0 deletions FodyWeavers.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8" ?>
<Weavers>
<JetBrainsAnnotations/>
</Weavers>
1 change: 1 addition & 0 deletions OpenTK.sln
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{2E399A9C
build/targets/netfx-mono.props = build/targets/netfx-mono.props
build/targets/stylecop.props = build/targets/stylecop.props
build/targets/nuget-common.props = build/targets/nuget-common.props
build/targets/jetbrains.props = build/targets/jetbrains.props
EndProjectSection
EndProject

Expand Down
17 changes: 17 additions & 0 deletions build/targets/jetbrains.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project>
<PropertyGroup>
<DefineConstants>$(DefineConstants);JETBRAINS_ANNOTATIONS</DefineConstants>
</PropertyGroup>

<ItemGroup>
<Content Include="$(AssemblyName).ExternalAnnotations.xml">
<InProject>false</InProject>
<CopyToOutputDirectory>CopyIfNewer</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="11.1.0" PrivateAssets="all" />
<PackageReference Include="JetBrainsAnnotations.Fody" Version="2.2.0" PrivateAssets="all" />
</ItemGroup>
</Project>
2 changes: 2 additions & 0 deletions src/Generator.Bind/Bind.ExternalAnnotations.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<assembly name="Bind" />
41 changes: 29 additions & 12 deletions src/Generator.Bind/BindingsWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
using Bind.Structures;
using Bind.Writers;
using Humanizer;
using JetBrains.Annotations;

namespace Bind
{
Expand All @@ -49,13 +50,18 @@ internal sealed class BindingsWriter
/// Writes the bindings for the given generator to file.
/// </summary>
/// <param name="generator">The generator.</param>
public void WriteBindings(IGenerator generator)
public void WriteBindings([NotNull] IGenerator generator)
{
Generator = generator;
WriteBindings(generator.Delegates, generator.Wrappers, generator.Enums);
}

private void WriteBindings(DelegateCollection delegates, FunctionCollection wrappers, EnumCollection enums)
private void WriteBindings
(
[NotNull] DelegateCollection delegates,
[NotNull] FunctionCollection wrappers,
[NotNull] EnumCollection enums
)
{
var baseOutputPath = Program.Arguments.OutputPath;

Expand All @@ -73,7 +79,7 @@ private void WriteBindings(DelegateCollection delegates, FunctionCollection wrap
WriteWrappers(wrappers, delegates);
}

private void WriteWrappers(FunctionCollection wrappers, DelegateCollection delegates)
private void WriteWrappers([NotNull] FunctionCollection wrappers, [NotNull] DelegateCollection delegates)
{
Trace.WriteLine($"Writing wrappers to:\t{Generator.Namespace}.{Generator.ClassName}");

Expand All @@ -85,7 +91,7 @@ private void WriteWrappers(FunctionCollection wrappers, DelegateCollection deleg
Directory.CreateDirectory(wrappersOutputDirectory);
}

// Create the interop setup neccesary
// Create the interop setup necessary
var tempInteropFilePath = Path.GetTempFileName();
using (var outputFile = File.Open(tempInteropFilePath, FileMode.OpenOrCreate))
{
Expand Down Expand Up @@ -271,7 +277,7 @@ private void WriteWrappers(FunctionCollection wrappers, DelegateCollection deleg
}
}

private void WriteMethod(SourceWriter sw, FunctionDefinition f)
private void WriteMethod([NotNull] SourceWriter sw, [NotNull] FunctionDefinition f)
{
WriteDocumentation(sw, f);

Expand Down Expand Up @@ -301,7 +307,7 @@ private void WriteMethod(SourceWriter sw, FunctionDefinition f)
}
}

private void WriteDocumentation(SourceWriter sw, FunctionDefinition f)
private void WriteDocumentation([NotNull] SourceWriter sw, [NotNull] FunctionDefinition f)
{
var docs = f.DocumentationDefinition;

Expand Down Expand Up @@ -427,7 +433,11 @@ private void WriteDocumentation(SourceWriter sw, FunctionDefinition f)
}
}

private void WriteConstants(SourceWriter sw, IEnumerable<ConstantDefinition> constants)
private void WriteConstants
(
[NotNull] SourceWriter sw,
[NotNull] IEnumerable<ConstantDefinition> constants
)
{
// Make sure everything is sorted. This will avoid random changes between
// consecutive runs of the program.
Expand Down Expand Up @@ -488,7 +498,7 @@ private void WriteConstants(SourceWriter sw, IEnumerable<ConstantDefinition> con
}
}

private void WriteEnums(EnumCollection enums, FunctionCollection wrappers)
private void WriteEnums([NotNull] EnumCollection enums, [NotNull] FunctionCollection wrappers)
{
// Build a dictionary of which functions use which enums
var enumCounts = new Dictionary<EnumDefinition, List<FunctionDefinition>>();
Expand Down Expand Up @@ -588,7 +598,12 @@ private void WriteEnums(EnumCollection enums, FunctionCollection wrappers)
/// <param name="enumCounts">A dictionary of the functions in which the enum is used.</param>
/// <param name="enum">The enum definition.</param>
/// <returns>The usage string.</returns>
private string GetEnumUsageString(IReadOnlyDictionary<EnumDefinition, List<FunctionDefinition>> enumCounts, EnumDefinition @enum)
[NotNull]
private string GetEnumUsageString
(
[NotNull] IReadOnlyDictionary<EnumDefinition, List<FunctionDefinition>> enumCounts,
[NotNull] EnumDefinition @enum
)
{
var functions = enumCounts[@enum]
.Select(w =>
Expand All @@ -614,15 +629,16 @@ private string GetEnumUsageString(IReadOnlyDictionary<EnumDefinition, List<Funct
return "Not used directly.";
}

private void WriteLicense(SourceWriter sw)
private void WriteLicense([NotNull] SourceWriter sw)
{
var licenseFilePath = Path.Combine(Program.Arguments.LicenseFile);
var licenseContents = File.ReadAllText(licenseFilePath).TrimEnd();

sw.WriteLine(licenseContents);
}

private string GetDeclarationString(DelegateDefinition d, bool isDelegate)
[NotNull]
private string GetDeclarationString([NotNull] DelegateDefinition d, bool isDelegate)
{
var sb = new StringBuilder();

Expand All @@ -641,7 +657,8 @@ private string GetDeclarationString(DelegateDefinition d, bool isDelegate)
return sb.ToString();
}

private string GetDeclarationString(FunctionDefinition f)
[NotNull]
private string GetDeclarationString([NotNull] FunctionDefinition f)
{
var sb = new StringBuilder();

Expand Down
42 changes: 24 additions & 18 deletions src/Generator.Bind/DocProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Xml.XPath;
using Bind.Generators;
using Bind.Structures;
using JetBrains.Annotations;

namespace Bind
{
Expand Down Expand Up @@ -43,7 +44,7 @@ internal class DocProcessor
/// Initializes a new instance of the <see cref="DocProcessor"/> class.
/// </summary>
/// <param name="generator">The API generator.</param>
public DocProcessor(IGenerator generator)
public DocProcessor([NotNull] IGenerator generator)
{
Generator = generator ?? throw new ArgumentNullException();

Expand All @@ -57,13 +58,19 @@ public DocProcessor(IGenerator generator)
foreach (var file in docFiles)
{
var name = Path.GetFileName(file);
if (name is null)
{
continue;
}

if (!_documentationFiles.ContainsKey(name))
{
_documentationFiles.Add(name, file);
}
}
}

[NotNull]
private IGenerator Generator { get; }

/// <summary>
Expand All @@ -72,7 +79,8 @@ public DocProcessor(IGenerator generator)
/// <param name="f">The function.</param>
/// <param name="processor">The enumeration processor.</param>
/// <returns>A documentation definition.</returns>
public DocumentationDefinition Process(FunctionDefinition f, EnumProcessor processor)
[NotNull]
public DocumentationDefinition Process([NotNull] FunctionDefinition f, [NotNull] EnumProcessor processor)
{
if (_documentationCache.ContainsKey(f.WrappedDelegateDefinition.Name))
{
Expand Down Expand Up @@ -111,7 +119,12 @@ public DocumentationDefinition Process(FunctionDefinition f, EnumProcessor proce
// found in the <!-- eqn: :--> comments in the docs.
// Todo: Some simple MathML tags do not include comments, find a solution.
// Todo: Some files include more than 1 function - find a way to map these extra functions.
private DocumentationDefinition ProcessFile(string file, EnumProcessor processor)
[NotNull]
private DocumentationDefinition ProcessFile
(
[NotNull, PathReference] string file,
[NotNull] EnumProcessor processor
)
{
if (_lastFile == file)
{
Expand Down Expand Up @@ -158,22 +171,14 @@ private DocumentationDefinition ProcessFile(string file, EnumProcessor processor
m = RemoveMathml.Match(text);
}

XDocument doc = null;
try
{
doc = XDocument.Parse(text);
_cached = ToInlineDocs(doc, processor);
return _cached;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.WriteLine(doc.ToString());
return null;
}
var doc = XDocument.Parse(text);
_cached = ToInlineDocs(doc, processor);

return _cached;
}

private DocumentationDefinition ToInlineDocs(XDocument doc, EnumProcessor enumProcessor)
[NotNull]
private DocumentationDefinition ToInlineDocs([NotNull] XNode doc, [NotNull] EnumProcessor enumProcessor)
{
if (doc == null || enumProcessor == null)
{
Expand Down Expand Up @@ -215,7 +220,8 @@ private DocumentationDefinition ToInlineDocs(XDocument doc, EnumProcessor enumPr
return inline;
}

private static string Cleanup(string text)
[NotNull]
private static string Cleanup([NotNull] string text)
{
return
string.Join(" ", text
Expand Down
Loading

0 comments on commit f536011

Please sign in to comment.