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

xoofx / CppAst.NET / 14782910566

01 May 2025 08:25PM UTC coverage: 78.46% (-0.1%) from 78.596%
14782910566

push

github

xoofx
Add CppInclusionDirective

1147 of 1868 branches covered (61.4%)

Branch coverage included in aggregate %.

11 of 12 new or added lines in 4 files covered. (91.67%)

238 existing lines in 4 files now uncovered.

4965 of 5922 relevant lines covered (83.84%)

2224.93 hits per line

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

79.87
/src/CppAst/CppPrimitiveType.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

7
namespace CppAst
8
{
9
    /// <summary>
10
    /// A C++ primitive type (e.g `int`, `void`, `bool`...)
11
    /// </summary>
12
    public sealed class CppPrimitiveType : CppType
13
    {
14
        /// <summary>
15
        /// Singleton instance of the `void` type.
16
        /// </summary>
17
        public static readonly CppPrimitiveType Void = new CppPrimitiveType(CppPrimitiveKind.Void);
1✔
18

19
        /// <summary>
20
        /// Singleton instance of the `bool` type.
21
        /// </summary>
22
        public static readonly CppPrimitiveType Bool = new CppPrimitiveType(CppPrimitiveKind.Bool);
1✔
23

24
        /// <summary>
25
        /// Singleton instance of the `wchar` type.
26
        /// </summary>
27
        public static readonly CppPrimitiveType WChar = new CppPrimitiveType(CppPrimitiveKind.WChar);
1✔
28

29
        /// <summary>
30
        /// Singleton instance of the `char` type.
31
        /// </summary>
32
        public static readonly CppPrimitiveType Char = new CppPrimitiveType(CppPrimitiveKind.Char);
1✔
33

34
        /// <summary>
35
        /// Singleton instance of the `short` type.
36
        /// </summary>
37
        public static readonly CppPrimitiveType Short = new CppPrimitiveType(CppPrimitiveKind.Short);
1✔
38

39
        /// <summary>
40
        /// Singleton instance of the `int` type.
41
        /// </summary>
42
        public static readonly CppPrimitiveType Int = new CppPrimitiveType(CppPrimitiveKind.Int);
1✔
43

44
        /// <summary>
45
        /// Singleton instance of the `long` type.
46
        /// </summary>
47
        public static readonly CppPrimitiveType Long = new CppPrimitiveType(CppPrimitiveKind.Long);
1✔
48

49
        /// <summary>
50
        /// Singleton instance of the `long long` type.
51
        /// </summary>
52
        public static readonly CppPrimitiveType LongLong = new CppPrimitiveType(CppPrimitiveKind.LongLong);
1✔
53

54
        /// <summary>
55
        /// Singleton instance of the `unsigned char` type.
56
        /// </summary>
57
        public static readonly CppPrimitiveType UnsignedChar = new CppPrimitiveType(CppPrimitiveKind.UnsignedChar);
1✔
58

59
        /// <summary>
60
        /// Singleton instance of the `unsigned short` type.
61
        /// </summary>
62
        public static readonly CppPrimitiveType UnsignedShort = new CppPrimitiveType(CppPrimitiveKind.UnsignedShort);
1✔
63

64
        /// <summary>
65
        /// Singleton instance of the `unsigned int` type.
66
        /// </summary>
67
        public static readonly CppPrimitiveType UnsignedInt = new CppPrimitiveType(CppPrimitiveKind.UnsignedInt);
1✔
68

69
        /// <summary>
70
        /// Singleton instance of the `unsigned long` type.
71
        /// </summary>
72
        public static readonly CppPrimitiveType UnsignedLong = new CppPrimitiveType(CppPrimitiveKind.UnsignedLong);
1✔
73
        
74
        /// <summary>
75
        /// Singleton instance of the `unsigned long long` type.
76
        /// </summary>
77
        public static readonly CppPrimitiveType UnsignedLongLong = new CppPrimitiveType(CppPrimitiveKind.UnsignedLongLong);
1✔
78

79
        /// <summary>
80
        /// Singleton instance of the `float` type.
81
        /// </summary>
82
        public static readonly CppPrimitiveType Float = new CppPrimitiveType(CppPrimitiveKind.Float);
1✔
83

84
        /// <summary>
85
        /// Singleton instance of the `float` type.
86
        /// </summary>
87
        public static readonly CppPrimitiveType Double = new CppPrimitiveType(CppPrimitiveKind.Double);
1✔
88

89
        /// <summary>
90
        /// Singleton instance of the `long double` type.
91
        /// </summary>
92
        public static readonly CppPrimitiveType LongDouble = new CppPrimitiveType(CppPrimitiveKind.LongDouble);
1✔
93

94
        /// <summary>
95
        /// ObjC `id` type.
96
        /// </summary>
97
        public static readonly CppPrimitiveType ObjCId = new CppPrimitiveType(CppPrimitiveKind.ObjCId);
1✔
98

99
        /// <summary>
100
        /// ObjC `SEL` type.
101
        /// </summary>
102
        public static readonly CppPrimitiveType ObjCSel = new CppPrimitiveType(CppPrimitiveKind.ObjCSel);
1✔
103

104
        /// <summary>
105
        /// ObjC `Class` type.
106
        /// </summary>
107
        public static readonly CppPrimitiveType ObjCClass = new CppPrimitiveType(CppPrimitiveKind.ObjCClass);
1✔
108

109
        /// <summary>
110
        /// ObjC `Object` type.
111
        /// </summary>
112
        public static readonly CppPrimitiveType ObjCObject = new CppPrimitiveType(CppPrimitiveKind.ObjCObject);
1✔
113

114
        /// <summary>
115
        /// Unsigned 128 bits integer type.
116
        /// </summary>
117
        public static readonly CppPrimitiveType UInt128 = new CppPrimitiveType(CppPrimitiveKind.UInt128);
1✔
118
        
119
        /// <summary>
120
        /// 128 bits integer type.
121
        /// </summary>
122
        public static readonly CppPrimitiveType Int128 = new CppPrimitiveType(CppPrimitiveKind.Int128);
1✔
123
        
124
        /// <summary>
125
        /// Float16 type.
126
        /// </summary>
127
        public static readonly CppPrimitiveType Float16 = new CppPrimitiveType(CppPrimitiveKind.Float16);
1✔
128
        
129
        /// <summary>
130
        /// BFloat16 type.
131
        /// </summary>
132
        public static readonly CppPrimitiveType BFloat16 = new CppPrimitiveType(CppPrimitiveKind.BFloat16);
1✔
133
        
134

135
        
136
        private readonly int _sizeOf;
137

138
        /// <summary>
139
        /// Base constructor of a primitive
140
        /// </summary>
141
        /// <param name="kind"></param>
142
        private CppPrimitiveType(CppPrimitiveKind kind) : base(CppTypeKind.Primitive)
24✔
143
        {
144
            Kind = kind;
145
            UpdateSize(out _sizeOf);
24✔
146
        }
24✔
147

148
        /// <summary>
149
        /// The kind of primitive.
150
        /// </summary>
151
        public CppPrimitiveKind Kind { get; }
152

153
        private void UpdateSize(out int sizeOf)
154
        {
155
            switch (Kind)
24!
156
            {
157
                case CppPrimitiveKind.Void:
158
                    sizeOf = 0;
1✔
159
                    break;
1✔
160
                case CppPrimitiveKind.Bool:
161
                    sizeOf = 1;
1✔
162
                    break;
1✔
163
                case CppPrimitiveKind.WChar:
164
                    sizeOf = 2;
1✔
165
                    break;
1✔
166
                case CppPrimitiveKind.Char:
167
                    sizeOf = 1;
1✔
168
                    break;
1✔
169
                case CppPrimitiveKind.Short:
170
                    sizeOf = 2;
1✔
171
                    break;
1✔
172
                case CppPrimitiveKind.Int:
173
                    sizeOf = 4;
1✔
174
                    break;
1✔
175
                case CppPrimitiveKind.Long:
176
                case CppPrimitiveKind.UnsignedLong:
177
                    sizeOf = 4; // This is incorrect
2✔
178
                    break;
2✔
179
                case CppPrimitiveKind.LongLong:
180
                    sizeOf = 8;
1✔
181
                    break;
1✔
182
                case CppPrimitiveKind.UnsignedChar:
183
                    sizeOf = 1;
1✔
184
                    break;
1✔
185
                case CppPrimitiveKind.UnsignedShort:
186
                    sizeOf = 2;
1✔
187
                    break;
1✔
188
                case CppPrimitiveKind.UnsignedInt:
189
                    sizeOf = 4;
1✔
190
                    break;
1✔
191
                case CppPrimitiveKind.UnsignedLongLong:
192
                    sizeOf = 8;
1✔
193
                    break;
1✔
194
                case CppPrimitiveKind.Float:
195
                    sizeOf = 4;
1✔
196
                    break;
1✔
197
                case CppPrimitiveKind.Double:
198
                    sizeOf = 8;
1✔
199
                    break;
1✔
200
                case CppPrimitiveKind.LongDouble:
201
                    sizeOf = 8;
1✔
202
                    break;
1✔
203
                case CppPrimitiveKind.ObjCId:
204
                case CppPrimitiveKind.ObjCSel:
205
                case CppPrimitiveKind.ObjCClass:
206
                case CppPrimitiveKind.ObjCObject:
207
                    sizeOf = 8; // Valid only for 64 bits
4✔
208
                    break;
4✔
209
                case CppPrimitiveKind.UInt128:
210
                case CppPrimitiveKind.Int128:
211
                    sizeOf = 16;
2✔
212
                    break;
2✔
213
                case CppPrimitiveKind.Float16:
214
                case CppPrimitiveKind.BFloat16:
215
                    sizeOf = 2;
2✔
216
                    break;
2✔
217
                default:
218
                    throw new ArgumentOutOfRangeException();
×
219
            }
220
        }
221

222
        /// <inheritdoc />
223
        public override string ToString()
224
        {
225
            return Kind switch
123!
226
            {
123✔
227
                CppPrimitiveKind.Void => "void",
37✔
228
                CppPrimitiveKind.WChar => "wchar_t",
4✔
229
                CppPrimitiveKind.Char => "char",
4✔
230
                CppPrimitiveKind.Short => "short",
4✔
231
                CppPrimitiveKind.Int => "int",
34✔
UNCOV
232
                CppPrimitiveKind.Long => "long",
×
UNCOV
233
                CppPrimitiveKind.UnsignedLong => "unsigned long",
×
234
                CppPrimitiveKind.LongLong => "long long",
4✔
235
                CppPrimitiveKind.UnsignedChar => "unsigned char",
4✔
236
                CppPrimitiveKind.UnsignedShort => "unsigned short",
4✔
237
                CppPrimitiveKind.UnsignedInt => "unsigned int",
6✔
238
                CppPrimitiveKind.UnsignedLongLong => "unsigned long long",
4✔
239
                CppPrimitiveKind.Float => "float",
8✔
240
                CppPrimitiveKind.Double => "double",
6✔
UNCOV
241
                CppPrimitiveKind.LongDouble => "long double",
×
242
                CppPrimitiveKind.Bool => "bool",
4✔
UNCOV
243
                CppPrimitiveKind.Int128 => "System.Int128",
×
UNCOV
244
                CppPrimitiveKind.UInt128 => "System.UInt128",
×
UNCOV
245
                CppPrimitiveKind.ObjCId => "ObjCId",
×
UNCOV
246
                CppPrimitiveKind.ObjCSel => "ObjCSel",
×
UNCOV
247
                CppPrimitiveKind.ObjCClass => "ObjCClass",
×
UNCOV
248
                CppPrimitiveKind.ObjCObject => "ObjCObject",
×
UNCOV
249
                CppPrimitiveKind.Float16 => "System.Half",
×
UNCOV
250
                CppPrimitiveKind.BFloat16 => "BFloat16",
×
UNCOV
251
                _ => throw new InvalidOperationException($"Unhandled PrimitiveKind: {Kind}")
×
252
            };
123✔
253
        }
254

255
        private bool Equals(CppPrimitiveType other)
256
        {
UNCOV
257
            return base.Equals(other) && Kind == other.Kind;
×
258
        }
259

260
        /// <inheritdoc />
261
        public override int SizeOf
262
        {
263
            get => _sizeOf;
50✔
UNCOV
264
            set => throw new InvalidOperationException("Cannot set the SizeOf of a primitive type");
×
265
        }
266

267
        /// <inheritdoc />
268
        public override CppType GetCanonicalType()
269
        {
270
            return this;
12✔
271
        }
272
    }
273
}
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