Skip to content

Commit e0946f7

Browse files
authored
Replace runtime C++ version detection with compile-time extraction from argv (#349)
1 parent 6dcf09a commit e0946f7

File tree

2 files changed

+18
-26
lines changed

2 files changed

+18
-26
lines changed

src/xinterpreter.cpp

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -73,31 +73,20 @@ namespace xcpp
7373
xeus::register_interpreter(this);
7474
}
7575

76-
static std::string get_stdopt()
76+
static std::string get_stdopt(int argc, const char* const* argv)
7777
{
78-
// We need to find what's the C++ version the interpreter runs with.
79-
const char* code = R"(
80-
int __get_cxx_version () {
81-
#if __cplusplus > 202302L
82-
return 26;
83-
#elif __cplusplus > 202002L
84-
return 23;
85-
#elif __cplusplus > 201703L
86-
return 20;
87-
#elif __cplusplus > 201402L
88-
return 17;
89-
#elif __cplusplus > 201103L || (defined(_WIN32) && _MSC_VER >= 1900)
90-
return 14;
91-
#elif __cplusplus >= 201103L
92-
return 11;
93-
#else
94-
return 0;
95-
#endif
96-
}
97-
__get_cxx_version ()
98-
)";
99-
auto cxx_version = Cpp::Evaluate(code);
100-
return std::to_string(cxx_version);
78+
std::string res = "14";
79+
for (int i = 0; i < argc; ++i)
80+
{
81+
std::string arg(argv[i]);
82+
auto pos = arg.find("-std=c++");
83+
if (pos != std::string::npos)
84+
{
85+
res = arg.substr(pos + 8);
86+
break;
87+
}
88+
}
89+
return res;
10190
}
10291

10392
interpreter::interpreter(int argc, const char* const* argv) :
@@ -109,7 +98,7 @@ __get_cxx_version ()
10998
{
11099
//NOLINTNEXTLINE (cppcoreguidelines-pro-bounds-pointer-arithmetic)
111100
createInterpreter(Args(argv ? argv + 1 : argv, argv + argc));
112-
m_version = get_stdopt();
101+
m_version = get_stdopt(argc, argv);
113102
redirect_output();
114103
init_preamble();
115104
init_magic();

test/test_interpreter.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,9 @@ TEST_SUITE("kernel_info_request")
254254
{
255255
TEST_CASE("good_status")
256256
{
257-
std::vector<const char*> Args = {/*"-v", "resource-dir", "....."*/};
257+
std::vector<const char*> Args = {
258+
"-v", "-std=c++23" // test input for get_stdopt
259+
};
258260
xcpp::interpreter interpreter((int)Args.size(), Args.data());
259261

260262
nl::json result = interpreter.kernel_info_request();
@@ -264,6 +266,7 @@ TEST_SUITE("kernel_info_request")
264266
REQUIRE(result["language_info"]["mimetype"] == "text/x-c++src");
265267
REQUIRE(result["language_info"]["codemirror_mode"] == "text/x-c++src");
266268
REQUIRE(result["language_info"]["file_extension"] == ".cpp");
269+
REQUIRE(result["language_info"]["version"] == "23");
267270
REQUIRE(result["status"] == "ok");
268271
}
269272

0 commit comments

Comments
 (0)