From f47a8aff6ac177ebf143f9604f040bb743da886a Mon Sep 17 00:00:00 2001 From: Dan Siegel Date: Wed, 16 Dec 2020 15:21:31 -0800 Subject: [PATCH] allow Pragma warning disable --- src/CodeGenHelpers/BuilderBase.cs | 22 +++++++++++++++++++-- src/CodeGenHelpers/BuilderBase{T}.cs | 5 +++++ src/CodeGenHelpers/CodeGenHelpers.projitems | 1 + src/CodeGenHelpers/CompilerWarnings.cs | 20 +++++++++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 src/CodeGenHelpers/CompilerWarnings.cs diff --git a/src/CodeGenHelpers/BuilderBase.cs b/src/CodeGenHelpers/BuilderBase.cs index dc55f23..596b972 100644 --- a/src/CodeGenHelpers/BuilderBase.cs +++ b/src/CodeGenHelpers/BuilderBase.cs @@ -1,10 +1,28 @@ -namespace CodeGenHelpers +using System.Collections.Generic; +using System.Linq; + +namespace CodeGenHelpers { public abstract class BuilderBase : IBuilder { + protected readonly List _pragmaWarnings = new List(); + internal abstract void Write(ref CodeWriter writer); - void IBuilder.Write(ref CodeWriter writer) => + void IBuilder.Write(ref CodeWriter writer) + { + var warnings = string.Join(", ", _pragmaWarnings.Distinct()); + if(_pragmaWarnings.Any()) + { + writer.AppendUnindentedLine($"#pragma warning disable {warnings}"); + } + Write(ref writer); + + if (_pragmaWarnings.Any()) + { + writer.AppendUnindentedLine($"#pragma warning restore {warnings}"); + } + } } } diff --git a/src/CodeGenHelpers/BuilderBase{T}.cs b/src/CodeGenHelpers/BuilderBase{T}.cs index 1167a3c..598c2d1 100644 --- a/src/CodeGenHelpers/BuilderBase{T}.cs +++ b/src/CodeGenHelpers/BuilderBase{T}.cs @@ -9,5 +9,10 @@ public abstract class BuilderBase : BuilderBase public abstract T AddNamespaceImport(ISymbol symbol); public abstract T AddNamespaceImport(INamespaceSymbol symbol); public abstract T AddAssemblyAttribute(string attribute); + public T DisableWarning(string buildCode) + { + _pragmaWarnings.Add(buildCode); + return this as T; + } } } diff --git a/src/CodeGenHelpers/CodeGenHelpers.projitems b/src/CodeGenHelpers/CodeGenHelpers.projitems index 13d6baf..29e460d 100644 --- a/src/CodeGenHelpers/CodeGenHelpers.projitems +++ b/src/CodeGenHelpers/CodeGenHelpers.projitems @@ -12,6 +12,7 @@ + diff --git a/src/CodeGenHelpers/CompilerWarnings.cs b/src/CodeGenHelpers/CompilerWarnings.cs new file mode 100644 index 0000000..538f480 --- /dev/null +++ b/src/CodeGenHelpers/CompilerWarnings.cs @@ -0,0 +1,20 @@ +namespace CodeGenHelpers +{ + public class CompilerWarnings + { + /// + /// The field is never used + /// + public const string CS0169 = nameof(CS0169); + + /// + /// The member is marked obsolete + /// + public const string CS0612 = nameof(CS0612); + + /// + /// The member is marked obsolete with a message + /// + public const string CS0618 = nameof(CS0618); + } +}