Allow line_bynumber to return more than one result

This commit is contained in:
Matt Seabold
2023-01-11 20:18:51 -05:00
parent 7979f8a41f
commit 8ea0dfe453

View File

@@ -4736,14 +4736,18 @@ static SpanInfoListEntry* FindSpanInfoByAddr (const SpanInfoList* L, cc65_addr A
static LineInfo* FindLineInfoByLine (const Collection* LineInfos, cc65_line Line) static int FindLineInfoByLine (const Collection* LineInfos, cc65_line Line,
/* Find the LineInfo for a given line number. The function returns the line unsigned *Index)
** info or NULL if none was found. /* Find the LineInfo for a given line number. The function returns true if the
** name was found. In this case, Index contains the index of the first item
** that matches. If the item wasn't found, the function returns false and
** Index contains the insert position for Name.
*/ */
{ {
/* Do a binary search */ /* Do a binary search */
int Lo = 0; int Lo = 0;
int Hi = (int) CollCount (LineInfos) - 1; int Hi = (int) CollCount (LineInfos) - 1;
int Found = 0;
while (Lo <= Hi) { while (Lo <= Hi) {
/* Mid of range */ /* Mid of range */
@@ -4755,16 +4759,20 @@ static LineInfo* FindLineInfoByLine (const Collection* LineInfos, cc65_line Line
/* Found? */ /* Found? */
if (Line > CurItem->Line) { if (Line > CurItem->Line) {
Lo = Cur + 1; Lo = Cur + 1;
} else if (Line < CurItem->Line) {
Hi = Cur - 1;
} else { } else {
/* Found */ Hi = Cur - 1;
return CurItem; /* Since we may have duplicates, repeat the search until we've
** the first item that has a match.
*/
if(Line == CurItem->Line) {
Found = 1;
}
} }
} }
/* Not found */ /* Pass back the index. This is also the insert position */
return 0; *Index = Lo;
return Found;
} }
@@ -6134,6 +6142,9 @@ const cc65_lineinfo* cc65_line_bynumber (cc65_dbginfo Handle, unsigned FileId,
const FileInfo* F; const FileInfo* F;
cc65_lineinfo* D; cc65_lineinfo* D;
LineInfo* L = 0; LineInfo* L = 0;
unsigned I;
unsigned Index;
unsigned Count;
/* Check the parameter */ /* Check the parameter */
assert (Handle != 0); assert (Handle != 0);
@@ -6150,18 +6161,31 @@ const cc65_lineinfo* cc65_line_bynumber (cc65_dbginfo Handle, unsigned FileId,
F = CollAt (&Info->FileInfoById, FileId); F = CollAt (&Info->FileInfoById, FileId);
/* Search in the file for the given line */ /* Search in the file for the given line */
L = FindLineInfoByLine (&F->LineInfoByLine, Line); if(!FindLineInfoByLine (&F->LineInfoByLine, Line, &Index)) {
/* Not found */
/* Bail out if we didn't find the line */
if (L == 0) {
return 0; return 0;
} }
/* Index contains the first position. Count how many lines with this number
** we have. Skip the first one, since we have at least one.
*/
Count = 1;
while ((unsigned) Index + Count < CollCount( &F->LineInfoByLine)) {
L = CollAt (&F->LineInfoByLine, (unsigned) Index + Count);
if (L->Line != Line) {
break;
}
++Count;
}
/* Prepare the struct we will return to the caller */ /* Prepare the struct we will return to the caller */
D = new_cc65_lineinfo (1); D = new_cc65_lineinfo (Count);
/* Copy the data */ /* Copy the data */
CopyLineInfo (D->data, L); for (I = 0; I < Count; ++I) {
CopyLineInfo (D->data + I, CollAt (&F->LineInfoByLine, Index++));
}
/* Return the allocated struct */ /* Return the allocated struct */
return D; return D;