Skip to content

Commit d1e7317

Browse files
committed
[project][-I] Return CURRENT_PROJECT for GetProject for file in -I dir
1 parent cd111cd commit d1e7317

14 files changed

Lines changed: 143 additions & 3 deletions

File tree

src/cxref.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ static unsigned menuFilterLevels[MAX_MENU_FILTER_LEVEL] = {
5858
static char *lockedProject = NULL;
5959
static char *lockedProjectRoot = NULL;
6060

61+
static bool fileIsUnderIncludePaths(char *fileName) {
62+
for (StringList *dir = options.includeDirs; dir != NULL; dir = dir->next) {
63+
int len = strlen(dir->string);
64+
if (strncmp(fileName, dir->string, len) == 0
65+
&& (fileName[len] == '/' || fileName[len] == '\0')) {
66+
return true;
67+
}
68+
}
69+
return false;
70+
}
6171

6272
/* *********************************************************************** */
6373

@@ -1794,8 +1804,10 @@ static void handleProject() {
17941804
if (lockedProject != NULL) {
17951805
/* Server is locked - check if this file belongs to the locked project */
17961806
if (lockedProjectRoot != NULL) {
1797-
/* Auto-detected project - check file path against project root */
1798-
if (strncmp(fileName, lockedProjectRoot, strlen(lockedProjectRoot)) == 0) {
1807+
/* Auto-detected project - file is in scope if under project root
1808+
* or under a configured include path */
1809+
if (strncmp(fileName, lockedProjectRoot, strlen(lockedProjectRoot)) == 0
1810+
|| fileIsUnderIncludePaths(fileName)) {
17991811
ppcGenRecord(PPC_SET_INFO, lockedProject);
18001812
} else {
18011813
ppcGenRecord(PPC_PROJECT_MISMATCH, lockedProject);

src/cxref_tests.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ Ensure(CxRef, will_return_no_active_project_if_no_optionfile_found) {
6666
expect(applyConventionBasedDatabasePath);
6767
expect(getFileItemWithFileNumber, when(fileNumber, is_equal_to(0)),
6868
will_return(&fileItem));
69-
expect(searchForProjectConfigFileAndProjectForFile, when(sourceFilename, is_equal_to_string("file.c")),
69+
expect(searchForProjectConfigFileAndProjectForFile,
70+
when(sourceFilename, is_equal_to_string("file.c")),
7071
will_set_contents_of_parameter(foundConfigFilename, "", 1),
7172
will_set_contents_of_parameter(foundProjectName, "", 1));
7273
expect(ppcGenRecord,
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
include ../Makefile.server
2+
3+
# Scenario: A project includes a header from an external directory via -I.
4+
# When the user navigates to that header and the client sends getprojectname
5+
# for it, the server should return the current project (not a mismatch),
6+
# because the file is reachable via the project's include paths.
7+
#
8+
# Note: this also accepts unknown CUs under -I dirs, which is too broad.
9+
# See test_getproject_unknown_cu_under_include_path (.suspended) for that case.
10+
11+
# Override: place .c-xrefrc in project/ for auto-detect, not in test root
12+
project/.c-xrefrc:
13+
@echo "[$(CURDIR)/project]" > $@
14+
@echo " $(CURDIR)/project" >> $@
15+
@echo " -I $(CURDIR)/external" >> $@
16+
17+
$(TEST): project/.c-xrefrc
18+
@-$(SERVER_DRIVER) commands.input $(EXTRA) > output.tmp
19+
@$(NORMALIZE) output.tmp > output
20+
$(VERIFY)
21+
22+
clean: clean-project-config
23+
clean-project-config:
24+
@-rm -rf project/.c-xrefrc project/.c-xref
25+
26+
trace: EXTRA = --extra '-debug -log=trace'
27+
trace: $(TEST)
28+
29+
GDB_COMMANDS =
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CXREF -xrefactory-II -server
2+
-olcxgetprojectname CURDIR/project/source.c
3+
<sync>
4+
-olcxgetprojectname CURDIR/external/external.h
5+
<sync>
6+
<exit>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
CXREF -xrefactory-II -server -o server-buffer
2+
-olcxgetprojectname CURDIR/project/source.c
3+
end-of-options
4+
5+
<sync>
6+
<set-info len=<n>>CURDIR/project</set-info>
7+
-olcxgetprojectname CURDIR/external/external.h
8+
end-of-options
9+
10+
<sync>
11+
<set-info len=<n>>CURDIR/project</set-info>
12+
-exit
13+
end-of-options
14+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
int external_function(void);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "external.h"
2+
3+
int main() {
4+
return external_function();
5+
}

tests/test_getproject_unknown_cu_under_include_path/.suspended

Whitespace-only changes.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
include ../Makefile.server
2+
3+
# Scenario: A CU (external.c) exists under an -I directory but was never
4+
# included or parsed by the project. When the user manually opens it and
5+
# the client sends getprojectname, the server should return a mismatch
6+
# — the file is not part of the current project's scope.
7+
#
8+
# Currently SUSPENDED because:
9+
# - The simple fileIsUnderIncludePaths check accepts any file under -I dirs
10+
# - Fixing this properly requires either:
11+
# (a) not adding getprojectname files to the file table (needs surgery
12+
# on the init flow which depends on file table registration), or
13+
# (b) file table support for removing/marking transient entries
14+
# See also: test_getproject_file_under_include_path for the passing case.
15+
16+
# Override: place .c-xrefrc in project/ for auto-detect, not in test root
17+
project/.c-xrefrc:
18+
@echo "[$(CURDIR)/project]" > $@
19+
@echo " $(CURDIR)/project" >> $@
20+
@echo " -I $(CURDIR)/external" >> $@
21+
22+
$(TEST): project/.c-xrefrc
23+
@-$(SERVER_DRIVER) commands.input $(EXTRA) > output.tmp
24+
@$(NORMALIZE) output.tmp > output
25+
$(VERIFY)
26+
27+
clean: clean-project-config
28+
clean-project-config:
29+
@-rm -rf project/.c-xrefrc project/.c-xref
30+
31+
trace: EXTRA = --extra '-debug -log=trace'
32+
trace: $(TEST)
33+
34+
GDB_COMMANDS =
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CXREF -xrefactory-II -server
2+
-olcxgetprojectname CURDIR/project/source.c
3+
<sync>
4+
-olcxgetprojectname CURDIR/external/external.h
5+
<sync>
6+
-olcxgetprojectname CURDIR/external/external.c
7+
<sync>
8+
<exit>

0 commit comments

Comments
 (0)