Skip to content

Commit 1bc2d0a

Browse files
Tal500Tal Hadad
authored andcommitted
change the rules of relativeness preserving to depend on the source file including it for relative path includes
1 parent 9ba3970 commit 1bc2d0a

2 files changed

Lines changed: 21 additions & 17 deletions

File tree

integration_test.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def test_relative_header_2(record_property, tmpdir, with_pragma_once, inv, sourc
6666
record_property("stderr", stderr)
6767
if with_pragma_once:
6868
assert stderr == ''
69-
if inv:
69+
if inv or not source_relative:
7070
assert f'#line 8 "{pathlib.PurePath(tmpdir).as_posix()}/test.h"' in stdout
7171
else:
7272
assert '#line 8 "test.h"' in stdout
@@ -93,16 +93,17 @@ def test_relative_header_3(record_property, tmpdir, is_sys, inv, source_relative
9393
assert "missing header: Header not found" in stderr
9494
else:
9595
assert stderr == ''
96-
if inv:
97-
assert f'#line 8 "{pathlib.PurePath(test_subdir).as_posix()}/test.h"' in stdout
98-
else:
96+
if source_relative and not inv:
9997
assert '#line 8 "test_subdir/test.h"' in stdout
98+
else:
99+
assert f'#line 8 "{pathlib.PurePath(test_subdir).as_posix()}/test.h"' in stdout
100100

101101
@pytest.mark.parametrize("use_short_path", (False, True))
102102
@pytest.mark.parametrize("relative_include_dir", (False, True))
103103
@pytest.mark.parametrize("is_sys", (False, True))
104104
@pytest.mark.parametrize("inv", (False, True))
105-
def test_relative_header_4(record_property, tmpdir, use_short_path, relative_include_dir, is_sys, inv):
105+
@pytest.mark.parametrize("source_relative", (False, True))
106+
def test_relative_header_4(record_property, tmpdir, use_short_path, relative_include_dir, is_sys, inv, source_relative):
106107
test_subdir = os.path.join(tmpdir, "test_subdir")
107108
os.mkdir(test_subdir)
108109
header_file, _ = __test_relative_header_create_header(test_subdir)
@@ -111,13 +112,14 @@ def test_relative_header_4(record_property, tmpdir, use_short_path, relative_inc
111112

112113
test_file = __test_relative_header_create_source(tmpdir, header_file, "test.h", is_include2_sys=is_sys, inv=inv)
113114

114-
args = [format_include_path_arg("test_subdir" if relative_include_dir else test_subdir), test_file]
115+
args = [format_include_path_arg("test_subdir" if relative_include_dir else test_subdir), "test.c" if source_relative else test_file]
115116

116117
_, stdout, stderr = simplecpp(args, cwd=tmpdir)
117118
record_property("stdout", stdout)
118119
record_property("stderr", stderr)
120+
119121
assert stderr == ''
120-
if (use_short_path and not inv) or (relative_include_dir and inv):
122+
if (source_relative and use_short_path and not inv) or (relative_include_dir and inv):
121123
assert '#line 8 "test_subdir/test.h"' in stdout
122124
else:
123125
assert f'#line 8 "{pathlib.PurePath(test_subdir).as_posix()}/test.h"' in stdout
@@ -126,7 +128,8 @@ def test_relative_header_4(record_property, tmpdir, use_short_path, relative_inc
126128
@pytest.mark.parametrize("relative_include_dir", (False, True))
127129
@pytest.mark.parametrize("is_sys", (False, True))
128130
@pytest.mark.parametrize("inv", (False, True))
129-
def test_relative_header_5(record_property, tmpdir, with_pragma_once, relative_include_dir, is_sys, inv): # test relative paths with ..
131+
@pytest.mark.parametrize("source_relative", (False, True))
132+
def test_relative_header_5(record_property, tmpdir, with_pragma_once, relative_include_dir, is_sys, inv, source_relative): # test relative paths with ..
130133
## in this test, the subdir role is the opposite then the previous - it contains the test.c file, while the parent tmpdir contains the header file
131134
header_file, double_include_error = __test_relative_header_create_header(tmpdir, with_pragma_once=with_pragma_once)
132135
if is_sys:
@@ -138,14 +141,14 @@ def test_relative_header_5(record_property, tmpdir, with_pragma_once, relative_i
138141
os.mkdir(test_subdir)
139142
test_file = __test_relative_header_create_source(test_subdir, header_file, header_file_second_path, is_include2_sys=is_sys, inv=inv)
140143

141-
args = ([format_include_path_arg(".." if relative_include_dir else tmpdir)] if is_sys else []) + ["test.c"]
144+
args = ([format_include_path_arg(".." if relative_include_dir else tmpdir)] if is_sys else []) + ["test.c" if source_relative else test_file]
142145

143146
_, stdout, stderr = simplecpp(args, cwd=test_subdir)
144147
record_property("stdout", stdout)
145148
record_property("stderr", stderr)
146149
if with_pragma_once:
147150
assert stderr == ''
148-
if (relative_include_dir or not is_sys) and inv:
151+
if (relative_include_dir if is_sys else source_relative) and inv:
149152
assert '#line 8 "../test.h"' in stdout
150153
else:
151154
assert f'#line 8 "{pathlib.PurePath(tmpdir).as_posix()}/test.h"' in stdout
@@ -156,15 +159,16 @@ def test_relative_header_5(record_property, tmpdir, with_pragma_once, relative_i
156159
@pytest.mark.parametrize("relative_include_dir", (False, True))
157160
@pytest.mark.parametrize("is_sys", (False, True))
158161
@pytest.mark.parametrize("inv", (False, True))
159-
def test_relative_header_6(record_property, tmpdir, with_pragma_once, relative_include_dir, is_sys, inv): # test relative paths with .. that is resolved only by an include dir
162+
@pytest.mark.parametrize("source_relative", (False, True))
163+
def test_relative_header_6(record_property, tmpdir, with_pragma_once, relative_include_dir, is_sys, inv, source_relative): # test relative paths with .. that is resolved only by an include dir
160164
## in this test, both the header and the source file are at the same dir, but there is a dummy inclusion dir as a subdir
161165
header_file, double_include_error = __test_relative_header_create_header(tmpdir, with_pragma_once=with_pragma_once)
162166

163167
test_subdir = os.path.join(tmpdir, "test_subdir")
164168
os.mkdir(test_subdir)
165169
test_file = __test_relative_header_create_source(tmpdir, header_file, "../test.h", is_include2_sys=is_sys, inv=inv)
166170

167-
args = [format_include_path_arg("test_subdir" if relative_include_dir else test_subdir), "test.c"]
171+
args = [format_include_path_arg("test_subdir" if relative_include_dir else test_subdir), "test.c" if source_relative else test_file]
168172

169173
_, stdout, stderr = simplecpp(args, cwd=tmpdir)
170174
record_property("stdout", stdout)

simplecpp.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3173,7 +3173,7 @@ static std::string openHeader(std::ifstream &f, const std::string &path)
31733173
return "";
31743174
}
31753175

3176-
static std::string getRelativeFileName(const std::string &baseFile, const std::string &header)
3176+
static std::string getRelativeFileName(const std::string &baseFile, const std::string &header, bool returnAbsolutePath)
31773177
{
31783178
const std::string baseFileSimplified = simplecpp::simplifyPath(baseFile);
31793179
const std::string baseFileAbsolute = isAbsolutePath(baseFileSimplified) ?
@@ -3185,12 +3185,12 @@ static std::string getRelativeFileName(const std::string &baseFile, const std::s
31853185
headerSimplified :
31863186
simplecpp::simplifyPath(dirPath(baseFileAbsolute) + headerSimplified);
31873187

3188-
return extractRelativePathFromAbsolute(path);
3188+
return returnAbsolutePath ? toAbsolutePath(path) : extractRelativePathFromAbsolute(path);
31893189
}
31903190

31913191
static std::string openHeaderRelative(std::ifstream &f, const std::string &sourcefile, const std::string &header)
31923192
{
3193-
return openHeader(f, getRelativeFileName(sourcefile, header));
3193+
return openHeader(f, getRelativeFileName(sourcefile, header, isAbsolutePath(sourcefile)));
31943194
}
31953195

31963196
// returns the simplified header path:
@@ -3273,8 +3273,8 @@ static std::string getFileIdPath(const std::map<std::string, simplecpp::TokenLis
32733273
}
32743274

32753275
if (!systemheader) {
3276-
const std::string relativeOrAbsoluteFilename = getRelativeFileName(sourcefile, header);// unknown if absolute or relative, but always simplified
3277-
const std::string match = findPathInMapBothRelativeAndAbsolute(filedata, relativeOrAbsoluteFilename);
3276+
const std::string absoluteFilename = getRelativeFileName(sourcefile, header, true);
3277+
const std::string match = findPathInMapBothRelativeAndAbsolute(filedata, absoluteFilename);
32783278
if (!match.empty()) {
32793279
return match;
32803280
}

0 commit comments

Comments
 (0)