diff --git a/include/boost/wave/util/cpp_macromap.hpp b/include/boost/wave/util/cpp_macromap.hpp index fad958a3..358be7a2 100644 --- a/include/boost/wave/util/cpp_macromap.hpp +++ b/include/boost/wave/util/cpp_macromap.hpp @@ -742,6 +742,12 @@ macromap::expand_tokensequence_worker( { // try to replace this identifier as a macro if (expand_operator_defined && (*first).get_value() == "defined") { + if (T_EOI == impl::next_token>::peek(first, last)) { + // no further tokens are available - likely because someone defined an object-like macro that expands to "defined" + // do what expand_macro would do in this situation: + act_token = *first++; + return act_token; + } // resolve operator defined() return resolve_defined(first, last, pending); } @@ -749,6 +755,11 @@ macromap::expand_tokensequence_worker( else if (boost::wave::need_has_include(ctx.get_language()) && expand_operator_has_include && (*first).get_value() == "__has_include") { + if (T_LEFTPAREN != impl::next_token>::peek(first, last)) { + // may be an object-like macro that expands to "__has_include" + act_token = *first++; + return act_token; + } // resolve operator __has_include() return resolve_has_include(first, last, pending); } diff --git a/test/testwave/testfiles/t_2_024.cpp b/test/testwave/testfiles/t_2_024.cpp index 8243b237..fdb4d628 100644 --- a/test/testwave/testfiles/t_2_024.cpp +++ b/test/testwave/testfiles/t_2_024.cpp @@ -93,3 +93,70 @@ //H 10: t_2_024.cpp(80): #else //H 10: t_2_024.cpp(83): #endif +// test object-like macro defined as __has_include +#define HI __has_include +#if HI() +#define FOUND_SELF_VIA_SYSTEM_PATH_AND_OBJLIKE +#else +#warning could not find this file via system path when expanded via an object-like macro +#endif + +//H 10: t_2_024.cpp(97): #define +//H 08: t_2_024.cpp(97): HI=__has_include +//H 10: t_2_024.cpp(98): #if +//H 01: t_2_024.cpp(97): HI +//H 02: __has_include +//H 03: __has_include +//H 11: t_2_024.cpp(98): #if HI(): 1 +//H 10: t_2_024.cpp(99): #define +//H 08: t_2_024.cpp(99): FOUND_SELF_VIA_SYSTEM_PATH_AND_OBJLIKE= +//H 10: t_2_024.cpp(100): #else + +#if HI("made_up_name_dont_create.hpp") +#warning this made up file should not exist but has_include thinks otherwise +#else +// the good case +#endif + +//H 10: t_2_024.cpp(115): #if +//H 01: t_2_024.cpp(97): HI +//H 02: __has_include +//H 03: __has_include +//H 11: t_2_024.cpp(115): #if HI("made_up_name_dont_create.hpp"): 0 +//H 10: t_2_024.cpp(119): #endif + +// test function-like macro that wraps __has_include +#define HIF(x) __has_include(x) +#if HIF() +#define FOUND_SELF_VIA_SYSTEM_PATH_AND_FUNCLIKE +#else +#warning could not find this file via system path when expanded via a function-like macro +#endif + +//H 10: t_2_024.cpp(129): #define +//H 08: t_2_024.cpp(129): HIF(x)=__has_include(x) +//H 10: t_2_024.cpp(130): #if +//H 00: t_2_024.cpp(130): HIF(), [t_2_024.cpp(129): HIF(x)=__has_include(x)] +//H 02: __has_include() +//H 03: 1 +//H 11: t_2_024.cpp(130): #if HIF(): 1 +//H 10: t_2_024.cpp(131): #define +//H 08: t_2_024.cpp(131): FOUND_SELF_VIA_SYSTEM_PATH_AND_FUNCLIKE= +//H 10: t_2_024.cpp(132): #else + +#define HIF(x) __has_include(x) +#if HIF("made_up_name_dont_create.hpp") +#warning found a file that should not exists but has_include thinks otherwise via function-like expansion +#else +#define DID_NOT_FIND_SELF_VIA_SYSTEM_PATH_AND_FUNCLIKE +#endif + +//H 10: t_2_024.cpp(147): #define +//H 10: t_2_024.cpp(148): #if +//H 00: t_2_024.cpp(148): HIF("made_up_name_dont_create.hpp"), [t_2_024.cpp(129): HIF(x)=__has_include(x)] +//H 02: __has_include("made_up_name_dont_create.hpp") +//H 03: 0 +//H 11: t_2_024.cpp(148): #if HIF("made_up_name_dont_create.hpp"): 0 +//H 10: t_2_024.cpp(151): #define +//H 08: t_2_024.cpp(151): DID_NOT_FIND_SELF_VIA_SYSTEM_PATH_AND_FUNCLIKE= +//H 10: t_2_024.cpp(152): #endif diff --git a/test/testwave/testfiles/t_2_025.cpp b/test/testwave/testfiles/t_2_025.cpp index d39161c7..399f9671 100644 --- a/test/testwave/testfiles/t_2_025.cpp +++ b/test/testwave/testfiles/t_2_025.cpp @@ -74,3 +74,25 @@ //H 10: t_2_025.cpp(60): #define //H 08: t_2_025.cpp(60): FOUND_ABRACKET= //H 10: t_2_025.cpp(61): #else + +// try an object-like macro that expands to __has_include +#define HI __has_include +#if HI(FOO(BAR)) +#define FOUND_DQUOTE_WITH_OBJLIKE +#else +#warning has_include could not find this file using quotes from object-like macro +#endif + +//H 10: t_2_025.cpp(79): #define +//H 08: t_2_025.cpp(79): HI=__has_include +//H 10: t_2_025.cpp(80): #if +//H 01: t_2_025.cpp(79): HI +//H 02: __has_include +//H 03: __has_include +//H 00: t_2_025.cpp(80): FOO(BAR), [t_2_025.cpp(35): FOO(X)="t_2_025.cpp"] +//H 02: "t_2_025.cpp" +//H 03: "t_2_025.cpp" +//H 11: t_2_025.cpp(80): #if HI(FOO(BAR)): 1 +//H 10: t_2_025.cpp(81): #define +//H 08: t_2_025.cpp(81): FOUND_DQUOTE_WITH_OBJLIKE= +//H 10: t_2_025.cpp(82): #else diff --git a/test/testwave/testfiles/t_2_034.cpp b/test/testwave/testfiles/t_2_034.cpp new file mode 100644 index 00000000..c7bf4287 --- /dev/null +++ b/test/testwave/testfiles/t_2_034.cpp @@ -0,0 +1,37 @@ +/*============================================================================= + Boost.Wave: A Standard compliant C++ preprocessor library + http://www.boost.org/ + + Copyright (c) 2026 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) +=============================================================================*/ + +// Verify that an object-like macro that expands to defined works as expected + +#define FOO +#define DEFND defined + +#if DEFND FOO +// this is good +#else +#warning defined expansion w/o parenthesis fails +#endif + +#if !DEFND FOO +#warning negated defined expansion w/o parenthesis fails +#else +// this is good +#endif + +#if DEFND(FOO) +// this is good +#else +#warning defined expansion with parenthesis fails +#endif + +#if !DEFND(FOO) +#warning negated defined expansion with parenthesis fails +#else +// this is good +#endif diff --git a/test/testwave/testfiles/test.cfg b/test/testwave/testfiles/test.cfg index 872f04a5..a18b1d7d 100644 --- a/test/testwave/testfiles/test.cfg +++ b/test/testwave/testfiles/test.cfg @@ -86,6 +86,7 @@ t_2_030.cpp t_2_031.cpp t_2_032.cpp t_2_033.cpp +t_2_034.cpp # # t_3: Predefined macros