From 8b064656e7a24f73ac898277eb678edccbdaf78d Mon Sep 17 00:00:00 2001 From: glank Date: Thu, 12 Jun 2025 22:08:09 +0200 Subject: [PATCH 1/3] Add path resolution tests --- test.cpp | 41 ++++++++++++++++++++++ testsuite/path-tests/include_a/header_a.h | 1 + testsuite/path-tests/include_a/same_name.h | 1 + testsuite/path-tests/include_b/header_b.h | 1 + testsuite/path-tests/include_b/same_name.h | 1 + testsuite/path-tests/once.h | 2 ++ 6 files changed, 47 insertions(+) create mode 100644 testsuite/path-tests/include_a/header_a.h create mode 100644 testsuite/path-tests/include_a/same_name.h create mode 100644 testsuite/path-tests/include_b/header_b.h create mode 100644 testsuite/path-tests/include_b/same_name.h create mode 100644 testsuite/path-tests/once.h diff --git a/test.cpp b/test.cpp index 86e6ddc8..51c54b31 100644 --- a/test.cpp +++ b/test.cpp @@ -2990,6 +2990,43 @@ static void fuzz_crash() } } +static void same_name() +{ + const char code[] = "#include \n" + "#include \n" + "TEST\n"; + + simplecpp::DUI dui; + dui.includePaths.push_back("./testsuite/path-tests/include_a"); + dui.includePaths.push_back("./testsuite/path-tests/include_b"); + + ASSERT_EQUALS("\n\nOK", preprocess(code, dui)); +} + +static void file_id() +{ + const char code[] = "#include \"once.h\"\n" + "#include \"Once.h\"\n" + + "#include \n" + "#include \n" + + "#include \"../path-tests/once.h\"\n" + "#include \"../path-tests/Once.h\"\n" + "#include \"../Path-Tests/once.h\"\n" + "#include \"../Path-Tests/Once.h\"\n" + + "#include \"include_a/../once.h\"\n" + "#include \"include_a/../Once.h\"\n" + "#include \"include_A/../once.h\"\n" + "#include \"include_A/../Once.h\"\n"; + + simplecpp::DUI dui; + dui.includePaths.push_back("./testsuite/path-tests"); + + ASSERT_EQUALS("\n#line 2 \"testsuite/path-tests/once.h\"\nONCE", preprocess(code, dui)); +} + int main(int argc, char **argv) { TEST_CASE(backslash); @@ -3212,6 +3249,10 @@ int main(int argc, char **argv) TEST_CASE(warning); + // path resolution + TEST_CASE(same_name); + TEST_CASE(file_id); + // utility functions. TEST_CASE(simplifyPath); TEST_CASE(simplifyPath_cppcheck); diff --git a/testsuite/path-tests/include_a/header_a.h b/testsuite/path-tests/include_a/header_a.h new file mode 100644 index 00000000..6ad8d333 --- /dev/null +++ b/testsuite/path-tests/include_a/header_a.h @@ -0,0 +1 @@ +#include "same_name.h" diff --git a/testsuite/path-tests/include_a/same_name.h b/testsuite/path-tests/include_a/same_name.h new file mode 100644 index 00000000..fee527cd --- /dev/null +++ b/testsuite/path-tests/include_a/same_name.h @@ -0,0 +1 @@ +#define TEST E diff --git a/testsuite/path-tests/include_b/header_b.h b/testsuite/path-tests/include_b/header_b.h new file mode 100644 index 00000000..6ad8d333 --- /dev/null +++ b/testsuite/path-tests/include_b/header_b.h @@ -0,0 +1 @@ +#include "same_name.h" diff --git a/testsuite/path-tests/include_b/same_name.h b/testsuite/path-tests/include_b/same_name.h new file mode 100644 index 00000000..9bb1b8aa --- /dev/null +++ b/testsuite/path-tests/include_b/same_name.h @@ -0,0 +1 @@ +#define TEST OK diff --git a/testsuite/path-tests/once.h b/testsuite/path-tests/once.h new file mode 100644 index 00000000..e0c59e63 --- /dev/null +++ b/testsuite/path-tests/once.h @@ -0,0 +1,2 @@ +#pragma once +ONCE From 993559197e96370fddd1756a5f086dc1dc08e747 Mon Sep 17 00:00:00 2001 From: glank Date: Thu, 12 Jun 2025 22:31:01 +0200 Subject: [PATCH 2/3] Fix false matches in getFileIdPath --- simplecpp.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/simplecpp.cpp b/simplecpp.cpp index 9ff66d8b..e4f98bb6 100755 --- a/simplecpp.cpp +++ b/simplecpp.cpp @@ -3278,6 +3278,13 @@ static std::string getFileIdPath(const std::map Date: Fri, 13 Jun 2025 16:06:00 +0200 Subject: [PATCH 3/3] Move tests to integration_test.py --- integration_test.py | 111 +++++++++++++++++++++ test.cpp | 43 +------- testsuite/path-tests/include_a/header_a.h | 1 - testsuite/path-tests/include_a/same_name.h | 1 - testsuite/path-tests/include_b/header_b.h | 1 - testsuite/path-tests/include_b/same_name.h | 1 - testsuite/path-tests/once.h | 2 - 7 files changed, 113 insertions(+), 47 deletions(-) delete mode 100644 testsuite/path-tests/include_a/header_a.h delete mode 100644 testsuite/path-tests/include_a/same_name.h delete mode 100644 testsuite/path-tests/include_b/header_b.h delete mode 100644 testsuite/path-tests/include_b/same_name.h delete mode 100644 testsuite/path-tests/once.h diff --git a/integration_test.py b/integration_test.py index 122ce9aa..ccef84eb 100644 --- a/integration_test.py +++ b/integration_test.py @@ -2,6 +2,7 @@ import os import pathlib +import platform import pytest from testutils import simplecpp, format_include_path_arg, format_include @@ -177,3 +178,113 @@ def test_relative_header_6(record_property, tmpdir, with_pragma_once, relative_i assert f'#line 8 "{pathlib.PurePath(tmpdir).as_posix()}/test.h"' in stdout else: assert double_include_error in stderr + +def test_same_name_header(record_property, tmpdir): + include_a = os.path.join(tmpdir, "include_a") + include_b = os.path.join(tmpdir, "include_b") + + test_file = os.path.join(tmpdir, "test.c") + header_a = os.path.join(include_a, "header_a.h") + header_b = os.path.join(include_b, "header_b.h") + same_name_a = os.path.join(include_a, "same_name.h") + same_name_b = os.path.join(include_b, "same_name.h") + + os.mkdir(include_a) + os.mkdir(include_b) + + with open(test_file, "wt") as f: + f.write(""" + #include + #include + TEST + """) + + with open(header_a, "wt") as f: + f.write(""" + #include "same_name.h" + """) + + with open(header_b, "wt") as f: + f.write(""" + #include "same_name.h" + """) + + with open(same_name_a, "wt") as f: + f.write(""" + #define TEST E + """) + + with open(same_name_b, "wt") as f: + f.write(""" + #define TEST OK + """) + + args = [ + format_include_path_arg(include_a), + format_include_path_arg(include_b), + test_file + ] + + _, stdout, stderr = simplecpp(args, cwd=tmpdir) + record_property("stdout", stdout) + record_property("stderr", stderr) + + assert "OK" in stdout + assert stderr == "" + +def test_pragma_once_matching(record_property, tmpdir): + if platform.system() == "win32": + names_to_test = [ + '"once.h"', + '"Once.h"', + '', + '', + '"../test_dir/once.h"', + '"../test_dir/Once.h"', + '"../Test_Dir/once.h"', + '"../Test_Dir/Once.h"', + '"test_subdir/../once.h"', + '"test_subdir/../Once.h"', + '"Test_Subdir/../once.h"', + '"Test_Subdir/../Once.h"', + ] + else: + names_to_test = [ + '"once.h"', + '', + '"../test_dir/once.h"', + '"test_subdir/../once.h"', + ] + + test_dir = os.path.join(tmpdir, "test_dir") + test_subdir = os.path.join(test_dir, "test_subdir") + + test_file = os.path.join(test_dir, "test.c") + once_header = os.path.join(test_dir, "once.h") + + os.mkdir(test_dir) + os.mkdir(test_subdir) + + with open(test_file, "wt") as f: + for n in names_to_test: + f.write(f""" + #include {n} + """); + + with open(once_header, "wt") as f: + f.write(f""" + #pragma once + ONCE + """); + + args = [ + format_include_path_arg(test_dir), + test_file + ] + + _, stdout, stderr = simplecpp(args, cwd=tmpdir) + record_property("stdout", stdout) + record_property("stderr", stderr) + + assert stdout.count("ONCE") == 1 + assert stderr == "" diff --git a/test.cpp b/test.cpp index 51c54b31..cec253b8 100644 --- a/test.cpp +++ b/test.cpp @@ -105,6 +105,8 @@ static std::string preprocess(const char code[], const simplecpp::DUI &dui, simp tokens.removeComments(); simplecpp::TokenList tokens2(files); simplecpp::preprocess(tokens2, tokens, files, filedata, dui, outputList); + for (auto &i : filedata) + delete i.second; return tokens2.stringify(); } @@ -2990,43 +2992,6 @@ static void fuzz_crash() } } -static void same_name() -{ - const char code[] = "#include \n" - "#include \n" - "TEST\n"; - - simplecpp::DUI dui; - dui.includePaths.push_back("./testsuite/path-tests/include_a"); - dui.includePaths.push_back("./testsuite/path-tests/include_b"); - - ASSERT_EQUALS("\n\nOK", preprocess(code, dui)); -} - -static void file_id() -{ - const char code[] = "#include \"once.h\"\n" - "#include \"Once.h\"\n" - - "#include \n" - "#include \n" - - "#include \"../path-tests/once.h\"\n" - "#include \"../path-tests/Once.h\"\n" - "#include \"../Path-Tests/once.h\"\n" - "#include \"../Path-Tests/Once.h\"\n" - - "#include \"include_a/../once.h\"\n" - "#include \"include_a/../Once.h\"\n" - "#include \"include_A/../once.h\"\n" - "#include \"include_A/../Once.h\"\n"; - - simplecpp::DUI dui; - dui.includePaths.push_back("./testsuite/path-tests"); - - ASSERT_EQUALS("\n#line 2 \"testsuite/path-tests/once.h\"\nONCE", preprocess(code, dui)); -} - int main(int argc, char **argv) { TEST_CASE(backslash); @@ -3249,10 +3214,6 @@ int main(int argc, char **argv) TEST_CASE(warning); - // path resolution - TEST_CASE(same_name); - TEST_CASE(file_id); - // utility functions. TEST_CASE(simplifyPath); TEST_CASE(simplifyPath_cppcheck); diff --git a/testsuite/path-tests/include_a/header_a.h b/testsuite/path-tests/include_a/header_a.h deleted file mode 100644 index 6ad8d333..00000000 --- a/testsuite/path-tests/include_a/header_a.h +++ /dev/null @@ -1 +0,0 @@ -#include "same_name.h" diff --git a/testsuite/path-tests/include_a/same_name.h b/testsuite/path-tests/include_a/same_name.h deleted file mode 100644 index fee527cd..00000000 --- a/testsuite/path-tests/include_a/same_name.h +++ /dev/null @@ -1 +0,0 @@ -#define TEST E diff --git a/testsuite/path-tests/include_b/header_b.h b/testsuite/path-tests/include_b/header_b.h deleted file mode 100644 index 6ad8d333..00000000 --- a/testsuite/path-tests/include_b/header_b.h +++ /dev/null @@ -1 +0,0 @@ -#include "same_name.h" diff --git a/testsuite/path-tests/include_b/same_name.h b/testsuite/path-tests/include_b/same_name.h deleted file mode 100644 index 9bb1b8aa..00000000 --- a/testsuite/path-tests/include_b/same_name.h +++ /dev/null @@ -1 +0,0 @@ -#define TEST OK diff --git a/testsuite/path-tests/once.h b/testsuite/path-tests/once.h deleted file mode 100644 index e0c59e63..00000000 --- a/testsuite/path-tests/once.h +++ /dev/null @@ -1,2 +0,0 @@ -#pragma once -ONCE