Skip to content

Commit 136094b

Browse files
authored
Merge pull request #5182 from bclothier/ReachableDebugCode
Replace #if DEBUG with conditional methods
2 parents 95e9513 + c4f7749 commit 136094b

34 files changed

+701
-929
lines changed

Rubberduck.CodeAnalysis/Inspections/Abstract/QuickFixBase.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Linq;
45
using NLog;
56
using Rubberduck.Parsing.Inspections.Abstract;
@@ -24,17 +25,28 @@ public void RegisterInspections(params Type[] inspections)
2425
{
2526
if (!inspections.All(s => s.GetInterfaces().Any(a => a == typeof(IInspection))))
2627
{
27-
#if DEBUG
28-
throw new ArgumentException($"Parameters must implement {nameof(IInspection)}", nameof(inspections));
29-
#else
28+
var dieNow = false;
29+
MustThrowException(ref dieNow);
30+
if (dieNow)
31+
{
32+
throw new ArgumentException($"Parameters must implement {nameof(IInspection)}",
33+
nameof(inspections));
34+
}
35+
3036
inspections.Where(s => s.GetInterfaces().All(i => i != typeof(IInspection))).ToList()
3137
.ForEach(i => Logger.Error($"Type {i.Name} does not implement {nameof(IInspection)}"));
32-
#endif
3338
}
3439

3540
_supportedInspections = inspections.ToHashSet();
3641
}
3742

43+
// ReSharper disable once RedundantAssignment : conditional must be void but we can use ref
44+
[Conditional("DEBUG")]
45+
private static void MustThrowException(ref bool dieNow)
46+
{
47+
dieNow = true;
48+
}
49+
3850
public void RemoveInspections(params Type[] inspections)
3951
{
4052
_supportedInspections = _supportedInspections.Except(inspections).ToHashSet();

Rubberduck.Core/UI/Command/MenuItems/CommandBars/SerializeProjectsCommandMenuItem.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.IO;
45
using System.Linq;
56
using Rubberduck.Parsing.ComReflection;
@@ -77,7 +78,12 @@ protected override void OnExecute(object parameter)
7778
_serializationProvider.SerializeProject(library);
7879
}
7980

80-
#if TRACE_COM_SAFE
81+
SerializeComSafe();
82+
}
83+
84+
[Conditional("TRACE_COM_SAFE")]
85+
private void SerializeComSafe()
86+
{
8187
//This block must be inside a conditional compilation block because the Serialize method
8288
//called is conditionally compiled and available only if the compilation constant TRACE_COM_SAFE is set.
8389
var path = !string.IsNullOrWhiteSpace(_serializationProvider.Target)
@@ -88,8 +94,8 @@ protected override void OnExecute(object parameter)
8894
{
8995
Directory.CreateDirectory(traceDirectory);
9096
}
97+
9198
Rubberduck.VBEditor.ComManagement.ComSafeManager.GetCurrentComSafe().Serialize(traceDirectory);
92-
#endif
9399
}
94100
}
95101
}

Rubberduck.Core/UI/Command/MenuItems/RefactorExtractMethodCommandMenuItem.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
using System.Drawing;
2+
using Rubberduck.Parsing.Common;
23
using Rubberduck.Parsing.VBA;
34
using Rubberduck.UI.Command.MenuItems.ParentMenus;
45
using Rubberduck.UI.Command.Refactorings;
56

67
namespace Rubberduck.UI.Command.MenuItems
78
{
8-
#if !DEBUG
9-
[Parsing.Common.Disabled]
10-
#endif
9+
[Disabled]
1110
public class RefactorExtractMethodCommandMenuItem : CommandMenuItemBase
1211
{
1312
public RefactorExtractMethodCommandMenuItem(RefactorExtractMethodCommand command)

Rubberduck.Core/UI/Command/MenuItems/RegexSearchReplaceCommandMenuItem.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1+
using Rubberduck.Parsing.Common;
12
using Rubberduck.UI.Command.MenuItems.ParentMenus;
23

34
namespace Rubberduck.UI.Command.MenuItems
45
{
5-
#if !DEBUG
6-
[Parsing.Common.Disabled]
7-
#endif
6+
[Disabled]
87
public class RegexSearchReplaceCommandMenuItem : CommandMenuItemBase
98
{
109
public RegexSearchReplaceCommandMenuItem(RegexSearchReplaceCommand command)

Rubberduck.Core/UI/Command/Refactorings/RefactorExtractMethodCommand.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212

1313
namespace Rubberduck.UI.Command.Refactorings
1414
{
15-
#if !DEBUG
1615
[Disabled]
17-
#endif
1816
[ComVisible(false)]
1917
public class RefactorExtractMethodCommand : CommandBase
2018
{

Rubberduck.Core/UI/Command/RegexSearchReplaceCommand.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
namespace Rubberduck.UI.Command
66
{
7-
#if !DEBUG
87
[Disabled]
9-
#endif
108
[ComVisible(false)]
119
public class RegexSearchReplaceCommand : CommandBase
1210
{

Rubberduck.Main/Extension.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,7 @@ public void OnConnection(object Application, ext_ConnectMode ConnectMode, object
7070
VbeProvider.Initialize(_vbe, _vbeNativeApi, _beepInterceptor);
7171
VbeNativeServices.HookEvents(_vbe);
7272

73-
#if DEBUG
74-
// FOR DEBUGGING/DEVELOPMENT PURPOSES, ALLOW ACCESS TO SOME VBETypeLibsAPI FEATURES FROM VBA
75-
_addin.Object = new VBETypeLibsAPI_Object(_vbe);
76-
#endif
73+
SetAddInObject();
7774

7875
switch (ConnectMode)
7976
{
@@ -92,6 +89,13 @@ public void OnConnection(object Application, ext_ConnectMode ConnectMode, object
9289
}
9390
}
9491

92+
[Conditional("DEBUG")]
93+
private void SetAddInObject()
94+
{
95+
// FOR DEBUGGING/DEVELOPMENT PURPOSES, ALLOW ACCESS TO SOME VBETypeLibsAPI FEATURES FROM VBA
96+
_addin.Object = new VBETypeLibsAPI_Object(_vbe);
97+
}
98+
9599
private Assembly LoadFromSameFolder(object sender, ResolveEventArgs args)
96100
{
97101
var folderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? string.Empty;
@@ -207,7 +211,9 @@ private void InitializeAddIn()
207211
catch (Exception exception)
208212
{
209213
_logger.Fatal(exception);
210-
System.Windows.Forms.MessageBox.Show(
214+
// TODO Use Rubberduck Interaction instead and provide exception stack trace as
215+
// an optional "more info" collapsible section to eliminate the conditional.
216+
MessageBox.Show(
211217
#if DEBUG
212218
exception.ToString(),
213219
#else
@@ -239,11 +245,7 @@ private void Startup()
239245
catch (Exception e)
240246
{
241247
_logger.Log(LogLevel.Fatal, e, "Startup sequence threw an unexpected exception.");
242-
#if DEBUG
243-
throw;
244-
#else
245-
throw new Exception("Rubberduck's startup sequence threw an unexpected exception. Please check the Rubberduck logs for more information and report an issue if necessary");
246-
#endif
248+
throw new Exception("Rubberduck's startup sequence threw an unexpected exception. Please check the Rubberduck logs for more information and report an issue if necessary", e);
247249
}
248250
}
249251

Rubberduck.Main/Root/RubberduckIoCInstaller.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Globalization;
45
using System.Linq;
56
using System.Reflection;
@@ -575,17 +576,24 @@ private void RegisterRubberduckCommandBar(IWindsorContainer container)
575576

576577
private Type[] RubberduckCommandBarItems()
577578
{
578-
return new[]
579+
var types = new List<Type>
579580
{
580581
typeof(ReparseCommandMenuItem),
581582
typeof(ShowParserErrorsCommandMenuItem),
582583
typeof(ContextSelectionLabelMenuItem),
583584
typeof(ContextDescriptionLabelMenuItem),
584-
typeof(ReferenceCounterLabelMenuItem),
585-
#if DEBUG
586-
typeof(SerializeProjectsCommandMenuItem)
587-
#endif
585+
typeof(ReferenceCounterLabelMenuItem)
588586
};
587+
588+
AttachRubberduckDebugCommandBarItems(ref types);
589+
590+
return types.ToArray();
591+
}
592+
593+
[Conditional("DEBUG")]
594+
private static void AttachRubberduckDebugCommandBarItems(ref List<Type> types)
595+
{
596+
types.Add(typeof(SerializeProjectsCommandMenuItem));
589597
}
590598

591599
private void RegisterParentMenus(IWindsorContainer container)

Rubberduck.Parsing/ComReflection/ComMember.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ internal enum DispId
2727

2828
[DataContract]
2929
[KnownType(typeof(ComBase))]
30-
#if DEBUG
3130
[DebuggerDisplay("{" + nameof(MemberDeclaration) + "}")]
32-
#endif
3331
public class ComMember : ComBase
3432
{
3533
[DataMember(IsRequired = true)]
@@ -143,8 +141,7 @@ private void LoadParameters(FUNCDESC funcDesc, ITypeInfo info)
143141
Parameters.Last().IsParamArray = true;
144142
}
145143
}
146-
#if DEBUG
147-
// ReSharper disable once UnusedMember.Local
144+
148145
private string MemberDeclaration
149146
{
150147
get
@@ -174,6 +171,5 @@ private string MemberDeclaration
174171
return $"{(IsHidden || IsRestricted ? "Private" : "Public")} {type} {Name}{(AsTypeName == null ? string.Empty : $" As {AsTypeName.TypeName}")}";
175172
}
176173
}
177-
#endif
178174
}
179175
}

Rubberduck.Parsing/ComReflection/ComParameter.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,15 @@ namespace Rubberduck.Parsing.ComReflection
1616
{
1717
[DataContract]
1818
[KnownType(typeof(ComTypeName))]
19-
#if DEBUG
2019
[DebuggerDisplay("{" + nameof(DeclarationName) + "}")]
21-
#endif
2220
public class ComParameter
2321
{
2422
public static ComParameter Void = new ComParameter { _typeName = new ComTypeName(null, string.Empty) };
2523

2624
[DataMember(IsRequired = true)]
2725
public string Name { get; private set; }
2826

29-
#if DEBUG
30-
// ReSharper disable once UseStringInterpolation
31-
public string DeclarationName => string.Format("{0}{1} {2} As {3}{4}{5}",
32-
IsOptional ? "Optional " : string.Empty,
33-
IsByRef ? "ByRef" : "ByVal",
34-
Name,
35-
TypeName,
36-
IsOptional && DefaultValue != null ? " = " : string.Empty,
37-
IsOptional && DefaultValue != null ?
38-
_typeName.IsEnumMember ? DefaultAsEnum : DefaultValue
39-
: string.Empty);
40-
#endif
27+
public string DeclarationName => $"{(IsOptional ? "Optional " : string.Empty)}{(IsByRef ? "ByRef" : "ByVal")} {Name} As {TypeName}{(IsOptional && DefaultValue != null ? " = " : string.Empty)}{(IsOptional && DefaultValue != null ? _typeName.IsEnumMember ? DefaultAsEnum : DefaultValue : string.Empty)}";
4128

4229
[DataMember(IsRequired = true)]
4330
public bool IsArray { get; private set; }

0 commit comments

Comments
 (0)