-
-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathCppParserOptions.cs
More file actions
222 lines (188 loc) · 8.1 KB
/
Copy pathCppParserOptions.cs
File metadata and controls
222 lines (188 loc) · 8.1 KB
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
// Copyright (c) Alexandre Mutel. All rights reserved.
// Licensed under the BSD-Clause 2 license.
// See license.txt file in the project root for full license information.
using System;
using System.Collections.Generic;
namespace CppAst
{
/// <summary>
/// Defines the options used by the <see cref="CppParser"/>
/// </summary>
public class CppParserOptions
{
/// <summary>
/// Default constructor.
/// </summary>
public CppParserOptions()
{
ParserKind = CppParserKind.Cpp;
SystemIncludeFolders = new List<string>();
IncludeFolders = new List<string>();
//Add a default macro here for CppAst.Net
Defines = new List<string>() {
"__cppast_run__", //Help us for identify the CppAst.Net handler
@"__cppast_impl(...)=__attribute__((annotate(#__VA_ARGS__)))", //Help us for use annotate attribute convenience
@"__cppast(...)=__cppast_impl(__VA_ARGS__)", //Add a macro wrapper here, so the argument with macro can be handle right for compiler.
};
AdditionalArguments = new List<string>()
{
"-Wno-pragma-once-outside-header"
};
AutoSquashTypedef = true;
ParseMacros = false;
ParseComments = true;
ParseSystemIncludes = true;
ParseTokenAttributes = false;
ParseCommentAttribute = false;
ParseFunctionBodies = false;
// Default triple targets
TargetCpu = IntPtr.Size == 8 ? CppTargetCpu.X86_64 : CppTargetCpu.X86;
TargetCpuSub = string.Empty;
TargetVendor = "pc";
TargetSystem = "windows";
TargetAbi = "";
}
/// <summary>
/// List of the include folders.
/// </summary>
public List<string> IncludeFolders { get; private set; }
/// <summary>
/// List of the system include folders.
/// </summary>
public List<string> SystemIncludeFolders { get; private set; }
/// <summary>
/// List of the defines.
/// </summary>
public List<string> Defines { get; private set; }
/// <summary>
/// List of the additional arguments passed directly to the C++ Clang compiler.
/// </summary>
public List<string> AdditionalArguments { get; private set; }
/// <summary>
/// Gets or sets the parser kind. Default is <see cref="CppParserKind.Cpp"/>. This is used to select the parser to use.
/// </summary>
public CppParserKind ParserKind { get; set; } = CppParserKind.Cpp;
/// <summary>
/// Gets or sets a boolean indicating whether to parser non-Doxygen comments in addition to Doxygen comments. Default is <c>true</c>
/// </summary>
public bool ParseComments { get; set; }
/// <summary>
/// Gets or sets a boolean indicating whether to parse macros. Default is <c>false</c>.
/// </summary>
public bool ParseMacros { get; set; }
/// <summary>
/// Gets or sets a boolean indicating whether un-named enum/struct referenced by a typedef will be renamed directly to the typedef name. Default is <c>true</c>
/// </summary>
public bool AutoSquashTypedef { get; set; }
/// <summary>
/// Gets or sets a boolean indicating whether to parse System Include headers. Default is <c>true</c>
/// </summary>
public bool ParseSystemIncludes { get; set; }
/// <summary>
/// Gets or sets a boolean indicating whether to parse meta attributes. Default is <c>false</c>
/// </summary>
public bool ParseTokenAttributes { get; set; }
/// <summary>
/// Gets or sets a boolean indicating whether to parse comment attributes. Default is <c>false</c>
/// </summary>
public bool ParseCommentAttribute { get; set; }
/// <summary>
/// Gets or sets a boolean indicating whether to parse function bodies. Default is <c>false</c>
/// </summary>
public bool ParseFunctionBodies { get; set; }
/// <summary>
/// Sets <see cref="ParseMacros"/> to <c>true</c> and return this instance.
/// </summary>
/// <returns>This instance</returns>
public CppParserOptions EnableMacros()
{
ParseMacros = true;
return this;
}
/// <summary>
/// Cpu Clang target. Default is <see cref="CppTargetCpu.X86"/>
/// </summary>
public CppTargetCpu TargetCpu { get; set; }
/// <summary>
/// Cpu sub Clang target. Default is ""
/// </summary>
public string TargetCpuSub { get; set; }
/// <summary>
/// Vendor Clang target. Default is "pc"
/// </summary>
public string TargetVendor { get; set; }
/// <summary>
/// System Clang target. Default is "windows"
/// </summary>
public string TargetSystem { get; set; }
/// <summary>
/// Abi Clang target. Default is ""
/// </summary>
public string TargetAbi { get; set; }
/// <summary>
/// Gets or sets a C/C++ pre-header included before the files/text to parse
/// </summary>
public string PreHeaderText { get; set; }
/// <summary>
/// Gets or sets a C/C++ post-header included after the files/text to parse
/// </summary>
public string PostHeaderText { get; set; }
/// <summary>
/// Clone this instance.
/// </summary>
/// <returns>Return a copy of this options.</returns>
public virtual CppParserOptions Clone()
{
var newOptions = (CppParserOptions)MemberwiseClone();
// Copy lists
newOptions.IncludeFolders = new List<string>(IncludeFolders);
newOptions.SystemIncludeFolders = new List<string>(SystemIncludeFolders);
newOptions.Defines = new List<string>(Defines);
newOptions.AdditionalArguments = new List<string>(AdditionalArguments);
return newOptions;
}
/// <summary>
/// Configure this instance with Windows and MSVC.
/// </summary>
/// <returns>This instance</returns>
public CppParserOptions ConfigureForWindowsMsvc(CppTargetCpu targetCpu = CppTargetCpu.X86, CppVisualStudioVersion vsVersion = CppVisualStudioVersion.VS2022)
{
// 1920
var highVersion = ((int)vsVersion) / 100; // => 19
var lowVersion = ((int)vsVersion) % 100; // => 20
var versionAsString = $"{highVersion}.{lowVersion}";
TargetCpu = targetCpu;
TargetCpuSub = string.Empty;
TargetVendor = "pc";
TargetSystem = "windows";
TargetAbi = $"msvc{versionAsString}";
// See https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=vs-2019
Defines.Add($"_MSC_VER={(int)vsVersion}");
Defines.Add("_WIN32=1");
switch (targetCpu)
{
case CppTargetCpu.X86:
Defines.Add("_M_IX86=600");
break;
case CppTargetCpu.X86_64:
Defines.Add("_M_AMD64=100");
Defines.Add("_M_X64=100");
Defines.Add("_WIN64=1");
break;
case CppTargetCpu.ARM:
Defines.Add("_M_ARM=7");
break;
case CppTargetCpu.ARM64:
Defines.Add("_M_ARM64=1");
Defines.Add("_WIN64=1");
break;
default:
throw new ArgumentOutOfRangeException(nameof(targetCpu), targetCpu, null);
}
AdditionalArguments.Add("-fms-extensions");
AdditionalArguments.Add("-fms-compatibility");
AdditionalArguments.Add($"-fms-compatibility-version={versionAsString}");
return this;
}
}
}