Add -where to report the current parser line and position within it

This commit is contained in:
rfm 2023-10-28 15:37:12 +01:00
parent 57f066442e
commit 1f5af5388b
2 changed files with 248 additions and 175 deletions

View file

@ -124,6 +124,7 @@
- (unsigned) skipToEndOfLine; - (unsigned) skipToEndOfLine;
- (unsigned) skipUnit; - (unsigned) skipUnit;
- (NSMutableArray*) sources; - (NSMutableArray*) sources;
- (NSString*) where;
@end @end
#endif #endif

View file

@ -3859,7 +3859,9 @@ countAttributes(NSSet *keys, NSDictionary *a)
{ {
NSString *directive = [self parseIdentifier]; NSString *directive = [self parseIdentifier];
if ([directive isEqual: @"define"] && inHeader) if ([directive isEqual: @"define"])
{
if (inHeader)
{ {
NSMutableDictionary *defn; NSMutableDictionary *defn;
@ -3875,7 +3877,8 @@ countAttributes(NSSet *keys, NSDictionary *a)
{ {
if (dict == nil) if (dict == nil)
{ {
dict = [[NSMutableDictionary alloc] initWithCapacity: 8]; dict = [[NSMutableDictionary alloc]
initWithCapacity: 8];
[info setObject: dict forKey: @"Macros"]; [info setObject: dict forKey: @"Macros"];
RELEASE(dict); RELEASE(dict);
} }
@ -3899,6 +3902,15 @@ countAttributes(NSSet *keys, NSDictionary *a)
} }
} }
} }
return pos;
}
else
{
/* Macro definition inside source is ignored since it is not
* visible to the outside world.
*/
return [self skipRemainderOfLine];
}
} }
else if ([directive isEqual: @"endif"]) else if ([directive isEqual: @"endif"])
{ {
@ -3910,6 +3922,7 @@ countAttributes(NSSet *keys, NSDictionary *a)
{ {
[ifStack removeLastObject]; [ifStack removeLastObject];
} }
return [self skipRemainderOfLine];
} }
else if ([directive isEqual: @"elif"]) else if ([directive isEqual: @"elif"])
{ {
@ -3922,6 +3935,7 @@ countAttributes(NSSet *keys, NSDictionary *a)
[ifStack removeLastObject]; [ifStack removeLastObject];
[ifStack addObject: [ifStack lastObject]]; [ifStack addObject: [ifStack lastObject]];
} }
return [self skipRemainderOfLine];
} }
else if ([directive isEqual: @"else"]) else if ([directive isEqual: @"else"])
{ {
@ -3934,6 +3948,7 @@ countAttributes(NSSet *keys, NSDictionary *a)
[ifStack removeLastObject]; [ifStack removeLastObject];
[ifStack addObject: [ifStack lastObject]]; [ifStack addObject: [ifStack lastObject]];
} }
return [self skipRemainderOfLine];
} }
else if ([directive isEqual: @"if"]) else if ([directive isEqual: @"if"])
{ {
@ -4061,11 +4076,10 @@ countAttributes(NSSet *keys, NSDictionary *a)
} }
[ifStack addObject: top]; [ifStack addObject: top];
RELEASE(top); RELEASE(top);
return [self skipRemainderOfLine];
} }
else if ([directive hasPrefix: @"if"]) else if ([directive isEqual: @"ifdef"])
{ {
BOOL isIfDef = [directive isEqual: @"ifdef"];
[self parseSpace: spaces]; [self parseSpace: spaces];
if (pos < length && buffer[pos] != '\n') if (pos < length && buffer[pos] != '\n')
{ {
@ -4075,48 +4089,65 @@ countAttributes(NSSet *keys, NSDictionary *a)
top = [[ifStack lastObject] mutableCopy]; top = [[ifStack lastObject] mutableCopy];
arg = [self parseIdentifier]; arg = [self parseIdentifier];
if ([arg isEqual: @"NO_GNUSTEP"]) if ([arg isEqual: @"NO_GNUSTEP"])
{
if (isIfDef)
{ {
[self log: @"Unexpected #ifdef NO_GNUSTEP (nonsense)"]; [self log: @"Unexpected #ifdef NO_GNUSTEP (nonsense)"];
} }
else
{
[top removeObjectForKey: @"MacOS-X"];
[top setObject: @"NotMacOS-X" forKey: @"NotMacOS-X"];
[top removeObjectForKey: @"OpenStep"];
[top setObject: @"NotOpenStep" forKey: @"NotOpenStep"];
}
}
else if ([arg isEqual: @"STRICT_MACOS_X"]) else if ([arg isEqual: @"STRICT_MACOS_X"])
{
if (isIfDef)
{ {
[top removeObjectForKey: @"NotMacOS-X"]; [top removeObjectForKey: @"NotMacOS-X"];
[top setObject: @"MacOS-X" forKey: @"MacOS-X"]; [top setObject: @"MacOS-X" forKey: @"MacOS-X"];
} }
else
{
[top removeObjectForKey: @"MacOS-X"];
[top setObject: @"NotMacOS-X" forKey: @"NotMacOS-X"];
}
}
else if ([arg isEqual: @"STRICT_OPENSTEP"]) else if ([arg isEqual: @"STRICT_OPENSTEP"])
{
if (isIfDef)
{ {
[top removeObjectForKey: @"NotOpenStep"]; [top removeObjectForKey: @"NotOpenStep"];
[top setObject: @"OpenStep" forKey: @"OpenStep"]; [top setObject: @"OpenStep" forKey: @"OpenStep"];
} }
else [ifStack addObject: top];
RELEASE(top);
}
return [self skipRemainderOfLine];
}
else if ([directive isEqual: @"ifndef"])
{
[self parseSpace: spaces];
if (pos < length && buffer[pos] != '\n')
{
NSMutableDictionary *top;
NSString *arg;
top = [[ifStack lastObject] mutableCopy];
arg = [self parseIdentifier];
if ([arg isEqual: @"NO_GNUSTEP"])
{
[top removeObjectForKey: @"MacOS-X"];
[top setObject: @"NotMacOS-X" forKey: @"NotMacOS-X"];
[top removeObjectForKey: @"OpenStep"];
[top setObject: @"NotOpenStep" forKey: @"NotOpenStep"];
}
else if ([arg isEqual: @"STRICT_MACOS_X"])
{
[top removeObjectForKey: @"MacOS-X"];
[top setObject: @"NotMacOS-X" forKey: @"NotMacOS-X"];
}
else if ([arg isEqual: @"STRICT_OPENSTEP"])
{ {
[top removeObjectForKey: @"OpenStep"]; [top removeObjectForKey: @"OpenStep"];
[top setObject: @"NotOpenStep" forKey: @"NotOpenStep"]; [top setObject: @"NotOpenStep" forKey: @"NotOpenStep"];
} }
}
[ifStack addObject: top]; [ifStack addObject: top];
RELEASE(top); RELEASE(top);
} }
return [self skipRemainderOfLine];
}
else if ([directive isEqual: @"import"]
|| [directive isEqual: @"include"])
{
return [self skipRemainderOfLine];
}
else
{
[self log: @"Warning - unknown preprocessor directive %@", directive];
return [self skipRemainderOfLine];
} }
} }
return [self skipRemainderOfLine]; return [self skipRemainderOfLine];
@ -4840,7 +4871,7 @@ fail:
{ {
while (pos < length) while (pos < length)
{ {
if (buffer[pos++] == '\n') if ('\n' == buffer[pos++])
{ {
break; break;
} }
@ -4975,5 +5006,46 @@ fail:
{ {
return AUTORELEASE([source mutableCopy]); return AUTORELEASE([source mutableCopy]);
} }
- (NSString*) where
{
int index;
int start = 0;
int end;
NSString *l;
NSString *s;
for (index = [lines count] - 1; index >= 0; index--)
{
NSNumber *num = [lines objectAtIndex: index];
if ((start = [num intValue]) <= (int)pos)
{
break;
}
}
if (index >= [lines count] || index < 0)
{
start = 0;
index = -1;
}
if (index + 1 < [lines count])
{
end = [[lines objectAtIndex: index + 1] intValue];
}
else
{
end = length;
}
l = [[NSString alloc] initWithCharactersNoCopy: buffer + start
length: end - start
freeWhenDone: NO];
s = [NSString stringWithFormat: @"Character %d in line %d:%@",
pos - start, index + 2, l];
RELEASE(l);
return s;
}
@end @end