Skip to content

Commit 674231a

Browse files
authored
added clean C/C++ keyword lists and use them in TokenList (#3774)
1 parent da09a92 commit 674231a

7 files changed

Lines changed: 293 additions & 91 deletions

File tree

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ LIBOBJ = $(libcppdir)/analyzerinfo.o \
227227
$(libcppdir)/fwdanalysis.o \
228228
$(libcppdir)/importproject.o \
229229
$(libcppdir)/infer.o \
230+
$(libcppdir)/keywords.o \
230231
$(libcppdir)/library.o \
231232
$(libcppdir)/mathlib.o \
232233
$(libcppdir)/path.o \
@@ -564,6 +565,9 @@ $(libcppdir)/importproject.o: lib/importproject.cpp externals/picojson/picojson.
564565
$(libcppdir)/infer.o: lib/infer.cpp lib/calculate.h lib/config.h lib/errortypes.h lib/infer.h lib/mathlib.h lib/valueptr.h lib/vfvalue.h
565566
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/infer.cpp
566567

568+
$(libcppdir)/keywords.o: lib/keywords.cpp lib/config.h lib/keywords.h lib/standards.h lib/utils.h
569+
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/keywords.cpp
570+
567571
$(libcppdir)/library.o: lib/library.cpp externals/tinyxml2/tinyxml2.h lib/astutils.h lib/config.h lib/errortypes.h lib/library.h lib/mathlib.h lib/path.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/symboldatabase.h lib/templatesimplifier.h lib/token.h lib/tokenlist.h lib/utils.h lib/valueflow.h lib/vfvalue.h
568572
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/library.cpp
569573

@@ -615,7 +619,7 @@ $(libcppdir)/token.o: lib/token.cpp lib/astutils.h lib/config.h lib/errortypes.h
615619
$(libcppdir)/tokenize.o: lib/tokenize.cpp externals/simplecpp/simplecpp.h lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/preprocessor.h lib/settings.h lib/sourcelocation.h lib/standards.h lib/summaries.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/valueflow.h lib/vfvalue.h
616620
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/tokenize.cpp
617621

618-
$(libcppdir)/tokenlist.o: lib/tokenlist.cpp externals/simplecpp/simplecpp.h lib/astutils.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenlist.h lib/utils.h lib/vfvalue.h
622+
$(libcppdir)/tokenlist.o: lib/tokenlist.cpp externals/simplecpp/simplecpp.h lib/astutils.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/keywords.h lib/library.h lib/mathlib.h lib/path.h lib/platform.h lib/settings.h lib/smallvector.h lib/sourcelocation.h lib/standards.h lib/suppressions.h lib/symboldatabase.h lib/templatesimplifier.h lib/timer.h lib/token.h lib/tokenlist.h lib/utils.h lib/vfvalue.h
619623
$(CXX) ${INCLUDE_FOR_LIB} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $(libcppdir)/tokenlist.cpp
620624

621625
$(libcppdir)/utils.o: lib/utils.cpp lib/config.h lib/utils.h

lib/cppcheck.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
<ClCompile Include="fwdanalysis.cpp" />
8484
<ClCompile Include="importproject.cpp" />
8585
<ClCompile Include="infer.cpp" />
86+
<ClCompile Include="keywords.cpp" />
8687
<ClCompile Include="library.cpp" />
8788
<ClCompile Include="mathlib.cpp" />
8889
<ClCompile Include="path.cpp" />
@@ -149,6 +150,7 @@
149150
<ClInclude Include="fwdanalysis.h" />
150151
<ClInclude Include="importproject.h" />
151152
<ClInclude Include="infer.h" />
153+
<ClInclude Include="keywords.h" />
152154
<ClInclude Include="library.h" />
153155
<ClInclude Include="mathlib.h" />
154156
<ClInclude Include="path.h" />

lib/keywords.cpp

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
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+

lib/keywords.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
#ifndef keywordsH
20+
#define keywordsH
21+
22+
#include "standards.h"
23+
24+
#include <string>
25+
#include <unordered_set>
26+
27+
class Keywords
28+
{
29+
public:
30+
static const std::unordered_set<std::string>& getAll(Standards::cstd_t cStd);
31+
static const std::unordered_set<std::string>& getAll(Standards::cppstd_t cppStd);
32+
33+
static const std::unordered_set<std::string>& getOnly(Standards::cstd_t cStd);
34+
static const std::unordered_set<std::string>& getOnly(Standards::cppstd_t cppStd);
35+
};
36+
37+
#endif

lib/lib.pri

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ HEADERS += $${PWD}/analyzer.h \
4444
$${PWD}/fwdanalysis.h \
4545
$${PWD}/importproject.h \
4646
$${PWD}/infer.h \
47+
$${PWD}/keywords.h \
4748
$${PWD}/library.h \
4849
$${PWD}/mathlib.h \
4950
$${PWD}/path.h \
@@ -110,6 +111,7 @@ SOURCES += $${PWD}/analyzerinfo.cpp \
110111
$${PWD}/fwdanalysis.cpp \
111112
$${PWD}/importproject.cpp \
112113
$${PWD}/infer.cpp \
114+
$${PWD}/keywords.cpp \
113115
$${PWD}/library.cpp \
114116
$${PWD}/mathlib.cpp \
115117
$${PWD}/path.cpp \

0 commit comments

Comments
 (0)