From ffdc3419a3b6e84b0d338be0be1a7da10fe72692 Mon Sep 17 00:00:00 2001 From: njnobles Date: Thu, 3 Oct 2024 12:49:37 -0700 Subject: [PATCH] Emit line directives for includes with directive on first line (#223) * Emit line directives for includes with directive on first line * Add nested include tests Thanks to Nick Nobles for this bug report and fix --- include/boost/wave/util/cpp_iterator.hpp | 5 +- test/testwave/testfiles/t_5_040.cpp | 74 ++++++++++++++++++++++++ test/testwave/testfiles/t_5_040_001.hpp | 22 +++++++ test/testwave/testfiles/t_5_040_002.hpp | 15 +++++ test/testwave/testfiles/t_5_040_003.hpp | 15 +++++ test/testwave/testfiles/t_5_040_004.hpp | 16 +++++ test/testwave/testfiles/t_5_040_005.hpp | 15 +++++ test/testwave/testfiles/test.cfg | 1 + 8 files changed, 161 insertions(+), 2 deletions(-) create mode 100644 test/testwave/testfiles/t_5_040.cpp create mode 100644 test/testwave/testfiles/t_5_040_001.hpp create mode 100644 test/testwave/testfiles/t_5_040_002.hpp create mode 100644 test/testwave/testfiles/t_5_040_003.hpp create mode 100644 test/testwave/testfiles/t_5_040_004.hpp create mode 100644 test/testwave/testfiles/t_5_040_005.hpp diff --git a/include/boost/wave/util/cpp_iterator.hpp b/include/boost/wave/util/cpp_iterator.hpp index 9c0a2966a..b35d7d9a1 100644 --- a/include/boost/wave/util/cpp_iterator.hpp +++ b/include/boost/wave/util/cpp_iterator.hpp @@ -1578,9 +1578,9 @@ pp_iterator_functor::on_include_helper(char const* f, char const* s, char const* current_name = 0; // never try to match current file name #endif -// call the 'found_include_directive' hook function + // call the 'found_include_directive' hook function if (ctx.get_hooks().found_include_directive(ctx.derived(), f, include_next)) - return true; // client returned false: skip file to include + return true; // client returned true: skip file to include file_path = util::impl::unescape_lit(file_path); std::string native_path_str; @@ -1607,6 +1607,7 @@ pp_iterator_functor::on_include_helper(char const* f, char const* s, boost::wave::enable_prefer_pp_numbers(ctx.get_language()), is_system ? base_iteration_context_type::system_header : base_iteration_context_type::user_header)); + new_iter_ctx->emitted_lines = (unsigned int)(-1); // force #line directive // call the include policy trace function ctx.get_hooks().opened_include_file(ctx.derived(), dir_path, file_path, diff --git a/test/testwave/testfiles/t_5_040.cpp b/test/testwave/testfiles/t_5_040.cpp new file mode 100644 index 000000000..fc2aaa12a --- /dev/null +++ b/test/testwave/testfiles/t_5_040.cpp @@ -0,0 +1,74 @@ +/*============================================================================= + Boost.Wave: A Standard compliant C++ preprocessor library + http://www.boost.org/ + + Copyright (c) 2024 Nick Nobles. Distributed under the Boost + Software License, Version 1.0. (See accompanying file + LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ + +// Line directives should be emitted for included files when they begin +// with a single #if/#ifdef/#define directive. Addresses github issue #222. +#include "t_5_040_001.hpp" // #if as first line should emit line directive +#include "t_5_040_002.hpp" // #define as first line should emit line directive +#include "t_5_040_003.hpp" // ensure nested includes emit line directive +#include "t_5_040_005.hpp" // ensure empty initial lines produce line directive + +t_5_040_a +#if 1 +t_5_040_b +#endif + +//R #line 2 "t_5_040_001.hpp" +//R t_5_040_001a +//R #line 6 "t_5_040_001.hpp" +//R t_5_040_001b +//R +//R t_5_040_001c +//R #line 2 "t_5_040_002.hpp" +//R t_5_040_002 +//R #line 2 "t_5_040_004.hpp" +//R t_5_040_004 +//R #line 2 "t_5_040_003.hpp" +//R t_5_040_003 +//R #line 15 "t_5_040_005.hpp" +//R t_5_040_005 +//R #line 17 "t_5_040.cpp" +//R t_5_040_a +//R +//R t_5_040_b + +//H 10: t_5_040.cpp(12): #include "t_5_040_001.hpp" +//H 04: "t_5_040_001.hpp" +//H 05: $S(t_5_040_001.hpp) ($B(t_5_040_001.hpp)) +//H 10: t_5_040_001.hpp(1): #if +//H 11: t_5_040_001.hpp(1): #if 1: 1 +//H 10: t_5_040_001.hpp(3): #endif +//H 10: t_5_040_001.hpp(7): #if +//H 11: t_5_040_001.hpp(7): #if 1: 1 +//H 10: t_5_040_001.hpp(9): #endif +//H 06: +//H 10: t_5_040.cpp(13): #include "t_5_040_002.hpp" +//H 04: "t_5_040_002.hpp" +//H 05: t_5_040_002.hpp ($B(t_5_040_002.hpp)) +//H 10: t_5_040_002.hpp(1): #define +//H 08: t_5_040_002.hpp(1): t_5_040_002_hpp= +//H 06: +//H 10: t_5_040.cpp(14): #include "t_5_040_003.hpp" +//H 04: "t_5_040_003.hpp" +//H 05: t_5_040_003.hpp ($B(t_5_040_003.hpp)) +//H 10: t_5_040_003.hpp(1): #include "t_5_040_004.hpp" +//H 04: "t_5_040_004.hpp" +//H 05: t_5_040_004.hpp ($B(t_5_040_004.hpp)) +//H 10: t_5_040_004.hpp(1): #if +//H 11: t_5_040_004.hpp(1): #if 1: 1 +//H 10: t_5_040_004.hpp(3): #endif +//H 06: +//H 06: +//H 10: t_5_040.cpp(15): #include "t_5_040_005.hpp" +//H 04: "t_5_040_005.hpp" +//H 05: t_5_040_005.hpp ($B(t_5_040_005.hpp)) +//H 06: +//H 10: t_5_040.cpp(18): #if +//H 11: t_5_040.cpp(18): #if 1: 1 +//H 10: t_5_040.cpp(20): #endif diff --git a/test/testwave/testfiles/t_5_040_001.hpp b/test/testwave/testfiles/t_5_040_001.hpp new file mode 100644 index 000000000..f8e75b9b5 --- /dev/null +++ b/test/testwave/testfiles/t_5_040_001.hpp @@ -0,0 +1,22 @@ +#if 1 +t_5_040_001a +#endif + +//Ensure line directive still properly gets replaced with empty line +t_5_040_001b +#if 1 +t_5_040_001c +#endif + +// Important: The #if must be the first line of this file to ensure proper testing +// of line directive emission. Do not move the copyright comment to the +// beginning of this file. + +/*============================================================================= + Boost.Wave: A Standard compliant C++ preprocessor library + http://www.boost.org/ + + Copyright (c) 2024 Nick Nobles. Distributed under the Boost + Software License, Version 1.0. (See accompanying file + LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ diff --git a/test/testwave/testfiles/t_5_040_002.hpp b/test/testwave/testfiles/t_5_040_002.hpp new file mode 100644 index 000000000..b40cb0c94 --- /dev/null +++ b/test/testwave/testfiles/t_5_040_002.hpp @@ -0,0 +1,15 @@ +#define t_5_040_002_hpp +t_5_040_002 + +// Important: The #define must be the first line of this file to ensure proper testing +// of line directive emission. Do not move the copyright comment to the +// beginning of this file. + +/*============================================================================= + Boost.Wave: A Standard compliant C++ preprocessor library + http://www.boost.org/ + + Copyright (c) 2024 Nick Nobles. Distributed under the Boost + Software License, Version 1.0. (See accompanying file + LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ diff --git a/test/testwave/testfiles/t_5_040_003.hpp b/test/testwave/testfiles/t_5_040_003.hpp new file mode 100644 index 000000000..d9c40ab1b --- /dev/null +++ b/test/testwave/testfiles/t_5_040_003.hpp @@ -0,0 +1,15 @@ +#include "t_5_040_004.hpp" +t_5_040_003 + +// Important: The #include must be the first line of this file to ensure proper testing +// of line directive emission. Do not move the copyright comment to the +// beginning of this file. + +/*============================================================================= + Boost.Wave: A Standard compliant C++ preprocessor library + http://www.boost.org/ + + Copyright (c) 2024 Nick Nobles. Distributed under the Boost + Software License, Version 1.0. (See accompanying file + LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ diff --git a/test/testwave/testfiles/t_5_040_004.hpp b/test/testwave/testfiles/t_5_040_004.hpp new file mode 100644 index 000000000..69d88286e --- /dev/null +++ b/test/testwave/testfiles/t_5_040_004.hpp @@ -0,0 +1,16 @@ +#if 1 +t_5_040_004 +#endif + +// Important: The #if must be the first line of this file to ensure proper testing +// of line directive emission. Do not move the copyright comment to the +// beginning of this file. + +/*============================================================================= + Boost.Wave: A Standard compliant C++ preprocessor library + http://www.boost.org/ + + Copyright (c) 2024 Nick Nobles. Distributed under the Boost + Software License, Version 1.0. (See accompanying file + LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ diff --git a/test/testwave/testfiles/t_5_040_005.hpp b/test/testwave/testfiles/t_5_040_005.hpp new file mode 100644 index 000000000..9fd77dfa6 --- /dev/null +++ b/test/testwave/testfiles/t_5_040_005.hpp @@ -0,0 +1,15 @@ + +// Important: The first line of this file must be blank to ensure proper testing +// of line directive emission. Do not move the copyright comment to the +// beginning of this file. + +/*============================================================================= + Boost.Wave: A Standard compliant C++ preprocessor library + http://www.boost.org/ + + Copyright (c) 2024 Jeff Trull. Distributed under the Boost + Software License, Version 1.0. (See accompanying file + LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ + +t_5_040_005 diff --git a/test/testwave/testfiles/test.cfg b/test/testwave/testfiles/test.cfg index 60dea4c7b..23b4e2c09 100644 --- a/test/testwave/testfiles/test.cfg +++ b/test/testwave/testfiles/test.cfg @@ -146,6 +146,7 @@ t_5_036.cpp t_5_037.cpp t_5_038.cpp t_5_039.cpp +t_5_040.cpp # # unit tests from the mcpp preprocessor validation suite