1//===--- CERTTidyModule.cpp - clang-tidy ----------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "../ClangTidy.h"
10#include "../ClangTidyModule.h"
11#include "../ClangTidyModuleRegistry.h"
12#include "../bugprone/BadSignalToKillThreadCheck.h"
13#include "../bugprone/ReservedIdentifierCheck.h"
14#include "../bugprone/SignalHandlerCheck.h"
15#include "../bugprone/SignedCharMisuseCheck.h"
16#include "../bugprone/SpuriouslyWakeUpFunctionsCheck.h"
17#include "../bugprone/SuspiciousMemoryComparisonCheck.h"
18#include "../bugprone/UnhandledSelfAssignmentCheck.h"
19#include "../bugprone/UnsafeFunctionsCheck.h"
20#include "../bugprone/UnusedReturnValueCheck.h"
21#include "../concurrency/ThreadCanceltypeAsynchronousCheck.h"
22#include "../google/UnnamedNamespaceInHeaderCheck.h"
23#include "../misc/NewDeleteOverloadsCheck.h"
24#include "../misc/NonCopyableObjects.h"
25#include "../misc/StaticAssertCheck.h"
26#include "../misc/ThrowByValueCatchByReferenceCheck.h"
27#include "../performance/MoveConstructorInitCheck.h"
28#include "../readability/UppercaseLiteralSuffixCheck.h"
29#include "CommandProcessorCheck.h"
30#include "DefaultOperatorNewAlignmentCheck.h"
31#include "DontModifyStdNamespaceCheck.h"
32#include "FloatLoopCounter.h"
33#include "LimitedRandomnessCheck.h"
34#include "MutatingCopyCheck.h"
35#include "NonTrivialTypesLibcMemoryCallsCheck.h"
36#include "ProperlySeededRandomGeneratorCheck.h"
37#include "SetLongJmpCheck.h"
38#include "StaticObjectExceptionCheck.h"
39#include "StrToNumCheck.h"
40#include "ThrownExceptionTypeCheck.h"
41#include "VariadicFunctionDefCheck.h"
42
43namespace {
44
45// Checked functions for cert-err33-c.
46// The following functions are deliberately excluded because they can be called
47// with NULL argument and in this case the check is not applicable:
48// `mblen, mbrlen, mbrtowc, mbtowc, wctomb, wctomb_s`.
49// FIXME: The check can be improved to handle such cases.
50const llvm::StringRef CertErr33CCheckedFunctions = "::aligned_alloc;"
51 "::asctime_s;"
52 "::at_quick_exit;"
53 "::atexit;"
54 "::bsearch;"
55 "::bsearch_s;"
56 "::btowc;"
57 "::c16rtomb;"
58 "::c32rtomb;"
59 "::calloc;"
60 "::clock;"
61 "::cnd_broadcast;"
62 "::cnd_init;"
63 "::cnd_signal;"
64 "::cnd_timedwait;"
65 "::cnd_wait;"
66 "::ctime_s;"
67 "::fclose;"
68 "::fflush;"
69 "::fgetc;"
70 "::fgetpos;"
71 "::fgets;"
72 "::fgetwc;"
73 "::fopen;"
74 "::fopen_s;"
75 "::fprintf;"
76 "::fprintf_s;"
77 "::fputc;"
78 "::fputs;"
79 "::fputwc;"
80 "::fputws;"
81 "::fread;"
82 "::freopen;"
83 "::freopen_s;"
84 "::fscanf;"
85 "::fscanf_s;"
86 "::fseek;"
87 "::fsetpos;"
88 "::ftell;"
89 "::fwprintf;"
90 "::fwprintf_s;"
91 "::fwrite;"
92 "::fwscanf;"
93 "::fwscanf_s;"
94 "::getc;"
95 "::getchar;"
96 "::getenv;"
97 "::getenv_s;"
98 "::gets_s;"
99 "::getwc;"
100 "::getwchar;"
101 "::gmtime;"
102 "::gmtime_s;"
103 "::localtime;"
104 "::localtime_s;"
105 "::malloc;"
106 "::mbrtoc16;"
107 "::mbrtoc32;"
108 "::mbsrtowcs;"
109 "::mbsrtowcs_s;"
110 "::mbstowcs;"
111 "::mbstowcs_s;"
112 "::memchr;"
113 "::mktime;"
114 "::mtx_init;"
115 "::mtx_lock;"
116 "::mtx_timedlock;"
117 "::mtx_trylock;"
118 "::mtx_unlock;"
119 "::printf_s;"
120 "::putc;"
121 "::putwc;"
122 "::raise;"
123 "::realloc;"
124 "::remove;"
125 "::rename;"
126 "::scanf;"
127 "::scanf_s;"
128 "::setlocale;"
129 "::setvbuf;"
130 "::signal;"
131 "::snprintf;"
132 "::snprintf_s;"
133 "::sprintf;"
134 "::sprintf_s;"
135 "::sscanf;"
136 "::sscanf_s;"
137 "::strchr;"
138 "::strerror_s;"
139 "::strftime;"
140 "::strpbrk;"
141 "::strrchr;"
142 "::strstr;"
143 "::strtod;"
144 "::strtof;"
145 "::strtoimax;"
146 "::strtok;"
147 "::strtok_s;"
148 "::strtol;"
149 "::strtold;"
150 "::strtoll;"
151 "::strtoul;"
152 "::strtoull;"
153 "::strtoumax;"
154 "::strxfrm;"
155 "::swprintf;"
156 "::swprintf_s;"
157 "::swscanf;"
158 "::swscanf_s;"
159 "::thrd_create;"
160 "::thrd_detach;"
161 "::thrd_join;"
162 "::thrd_sleep;"
163 "::time;"
164 "::timespec_get;"
165 "::tmpfile;"
166 "::tmpfile_s;"
167 "::tmpnam;"
168 "::tmpnam_s;"
169 "::tss_create;"
170 "::tss_get;"
171 "::tss_set;"
172 "::ungetc;"
173 "::ungetwc;"
174 "::vfprintf;"
175 "::vfprintf_s;"
176 "::vfscanf;"
177 "::vfscanf_s;"
178 "::vfwprintf;"
179 "::vfwprintf_s;"
180 "::vfwscanf;"
181 "::vfwscanf_s;"
182 "::vprintf_s;"
183 "::vscanf;"
184 "::vscanf_s;"
185 "::vsnprintf;"
186 "::vsnprintf_s;"
187 "::vsprintf;"
188 "::vsprintf_s;"
189 "::vsscanf;"
190 "::vsscanf_s;"
191 "::vswprintf;"
192 "::vswprintf_s;"
193 "::vswscanf;"
194 "::vswscanf_s;"
195 "::vwprintf_s;"
196 "::vwscanf;"
197 "::vwscanf_s;"
198 "::wcrtomb;"
199 "::wcschr;"
200 "::wcsftime;"
201 "::wcspbrk;"
202 "::wcsrchr;"
203 "::wcsrtombs;"
204 "::wcsrtombs_s;"
205 "::wcsstr;"
206 "::wcstod;"
207 "::wcstof;"
208 "::wcstoimax;"
209 "::wcstok;"
210 "::wcstok_s;"
211 "::wcstol;"
212 "::wcstold;"
213 "::wcstoll;"
214 "::wcstombs;"
215 "::wcstombs_s;"
216 "::wcstoul;"
217 "::wcstoull;"
218 "::wcstoumax;"
219 "::wcsxfrm;"
220 "::wctob;"
221 "::wctrans;"
222 "::wctype;"
223 "::wmemchr;"
224 "::wprintf_s;"
225 "::wscanf;"
226 "::wscanf_s;";
227
228} // namespace
229
230namespace clang::tidy {
231namespace cert {
232
233class CERTModule : public ClangTidyModule {
234public:
235 void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
236 // C++ checkers
237 // CON
238 CheckFactories.registerCheck<bugprone::SpuriouslyWakeUpFunctionsCheck>(
239 CheckName: "cert-con54-cpp");
240 // DCL
241 CheckFactories.registerCheck<VariadicFunctionDefCheck>(CheckName: "cert-dcl50-cpp");
242 CheckFactories.registerCheck<bugprone::ReservedIdentifierCheck>(
243 CheckName: "cert-dcl51-cpp");
244 CheckFactories.registerCheck<misc::NewDeleteOverloadsCheck>(
245 CheckName: "cert-dcl54-cpp");
246 CheckFactories.registerCheck<DontModifyStdNamespaceCheck>(
247 CheckName: "cert-dcl58-cpp");
248 CheckFactories.registerCheck<google::build::UnnamedNamespaceInHeaderCheck>(
249 CheckName: "cert-dcl59-cpp");
250 // ERR
251 CheckFactories.registerCheck<misc::ThrowByValueCatchByReferenceCheck>(
252 CheckName: "cert-err09-cpp");
253 CheckFactories.registerCheck<SetLongJmpCheck>(CheckName: "cert-err52-cpp");
254 CheckFactories.registerCheck<StaticObjectExceptionCheck>(CheckName: "cert-err58-cpp");
255 CheckFactories.registerCheck<ThrownExceptionTypeCheck>(CheckName: "cert-err60-cpp");
256 CheckFactories.registerCheck<misc::ThrowByValueCatchByReferenceCheck>(
257 CheckName: "cert-err61-cpp");
258 // MEM
259 CheckFactories.registerCheck<DefaultOperatorNewAlignmentCheck>(
260 CheckName: "cert-mem57-cpp");
261 // MSC
262 CheckFactories.registerCheck<LimitedRandomnessCheck>(CheckName: "cert-msc50-cpp");
263 CheckFactories.registerCheck<ProperlySeededRandomGeneratorCheck>(
264 CheckName: "cert-msc51-cpp");
265 CheckFactories.registerCheck<bugprone::SignalHandlerCheck>(
266 CheckName: "cert-msc54-cpp");
267 // OOP
268 CheckFactories.registerCheck<performance::MoveConstructorInitCheck>(
269 CheckName: "cert-oop11-cpp");
270 CheckFactories.registerCheck<bugprone::UnhandledSelfAssignmentCheck>(
271 CheckName: "cert-oop54-cpp");
272 CheckFactories.registerCheck<NonTrivialTypesLibcMemoryCallsCheck>(
273 CheckName: "cert-oop57-cpp");
274 CheckFactories.registerCheck<MutatingCopyCheck>(
275 CheckName: "cert-oop58-cpp");
276
277 // C checkers
278 // CON
279 CheckFactories.registerCheck<bugprone::SpuriouslyWakeUpFunctionsCheck>(
280 CheckName: "cert-con36-c");
281 // DCL
282 CheckFactories.registerCheck<misc::StaticAssertCheck>(CheckName: "cert-dcl03-c");
283 CheckFactories.registerCheck<readability::UppercaseLiteralSuffixCheck>(
284 CheckName: "cert-dcl16-c");
285 CheckFactories.registerCheck<bugprone::ReservedIdentifierCheck>(
286 CheckName: "cert-dcl37-c");
287 // ENV
288 CheckFactories.registerCheck<CommandProcessorCheck>(CheckName: "cert-env33-c");
289 // ERR
290 CheckFactories.registerCheck<bugprone::UnusedReturnValueCheck>(
291 CheckName: "cert-err33-c");
292 CheckFactories.registerCheck<StrToNumCheck>(CheckName: "cert-err34-c");
293 // EXP
294 CheckFactories.registerCheck<bugprone::SuspiciousMemoryComparisonCheck>(
295 CheckName: "cert-exp42-c");
296 // FLP
297 CheckFactories.registerCheck<FloatLoopCounter>(CheckName: "cert-flp30-c");
298 CheckFactories.registerCheck<bugprone::SuspiciousMemoryComparisonCheck>(
299 CheckName: "cert-flp37-c");
300 // FIO
301 CheckFactories.registerCheck<misc::NonCopyableObjectsCheck>(CheckName: "cert-fio38-c");
302 // MSC
303 CheckFactories.registerCheck<bugprone::UnsafeFunctionsCheck>(
304 CheckName: "cert-msc24-c");
305 CheckFactories.registerCheck<LimitedRandomnessCheck>(CheckName: "cert-msc30-c");
306 CheckFactories.registerCheck<ProperlySeededRandomGeneratorCheck>(
307 CheckName: "cert-msc32-c");
308 CheckFactories.registerCheck<bugprone::UnsafeFunctionsCheck>(
309 CheckName: "cert-msc33-c");
310 // POS
311 CheckFactories.registerCheck<bugprone::BadSignalToKillThreadCheck>(
312 CheckName: "cert-pos44-c");
313 CheckFactories
314 .registerCheck<concurrency::ThreadCanceltypeAsynchronousCheck>(
315 CheckName: "cert-pos47-c");
316 // SIG
317 CheckFactories.registerCheck<bugprone::SignalHandlerCheck>(CheckName: "cert-sig30-c");
318 // STR
319 CheckFactories.registerCheck<bugprone::SignedCharMisuseCheck>(
320 CheckName: "cert-str34-c");
321 }
322
323 ClangTidyOptions getModuleOptions() override {
324 ClangTidyOptions Options;
325 ClangTidyOptions::OptionMap &Opts = Options.CheckOptions;
326 Opts["cert-dcl16-c.NewSuffixes"] = "L;LL;LU;LLU";
327 Opts["cert-err33-c.CheckedFunctions"] = CertErr33CCheckedFunctions;
328 Opts["cert-err33-c.AllowCastToVoid"] = "true";
329 Opts["cert-oop54-cpp.WarnOnlyIfThisHasSuspiciousField"] = "false";
330 Opts["cert-str34-c.DiagnoseSignedUnsignedCharComparisons"] = "false";
331 return Options;
332 }
333};
334
335} // namespace cert
336
337// Register the MiscTidyModule using this statically initialized variable.
338static ClangTidyModuleRegistry::Add<cert::CERTModule>
339 X("cert-module",
340 "Adds lint checks corresponding to CERT secure coding guidelines.");
341
342// This anchor is used to force the linker to link in the generated object file
343// and thus register the CERTModule.
344volatile int CERTModuleAnchorSource = 0;
345
346} // namespace clang::tidy
347

source code of clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp