@@ -38,17 +38,20 @@ export class CIncludeResolver {
3838 * @param file The file to resolve inclusions for.
3939 * @returns The inclusions of the file.
4040 */
41- #resolveInclusions( file : {
42- path : string ;
43- rootNode : Parser . SyntaxNode ;
44- } ) : Inclusions {
41+ #resolveInclusions(
42+ file : { path : string ; rootNode : Parser . SyntaxNode } ,
43+ visitedFiles = new Set < string > ( ) ,
44+ ) : Inclusions {
4545 const inclusions : Inclusions = {
4646 filepath : file . path ,
4747 symbols : new Map ( ) ,
4848 internal : [ ] ,
4949 standard : [ ] ,
5050 } ;
5151
52+ // Add the current file to the visited set to prevent infinite recursion
53+ visitedFiles . add ( file . path ) ;
54+
5255 const includeNodes = C_INCLUDE_QUERY . captures ( file . rootNode ) ;
5356 const standardIncludeNodes = C_STANDARD_INCLUDE_QUERY . captures (
5457 file . rootNode ,
@@ -58,10 +61,21 @@ export class CIncludeResolver {
5861 const path = node . node . text ;
5962 inclusions . internal . push ( path ) ;
6063 const includedfile = this . #getFile( path , file . path ) ;
61- if ( includedfile ) {
64+ if ( includedfile && ! visitedFiles . has ( includedfile . file . path ) ) {
65+ // Add the included file's symbols to the current file's symbols
6266 for ( const [ name , symbol ] of includedfile . symbols ) {
6367 inclusions . symbols . set ( name , symbol ) ;
6468 }
69+ // Recursively resolve inclusions for the included file
70+ const nestedInclusions = this . #resolveInclusions(
71+ includedfile . file ,
72+ visitedFiles ,
73+ ) ;
74+ for ( const [ name , symbol ] of nestedInclusions . symbols ) {
75+ inclusions . symbols . set ( name , symbol ) ;
76+ }
77+ inclusions . internal . push ( ...nestedInclusions . internal ) ;
78+ inclusions . standard . push ( ...nestedInclusions . standard ) ;
6579 }
6680 }
6781
0 commit comments