From 8ea0dfe45395f9357eb7e523044344d09db490e5 Mon Sep 17 00:00:00 2001 From: Matt Seabold Date: Wed, 11 Jan 2023 20:18:51 -0500 Subject: [PATCH 1/3] Allow line_bynumber to return more than one result --- src/dbginfo/dbginfo.c | 54 +++++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/src/dbginfo/dbginfo.c b/src/dbginfo/dbginfo.c index fdebe6910..c8db5bea6 100644 --- a/src/dbginfo/dbginfo.c +++ b/src/dbginfo/dbginfo.c @@ -4736,14 +4736,18 @@ static SpanInfoListEntry* FindSpanInfoByAddr (const SpanInfoList* L, cc65_addr A -static LineInfo* FindLineInfoByLine (const Collection* LineInfos, cc65_line Line) -/* Find the LineInfo for a given line number. The function returns the line -** info or NULL if none was found. +static int FindLineInfoByLine (const Collection* LineInfos, cc65_line Line, + unsigned *Index) +/* 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 */ int Lo = 0; int Hi = (int) CollCount (LineInfos) - 1; + int Found = 0; while (Lo <= Hi) { /* Mid of range */ @@ -4755,16 +4759,20 @@ static LineInfo* FindLineInfoByLine (const Collection* LineInfos, cc65_line Line /* Found? */ if (Line > CurItem->Line) { Lo = Cur + 1; - } else if (Line < CurItem->Line) { - Hi = Cur - 1; } else { - /* Found */ - return CurItem; + Hi = Cur - 1; + /* 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 */ - return 0; + /* Pass back the index. This is also the insert position */ + *Index = Lo; + return Found; } @@ -6134,6 +6142,9 @@ const cc65_lineinfo* cc65_line_bynumber (cc65_dbginfo Handle, unsigned FileId, const FileInfo* F; cc65_lineinfo* D; LineInfo* L = 0; + unsigned I; + unsigned Index; + unsigned Count; /* Check the parameter */ assert (Handle != 0); @@ -6150,18 +6161,31 @@ const cc65_lineinfo* cc65_line_bynumber (cc65_dbginfo Handle, unsigned FileId, F = CollAt (&Info->FileInfoById, FileId); /* Search in the file for the given line */ - L = FindLineInfoByLine (&F->LineInfoByLine, Line); - - /* Bail out if we didn't find the line */ - if (L == 0) { + if(!FindLineInfoByLine (&F->LineInfoByLine, Line, &Index)) { + /* Not found */ 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 */ - D = new_cc65_lineinfo (1); + D = new_cc65_lineinfo (Count); /* 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 D; From 9ee0c835e5f7698f1376b58d38a6cfd5f1514bfc Mon Sep 17 00:00:00 2001 From: Matt Seabold Date: Tue, 17 Jan 2023 22:15:27 -0500 Subject: [PATCH 2/3] Update documentation for line_bynumber --- src/dbginfo/dbginfo.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/dbginfo/dbginfo.h b/src/dbginfo/dbginfo.h index 38d891e7c..95aae837c 100644 --- a/src/dbginfo/dbginfo.h +++ b/src/dbginfo/dbginfo.h @@ -258,7 +258,8 @@ const cc65_lineinfo* cc65_line_bynumber (cc65_dbginfo handle, unsigned source_id, cc65_line line); /* Return line information for a source file/line number combination. The -** function returns NULL if no line information was found. +** function returns NULL if no line information was found, otherwise a list +** of line infos. */ const cc65_lineinfo* cc65_line_bysource (cc65_dbginfo Handle, unsigned source_id); From 3360734487a08e32e473476b29c5ea4c6207c752 Mon Sep 17 00:00:00 2001 From: Matt Seabold Date: Tue, 17 Jan 2023 22:17:23 -0500 Subject: [PATCH 3/3] Update comment to match in source file --- src/dbginfo/dbginfo.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/dbginfo/dbginfo.c b/src/dbginfo/dbginfo.c index c8db5bea6..fee41012c 100644 --- a/src/dbginfo/dbginfo.c +++ b/src/dbginfo/dbginfo.c @@ -6135,7 +6135,8 @@ const cc65_lineinfo* cc65_line_byid (cc65_dbginfo Handle, unsigned Id) const cc65_lineinfo* cc65_line_bynumber (cc65_dbginfo Handle, unsigned FileId, cc65_line Line) /* Return line information for a source file/line number combination. The -** function returns NULL if no line information was found. +** function returns NULL if no line information was found, otherwise a list +** of line infos. */ { const DbgInfo* Info;