Skip to content

Commit b545d00

Browse files
committed
Use Cpp::GetLanguageStandard to get language standard
1 parent 506b645 commit b545d00

5 files changed

Lines changed: 33 additions & 28 deletions

File tree

notebooks/tinyraytracer.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"file_extension": ".cpp",
1111
"mimetype": "text/x-c++src",
1212
"name": "C++",
13-
"version": "23"
13+
"version": "cxx23"
1414
}
1515
},
1616
"nbformat_minor": 5,

notebooks/xeus-cpp-lite-demo.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"file_extension": ".cpp",
1111
"mimetype": "text/x-c++src",
1212
"name": "C++",
13-
"version": "23"
13+
"version": "cxx23"
1414
}
1515
},
1616
"nbformat_minor": 5,

notebooks/xeus-cpp.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@
886886
"file_extension": ".cpp",
887887
"mimetype": "text/x-c++src",
888888
"name": "C++",
889-
"version": "20"
889+
"version": "cxx20"
890890
}
891891
},
892892
"nbformat": 4,

src/xinterpreter.cpp

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -75,33 +75,38 @@ namespace xcpp
7575
xeus::register_interpreter(this);
7676
}
7777

78+
// FIXME: This function should be upstreamed to CppInterOp. See
79+
// https://github.com/compiler-research/CppInterOp/issues/879
7880
static std::string get_stdopt()
7981
{
80-
// We need to find what's the C++ version the interpreter runs with.
81-
const char* code = R"(
82-
int __get_cxx_version () {
83-
#if __cplusplus > 202302L
84-
return 26;
85-
#elif __cplusplus > 202002L
86-
return 23;
87-
#elif __cplusplus > 201703L
88-
return 20;
89-
#elif __cplusplus > 201402L
90-
return 17;
91-
#elif __cplusplus > 201103L || (defined(_WIN32) && _MSC_VER >= 1900)
92-
return 14;
93-
#elif __cplusplus >= 201103L
94-
return 11;
95-
#else
96-
return 0;
97-
#endif
98-
}
99-
__get_cxx_version ()
100-
)";
101-
auto cxx_version = Cpp::Evaluate(code);
102-
return std::to_string(cxx_version);
103-
}
82+
switch (Cpp::GetLanguageStandard(nullptr))
83+
{
84+
case Cpp::InterpreterLanguageStandard::c11:
85+
return "c11";
86+
case Cpp::InterpreterLanguageStandard::c17:
87+
return "c17";
88+
case Cpp::InterpreterLanguageStandard::c23:
89+
return "c23";
90+
case Cpp::InterpreterLanguageStandard::c2y:
91+
return "c2y";
92+
case Cpp::InterpreterLanguageStandard::cxx11:
93+
return "cxx11";
94+
case Cpp::InterpreterLanguageStandard::cxx14:
95+
return "cxx14";
96+
case Cpp::InterpreterLanguageStandard::cxx17:
97+
return "cxx17";
98+
case Cpp::InterpreterLanguageStandard::cxx20:
99+
return "cxx20";
100+
case Cpp::InterpreterLanguageStandard::cxx23:
101+
return "cxx23";
102+
case Cpp::InterpreterLanguageStandard::cxx26:
103+
return "cxx26";
104+
case Cpp::InterpreterLanguageStandard::lang_unspecified:
105+
return "lang_unspecified";
106+
}
104107

108+
return "unknown";
109+
}
105110
interpreter::interpreter(int argc, const char* const* argv) :
106111
xmagics()
107112
, p_cout_strbuf(nullptr)

test/test_interpreter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ TEST_SUITE("kernel_info_request")
407407
REQUIRE(result["language_info"]["mimetype"] == "text/x-c++src");
408408
REQUIRE(result["language_info"]["codemirror_mode"] == "text/x-c++src");
409409
REQUIRE(result["language_info"]["file_extension"] == ".cpp");
410-
REQUIRE(result["language_info"]["version"] == "23");
410+
REQUIRE(result["language_info"]["version"] == "cxx23");
411411
REQUIRE(result["status"] == "ok");
412412
}
413413

0 commit comments

Comments
 (0)