Skip to content

Commit 4c60111

Browse files
authored
Merge pull request DeusData#983 from Sinjan-Debnath/main
fix: resolve C/C++ includes to header file targets by matching stems
2 parents 23f262c + 632fd08 commit 4c60111

8 files changed

Lines changed: 337 additions & 4 deletions

File tree

‎internal/cbm/extract_defs.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5960,8 +5960,8 @@ static void extract_class_fields(CBMExtractCtx *ctx, TSNode class_node, const ch
59605960
* For the nested case, the child has no "type" field directly. Detect by
59615961
* walking named children for a variable_declaration. */
59625962
TSNode type_node = ts_node_child_by_field_name(child, TS_FIELD("type"));
5963-
TSNode name_node =
5964-
ts_node_is_null(type_node) ? (TSNode){0} : resolve_field_name_node(child);
5963+
TSNode empty_node = {0};
5964+
TSNode name_node = ts_node_is_null(type_node) ? empty_node : resolve_field_name_node(child);
59655965

59665966
if (ts_node_is_null(type_node)) {
59675967
uint32_t cnc = ts_node_named_child_count(child);

‎src/pipeline/pass_pkgmap.c‎

Lines changed: 159 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,12 @@ static void parse_pyproject_toml(const char *source, int source_len, const char
426426
pkg_entries_push(entries, name, entry);
427427

428428
/* Also register <name>/__init__ as alternative (no src/ prefix) */
429-
snprintf(suffix, sizeof(suffix), "%s/__init__", name_copy);
430-
char *alt_entry = build_entry_path(rel_path, suffix);
429+
char *alt_entry = NULL;
430+
if (name_copy) {
431+
snprintf(suffix, sizeof(suffix), "%s/__init__", name_copy);
432+
alt_entry = build_entry_path(rel_path, suffix);
433+
}
434+
431435
if (name_copy && alt_entry) {
432436
pkg_entries_push(entries, name_copy, alt_entry);
433437
} else {
@@ -1265,6 +1269,151 @@ static bool import_targetable_label(const char *label) {
12651269
return false;
12661270
}
12671271

1272+
static const char *path_leaf(const char *path) {
1273+
const char *leaf = path;
1274+
for (const char *p = path; p && *p; p++) {
1275+
if (*p == '/' || *p == '\\') {
1276+
leaf = p + 1;
1277+
}
1278+
}
1279+
return leaf;
1280+
}
1281+
1282+
static bool is_header_include(const char *path) {
1283+
if (!path || !path[0]) {
1284+
return false;
1285+
}
1286+
static const char *header_exts[] = {".h", ".hh", ".hpp", ".hxx", ".inc", ".inl", ".ipp", NULL};
1287+
for (const char **ext = header_exts; *ext; ext++) {
1288+
size_t path_len = strlen(path);
1289+
size_t ext_len = strlen(*ext);
1290+
if (path_len > ext_len && strcmp(path + path_len - ext_len, *ext) == 0) {
1291+
return true;
1292+
}
1293+
}
1294+
return false;
1295+
}
1296+
1297+
static bool is_c_family_source(const char *source_rel) {
1298+
if (!source_rel || !source_rel[0]) {
1299+
return false;
1300+
}
1301+
static const char *exts[] = {".c", ".cc", ".cpp", ".cxx", ".c++",
1302+
".h", ".hh", ".hpp", ".hxx", NULL};
1303+
for (const char **ext = exts; *ext; ext++) {
1304+
size_t path_len = strlen(source_rel);
1305+
size_t ext_len = strlen(*ext);
1306+
if (path_len > ext_len && strcmp(source_rel + path_len - ext_len, *ext) == 0) {
1307+
return true;
1308+
}
1309+
}
1310+
return false;
1311+
}
1312+
1313+
static const cbm_gbuf_node_t *resolve_exact_file_node(const cbm_pipeline_ctx_t *ctx,
1314+
const char *file_path,
1315+
const char *source_file_qn) {
1316+
if (!ctx || !file_path || !file_path[0]) {
1317+
return NULL;
1318+
}
1319+
1320+
const char *leaf = path_leaf(file_path);
1321+
if (!leaf || !leaf[0]) {
1322+
return NULL;
1323+
}
1324+
1325+
char stem[PKGMAP_PATH_BUF];
1326+
snprintf(stem, sizeof(stem), "%s", leaf);
1327+
char *last_dot = strrchr(stem, '.');
1328+
if (last_dot && last_dot != stem) {
1329+
*last_dot = '\0';
1330+
}
1331+
1332+
const char *names[2] = {stem[0] ? stem : NULL, leaf};
1333+
const cbm_gbuf_node_t *best = NULL;
1334+
for (int ni = 0; ni < 2; ni++) {
1335+
const char *name = names[ni];
1336+
if (!name || !name[0]) {
1337+
continue;
1338+
}
1339+
1340+
const cbm_gbuf_node_t **hits = NULL;
1341+
int hit_count = 0;
1342+
if (cbm_gbuf_find_by_name(ctx->gbuf, name, &hits, &hit_count) != 0 || !hits) {
1343+
continue;
1344+
}
1345+
1346+
for (int i = 0; i < hit_count; i++) {
1347+
const cbm_gbuf_node_t *cand = hits[i];
1348+
if (!cand || !cand->file_path) {
1349+
continue;
1350+
}
1351+
1352+
/* Tie-breaker: Ensure the candidate's actual file_path ends with the
1353+
* specific include path requested (e.g., 'a/util.h' instead of just 'util.h'). */
1354+
if (!ends_with(cand->file_path, file_path)) {
1355+
continue;
1356+
}
1357+
1358+
if (!cand->label || !import_targetable_label(cand->label)) {
1359+
continue;
1360+
}
1361+
if (source_file_qn && cand->qualified_name &&
1362+
strcmp(cand->qualified_name, source_file_qn) == 0) {
1363+
continue;
1364+
}
1365+
if (strcmp(cand->label, "File") == 0) {
1366+
return cand;
1367+
}
1368+
if (!best) {
1369+
best = cand;
1370+
}
1371+
}
1372+
if (best) {
1373+
return best;
1374+
}
1375+
}
1376+
return NULL;
1377+
}
1378+
1379+
static const cbm_gbuf_node_t *resolve_header_include(const cbm_pipeline_ctx_t *ctx,
1380+
const char *source_rel,
1381+
const char *source_file_qn,
1382+
const char *module_path) {
1383+
if (!is_c_family_source(source_rel) || !is_header_include(module_path)) {
1384+
return NULL;
1385+
}
1386+
1387+
const char *base = module_path;
1388+
if (base[0] == '.' && base[1] == '/') {
1389+
base += 2;
1390+
}
1391+
1392+
const cbm_gbuf_node_t *exact = resolve_exact_file_node(ctx, base, source_file_qn);
1393+
if (exact) {
1394+
return exact;
1395+
}
1396+
1397+
if (source_rel && source_rel[0]) {
1398+
char *dir = path_dirname(source_rel);
1399+
if (dir) {
1400+
char candidate[PKGMAP_PATH_BUF];
1401+
if (dir[0]) {
1402+
snprintf(candidate, sizeof(candidate), "%s/%s", dir, base);
1403+
} else {
1404+
snprintf(candidate, sizeof(candidate), "%s", base);
1405+
}
1406+
free(dir);
1407+
exact = resolve_exact_file_node(ctx, candidate, source_file_qn);
1408+
if (exact) {
1409+
return exact;
1410+
}
1411+
}
1412+
}
1413+
1414+
return NULL;
1415+
}
1416+
12681417
/* Resolve a sibling-file import: a bare path/name (no leading "./") that names
12691418
* a file relative to the importer's directory. This covers build/markup
12701419
* grammars whose import string is a sibling filename or directory rather than a
@@ -1352,6 +1501,14 @@ const cbm_gbuf_node_t *cbm_pipeline_resolve_import_node(const cbm_pipeline_ctx_t
13521501
return NULL;
13531502
}
13541503

1504+
/* Prefer exact header-file nodes for C/C++ includes so same-stem source or
1505+
* module nodes do not steal the edge target. */
1506+
const cbm_gbuf_node_t *header_target =
1507+
resolve_header_include(ctx, source_rel, source_file_qn, imp->module_path);
1508+
if (header_target) {
1509+
return header_target;
1510+
}
1511+
13551512
/* Strategy 1: module-path resolution → existing node (Python/TS/Go).
13561513
* No label filter here: directory-module languages (Go/Java packages)
13571514
* legitimately resolve straight to a Folder node -- that's the intended,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "NodeController.h"
2+
3+
int node_controller_value() {
4+
return 1;
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#pragma once
2+
3+
struct NodeController {
4+
int value;
5+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "SystemController.h"
2+
3+
int system_controller_value() {
4+
return 2;
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#pragma once
2+
3+
struct SystemController {
4+
int value;
5+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "NodeController.h"
2+
#include <SystemController.h>
3+
4+
int main() {
5+
return 0;
6+
}

0 commit comments

Comments
 (0)