|
| 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 | +// see https://en.cppreference.com/w/c/keyword |
| 22 | + |
| 23 | +#define C90_KEYWORDS \ |
| 24 | + "auto", "break", "case", "char", "const", "continue", "default", \ |
| 25 | + "do", "double", "else", "enum", "extern", "float", "for", "goto", "if", "int", "long", \ |
| 26 | + "register", "return", "short", "signed", "sizeof", "static", "struct", "switch", "typedef", \ |
| 27 | + "union", "unsigned", "void", "volatile", "while" |
| 28 | + |
| 29 | +#define C99_KEYWORDS \ |
| 30 | + "inline", "restrict", "_Bool", "_Complex", "_Imaginary" |
| 31 | + |
| 32 | +#define C11_KEYWORDS \ |
| 33 | + "_Alignas", "_Alignof", "_Atomic", "_Generic", "_Noreturn", "_Static_assert", "_Thread_local" |
| 34 | + |
| 35 | +#ifdef __clang__ |
| 36 | +#pragma clang diagnostic push |
| 37 | +#pragma clang diagnostic ignored "-Wunused-macros" |
| 38 | +#endif |
| 39 | + |
| 40 | +#define C23_KEYWORDS \ |
| 41 | + "alignas", "alignof", "bool", "false", "nullptr", "static_assert", "thread_local", "true", "typeof", "typeof_unqual", \ |
| 42 | + "_BitInt", "_Decimal128", "_Decimal32", "_Decimal64" |
| 43 | + |
| 44 | +#ifdef __clang__ |
| 45 | +#pragma clang diagnostic pop |
| 46 | +#endif |
| 47 | + |
| 48 | +static const std::unordered_set<std::string> c89_keywords_all = { |
| 49 | + C90_KEYWORDS |
| 50 | +}; |
| 51 | + |
| 52 | +static const std::unordered_set<std::string> c89_keywords = c89_keywords_all; |
| 53 | + |
| 54 | +static const std::unordered_set<std::string> c99_keywords_all = { |
| 55 | + C90_KEYWORDS, C99_KEYWORDS |
| 56 | +}; |
| 57 | + |
| 58 | +static const std::unordered_set<std::string> c99_keywords = { |
| 59 | + C99_KEYWORDS |
| 60 | +}; |
| 61 | + |
| 62 | +static const std::unordered_set<std::string> c11_keywords_all = { |
| 63 | + C90_KEYWORDS, C99_KEYWORDS, C11_KEYWORDS |
| 64 | +}; |
| 65 | + |
| 66 | +static const std::unordered_set<std::string> c11_keywords = { |
| 67 | + C11_KEYWORDS |
| 68 | +}; |
| 69 | + |
| 70 | +/* |
| 71 | + static const std::unordered_set<std::string> c23_keywords_all = { |
| 72 | + C90_KEYWORDS, C99_KEYWORDS, C11_KEYWORDS, C23_KEYWORDS |
| 73 | + }; |
| 74 | +
|
| 75 | + static const std::unordered_set<std::string> c23_keywords = { |
| 76 | + C23_KEYWORDS |
| 77 | + }; |
| 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 | + "static_cast", "struct", "switch", "template", "this", "throw", \ |
| 90 | + "true", "try", "typedef", "typeid", "typename", "union", "unsigned", "using", \ |
| 91 | + "virtual", "void", "volatile", "wchar_t", "while", "xor", "xor_eq" |
| 92 | + |
| 93 | +#define CPP11_KEYWORDS \ |
| 94 | + "alignas", "alignof", "char16_t", "char32_t", "constexpr", "decltype", \ |
| 95 | + "noexcept", "nullptr", "static_assert", "thread_local" |
| 96 | + |
| 97 | +#define CPP20_KEYWORDS \ |
| 98 | + "concept", "consteval", "constinit", "co_await", \ |
| 99 | + "co_return", "co_yield", "requires" |
| 100 | + |
| 101 | +#ifdef __clang__ |
| 102 | +#pragma clang diagnostic push |
| 103 | +#pragma clang diagnostic ignored "-Wunused-macros" |
| 104 | +#endif |
| 105 | + |
| 106 | +#define CPP_TMTS_KEYWORDS \ |
| 107 | + "atomic_cancel", "atomic_commit", "atomic_noexcept", "synchronized" |
| 108 | + |
| 109 | +#define CPP_REFL_TS_KEYWORDS \ |
| 110 | + "reflexpr" |
| 111 | + |
| 112 | +#ifdef __clang__ |
| 113 | +#pragma clang diagnostic pop |
| 114 | +#endif |
| 115 | + |
| 116 | +static const std::unordered_set<std::string> cpp03_keywords_all = { |
| 117 | + CPP03_KEYWORDS |
| 118 | +}; |
| 119 | + |
| 120 | +static const std::unordered_set<std::string> cpp03_keywords = cpp03_keywords_all; |
| 121 | + |
| 122 | +static const std::unordered_set<std::string> cpp11_keywords_all = { |
| 123 | + CPP03_KEYWORDS, CPP11_KEYWORDS |
| 124 | +}; |
| 125 | + |
| 126 | +static const std::unordered_set<std::string> cpp11_keywords = { |
| 127 | + CPP11_KEYWORDS |
| 128 | +}; |
| 129 | + |
| 130 | +static const std::unordered_set<std::string> cpp14_keywords_all = cpp11_keywords_all; |
| 131 | + |
| 132 | +static const std::unordered_set<std::string> cpp14_keywords; |
| 133 | + |
| 134 | +static const std::unordered_set<std::string> cpp17_keywords_all = cpp11_keywords_all; |
| 135 | + |
| 136 | +static const std::unordered_set<std::string> cpp17_keywords; |
| 137 | + |
| 138 | +static const std::unordered_set<std::string> cpp20_keywords_all = { |
| 139 | + CPP03_KEYWORDS, CPP11_KEYWORDS, CPP20_KEYWORDS |
| 140 | +}; |
| 141 | + |
| 142 | +static const std::unordered_set<std::string> cpp20_keywords = { |
| 143 | + CPP20_KEYWORDS |
| 144 | +}; |
| 145 | + |
| 146 | +static const std::unordered_set<std::string> cpp23_keywords; |
| 147 | + |
| 148 | +static const std::unordered_set<std::string> cpp23_keywords_all = cpp20_keywords_all; |
| 149 | + |
| 150 | +// cppcheck-suppress unusedFunction |
| 151 | +const std::unordered_set<std::string>& Keywords::getAll(Standards::cstd_t cStd) |
| 152 | +{ |
| 153 | + // cppcheck-suppress missingReturn |
| 154 | + switch (cStd) { |
| 155 | + case Standards::cstd_t::C89: |
| 156 | + return c89_keywords_all; |
| 157 | + case Standards::cstd_t::C99: |
| 158 | + return c99_keywords_all; |
| 159 | + case Standards::cstd_t::C11: |
| 160 | + return c11_keywords_all; |
| 161 | + /*case Standards::cstd_t::C23: |
| 162 | + return c23_keywords_all;*/ |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +// cppcheck-suppress unusedFunction |
| 167 | +const std::unordered_set<std::string>& Keywords::getAll(Standards::cppstd_t cppStd) { |
| 168 | + // cppcheck-suppress missingReturn |
| 169 | + switch (cppStd) { |
| 170 | + case Standards::cppstd_t::CPP03: |
| 171 | + return cpp03_keywords_all; |
| 172 | + case Standards::cppstd_t::CPP11: |
| 173 | + return cpp11_keywords_all; |
| 174 | + case Standards::cppstd_t::CPP14: |
| 175 | + return cpp14_keywords_all; |
| 176 | + case Standards::cppstd_t::CPP17: |
| 177 | + return cpp17_keywords_all; |
| 178 | + case Standards::cppstd_t::CPP20: |
| 179 | + return cpp20_keywords_all; |
| 180 | + case Standards::cppstd_t::CPP23: |
| 181 | + return cpp23_keywords_all; |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +// cppcheck-suppress unusedFunction |
| 186 | +const std::unordered_set<std::string>& Keywords::getOnly(Standards::cstd_t cStd) |
| 187 | +{ |
| 188 | + // cppcheck-suppress missingReturn |
| 189 | + switch (cStd) { |
| 190 | + case Standards::cstd_t::C89: |
| 191 | + return c89_keywords; |
| 192 | + case Standards::cstd_t::C99: |
| 193 | + return c99_keywords; |
| 194 | + case Standards::cstd_t::C11: |
| 195 | + return c11_keywords; |
| 196 | + /*case Standards::cstd_t::C23: |
| 197 | + return c23_keywords_all;*/ |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +// cppcheck-suppress unusedFunction |
| 202 | +const std::unordered_set<std::string>& Keywords::getOnly(Standards::cppstd_t cppStd) |
| 203 | +{ |
| 204 | + // cppcheck-suppress missingReturn |
| 205 | + switch (cppStd) { |
| 206 | + case Standards::cppstd_t::CPP03: |
| 207 | + return cpp03_keywords; |
| 208 | + case Standards::cppstd_t::CPP11: |
| 209 | + return cpp11_keywords; |
| 210 | + case Standards::cppstd_t::CPP14: |
| 211 | + return cpp14_keywords; |
| 212 | + case Standards::cppstd_t::CPP17: |
| 213 | + return cpp17_keywords; |
| 214 | + case Standards::cppstd_t::CPP20: |
| 215 | + return cpp20_keywords; |
| 216 | + case Standards::cppstd_t::CPP23: |
| 217 | + return cpp23_keywords; |
| 218 | + } |
| 219 | +} |
| 220 | + |
0 commit comments