Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Generator/Driver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,20 @@ void ValidateOptions()
}

if (Options.NoGenIncludeDirs != null)
{
foreach (var incDir in Options.NoGenIncludeDirs)
ParserOptions.AddIncludeDirs(incDir);
}

NewLineType newLineType = Options.OutputNewLineType;
if (Options.OutputNewLineType == NewLineType.Auto)
{
var sourceNewLineType = GeneratorHelpers.GetNewLineTypeFromGitConfig(Options.OutputDir);

newLineType = sourceNewLineType ?? NewLineType.Host;
}

TextGenerator.SetLineEndingType(newLineType);
}

public void Setup()
Expand Down
8 changes: 5 additions & 3 deletions src/Generator/Generators/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,13 @@ public virtual void GenerateComment(RawComment comment)

var lines = new List<string>();

if (comment.BriefText.Contains("\n"))
if (comment.BriefText.Contains('\n'))
{
var commentLines = HtmlEncoder.HtmlEncode(comment.BriefText)
.Split('\n', '\r');

lines.Add("<summary>");
foreach (string line in HtmlEncoder.HtmlEncode(comment.BriefText).Split(
Environment.NewLine.ToCharArray()))
foreach (string line in commentLines)
{
if (string.IsNullOrWhiteSpace(line))
continue;
Expand Down
11 changes: 11 additions & 0 deletions src/Generator/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ public enum GenerationOutputMode
FilePerUnit
}

public enum NewLineType
{
Auto, // Attempt to auto-detect the new line type using git repository settings of the output directory
Host, // Same as Environment.NewLine on the source generator host
LF,
CR,
CRLF,
}

public class DriverOptions
{
public DriverOptions()
Expand Down Expand Up @@ -84,6 +93,8 @@ public bool DoAllModulesHaveLibraries() =>

public string OutputDir;

public NewLineType OutputNewLineType = NewLineType.Auto;

public bool OutputInteropIncludes;
public bool GenerateFunctionTemplates;
/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/Generator/Passes/GetterSetterToPropertyPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,8 @@ private static void CombineComments(Property property)
Method setter = property.SetMethod;
if (getter != setter && setter?.Comment != null)
{
comment.BriefText += Environment.NewLine + setter.Comment.BriefText;
comment.Text += Environment.NewLine + setter.Comment.Text;
comment.BriefText += TextGenerator.NewLineChar + setter.Comment.BriefText;
comment.Text += TextGenerator.NewLineChar + setter.Comment.Text;
comment.FullComment.Blocks.AddRange(setter.Comment.FullComment.Blocks);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Generator/Utils/BlockGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,12 @@ public virtual StringBuilder Generate()
if (previousBlock != null &&
(previousBlock.NewLineKind == NewLineKind.BeforeNextBlock ||
(previousBlock.NewLineKind == NewLineKind.IfNotEmpty && !previousBlockEmpty)))
builder.AppendLine();
builder.Append(TextGenerator.NewLineChar);

builder.Append(childText);

if (childBlock.NewLineKind == NewLineKind.Always)
builder.AppendLine();
builder.Append(TextGenerator.NewLineChar);

previousBlock = childBlock;
previousBlockEmpty = childText.Length == 0;
Expand Down Expand Up @@ -318,7 +318,7 @@ internal ref struct PushedBlock
private readonly BlockGenerator generator;
private readonly NewLineKind next;

public PushedBlock(BlockGenerator generator, NewLineKind next)
public PushedBlock(BlockGenerator generator, NewLineKind next)
{
this.generator = generator;
this.next = next;
Expand Down
76 changes: 40 additions & 36 deletions src/Generator/Utils/ProcessHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,50 @@ public static class ProcessHelper
{
public static string Run(string path, string args, out int error, out string errorMessage)
{
using (var process = new Process())
return RunFrom(null, path, args, out error, out errorMessage);
}

public static string RunFrom(string workingDir, string path, string args, out int error, out string errorMessage)
{
using var process = new Process();
process.StartInfo.WorkingDirectory = workingDir;
process.StartInfo.FileName = path;
process.StartInfo.Arguments = args;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;

var reterror = new StringBuilder();
var retout = new StringBuilder();
process.OutputDataReceived += (sender, outargs) =>
{
if (string.IsNullOrEmpty(outargs.Data))
return;

if (retout.Length > 0)
retout.AppendLine();
retout.Append(outargs.Data);
};
process.ErrorDataReceived += (sender, errargs) =>
{
process.StartInfo.FileName = path;
process.StartInfo.Arguments = args;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
if (string.IsNullOrEmpty(errargs.Data))
return;

var reterror = new StringBuilder();
var retout = new StringBuilder();
process.OutputDataReceived += (sender, outargs) =>
{
if (!string.IsNullOrEmpty(outargs.Data))
{
if (retout.Length > 0)
retout.AppendLine();
retout.Append(outargs.Data);
}
};
process.ErrorDataReceived += (sender, errargs) =>
{
if (!string.IsNullOrEmpty(errargs.Data))
{
if (reterror.Length > 0)
reterror.AppendLine();
reterror.Append(errargs.Data);
}
};
if (reterror.Length > 0)
reterror.AppendLine();
reterror.Append(errargs.Data);
};

process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
process.CancelOutputRead();
process.CancelErrorRead();
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
process.CancelOutputRead();
process.CancelErrorRead();

error = process.ExitCode;
errorMessage = reterror.ToString();
return retout.ToString();
}
error = process.ExitCode;
errorMessage = reterror.ToString();
return retout.ToString();
}
}
}
25 changes: 19 additions & 6 deletions src/Generator/Utils/TextGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ public interface ITextGenerator

public class TextGenerator : ITextGenerator
{
public static string NewLineChar;

public const uint DefaultIndentation = 4;

public StringBuilder StringBuilder = new StringBuilder();
public StringBuilder StringBuilder = new();
public bool IsStartOfLine { get; set; }
public bool NeedsNewLine { get; set; }
public uint CurrentIndentation { get; set; }
Expand All @@ -45,6 +47,19 @@ public TextGenerator Clone()
return new TextGenerator(this);
}

public static void SetLineEndingType(NewLineType newLineType)
{
NewLineChar = newLineType switch
{
NewLineType.Host => Environment.NewLine,
NewLineType.CR => "\r",
NewLineType.LF => "\n",
NewLineType.CRLF => "\r\n",
NewLineType.Auto => throw new ArgumentException("Don't use Auto here.", nameof(newLineType)),
_ => throw new ArgumentOutOfRangeException(nameof(newLineType))
};
}

public void Write(string msg, params object[] args)
{
if (string.IsNullOrEmpty(msg))
Expand All @@ -54,11 +69,9 @@ public void Write(string msg, params object[] args)
msg = string.Format(msg, args);

if (IsStartOfLine && !string.IsNullOrWhiteSpace(msg))
StringBuilder.Append(new string(' ',
(int)(CurrentIndentation * DefaultIndentation)));
StringBuilder.Append(new string(' ', (int)(CurrentIndentation * DefaultIndentation)));

if (msg.Length > 0)
IsStartOfLine = msg.EndsWith(Environment.NewLine);
IsStartOfLine = msg.Length > 0 && msg.EndsWith(NewLineChar);

StringBuilder.Append(msg);
}
Expand Down Expand Up @@ -90,7 +103,7 @@ internal TextBlock WriteBlock((BlockKind kind, string msg) info)

public void NewLine()
{
StringBuilder.AppendLine(string.Empty);
StringBuilder.Append(NewLineChar);
IsStartOfLine = true;
}

Expand Down
58 changes: 57 additions & 1 deletion src/Generator/Utils/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using CppSharp.Utils;

namespace CppSharp
{
Expand Down Expand Up @@ -83,7 +84,7 @@ public static void TrimUnderscores(this StringBuilder stringBuilder)
{
while (stringBuilder.Length > 0 && stringBuilder[0] == '_')
stringBuilder.Remove(0, 1);
while (stringBuilder.Length > 0 && stringBuilder[stringBuilder.Length - 1] == '_')
while (stringBuilder.Length > 0 && stringBuilder[^1] == '_')
stringBuilder.Remove(stringBuilder.Length - 1, 1);
}
}
Expand Down Expand Up @@ -130,4 +131,59 @@ public static string GetRelativePath(string fromPath, string toPath)
return uri1.MakeRelativeUri(uri2).ToString();
}
}

public static class GeneratorHelpers
{
public static NewLineType? GetNewLineTypeFromGitConfig(string outputDir)
{
try
{
if (!bool.TryParse(
ProcessHelper.RunFrom(outputDir,
"git", "rev-parse --is-inside-work-tree",
out _, out _
),
out bool isInsideWorkTree))
{
return null;
}

if (!isInsideWorkTree)
return null;

// Check git config core.eol setting
var eolConfig = ProcessHelper.RunFrom(outputDir,
"git", "config --get core.eol",
out _, out _)
.Trim().ToLowerInvariant();

switch (eolConfig)
{
case "lf":
return NewLineType.LF;
case "crlf":
return NewLineType.CRLF;
}

// Otherwise check git config core.autocrlf setting
var autoCrLf = ProcessHelper.RunFrom(outputDir,
"git", "config --get core.autocrlf",
out _, out _)
.Trim().ToLowerInvariant();

return autoCrLf switch
{
"input" => NewLineType.LF,
"true" => NewLineType.CRLF,
"false" => NewLineType.Host,

_ => null
};
}
catch (Exception)
{
return null;
}
}
}
}