From ec96889b15c20a6d60267bb3d4c9cc33550a4688 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Pedro?= Date: Wed, 29 Aug 2018 21:48:21 +0100 Subject: [PATCH] Code cleanup. --- .../AD7.Impl/AD7Expression.cs | 11 ++-- .../Commands/DebugExecCommand.cs | 25 +++++--- .../Commands/DebugLaunchCommand.cs | 62 ++++++++++++------- .../Commands/DebugLogCommand.cs | 53 ++++++++++------ .../Engine.Impl/OperationThread.cs | 8 ++- 5 files changed, 105 insertions(+), 54 deletions(-) diff --git a/source/Cosmos.VS.DebugEngine/AD7.Impl/AD7Expression.cs b/source/Cosmos.VS.DebugEngine/AD7.Impl/AD7Expression.cs index 068ad8a54..7b0ba3939 100644 --- a/source/Cosmos.VS.DebugEngine/AD7.Impl/AD7Expression.cs +++ b/source/Cosmos.VS.DebugEngine/AD7.Impl/AD7Expression.cs @@ -1,4 +1,6 @@ using System; +using System.Diagnostics.CodeAnalysis; + using Microsoft.VisualStudio; using Microsoft.VisualStudio.Debugger.Interop; @@ -16,9 +18,9 @@ namespace Cosmos.VS.DebugEngine.AD7.Impl public AD7Expression(DebugLocalInfo pVar, AD7Process pProcess, AD7StackFrame pStackFrame) { - this.m_var = pVar; - this.Process = pProcess; - this.StackFrame = pStackFrame; + m_var = pVar; + Process = pProcess; + StackFrame = pStackFrame; } // This method cancels asynchronous expression evaluation as started by a call to the IDebugExpression2::EvaluateAsync method. @@ -33,6 +35,7 @@ namespace Cosmos.VS.DebugEngine.AD7.Impl // must be sent to the IDebugEventCallback2 event callback // // This is primarily used for the immediate window which this engine does not currently support. + [SuppressMessage("AsyncUsage.CSharp.Naming", "AvoidAsyncSuffix:Avoid Async suffix", Scope = "member")] int IDebugExpression2.EvaluateAsync(enum_EVALFLAGS dwFlags, IDebugEventCallback2 pExprCallback) { throw new NotImplementedException(); @@ -46,4 +49,4 @@ namespace Cosmos.VS.DebugEngine.AD7.Impl } } -} \ No newline at end of file +} diff --git a/source/Cosmos.VS.DebugEngine/Commands/DebugExecCommand.cs b/source/Cosmos.VS.DebugEngine/Commands/DebugExecCommand.cs index 67c195ddd..c6cdba6cb 100644 --- a/source/Cosmos.VS.DebugEngine/Commands/DebugExecCommand.cs +++ b/source/Cosmos.VS.DebugEngine/Commands/DebugExecCommand.cs @@ -2,6 +2,7 @@ using System.Globalization; using System.Runtime.InteropServices; using Microsoft.VisualStudio; +using Microsoft.VisualStudio.Shell; using Microsoft.VisualStudio.Shell.Interop; namespace Cosmos.VS.DebugEngine.Commands @@ -25,25 +26,31 @@ namespace Cosmos.VS.DebugEngine.Commands string arguments; hr = EnsureString(pvaIn, out arguments); - if (hr != VSConstants.S_OK) - return hr; - if (string.IsNullOrWhiteSpace(arguments)) + if (hr != VSConstants.S_OK) + { + return hr; + } + + if (String.IsNullOrWhiteSpace(arguments)) + { throw new ArgumentException("Expected a Cosmos command to execute (ex: Debug.CosmosDebugExec info sharedlibrary)"); + } DebugExec(arguments); return VSConstants.S_OK; } - private void DebugExec(string command) + private void DebugExec(string command) { + ThreadHelper.ThrowIfNotOnUIThread(); + var commandWindow = (IVsCommandWindow)serviceProvider.GetService(typeof(SVsCommandWindow)); - bool atBreak = false; - var debugger = serviceProvider.GetService(typeof(SVsShellDebugger)) as IVsDebugger; - if (debugger != null) + var atBreak = false; + if (serviceProvider.GetService(typeof(SVsShellDebugger)) is IVsDebugger debugger) { - DBGMODE[] mode = new DBGMODE[1]; + var mode = new DBGMODE[1]; if (debugger.GetMode(mode) == VSConstants.S_OK) { atBreak = mode[0] == DBGMODE.DBGMODE_Break; @@ -85,4 +92,4 @@ namespace Cosmos.VS.DebugEngine.Commands } } } -} \ No newline at end of file +} diff --git a/source/Cosmos.VS.DebugEngine/Commands/DebugLaunchCommand.cs b/source/Cosmos.VS.DebugEngine/Commands/DebugLaunchCommand.cs index 5090a5506..caf0f82ab 100644 --- a/source/Cosmos.VS.DebugEngine/Commands/DebugLaunchCommand.cs +++ b/source/Cosmos.VS.DebugEngine/Commands/DebugLaunchCommand.cs @@ -3,6 +3,7 @@ using System.Globalization; using System.IO; using System.Runtime.InteropServices; using Microsoft.VisualStudio; +using Microsoft.VisualStudio.Shell; using Microsoft.VisualStudio.Shell.Interop; namespace Cosmos.VS.DebugEngine.Commands @@ -19,11 +20,13 @@ namespace Cosmos.VS.DebugEngine.Commands public DebugLaunchCommand(IServiceProvider serviceProvider) : base(serviceProvider) - { + { } public override int Execute(uint nCmdExecOpt, IntPtr pvaIn, IntPtr pvaOut) { + ThreadHelper.ThrowIfNotOnUIThread(); + int hr; if (IsQueryParameterList(pvaIn, pvaOut, nCmdExecOpt)) @@ -32,53 +35,66 @@ namespace Cosmos.VS.DebugEngine.Commands return VSConstants.S_OK; } - string arguments; - hr = EnsureString(pvaIn, out arguments); - if (hr != VSConstants.S_OK) - return hr; + hr = EnsureString(pvaIn, out var arguments); - IVsParseCommandLine parseCommandLine = (IVsParseCommandLine)serviceProvider.GetService(typeof(SVsParseCommandLine)); - hr = parseCommandLine.ParseCommandTail(arguments, iMaxParams: -1); - if (ErrorHandler.Failed(hr)) + if (hr != VSConstants.S_OK) + { return hr; + } + + var parseCommandLine = (IVsParseCommandLine)serviceProvider.GetService(typeof(SVsParseCommandLine)); + hr = parseCommandLine.ParseCommandTail(arguments, iMaxParams: -1); + + if (ErrorHandler.Failed(hr)) + { + return hr; + } hr = parseCommandLine.HasParams(); + if (ErrorHandler.Failed(hr)) + { return hr; + } + if (hr == VSConstants.S_OK || parseCommandLine.HasSwitches() != VSConstants.S_OK) { - string message = string.Concat("Unexpected syntax for CosmosDebugLaunch command. Expected:\n", + var message = String.Concat("Unexpected syntax for CosmosDebugLaunch command. Expected:\n", "Debug.CosmosDebugLaunch /Executable: /OptionsFile:"); throw new ApplicationException(message); } hr = parseCommandLine.EvaluateSwitches(DebugLaunchCommandSyntax); if (ErrorHandler.Failed(hr)) + { return hr; + } - string executable; - if (parseCommandLine.GetSwitchValue((int)DebugLaunchCommandSwitchEnum.Executable, out executable) != VSConstants.S_OK || - string.IsNullOrWhiteSpace(executable)) + if (parseCommandLine.GetSwitchValue((int)DebugLaunchCommandSwitchEnum.Executable, out var executable) != VSConstants.S_OK || + String.IsNullOrWhiteSpace(executable)) { throw new ArgumentException("Executable must be specified"); } - bool checkExecutableExists = false; - string options = string.Empty; + var checkExecutableExists = false; + var options = String.Empty; - string optionsFilePath; - if (parseCommandLine.GetSwitchValue((int)DebugLaunchCommandSwitchEnum.OptionsFile, out optionsFilePath) == 0) + if (parseCommandLine.GetSwitchValue((int)DebugLaunchCommandSwitchEnum.OptionsFile, out var optionsFilePath) == 0) { // When using the options file, we want to allow the executable to be just a logical name, but if // one enters a real path, we should make sure it isn't mistyped. If the path contains a slash, we assume it // is meant to be a real path so enforce that it exists checkExecutableExists = (executable.IndexOf('\\') >= 0); - if (string.IsNullOrWhiteSpace(optionsFilePath)) + if (String.IsNullOrWhiteSpace(optionsFilePath)) + { throw new ArgumentException("Value expected for '/OptionsFile' option"); + } if (!File.Exists(optionsFilePath)) - throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "Options file '{0}' does not exist", optionsFilePath)); + { + throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, "Options file '{0}' does not exist", optionsFilePath)); + } options = File.ReadAllText(optionsFilePath); } @@ -87,7 +103,7 @@ namespace Cosmos.VS.DebugEngine.Commands { if (!File.Exists(executable)) { - throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "Executable '{0}' does not exist", executable)); + throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, "Executable '{0}' does not exist", executable)); } executable = Path.GetFullPath(executable); @@ -100,13 +116,15 @@ namespace Cosmos.VS.DebugEngine.Commands private void LaunchDebugTarget(string filePath, string options) { - IVsDebugger4 debugger = (IVsDebugger4)serviceProvider.GetService(typeof(IVsDebugger)); - VsDebugTargetInfo4[] debugTargets = new VsDebugTargetInfo4[1]; + ThreadHelper.ThrowIfNotOnUIThread(); + + var debugger = (IVsDebugger4)serviceProvider.GetService(typeof(IVsDebugger)); + var debugTargets = new VsDebugTargetInfo4[1]; debugTargets[0].dlo = (uint)DEBUG_LAUNCH_OPERATION.DLO_CreateProcess; debugTargets[0].bstrExe = filePath; debugTargets[0].bstrOptions = options; debugTargets[0].guidLaunchDebugEngine = Guids.DebugEngineGuid; - VsDebugTargetProcessInfo[] processInfo = new VsDebugTargetProcessInfo[debugTargets.Length]; + var processInfo = new VsDebugTargetProcessInfo[debugTargets.Length]; debugger.LaunchDebugTargets4(1, debugTargets, processInfo); } diff --git a/source/Cosmos.VS.DebugEngine/Commands/DebugLogCommand.cs b/source/Cosmos.VS.DebugEngine/Commands/DebugLogCommand.cs index 58bd761cc..c4cc80a18 100644 --- a/source/Cosmos.VS.DebugEngine/Commands/DebugLogCommand.cs +++ b/source/Cosmos.VS.DebugEngine/Commands/DebugLogCommand.cs @@ -2,6 +2,7 @@ using System.Globalization; using System.Runtime.InteropServices; using Microsoft.VisualStudio; +using Microsoft.VisualStudio.Shell; using Microsoft.VisualStudio.Shell.Interop; namespace Cosmos.VS.DebugEngine.Commands @@ -24,6 +25,8 @@ namespace Cosmos.VS.DebugEngine.Commands public override int Execute(uint nCmdLogOpt, IntPtr pvaIn, IntPtr pvaOut) { + ThreadHelper.ThrowIfNotOnUIThread(); + int hr; if (IsQueryParameterList(pvaIn, pvaOut, nCmdLogOpt)) @@ -32,47 +35,59 @@ namespace Cosmos.VS.DebugEngine.Commands return VSConstants.S_OK; } - string arguments; - hr = EnsureString(pvaIn, out arguments); - if (hr != VSConstants.S_OK) - return hr; + hr = EnsureString(pvaIn, out var arguments); - IVsParseCommandLine parseCommandLine = (IVsParseCommandLine)serviceProvider.GetService(typeof(SVsParseCommandLine)); - hr = parseCommandLine.ParseCommandTail(arguments, iMaxParams: -1); - if (ErrorHandler.Failed(hr)) + if (hr != VSConstants.S_OK) + { return hr; + } + + var parseCommandLine = (IVsParseCommandLine)serviceProvider.GetService(typeof(SVsParseCommandLine)); + hr = parseCommandLine.ParseCommandTail(arguments, iMaxParams: -1); + + if (ErrorHandler.Failed(hr)) + { + return hr; + } hr = parseCommandLine.HasParams(); + if (ErrorHandler.Failed(hr)) + { return hr; + } + if (hr == VSConstants.S_OK || parseCommandLine.HasSwitches() != VSConstants.S_OK) { - string message = string.Concat("Unexpected syntax for CosmosDebugLaunch command. Expected:\n", + string message = String.Concat("Unexpected syntax for CosmosDebugLaunch command. Expected:\n", "Debug.CosmosDebugLog [/On: [/OutputWindow] | /Off]"); throw new ApplicationException(message); } hr = parseCommandLine.EvaluateSwitches(DebugLogCommandSyntax); - if (ErrorHandler.Failed(hr)) - return hr; - string logPath = string.Empty; - bool logToOutput = false; + if (ErrorHandler.Failed(hr)) + { + return hr; + } + + var logPath = String.Empty; + var logToOutput = false; if (parseCommandLine.GetSwitchValue((int)DebugLogCommandSwitchEnum.On, out logPath) == VSConstants.S_OK) { logToOutput = parseCommandLine.IsSwitchPresent((int)DebugLogCommandSwitchEnum.OutputWindow) == VSConstants.S_OK; if (parseCommandLine.IsSwitchPresent((int)DebugLogCommandSwitchEnum.Off) == VSConstants.S_OK) { - throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "/On and /Off cannot both appear on command line")); + throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, "/On and /Off cannot both appear on command line")); } - if (!logToOutput && string.IsNullOrEmpty(logPath)) + if (!logToOutput && String.IsNullOrEmpty(logPath)) { - throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "Must specify a log file (/On:) or /OutputWindow")); + throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, "Must specify a log file (/On:) or /OutputWindow")); } } else if (parseCommandLine.IsSwitchPresent((int)DebugLogCommandSwitchEnum.Off) != VSConstants.S_OK) { - throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "One of /On or /Off must be present on command line")); + throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, "One of /On or /Off must be present on command line")); } EnableLogging(logToOutput, logPath); @@ -82,6 +97,8 @@ namespace Cosmos.VS.DebugEngine.Commands private void EnableLogging(bool sendToOutputWindow, string logFile) { + ThreadHelper.ThrowIfNotOnUIThread(); + try { //MIDebugCommandDispatcher.EnableLogging(sendToOutputWindow, logFile); @@ -89,8 +106,8 @@ namespace Cosmos.VS.DebugEngine.Commands catch (Exception e) { var commandWindow = (IVsCommandWindow)serviceProvider.GetService(typeof(SVsCommandWindow)); - commandWindow.Print(string.Format(CultureInfo.CurrentCulture, "Error: {0}\r\n", e.Message)); + commandWindow.Print(String.Format(CultureInfo.CurrentCulture, "Error: {0}\r\n", e.Message)); } } } -} \ No newline at end of file +} diff --git a/source/Cosmos.VS.DebugEngine/Engine.Impl/OperationThread.cs b/source/Cosmos.VS.DebugEngine/Engine.Impl/OperationThread.cs index 81ae73787..2b0d78ca2 100644 --- a/source/Cosmos.VS.DebugEngine/Engine.Impl/OperationThread.cs +++ b/source/Cosmos.VS.DebugEngine/Engine.Impl/OperationThread.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; using System.Threading; namespace Cosmos.VS.DebugEngine.Engine.Impl @@ -24,7 +25,7 @@ namespace Cosmos.VS.DebugEngine.Engine.Impl m_opComplete = new ManualResetEvent(true); m_quitOperation = new Operation(delegate() { }); - Thread thread = new Thread(new ThreadStart(ThreadFunc)); + var thread = new Thread(new ThreadStart(ThreadFunc)); thread.Start(); } @@ -37,15 +38,20 @@ namespace Cosmos.VS.DebugEngine.Engine.Impl public void RunOperation(Operation op) { if (op == null) + { throw new ArgumentNullException(); + } SetOperationInternal(op, true); } + [SuppressMessage("AsyncUsage.CSharp.Naming", "AvoidAsyncSuffix:Avoid Async suffix", Scope = "member")] public void RunOperationAsync(Operation op) { if (op == null) + { throw new ArgumentNullException(); + } SetOperationInternal(op, false); }