-
Notifications
You must be signed in to change notification settings - Fork 520
/
Copy pathCheckMacrosPass.cs
244 lines (197 loc) · 8.41 KB
/
CheckMacrosPass.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using System;
using System.Collections.Generic;
using System.Linq;
using CppSharp.AST;
namespace CppSharp.Passes
{
/// <summary>
/// This pass implements checks for helper macros in declarations.
/// The supported macros are listed below with a "CS" prefix which
/// stands for CppSharp. Using custom prefixes is also supported by
/// passing the value to the constructor of the pass.
///
/// CS_IGNORE_FILE (translation units)
/// Used to ignore whole translation units.
///
/// CS_IGNORE (declarations)
/// Used to ignore declarations from being processed.
///
/// CS_IGNORE_GEN (declarations)
/// Used to ignore declaration from being generated.
///
/// CS_IGNORE_FILE (.h)
/// Used to ignore all declarations of one header.
///
/// CS_VALUE_TYPE (classes and structs)
/// Used to flag that a class or struct is a value type.
///
/// CS_IN_OUT / CS_OUT (parameters)
/// Used in function parameters to specify their usage kind.
///
/// CS_FLAGS (enums)
/// Used to specify that enumerations represent bitwise flags.
///
/// CS_READONLY (fields and properties)
/// Used in fields and properties to specify read-only semantics.
///
/// CS_EQUALS / CS_HASHCODE (methods)
/// Used to flag method as representing the .NET Equals or
/// Hashcode methods.
///
/// CS_CONSTRAINT(TYPE [, TYPE]*) (templates)
/// Used to define constraint of generated generic type or generic method.
///
/// CS_INTERNAL (declarations)
/// Used to flag a method as internal to an assembly. So, it is
/// not accessible outside that assembly.
///
/// There isn't a standardized header provided by CppSharp so you will
/// have to define these on your own.
/// </summary>
public class CheckMacroPass : TranslationUnitPass
{
private readonly string Prefix;
public CheckMacroPass(string prefix = "CS")
{
Prefix = prefix;
}
public override bool VisitDeclaration(Declaration decl)
{
if (AlreadyVisited(decl))
return false;
if (decl is DeclarationContext && !(decl is Class) && !(decl is Function))
return true;
var expansions = decl.PreprocessedEntities.OfType<MacroExpansion>();
if (expansions.Any(e => e.Text == Prefix + "_INTERNAL"))
decl.Access = AccessSpecifier.Internal;
CheckIgnoreMacros(decl, expansions);
return true;
}
void CheckIgnoreMacros(Declaration decl, IEnumerable<MacroExpansion> expansions)
{
if (expansions.Any(e => e.Text == Prefix + "_IGNORE" &&
e.MacroLocation != MacroLocation.ClassBody &&
e.MacroLocation != MacroLocation.FunctionBody &&
e.MacroLocation != MacroLocation.FunctionParameters))
{
Diagnostics.Debug("Decl '{0}' was ignored due to ignore macro",
decl.Name);
decl.ExplicitlyIgnore();
}
if (expansions.Any(e => e.Text == Prefix + "_IGNORE_GEN" &&
e.MacroLocation != MacroLocation.ClassBody &&
e.MacroLocation != MacroLocation.FunctionBody &&
e.MacroLocation != MacroLocation.FunctionParameters))
decl.GenerationKind = GenerationKind.Internal;
}
public override bool VisitTranslationUnit(TranslationUnit unit)
{
var expansions = unit.PreprocessedEntities.OfType<MacroExpansion>();
if (expansions.Any(e => e.Text == Prefix + "_IGNORE_FILE"))
{
unit.ExplicitlyIgnore();
}
return base.VisitTranslationUnit(unit);
}
public override bool VisitClassDecl(Class @class)
{
var expansions = @class.PreprocessedEntities.OfType<MacroExpansion>();
if (expansions.Any(e => e.Text == Prefix + "_VALUE_TYPE"))
@class.Type = ClassType.ValueType;
// If the class is a forward declaration, then we process the macro expansions
// of the complete class as if they were specified on the forward declaration.
if (@class.CompleteDeclaration != null)
{
var completeExpansions = @class.CompleteDeclaration.PreprocessedEntities
.OfType<MacroExpansion>();
CheckIgnoreMacros(@class, completeExpansions);
}
return base.VisitClassDecl(@class);
}
public override bool VisitParameterDecl(Parameter parameter)
{
if (!VisitDeclaration(parameter))
return false;
var expansions = parameter.PreprocessedEntities.OfType<MacroExpansion>();
if (expansions.Any(e => e.Text == Prefix + "_OUT"))
parameter.Usage = ParameterUsage.Out;
if (expansions.Any(e => e.Text == Prefix + "_IN_OUT"))
parameter.Usage = ParameterUsage.InOut;
return true;
}
public override bool VisitEnumDecl(Enumeration @enum)
{
if (!VisitDeclaration(@enum))
return false;
var expansions = @enum.PreprocessedEntities.OfType<MacroExpansion>();
if (expansions.Any(e => e.Text == Prefix + "_FLAGS"))
@enum.SetFlags();
return true;
}
public override bool VisitMethodDecl(Method method)
{
var expansions = method.PreprocessedEntities.OfType<MacroExpansion>();
if (expansions.Any(e => e.Text == Prefix + "_HASHCODE"
|| e.Text == Prefix + "_EQUALS"))
method.ExplicitlyIgnore();
return base.VisitMethodDecl(method);
}
public override bool VisitFieldDecl(Field field)
{
if (!VisitDeclaration(field))
return false;
var expansions = field.PreprocessedEntities.OfType<MacroExpansion>();
if (expansions.Any(e => e.Text == Prefix + "_READONLY"))
{
var quals = field.QualifiedType.Qualifiers;
quals.IsConst = true;
var qualType = field.QualifiedType;
qualType.Qualifiers = quals;
field.QualifiedType = qualType;
}
return true;
}
public override bool VisitProperty(Property property)
{
var expansions = property.PreprocessedEntities.OfType<MacroExpansion>();
if (expansions.Any(e => e.Text == Prefix + "_READONLY"))
{
var setMethod = property.SetMethod;
if (setMethod != null)
property.SetMethod.ExplicitlyIgnore();
}
return base.VisitProperty(property);
}
public override bool VisitClassTemplateDecl(ClassTemplate template)
{
CheckForTemplateConstraints(template);
return base.VisitClassTemplateDecl(template);
}
public override bool VisitFunctionTemplateDecl(FunctionTemplate template)
{
CheckForTemplateConstraints(template);
return base.VisitFunctionTemplateDecl(template);
}
private void CheckForTemplateConstraints(Template template)
{
var expansions = template.PreprocessedEntities.OfType<MacroExpansion>();
var expansion = expansions.FirstOrDefault(
e => e.Text.StartsWith(Prefix + "_CONSTRAINT", StringComparison.Ordinal));
if (expansion == null)
return;
var args = GetArguments(expansion.Text);
for (var i = 0; i < template.Parameters.Count && i < args.Length; ++i)
{
var templateParam = template.Parameters[i] as TypeTemplateParameter;
if (templateParam != null)
templateParam.Constraint = args[i];
}
}
private static string[] GetArguments(string str)
{
str = str.Substring(str.LastIndexOf('('));
str = str.Trim('(', ')');
return str.Split(',');
}
}
}