• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Sholtee / proxygen / 1046

27 Apr 2025 01:21PM UTC coverage: 92.705% (+0.6%) from 92.112%
1046

push

appveyor

Sholtee
Merge branch 'v10-preview1'

4791 of 5168 relevant lines covered (92.71%)

0.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.22
/SRC/Private/SyntaxFactories/UnitSyntaxFactoryBase.cs
1
/********************************************************************************
2
* UnitSyntaxFactoryBase.cs                                                      *
3
*                                                                               *
4
* Author: Denes Solti                                                           *
5
********************************************************************************/
6
using System;
7
using System.CodeDom.Compiler;
8
using System.Collections.Generic;
9
using System.Diagnostics;
10
using System.Linq;
11
using System.Runtime.CompilerServices;
12
using System.Threading;
13

14
using Microsoft.CodeAnalysis;
15
using Microsoft.CodeAnalysis.CSharp;
16
using Microsoft.CodeAnalysis.CSharp.Syntax;
17

18
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
19

20
namespace Solti.Utils.Proxy.Internals
21
{
22
    internal abstract class UnitSyntaxFactoryBase(SyntaxFactoryContext context) : ClassSyntaxFactoryBase(context)
1✔
23
    {
24
        private static AttributeListSyntax Attributes(params AttributeSyntax[] attributes) => AttributeList
1✔
25
        (
1✔
26
            attributes.ToSyntaxList()
1✔
27
        );
1✔
28

29
        #if DEBUG
30
        internal
31
        #endif
32
        protected abstract IEnumerable<ClassDeclarationSyntax> ResolveClasses(object context, CancellationToken cancellation);
33

34
        #if DEBUG
35
        internal
36
        #endif
37
        protected virtual CompilationUnitSyntax ResolveUnitCore(object context, CancellationToken cancellation)
38
        {
1✔
39
            List<MemberDeclarationSyntax> members = [..ResolveUnitMembers(context, cancellation)];
1✔
40

41
            if (members.Any() && Context.OutputType is OutputType.Unit)
×
42
            {
1✔
43
                members[0] = members[0] switch
1✔
44
                {
1✔
45
                    NamespaceDeclarationSyntax ns => ns.WithNamespaceKeyword
1✔
46
                    (
1✔
47
                        Token
1✔
48
                        (
1✔
49
                            leading: DisableWarnings(ns.NamespaceKeyword),
1✔
50
                            kind: SyntaxKind.NamespaceKeyword,
1✔
51
                            trailing: TriviaList()
1✔
52
                        )
1✔
53
                    ),
1✔
54
                    ClassDeclarationSyntax cls => cls.WithAttributeLists
1✔
55
                    (
1✔
56
                        cls.AttributeLists.Replace
1✔
57
                        (
1✔
58
                            //
1✔
59
                            // Due to ResolveUnitMembers(), we always have attributes
1✔
60
                            //
1✔
61

1✔
62
                            cls.AttributeLists[0],
1✔
63
                            cls.AttributeLists[0].WithOpenBracketToken
1✔
64
                            (
1✔
65
                                Token
1✔
66
                                (
1✔
67
                                    leading: DisableWarnings(cls.AttributeLists[0].OpenBracketToken),
1✔
68
                                    kind: SyntaxKind.OpenBracketToken,
1✔
69
                                    trailing: TriviaList()
1✔
70
                                )
1✔
71
                            )
1✔
72
                        )
1✔
73
                    ),
1✔
74
                    _ => members[0]
×
75
                };
1✔
76

77
                static SyntaxTriviaList DisableWarnings(SyntaxToken token) =>
78
                    //
79
                    // Disable all compiler warnings:
80
                    // https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives#pragma-warning
81
                    //
82

83
                    token.LeadingTrivia.Insert
1✔
84
                    (
1✔
85
                        0,
1✔
86
                        Trivia
1✔
87
                        (
1✔
88
                            PragmaWarningDirectiveTrivia
1✔
89
                            (
1✔
90
                                Token(SyntaxKind.DisableKeyword),
1✔
91
                                isActive: true
1✔
92
                            )
1✔
93
                        )
1✔
94
                    );
1✔
95
            }
1✔
96

97
            return CompilationUnit().WithMembers
1✔
98
            (
1✔
99
                List(members)
1✔
100
            );
1✔
101
        }
1✔
102

103
        #if DEBUG
104
        internal
105
        #endif
106
        protected virtual IEnumerable<MemberDeclarationSyntax> ResolveUnitMembers(object context, CancellationToken cancellation) => ResolveClasses(context, cancellation).Select
1✔
107
        (
1✔
108
            cls => cls.WithAttributeLists
1✔
109
            (
1✔
110
                SingletonList
1✔
111
                (
1✔
112
                    Attributes
1✔
113
                    (
1✔
114
                        //
1✔
115
                        // https://docs.microsoft.com/en-us/visualstudio/code-quality/in-source-suppression-overview?view=vs-2019#generated-code
1✔
116
                        //
1✔
117

1✔
118
                        ResolveAttribute<GeneratedCodeAttribute>
1✔
119
                        (
1✔
120
                            "ProxyGen.NET".AsLiteral(),
1✔
121
                            GetType()
1✔
122
                                .Assembly
1✔
123
                                .GetName()
1✔
124
                                .Version
1✔
125
                                .ToString()
1✔
126
                                .AsLiteral()
1✔
127
                        ),
1✔
128

1✔
129
                        //
1✔
130
                        // See xXx.Designer.cs
1✔
131
                        //
1✔
132

1✔
133
                        ResolveAttribute<DebuggerNonUserCodeAttribute>(),
1✔
134
                        ResolveAttribute<CompilerGeneratedAttribute>()
1✔
135
                    )
1✔
136
                )
1✔
137
            )
1✔
138
        );
1✔
139

140
        public CompilationUnitSyntax ResolveUnit(object context, CancellationToken cancellation)
141
        {
1✔
142
            Logger.Log(LogLevel.Info, "REUN-200", $"Starting unit resolution in \"{Context.OutputType}\" mode");
1✔
143

144
            CompilationUnitSyntax result;
145

146
            try
147
            {
1✔
148
                result = ResolveUnitCore(context, cancellation);            
1✔
149
            }
1✔
150
            catch (Exception ex) when (ex is not OperationCanceledException)
1✔
151
            {
1✔
152
                if (ex.IsUser())
1✔
153
                    Logger.Log(LogLevel.Warn, "REUN-300", ex.ToString());
1✔
154
                else
155
                    Logger.Log(LogLevel.Error, "REUN-400", $"Failed to resolve the unit: {ex}");
×
156
                throw;
1✔
157
            }
158

159
            Logger.Log(LogLevel.Info, "REUN-201", $"Unit resolved");
1✔
160

161
            Logger.WriteSource(result);
1✔
162
            return result;
1✔
163
        }
1✔
164
    }
165
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc