1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use core::fmt;
/// C++ language standard level
/// Defines the supported C++ standard versions, used to control the behavior of the parser
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum CppLanguageLevel {
/// C++11 Standard (ISO/IEC 14882:2011)
/// Introduced auto, lambda, rvalue references, smart pointers, and other modern features
Cpp11,
/// C++14 Standard (ISO/IEC 14882:2014)
/// Refinement of C++11, added generic lambdas, variable templates, etc.
Cpp14,
/// C++17 Standard (ISO/IEC 14882:2017)
/// Introduced structured bindings, if constexpr, class template argument deduction, etc.
Cpp17,
/// C++20 Standard (ISO/IEC 14882:2020)
/// Introduced concepts, modules, coroutines, ranges, and other major features
Cpp20,
/// C++23 Standard (ISO/IEC 14882:2023)
/// The latest C++ standard, introducing more modern features
Cpp23,
/// GNU C++ Extensions
/// Supports GCC-specific C++ extension syntax
GnuCpp,
/// Microsoft Visual C++ Extensions
/// Supports MSVC-specific C++ extension syntax
MsvcCpp,
}
impl fmt::Display for CppLanguageLevel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CppLanguageLevel::Cpp11 => write!(f, "C++11"),
CppLanguageLevel::Cpp14 => write!(f, "C++14"),
CppLanguageLevel::Cpp17 => write!(f, "C++17"),
CppLanguageLevel::Cpp20 => write!(f, "C++20"),
CppLanguageLevel::Cpp23 => write!(f, "C++23"),
CppLanguageLevel::GnuCpp => write!(f, "GNU C++"),
CppLanguageLevel::MsvcCpp => write!(f, "MSVC C++"),
}
}
}
impl CppLanguageLevel {
/// Check if the current language level supports the specified feature
pub fn supports_feature(&self, feature: CppFeature) -> bool {
use CppFeature::*;
match feature {
// C++98/03 features
BasicOOP | Templates | Exceptions | Namespaces => true,
// C++11 features
Auto | Lambda | RValueReferences | SmartPointers | VariadicTemplates
| ThreadSupport | RegexSupport | RandomNumbers | TimeUtilities => {
*self >= CppLanguageLevel::Cpp11
}
// C++14 features
GenericLambda | VariableTemplates | BinaryLiterals | DigitSeparators => {
*self >= CppLanguageLevel::Cpp14
}
// C++17 features
StructuredBindings
| IfConstexpr
| ClassTemplateArgumentDeduction
| FoldExpressions
| InlineVariables => *self >= CppLanguageLevel::Cpp17,
// C++20 features
Concepts
| Modules
| Coroutines
| Ranges
| ThreeWayComparison
| DesignatedInitializers => *self >= CppLanguageLevel::Cpp20,
// C++23 features
DeducingThis | IfConsteval | MultidimensionalSubscript => {
*self >= CppLanguageLevel::Cpp23
}
// Compiler-specific extensions
GnuExtensions => matches!(self, CppLanguageLevel::GnuCpp),
MsvcExtensions => matches!(self, CppLanguageLevel::MsvcCpp),
}
}
/// Get the default language level used when no build configuration is known.
pub const fn default_level() -> Self {
CppLanguageLevel::Cpp23
}
/// Parse language level from a `-std=` style spelling.
pub fn parse(s: &str) -> Option<Self> {
match s.to_lowercase().as_str() {
"c++11" | "11" => Some(CppLanguageLevel::Cpp11),
"c++14" | "14" => Some(CppLanguageLevel::Cpp14),
"c++17" | "17" => Some(CppLanguageLevel::Cpp17),
"c++20" | "20" => Some(CppLanguageLevel::Cpp20),
"c++23" | "23" => Some(CppLanguageLevel::Cpp23),
"gnu" | "gnu++" => Some(CppLanguageLevel::GnuCpp),
"msvc" | "visual c++" => Some(CppLanguageLevel::MsvcCpp),
_ => None,
}
}
}
impl Default for CppLanguageLevel {
fn default() -> Self {
CppLanguageLevel::default_level()
}
}
/// C++ feature enumeration
/// Used to check whether a specific language level supports a certain feature
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CppFeature {
// C++98/03 basic features
BasicOOP, // Basic object-oriented programming
Templates, // Templates
Exceptions, // Exception handling
Namespaces, // Namespaces
// C++11 features
Auto, // auto keyword
Lambda, // lambda expressions
RValueReferences, // rvalue references
SmartPointers, // smart pointers
VariadicTemplates, // variadic templates
ThreadSupport, // thread support
RegexSupport, // regular expressions
RandomNumbers, // random number generation
TimeUtilities, // time utilities
// C++14 features
GenericLambda, // generic lambda
VariableTemplates, // variable templates
BinaryLiterals, // binary literals
DigitSeparators, // digit separators
// C++17 features
StructuredBindings, // structured bindings
IfConstexpr, // if constexpr
ClassTemplateArgumentDeduction, // class template argument deduction
FoldExpressions, // fold expressions
InlineVariables, // inline variables
// C++20 features
Concepts, // concepts
Modules, // modules
Coroutines, // coroutines
Ranges, // ranges
ThreeWayComparison, // three-way comparison
DesignatedInitializers, // designated initializers
// C++23 features
DeducingThis, // deducing this
IfConsteval, // if consteval
MultidimensionalSubscript, // multidimensional subscript operator
// Compiler extensions
GnuExtensions, // GNU extensions
MsvcExtensions, // MSVC extensions
}