/src/cppcheck/lib/keywords.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Cppcheck - A tool for static C/C++ code analysis |
3 | | * Copyright (C) 2007-2023 Cppcheck team. |
4 | | * |
5 | | * This program is free software: you can redistribute it and/or modify |
6 | | * it under the terms of the GNU General Public License as published by |
7 | | * the Free Software Foundation, either version 3 of the License, or |
8 | | * (at your option) any later version. |
9 | | * |
10 | | * This program is distributed in the hope that it will be useful, |
11 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | | * GNU General Public License for more details. |
14 | | * |
15 | | * You should have received a copy of the GNU General Public License |
16 | | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | | */ |
18 | | |
19 | | #include "keywords.h" |
20 | | |
21 | | #include <cassert> |
22 | | |
23 | | // see https://en.cppreference.com/w/c/keyword |
24 | | |
25 | | #define C90_KEYWORDS \ |
26 | | "auto", "break", "case", "char", "const", "continue", "default", \ |
27 | | "do", "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", "long", \ |
28 | | "register", "return", "short", "signed", "sizeof", "static", "struct", "switch", "typedef", \ |
29 | | "union", "unsigned", "void", "volatile", "while" |
30 | | |
31 | | #define C99_KEYWORDS \ |
32 | | "inline", "restrict", "_Bool", "_Complex", "_Imaginary" |
33 | | |
34 | | #define C11_KEYWORDS \ |
35 | | "_Alignas", "_Alignof", "_Atomic", "_Generic", "_Noreturn", "_Static_assert", "_Thread_local" |
36 | | |
37 | | #ifdef __clang__ |
38 | | #pragma clang diagnostic push |
39 | | #pragma clang diagnostic ignored "-Wunused-macros" |
40 | | #endif |
41 | | |
42 | | #define C23_KEYWORDS \ |
43 | | "alignas", "alignof", "bool", "constexpr", "false", "nullptr", "static_assert", "thread_local", "true", "typeof", "typeof_unqual", \ |
44 | | "_BitInt", "_Decimal128", "_Decimal32", "_Decimal64" |
45 | | |
46 | | #ifdef __clang__ |
47 | | #pragma clang diagnostic pop |
48 | | #endif |
49 | | |
50 | | static const std::unordered_set<std::string> c89_keywords_all = { |
51 | | C90_KEYWORDS |
52 | | }; |
53 | | |
54 | | static const std::unordered_set<std::string> c89_keywords = c89_keywords_all; |
55 | | |
56 | | static const std::unordered_set<std::string> c99_keywords_all = { |
57 | | C90_KEYWORDS, C99_KEYWORDS |
58 | | }; |
59 | | |
60 | | static const std::unordered_set<std::string> c99_keywords = { |
61 | | C99_KEYWORDS |
62 | | }; |
63 | | |
64 | | static const std::unordered_set<std::string> c11_keywords_all = { |
65 | | C90_KEYWORDS, C99_KEYWORDS, C11_KEYWORDS |
66 | | }; |
67 | | |
68 | | static const std::unordered_set<std::string> c11_keywords = { |
69 | | C11_KEYWORDS |
70 | | }; |
71 | | |
72 | | /* |
73 | | static const std::unordered_set<std::string> c23_keywords_all = { |
74 | | C90_KEYWORDS, C99_KEYWORDS, C11_KEYWORDS, C23_KEYWORDS |
75 | | }; |
76 | | |
77 | | static const std::unordered_set<std::string> c23_keywords = { |
78 | | C23_KEYWORDS |
79 | | }; |
80 | | */ |
81 | | |
82 | | // see https://en.cppreference.com/w/cpp/keyword |
83 | | |
84 | | #define CPP03_KEYWORDS \ |
85 | | "and", "and_eq", "asm", "auto", "bitand", "bitor", "bool", "break", "case", "catch", "char", \ |
86 | | "class", "compl", "const", "const_cast", "continue", "default", \ |
87 | | "delete", "do", "double", "dynamic_cast", "else", "enum", "explicit", "export", "extern", "false", \ |
88 | | "float", "for", "friend", "goto", "if", "inline", "int", "long", \ |
89 | | "mutable", "namespace", "new", "not", "not_eq", "operator", \ |
90 | | "or", "or_eq", "private", "protected", "public", "register", "reinterpret_cast", \ |
91 | | "return", "short", "signed", "sizeof", "static", \ |
92 | | "static_cast", "struct", "switch", "template", "this", "throw", \ |
93 | | "true", "try", "typedef", "typeid", "typename", "union", "unsigned", "using", \ |
94 | | "virtual", "void", "volatile", "wchar_t", "while", "xor", "xor_eq" |
95 | | |
96 | | #define CPP11_KEYWORDS \ |
97 | | "alignas", "alignof", "char16_t", "char32_t", "constexpr", "decltype", \ |
98 | | "noexcept", "nullptr", "static_assert", "thread_local" |
99 | | |
100 | | #define CPP20_KEYWORDS \ |
101 | | "char8_t", "concept", "consteval", "constinit", "co_await", \ |
102 | | "co_return", "co_yield", "requires" |
103 | | |
104 | | #ifdef __clang__ |
105 | | #pragma clang diagnostic push |
106 | | #pragma clang diagnostic ignored "-Wunused-macros" |
107 | | #endif |
108 | | |
109 | | #define CPP_TMTS_KEYWORDS \ |
110 | | "atomic_cancel", "atomic_commit", "atomic_noexcept", "synchronized" |
111 | | |
112 | | #define CPP_REFL_TS_KEYWORDS \ |
113 | | "reflexpr" |
114 | | |
115 | | #ifdef __clang__ |
116 | | #pragma clang diagnostic pop |
117 | | #endif |
118 | | |
119 | | static const std::unordered_set<std::string> cpp03_keywords_all = { |
120 | | CPP03_KEYWORDS |
121 | | }; |
122 | | |
123 | | static const std::unordered_set<std::string> cpp03_keywords = cpp03_keywords_all; |
124 | | |
125 | | static const std::unordered_set<std::string> cpp11_keywords_all = { |
126 | | CPP03_KEYWORDS, CPP11_KEYWORDS |
127 | | }; |
128 | | |
129 | | static const std::unordered_set<std::string> cpp11_keywords = { |
130 | | CPP11_KEYWORDS |
131 | | }; |
132 | | |
133 | | static const std::unordered_set<std::string> cpp14_keywords_all = cpp11_keywords_all; |
134 | | |
135 | | static const std::unordered_set<std::string> cpp14_keywords; |
136 | | |
137 | | static const std::unordered_set<std::string> cpp17_keywords_all = cpp11_keywords_all; |
138 | | |
139 | | static const std::unordered_set<std::string> cpp17_keywords; |
140 | | |
141 | | static const std::unordered_set<std::string> cpp20_keywords_all = { |
142 | | CPP03_KEYWORDS, CPP11_KEYWORDS, CPP20_KEYWORDS |
143 | | }; |
144 | | |
145 | | static const std::unordered_set<std::string> cpp20_keywords = { |
146 | | CPP20_KEYWORDS |
147 | | }; |
148 | | |
149 | | static const std::unordered_set<std::string> cpp23_keywords; |
150 | | |
151 | | static const std::unordered_set<std::string> cpp23_keywords_all = cpp20_keywords_all; |
152 | | |
153 | | // cppcheck-suppress unusedFunction |
154 | | const std::unordered_set<std::string>& Keywords::getAll(Standards::cstd_t cStd) |
155 | 0 | { |
156 | | // cppcheck-suppress missingReturn |
157 | 0 | switch (cStd) { |
158 | 0 | case Standards::cstd_t::C89: |
159 | 0 | return c89_keywords_all; |
160 | 0 | case Standards::cstd_t::C99: |
161 | 0 | return c99_keywords_all; |
162 | 0 | case Standards::cstd_t::C11: |
163 | 0 | return c11_keywords_all; |
164 | | /*case Standards::cstd_t::C23: |
165 | | return c23_keywords_all;*/ |
166 | 0 | } |
167 | 0 | assert(false && "unreachable"); |
168 | 0 | } |
169 | | |
170 | | // cppcheck-suppress unusedFunction |
171 | 2 | const std::unordered_set<std::string>& Keywords::getAll(Standards::cppstd_t cppStd) { |
172 | | // cppcheck-suppress missingReturn |
173 | 2 | switch (cppStd) { |
174 | 0 | case Standards::cppstd_t::CPP03: |
175 | 0 | return cpp03_keywords_all; |
176 | 0 | case Standards::cppstd_t::CPP11: |
177 | 0 | return cpp11_keywords_all; |
178 | 0 | case Standards::cppstd_t::CPP14: |
179 | 0 | return cpp14_keywords_all; |
180 | 0 | case Standards::cppstd_t::CPP17: |
181 | 0 | return cpp17_keywords_all; |
182 | 1 | case Standards::cppstd_t::CPP20: |
183 | 1 | return cpp20_keywords_all; |
184 | 1 | case Standards::cppstd_t::CPP23: |
185 | 1 | return cpp23_keywords_all; |
186 | 2 | } |
187 | 0 | assert(false && "unreachable"); |
188 | 0 | } |
189 | | |
190 | | // cppcheck-suppress unusedFunction |
191 | | const std::unordered_set<std::string>& Keywords::getOnly(Standards::cstd_t cStd) |
192 | 0 | { |
193 | | // cppcheck-suppress missingReturn |
194 | 0 | switch (cStd) { |
195 | 0 | case Standards::cstd_t::C89: |
196 | 0 | return c89_keywords; |
197 | 0 | case Standards::cstd_t::C99: |
198 | 0 | return c99_keywords; |
199 | 0 | case Standards::cstd_t::C11: |
200 | 0 | return c11_keywords; |
201 | | /*case Standards::cstd_t::C23: |
202 | | return c23_keywords_all;*/ |
203 | 0 | } |
204 | 0 | assert(false && "unreachable"); |
205 | 0 | } |
206 | | |
207 | | // cppcheck-suppress unusedFunction |
208 | | const std::unordered_set<std::string>& Keywords::getOnly(Standards::cppstd_t cppStd) |
209 | 0 | { |
210 | | // cppcheck-suppress missingReturn |
211 | 0 | switch (cppStd) { |
212 | 0 | case Standards::cppstd_t::CPP03: |
213 | 0 | return cpp03_keywords; |
214 | 0 | case Standards::cppstd_t::CPP11: |
215 | 0 | return cpp11_keywords; |
216 | 0 | case Standards::cppstd_t::CPP14: |
217 | 0 | return cpp14_keywords; |
218 | 0 | case Standards::cppstd_t::CPP17: |
219 | 0 | return cpp17_keywords; |
220 | 0 | case Standards::cppstd_t::CPP20: |
221 | 0 | return cpp20_keywords; |
222 | 0 | case Standards::cppstd_t::CPP23: |
223 | 0 | return cpp23_keywords; |
224 | 0 | } |
225 | 0 | assert(false && "unreachable"); |
226 | 0 | } |
227 | | |