From 4b95cc16905661d8f7d9242bd2e0334590113e39 Mon Sep 17 00:00:00 2001
From: Joao Matos <joao@tritao.eu>
Date: Fri, 3 Feb 2023 17:52:14 +0000
Subject: [PATCH] Minor code refactorings.

---
 src/CLI/CLI.cs                       |  2 +-
 src/CLI/Generator.cs                 | 26 ++++----------------------
 src/Generator.Tests/GeneratorTest.cs |  9 +++------
 src/Parser/Parser.cs                 |  2 +-
 src/Parser/ParserOptions.cs          | 25 +++++++++++--------------
 5 files changed, 20 insertions(+), 44 deletions(-)

diff --git a/src/CLI/CLI.cs b/src/CLI/CLI.cs
index 54b2ed6c..c2cc3a4b 100644
--- a/src/CLI/CLI.cs
+++ b/src/CLI/CLI.cs
@@ -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)
diff --git a/src/CLI/Generator.cs b/src/CLI/Generator.cs
index 5c6f72be..1b5d26f6 100644
--- a/src/CLI/Generator.cs
+++ b/src/CLI/Generator.cs
@@ -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
                 return false;
             }
 
-            if (!options.Platform.HasValue)
-                options.Platform = GetCurrentPlatform();
+            options.Platform ??= Platform.Host;
 
             if (string.IsNullOrEmpty(options.OutputDir))
             {
@@ -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}");
 
diff --git a/src/Generator.Tests/GeneratorTest.cs b/src/Generator.Tests/GeneratorTest.cs
index feff986f..bc8f4219 100644
--- a/src/Generator.Tests/GeneratorTest.cs
+++ b/src/Generator.Tests/GeneratorTest.cs
@@ -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
                 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");
diff --git a/src/Parser/Parser.cs b/src/Parser/Parser.cs
index fa1a889b..66d5dace 100644
--- a/src/Parser/Parser.cs
+++ b/src/Parser/Parser.cs
@@ -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();
diff --git a/src/Parser/ParserOptions.cs b/src/Parser/ParserOptions.cs
index 41dffa66..7fc997a1 100644
--- a/src/Parser/ParserOptions.cs
+++ b/src/Parser/ParserOptions.cs
@@ -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
                 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
             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
 
         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