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

xoofx / CppAst.NET / 14764148296

30 Apr 2025 08:52PM UTC coverage: 78.596% (-0.2%) from 78.842%
14764148296

push

github

web-flow
Merge pull request #110 from xoofx/objective-c

Add support for Objective-C

1123 of 1824 branches covered (61.57%)

Branch coverage included in aggregate %.

456 of 566 new or added lines in 13 files covered. (80.57%)

2 existing lines in 1 file now uncovered.

4888 of 5824 relevant lines covered (83.93%)

2189.8 hits per line

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

44.22
/src/CppAst/CppClass.cs
1
// Copyright (c) Alexandre Mutel. All rights reserved.
2
// Licensed under the BSD-Clause 2 license.
3
// See license.txt file in the project root for full license information.
4

5
using System;
6
using System.Collections.Generic;
7
using System.Text;
8

9
namespace CppAst
10
{
11
    /// <summary>
12
    /// A C++ class, struct or union.
13
    /// </summary>
14
    public class CppClass : CppTypeDeclaration, ICppMemberWithVisibility, ICppDeclarationContainer, ICppTemplateOwner
15
    {
16
        /// <summary>
17
        /// Creates a new instance.
18
        /// </summary>
19
        /// <param name="name">Name of this type.</param>
20
        public CppClass(string name) : base(CppTypeKind.StructOrClass)
161✔
21
        {
22
            Name = name ?? throw new ArgumentNullException(nameof(name));
161!
23
            BaseTypes = new List<CppBaseType>();
24
            Fields = new CppContainerList<CppField>(this);
161✔
25
            Constructors = new CppContainerList<CppFunction>(this);
161✔
26
            Destructors = new CppContainerList<CppFunction>(this);
161✔
27
            Functions = new CppContainerList<CppFunction>(this);
161✔
28
            Enums = new CppContainerList<CppEnum>(this);
161✔
29
            Classes = new CppContainerList<CppClass>(this);
161✔
30
            Typedefs = new CppContainerList<CppTypedef>(this);
161✔
31
            TemplateParameters = new List<CppType>();
32
            Attributes = new List<CppAttribute>();
33
            TokenAttributes = new List<CppAttribute>();
34
            ObjCImplementedProtocols = new List<CppClass>();
35
            Properties = new CppContainerList<CppProperty>(this);
161✔
36
            ObjCCategories = new List<CppClass>();
37
            ObjCCategoryName = string.Empty;
161✔
38
        }
161✔
39

40
        /// <summary>
41
        /// Kind of the instance (`class` `struct` or `union`)
42
        /// </summary>
43
        public CppClassKind ClassKind { get; set; }
44

45
        public CppTemplateKind TemplateKind { get; set; }
46

47
        /// <inheritdoc />
48
        public string Name { get; set; }
49
        
50
        /// <summary>
51
        /// Gets or sets the target of the Objective-C category. Null if this class is not an <see cref="CppClassKind.ObjCInterfaceCategory"/>.
52
        /// </summary>
53
        public CppClass ObjCCategoryTargetClass { get; set; }
54

55
        /// <summary>
56
        /// Gets or sets the name of the Objective-C category. Empty if this class is not an <see cref="CppClassKind.ObjCInterfaceCategory"/>
57
        /// </summary>
58
        public string ObjCCategoryName { get; set; }
59

60
        public override string FullName
61
        {
62
            get
63
            {
64
                StringBuilder sb = new StringBuilder();
4✔
65
                string fullparent = FullParentName;
4✔
66
                if (string.IsNullOrEmpty(fullparent))
4!
67
                {
68
                    sb.Append(Name);
×
69
                }
70
                else
71
                {
72
                    sb.Append($"{fullparent}::{Name}");
4✔
73
                }
74

75
                if (TemplateKind == CppTemplateKind.TemplateClass
4✔
76
                    || TemplateKind == CppTemplateKind.PartialTemplateClass)
4✔
77
                {
78
                    sb.Append('<');
2✔
79
                    for (int i = 0; i < TemplateParameters.Count; i++)
8✔
80
                    {
81
                        var tp = TemplateParameters[i];
2✔
82
                        if (i != 0)
2!
83
                        {
84
                            sb.Append(", ");
×
85
                        }
86
                        sb.Append(tp.ToString());
2✔
87
                    }
88
                    sb.Append('>');
2✔
89
                }
90
                else if (TemplateKind == CppTemplateKind.TemplateSpecializedClass)
2✔
91
                {
92
                    sb.Append('<');
2✔
93
                    for (int i = 0; i < TemplateSpecializedArguments.Count; i++)
8✔
94
                    {
95
                        var ta = TemplateSpecializedArguments[i];
2✔
96
                        if (i != 0)
2!
97
                        {
98
                            sb.Append(", ");
×
99
                        }
100
                        sb.Append(ta.ArgString);
2✔
101
                    }
102
                    sb.Append('>');
2✔
103
                }
104
                //else if(TemplateKind == CppTemplateKind.PartialTemplateClass)
105
                //{
106
                //    sb.Append('<');
107
                //    sb.Append('>');
108
                //}
109
                return sb.ToString();
4✔
110
            }
111
        }
112

113
        /// <inheritdoc />
114
        public CppVisibility Visibility { get; set; }
115

116
        /// <inheritdoc />
117
        public List<CppAttribute> Attributes { get; }
118

119
        [Obsolete("TokenAttributes is deprecated. please use system attributes and annotate attributes")]
120
        public List<CppAttribute> TokenAttributes { get; }
121

122
        public MetaAttributeMap MetaAttributes { get; private set; } = new MetaAttributeMap();
123

124
        /// <summary>
125
        /// Gets or sets a boolean indicating if this type is a definition. If <c>false</c> the type was only declared but is not defined.
126
        /// </summary>
127
        public bool IsDefinition { get; set; }
128

129
        /// <summary>
130
        /// Gets or sets a boolean indicating if this declaration is anonymous.
131
        /// </summary>
132
        public bool IsAnonymous { get; set; }
133

134
        /// <summary>
135
        /// Get the base types of this type.
136
        /// </summary>
137
        public List<CppBaseType> BaseTypes { get; }
138
        
139
        /// <summary>
140
        /// Get the Objective-C implemented protocols.
141
        /// </summary>
142
        public List<CppClass> ObjCImplementedProtocols { get; }
143
        
144
        /// <inheritdoc />
145
        public CppContainerList<CppField> Fields { get; }
146

147
        /// <inheritdoc />
148
        public CppContainerList<CppProperty> Properties { get; }
149

150
        /// <summary>
151
        /// Gets the constructors of this instance.
152
        /// </summary>
153
        public CppContainerList<CppFunction> Constructors { get; set; }
154

155
        /// <summary>
156
        /// Gets the destructors of this instance.
157
        /// </summary>
158
        public CppContainerList<CppFunction> Destructors { get; set; }
159

160
        /// <inheritdoc />
161
        public CppContainerList<CppFunction> Functions { get; }
162

163
        /// <inheritdoc />
164
        public CppContainerList<CppEnum> Enums { get; }
165

166
        /// <inheritdoc />
167
        public CppContainerList<CppClass> Classes { get; }
168

169
        /// <inheritdoc />
170
        public CppContainerList<CppTypedef> Typedefs { get; }
171
        
172
        /// <summary>
173
        /// Gets the Objective-C categories of this instance.
174
        /// </summary>
175
        public List<CppClass> ObjCCategories { get; }
176

177
        /// <inheritdoc />
178
        public List<CppType> TemplateParameters { get; }
179

180
        public List<CppTemplateArgument> TemplateSpecializedArguments { get; } = new List<CppTemplateArgument>();
181

182
        /// <summary>
183
        /// Gets the specialized class template of this instance.
184
        /// </summary>
185
        public CppClass SpecializedTemplate { get; set; }
186

187

188
        public bool IsEmbeded => Parent is CppClass;
×
189

190
        public bool IsAbstract { get; set; }
191

192

193
        /// <inheritdoc />
194
        public override int SizeOf { get; set; }
195

196
        /// <summary>
197
        /// Gets the alignment of this instance.
198
        /// </summary>
199
        public int AlignOf { get; set; }
200

201
        /// <inheritdoc />
202
        public override CppType GetCanonicalType()
203
        {
204
            return this;
×
205
        }
206

207
        /// <inheritdoc />
208
        public override string ToString()
209
        {
210
            var builder = new StringBuilder();
1✔
211
            switch (ClassKind)
1!
212
            {
213
                case CppClassKind.Class:
214
                    builder.Append("class ");
×
215
                    break;
×
216
                case CppClassKind.Struct:
217
                    builder.Append("struct ");
1✔
218
                    break;
1✔
219
                case CppClassKind.Union:
220
                    builder.Append("union ");
×
221
                    break;
×
222
                case CppClassKind.ObjCInterface:
223
                case CppClassKind.ObjCInterfaceCategory:
NEW
224
                    builder.Append("@interface ");
×
NEW
225
                    break;
×
226
                case CppClassKind.ObjCProtocol:
NEW
227
                    builder.Append("@protocol ");
×
NEW
228
                    break;
×
229
                default:
230
                    throw new ArgumentOutOfRangeException();
×
231
            }
232

233
            if (!string.IsNullOrEmpty(Name))
1✔
234
            {
235
                builder.Append(Name);
1✔
236
            }
237
            
238
            //Add template arguments here
239
            if(TemplateKind != CppTemplateKind.NormalClass)
1!
240
            {
241
                builder.Append("<");
×
242

243
                if(TemplateKind == CppTemplateKind.TemplateSpecializedClass)
×
244
                {
245
                    for(var i = 0; i < TemplateSpecializedArguments.Count; i++)
×
246
                    {
247
                        if(i > 0) builder.Append(", ");
×
248
                        builder.Append(TemplateSpecializedArguments[i].ToString());
×
249
                    }
250
                }
251
                else if(TemplateKind == CppTemplateKind.TemplateClass)
×
252
                {
NEW
253
                    for (var i = 0; i < TemplateParameters.Count; i++)
×
254
                    {
NEW
255
                        if (i > 0) builder.Append(", ");
×
NEW
256
                        builder.Append(TemplateParameters[i].ToString());
×
257
                    }
258
                }
259

260
                builder.Append(">");
×
261
            }
262

263
            if (BaseTypes.Count > 0)
1!
264
            {
NEW
265
                builder.Append(" : ");
×
NEW
266
                for (var i = 0; i < BaseTypes.Count; i++)
×
267
                {
NEW
268
                    var baseType = BaseTypes[i];
×
NEW
269
                    if (i > 0) builder.Append(", ");
×
NEW
270
                    builder.Append(baseType);
×
271
                }
272
            }
273
            
274
            if (!string.IsNullOrEmpty(ObjCCategoryName))
1!
275
            {
NEW
276
                builder.Append(" (").Append(ObjCCategoryName).Append(')');
×
277
            }
278

279
            if (ObjCImplementedProtocols.Count > 0)
1!
280
            {
NEW
281
                builder.Append(" <");
×
NEW
282
                for (var i = 0; i < ObjCImplementedProtocols.Count; i++)
×
283
                {
NEW
284
                    var protocol = ObjCImplementedProtocols[i];
×
NEW
285
                    if (i > 0) builder.Append(", ");
×
NEW
286
                    builder.Append(protocol.Name);
×
287
                }
NEW
288
                builder.Append(">");
×
289
            }
290
            
291
            return builder.ToString();
1✔
292
        }
293

294
        public override IEnumerable<ICppDeclaration> Children()
295
        {
296
            foreach (var item in CppContainerHelper.Children(this))
×
297
            {
298
                yield return item;
×
299
            }
300

301
            foreach (var item in Constructors)
×
302
            {
303
                yield return item;
×
304
            } 
305

306
            foreach (var item in Destructors)
×
307
            {
308
                yield return item;
×
309
            }
310
        }
×
311
    }
312
}
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