Cppcheck
keywords.cpp
Go to the documentation of this file.
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 "utils.h"
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 = {
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 = {
58 };
59 
60 static const std::unordered_set<std::string> c99_keywords = {
62 };
63 
64 static const std::unordered_set<std::string> c11_keywords_all = {
66 };
67 
68 static const std::unordered_set<std::string> c11_keywords = {
70 };
71 
72 static const std::unordered_set<std::string> c23_keywords_all = {
74 };
75 
76 static const std::unordered_set<std::string> c23_keywords = {
78 };
79 
80 // see https://en.cppreference.com/w/cpp/keyword
81 
82 #define CPP03_KEYWORDS \
83  "and", "and_eq", "asm", "auto", "bitand", "bitor", "bool", "break", "case", "catch", "char", \
84  "class", "compl", "const", "const_cast", "continue", "default", \
85  "delete", "do", "double", "dynamic_cast", "else", "enum", "explicit", "export", "extern", "false", \
86  "float", "for", "friend", "goto", "if", "inline", "int", "long", \
87  "mutable", "namespace", "new", "not", "not_eq", "operator", \
88  "or", "or_eq", "private", "protected", "public", "register", "reinterpret_cast", \
89  "return", "short", "signed", "sizeof", "static", \
90  "static_cast", "struct", "switch", "template", "this", "throw", \
91  "true", "try", "typedef", "typeid", "typename", "union", "unsigned", "using", \
92  "virtual", "void", "volatile", "wchar_t", "while", "xor", "xor_eq"
93 
94 #define CPP11_KEYWORDS \
95  "alignas", "alignof", "char16_t", "char32_t", "constexpr", "decltype", \
96  "noexcept", "nullptr", "static_assert", "thread_local"
97 
98 #define CPP20_KEYWORDS \
99  "char8_t", "concept", "consteval", "constinit", "co_await", \
100  "co_return", "co_yield", "requires"
101 
102 #ifdef __clang__
103 #pragma clang diagnostic push
104 #pragma clang diagnostic ignored "-Wunused-macros"
105 #endif
106 
107 #define CPP_TMTS_KEYWORDS \
108  "atomic_cancel", "atomic_commit", "atomic_noexcept", "synchronized"
109 
110 #define CPP_REFL_TS_KEYWORDS \
111  "reflexpr"
112 
113 #ifdef __clang__
114 #pragma clang diagnostic pop
115 #endif
116 
117 static const std::unordered_set<std::string> cpp03_keywords_all = {
119 };
120 
121 static const std::unordered_set<std::string> cpp03_keywords = cpp03_keywords_all;
122 
123 static const std::unordered_set<std::string> cpp11_keywords_all = {
125 };
126 
127 static const std::unordered_set<std::string> cpp11_keywords = {
129 };
130 
131 static const std::unordered_set<std::string> cpp14_keywords_all = cpp11_keywords_all;
132 
133 static const std::unordered_set<std::string> cpp14_keywords;
134 
135 static const std::unordered_set<std::string> cpp17_keywords_all = cpp11_keywords_all;
136 
137 static const std::unordered_set<std::string> cpp17_keywords;
138 
139 static const std::unordered_set<std::string> cpp20_keywords_all = {
141 };
142 
143 static const std::unordered_set<std::string> cpp20_keywords = {
145 };
146 
147 static const std::unordered_set<std::string> cpp23_keywords;
148 
149 static const std::unordered_set<std::string> cpp23_keywords_all = cpp20_keywords_all;
150 
151 // cppcheck-suppress unusedFunction
152 const std::unordered_set<std::string>& Keywords::getAll(Standards::cstd_t cStd)
153 {
154  // cppcheck-suppress missingReturn
155  switch (cStd) {
156  case Standards::cstd_t::C89:
157  return c89_keywords_all;
158  case Standards::cstd_t::C99:
159  return c99_keywords_all;
160  case Standards::cstd_t::C11:
161  case Standards::cstd_t::C17:
162  return c11_keywords_all;
163  case Standards::cstd_t::C23:
164  return c23_keywords_all;
165  }
167 }
168 
169 // cppcheck-suppress unusedFunction
170 const std::unordered_set<std::string>& Keywords::getAll(Standards::cppstd_t cppStd) {
171  // cppcheck-suppress missingReturn
172  switch (cppStd) {
173  case Standards::cppstd_t::CPP03:
174  return cpp03_keywords_all;
175  case Standards::cppstd_t::CPP11:
176  return cpp11_keywords_all;
177  case Standards::cppstd_t::CPP14:
178  return cpp14_keywords_all;
179  case Standards::cppstd_t::CPP17:
180  return cpp17_keywords_all;
181  case Standards::cppstd_t::CPP20:
182  return cpp20_keywords_all;
183  case Standards::cppstd_t::CPP23:
184  return cpp23_keywords_all;
185  }
187 }
188 
189 // cppcheck-suppress unusedFunction
190 const std::unordered_set<std::string>& Keywords::getOnly(Standards::cstd_t cStd)
191 {
192  // cppcheck-suppress missingReturn
193  switch (cStd) {
194  case Standards::cstd_t::C89:
195  return c89_keywords;
196  case Standards::cstd_t::C99:
197  return c99_keywords;
198  case Standards::cstd_t::C11:
199  case Standards::cstd_t::C17:
200  return c11_keywords;
201  case Standards::cstd_t::C23:
202  return c23_keywords_all;
203  }
205 }
206 
207 // cppcheck-suppress unusedFunction
208 const std::unordered_set<std::string>& Keywords::getOnly(Standards::cppstd_t cppStd)
209 {
210  // cppcheck-suppress missingReturn
211  switch (cppStd) {
212  case Standards::cppstd_t::CPP03:
213  return cpp03_keywords;
214  case Standards::cppstd_t::CPP11:
215  return cpp11_keywords;
216  case Standards::cppstd_t::CPP14:
217  return cpp14_keywords;
218  case Standards::cppstd_t::CPP17:
219  return cpp17_keywords;
220  case Standards::cppstd_t::CPP20:
221  return cpp20_keywords;
222  case Standards::cppstd_t::CPP23:
223  return cpp23_keywords;
224  }
226 }
227 
static const std::unordered_set< std::string > & getAll(Standards::cstd_t cStd)
Definition: keywords.cpp:152
static const std::unordered_set< std::string > & getOnly(Standards::cstd_t cStd)
Definition: keywords.cpp:190
#define CPP20_KEYWORDS
Definition: keywords.cpp:98
static const std::unordered_set< std::string > c99_keywords_all
Definition: keywords.cpp:56
static const std::unordered_set< std::string > cpp20_keywords_all
Definition: keywords.cpp:139
#define CPP11_KEYWORDS
Definition: keywords.cpp:94
static const std::unordered_set< std::string > cpp03_keywords
Definition: keywords.cpp:121
#define C11_KEYWORDS
Definition: keywords.cpp:34
#define C90_KEYWORDS
Definition: keywords.cpp:25
static const std::unordered_set< std::string > c23_keywords_all
Definition: keywords.cpp:72
static const std::unordered_set< std::string > cpp20_keywords
Definition: keywords.cpp:143
#define C23_KEYWORDS
Definition: keywords.cpp:42
static const std::unordered_set< std::string > cpp03_keywords_all
Definition: keywords.cpp:117
static const std::unordered_set< std::string > cpp14_keywords_all
Definition: keywords.cpp:131
static const std::unordered_set< std::string > c23_keywords
Definition: keywords.cpp:76
#define CPP03_KEYWORDS
Definition: keywords.cpp:82
static const std::unordered_set< std::string > c89_keywords
Definition: keywords.cpp:54
static const std::unordered_set< std::string > cpp11_keywords
Definition: keywords.cpp:127
static const std::unordered_set< std::string > cpp23_keywords_all
Definition: keywords.cpp:149
static const std::unordered_set< std::string > cpp17_keywords_all
Definition: keywords.cpp:135
#define C99_KEYWORDS
Definition: keywords.cpp:31
static const std::unordered_set< std::string > cpp11_keywords_all
Definition: keywords.cpp:123
static const std::unordered_set< std::string > cpp23_keywords
Definition: keywords.cpp:147
static const std::unordered_set< std::string > cpp14_keywords
Definition: keywords.cpp:133
static const std::unordered_set< std::string > c99_keywords
Definition: keywords.cpp:60
static const std::unordered_set< std::string > c11_keywords
Definition: keywords.cpp:68
static const std::unordered_set< std::string > c11_keywords_all
Definition: keywords.cpp:64
static const std::unordered_set< std::string > c89_keywords_all
Definition: keywords.cpp:50
static const std::unordered_set< std::string > cpp17_keywords
Definition: keywords.cpp:137
NORETURN void unreachable()
Definition: utils.h:369
cstd_t
C code standard.
Definition: standards.h:40
cppstd_t
C++ code standard.
Definition: standards.h:43