Browse Source

Minor code refactorings.

pull/1711/head
Joao Matos 2 years ago
parent
commit
4b95cc1690
  1. 2
      src/CLI/CLI.cs
  2. 26
      src/CLI/Generator.cs
  3. 9
      src/Generator.Tests/GeneratorTest.cs
  4. 2
      src/Parser/Parser.cs
  5. 25
      src/Parser/ParserOptions.cs

2
src/CLI/CLI.cs

@ -288,7 +288,7 @@ namespace CppSharp @@ -288,7 +288,7 @@ namespace CppSharp
Generator gen = new Generator(options);
bool validOptions = gen.ValidateOptions(errorMessages);
var validOptions = gen.ValidateOptions(errorMessages);
PrintErrorMessages(errorMessages);
if (errorMessages.Any() || !validOptions)

26
src/CLI/Generator.cs

@ -13,30 +13,13 @@ namespace CppSharp @@ -13,30 +13,13 @@ namespace CppSharp
{
class Generator : ILibrary
{
private Options options = null;
private readonly Options options;
private string triple = "";
private CppAbi abi = CppAbi.Microsoft;
public Generator(Options options)
{
if (options == null)
throw new ArgumentNullException(nameof(options));
this.options = options;
}
static TargetPlatform GetCurrentPlatform()
{
if (Platform.IsWindows)
return TargetPlatform.Windows;
if (Platform.IsMacOS)
return TargetPlatform.MacOS;
if (Platform.IsLinux)
return TargetPlatform.Linux;
throw new System.NotImplementedException("Unknown host platform");
this.options = options ?? throw new ArgumentNullException(nameof(options));
}
void SetupTargetTriple()
@ -78,8 +61,7 @@ namespace CppSharp @@ -78,8 +61,7 @@ namespace CppSharp
return false;
}
if (!options.Platform.HasValue)
options.Platform = GetCurrentPlatform();
options.Platform ??= Platform.Host;
if (string.IsNullOrEmpty(options.OutputDir))
{
@ -189,7 +171,7 @@ namespace CppSharp @@ -189,7 +171,7 @@ namespace CppSharp
public void Run()
{
StringBuilder messageBuilder = new StringBuilder();
var messageBuilder = new StringBuilder();
messageBuilder.Append($"Generating {GetGeneratorKindName(options.Kind)}");
messageBuilder.Append($" bindings for {GetPlatformName(options.Platform)} {options.Architecture}");

9
src/Generator.Tests/GeneratorTest.cs

@ -72,7 +72,7 @@ namespace CppSharp.Utils @@ -72,7 +72,7 @@ namespace CppSharp.Utils
public static string GetTestsDirectory(string name)
{
var directory = new DirectoryInfo(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? string.Empty);
while (directory != null)
{
@ -89,15 +89,12 @@ namespace CppSharp.Utils @@ -89,15 +89,12 @@ namespace CppSharp.Utils
directory = directory.Parent;
}
throw new Exception(string.Format(
"Tests directory for project '{0}' was not found", name));
throw new Exception($"Tests directory for project '{name}' was not found");
}
static string GetOutputDirectory()
{
string exePath = new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath;
var directory = Directory.GetParent(exePath);
var directory = Directory.GetParent(Assembly.GetExecutingAssembly().Location);
while (directory != null)
{
var path = Path.Combine(directory.FullName, "build");

2
src/Parser/Parser.cs

@ -59,7 +59,7 @@ namespace CppSharp @@ -59,7 +59,7 @@ namespace CppSharp
/// <summary>
/// Converts a native parser AST to a managed AST.
/// </summary>
static public AST.ASTContext ConvertASTContext(ASTContext context)
public static AST.ASTContext ConvertASTContext(ASTContext context)
{
var converter = new ASTConverter(context);
return converter.Convert();

25
src/Parser/ParserOptions.cs

@ -5,9 +5,7 @@ using System.Globalization; @@ -5,9 +5,7 @@ using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using LanguageVersion = CppSharp.Parser.LanguageVersion;
namespace CppSharp.Parser
{
@ -200,20 +198,22 @@ namespace CppSharp.Parser @@ -200,20 +198,22 @@ namespace CppSharp.Parser
var versions = Directory.EnumerateDirectories(Path.Combine(headersPath,
"usr", "include", "c++"));
if (versions.Count() == 0)
if (!versions.Any())
throw new Exception("No valid GCC version found on system include paths");
string gccVersionPath = versions.First();
var gccVersionPath = versions.First();
longVersion = shortVersion = gccVersionPath.Substring(
gccVersionPath.LastIndexOf(Path.DirectorySeparatorChar) + 1);
return;
}
var info = new ProcessStartInfo(Environment.GetEnvironmentVariable("CXX") ?? "gcc", "-v");
info.RedirectStandardError = true;
info.CreateNoWindow = true;
info.UseShellExecute = false;
var info = new ProcessStartInfo(Environment.GetEnvironmentVariable("CXX") ?? "gcc", "-v")
{
RedirectStandardError = true,
CreateNoWindow = true,
UseShellExecute = false
};
var process = Process.Start(info);
if (process == null)
@ -237,13 +237,12 @@ namespace CppSharp.Parser @@ -237,13 +237,12 @@ namespace CppSharp.Parser
NoBuiltinIncludes = true;
NoStandardIncludes = true;
string compiler, longVersion, shortVersion;
GetUnixCompilerInfo(headersPath, out compiler, out longVersion, out shortVersion);
GetUnixCompilerInfo(headersPath, out var compiler, out var longVersion, out var shortVersion);
AddSystemIncludeDirs(BuiltinsDir);
AddArguments($"-fgnuc-version={longVersion}");
string majorVersion = shortVersion.Split('.')[0];
var majorVersion = shortVersion.Split('.')[0];
string[] versions = { longVersion, shortVersion, majorVersion };
string[] triples = { "x86_64-linux-gnu", "x86_64-pc-linux-gnu" };
if (compiler == "gcc")
@ -287,9 +286,7 @@ namespace CppSharp.Parser @@ -287,9 +286,7 @@ namespace CppSharp.Parser
private void SetupArguments()
{
// do not remove the CppSharp prefix becase the Mono C# compiler breaks
if (!LanguageVersion.HasValue)
LanguageVersion = CppSharp.Parser.LanguageVersion.CPP14_GNU;
LanguageVersion ??= CppSharp.Parser.LanguageVersion.CPP14_GNU;
// As of Clang revision 5e866e411caa we are required to pass "-fgnuc-version="
// to get the __GNUC__ symbol defined. macOS and Linux system headers require

Loading…
Cancel
Save