@@ -119,7 +119,7 @@ static bool ParseToNumber(string num, Enumeration @enum, out long val)
119
119
evaluator . Variables . Add ( item . Name . ToLower ( ) , item . Value ) ;
120
120
}
121
121
try
122
- {
122
+ {
123
123
var ret = evaluator . Evaluate ( "(long)" + num . ReplaceLineBreaks ( " " ) . Replace ( '\\ ' , ' ' ) ) ;
124
124
val = ( long ) ret ;
125
125
return true ;
@@ -149,8 +149,14 @@ public static Enumeration GenerateEnumFromMacros(this ASTContext context, string
149
149
var pattern = string . Join ( "|" , macros ) ;
150
150
var regex = new Regex ( pattern ) ;
151
151
152
+ int maxItems = 0 ;
153
+ TranslationUnit unitToAttach = null ;
154
+ ulong maxValue = 0 ;
155
+
152
156
foreach ( var unit in context . TranslationUnits )
153
157
{
158
+ int numItems = 0 ;
159
+
154
160
foreach ( var macro in unit . PreprocessedEntities . OfType < MacroDefinition > ( ) )
155
161
{
156
162
var match = regex . Match ( macro . Name ) ;
@@ -159,27 +165,31 @@ public static Enumeration GenerateEnumFromMacros(this ASTContext context, string
159
165
if ( macro . Enumeration != null )
160
166
continue ;
161
167
162
- // Skip this macro if the enum already has an item with same entry .
168
+ // Skip this macro if the enum already has an item with same name .
163
169
if ( @enum . Items . Exists ( it => it . Name == macro . Name ) )
164
170
continue ;
165
171
166
- // Set the namespace to the namespace we found the 1st item in
167
- if ( @enum . Namespace == null )
168
- @enum . Namespace = unit ;
169
-
170
172
var item = @enum . GenerateEnumItemFromMacro ( macro ) ;
171
173
@enum . AddItem ( item ) ;
172
-
173
174
macro . Enumeration = @enum ;
175
+
176
+ maxValue = Math . Max ( maxValue , item . Value ) ;
177
+ numItems ++ ;
174
178
}
175
179
176
- if ( @enum . Items . Count > 0 )
180
+ if ( numItems > maxItems )
177
181
{
178
- unit . Declarations . Add ( @enum ) ;
179
- break ;
182
+ maxItems = numItems ;
183
+ unitToAttach = unit ;
180
184
}
181
185
}
182
186
187
+ if ( @enum . Items . Count > 0 )
188
+ {
189
+ @enum . Namespace = unitToAttach ;
190
+ unitToAttach . Declarations . Add ( @enum ) ;
191
+ }
192
+
183
193
return @enum ;
184
194
}
185
195
0 commit comments