1
1
using CppSharp . Parser . AST ;
2
2
using System . Reflection ;
3
3
using LanguageVersion = CppSharp . Parser . LanguageVersion ;
4
+ using System . Globalization ;
5
+ using System . Collections . Generic ;
6
+ using System . Linq ;
4
7
5
8
namespace CppSharp . Parser
6
9
{
@@ -46,7 +49,7 @@ public enum LanguageVersion
46
49
/// C++ programming language (year 2017, GNU variant).
47
50
/// </summary>
48
51
CPP17_GNU ,
49
- } ;
52
+ }
50
53
51
54
public class ParserOptions : CppParserOptions
52
55
{
@@ -63,9 +66,88 @@ public ParserOptions()
63
66
public bool EnableRTTI { get ; set ; }
64
67
public LanguageVersion ? LanguageVersion { get ; set ; }
65
68
69
+ public ParserOptions BuildForSourceFile (
70
+ IEnumerable < CppSharp . AST . Module > modules , string file = null )
71
+ {
72
+ var options = new ParserOptions
73
+ {
74
+ Abi = this . Abi ,
75
+ ToolSetToUse = this . ToolSetToUse ,
76
+ TargetTriple = this . TargetTriple ,
77
+ NoStandardIncludes = this . NoStandardIncludes ,
78
+ NoBuiltinIncludes = this . NoBuiltinIncludes ,
79
+ MicrosoftMode = this . MicrosoftMode ,
80
+ Verbose = this . Verbose ,
81
+ LanguageVersion = this . LanguageVersion
82
+ } ;
83
+
84
+ // This eventually gets passed to Clang's MSCompatibilityVersion, which
85
+ // is in turn used to derive the value of the built-in define _MSC_VER.
86
+ // It used to receive a 4-digit based identifier but now expects a full
87
+ // version MSVC digit, so check if we still have the old version and
88
+ // convert to the right format.
89
+
90
+ if ( ToolSetToUse . ToString ( CultureInfo . InvariantCulture ) . Length == 4 )
91
+ ToolSetToUse *= 100000 ;
92
+
93
+ for ( uint i = 0 ; i < ArgumentsCount ; ++ i )
94
+ {
95
+ var arg = GetArguments ( i ) ;
96
+ options . AddArguments ( arg ) ;
97
+ }
98
+
99
+ for ( uint i = 0 ; i < IncludeDirsCount ; ++ i )
100
+ {
101
+ var include = GetIncludeDirs ( i ) ;
102
+ options . AddIncludeDirs ( include ) ;
103
+ }
104
+
105
+ for ( uint i = 0 ; i < SystemIncludeDirsCount ; ++ i )
106
+ {
107
+ var include = GetSystemIncludeDirs ( i ) ;
108
+ options . AddSystemIncludeDirs ( include ) ;
109
+ }
110
+
111
+ for ( uint i = 0 ; i < DefinesCount ; ++ i )
112
+ {
113
+ var define = GetDefines ( i ) ;
114
+ options . AddDefines ( define ) ;
115
+ }
116
+
117
+ for ( uint i = 0 ; i < UndefinesCount ; ++ i )
118
+ {
119
+ var define = GetUndefines ( i ) ;
120
+ options . AddUndefines ( define ) ;
121
+ }
122
+
123
+ for ( uint i = 0 ; i < LibraryDirsCount ; ++ i )
124
+ {
125
+ var lib = GetLibraryDirs ( i ) ;
126
+ options . AddLibraryDirs ( lib ) ;
127
+ }
128
+
129
+ foreach ( var module in modules . Where (
130
+ m => file == null || m . Headers . Contains ( file ) ) )
131
+ {
132
+ foreach ( var include in module . IncludeDirs )
133
+ options . AddIncludeDirs ( include ) ;
134
+
135
+ foreach ( var define in module . Defines )
136
+ options . AddDefines ( define ) ;
137
+
138
+ foreach ( var undefine in module . Undefines )
139
+ options . AddUndefines ( undefine ) ;
140
+
141
+ foreach ( var libraryDir in module . LibraryDirs )
142
+ options . AddLibraryDirs ( libraryDir ) ;
143
+ }
144
+
145
+ return options ;
146
+ }
147
+
66
148
public void SetupMSVC ( )
67
149
{
68
- VisualStudioVersion vsVersion = VisualStudioVersion . Latest ;
150
+ var vsVersion = VisualStudioVersion . Latest ;
69
151
70
152
// Silence "warning CS0162: Unreachable code detected"
71
153
#pragma warning disable 162
@@ -107,6 +189,7 @@ public void SetupMSVC(VisualStudioVersion vsVersion)
107
189
foreach ( var include in MSVCToolchain . GetSystemIncludes ( vsVersion ) )
108
190
AddSystemIncludeDirs ( include ) ;
109
191
192
+ // do not remove the CppSharp prefix becase the Mono C# compiler breaks
110
193
if ( ! LanguageVersion . HasValue )
111
194
LanguageVersion = CppSharp . Parser . LanguageVersion . CPP14_GNU ;
112
195
@@ -145,6 +228,7 @@ public void Setup()
145
228
146
229
private void SetupArguments ( )
147
230
{
231
+ // do not remove the CppSharp prefix becase the Mono C# compiler breaks
148
232
if ( ! LanguageVersion . HasValue )
149
233
LanguageVersion = CppSharp . Parser . LanguageVersion . CPP14_GNU ;
150
234
0 commit comments