clang-tools 22.0.0git
CERTTidyModule.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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"
39#include "FloatLoopCounter.h"
41#include "MutatingCopyCheck.h"
45
46namespace {
47
48// Checked functions for cert-err33-c.
49// The following functions are deliberately excluded because they can be
50// called with NULL argument and in this case the check is not applicable:
51// `mblen, mbrlen, mbrtowc, mbtowc, wctomb, wctomb_s`.
52// FIXME: The check can be improved to handle such cases.
53const llvm::StringRef CertErr33CCheckedFunctions = "^::aligned_alloc$;"
54 "^::asctime_s$;"
55 "^::at_quick_exit$;"
56 "^::atexit$;"
57 "^::bsearch$;"
58 "^::bsearch_s$;"
59 "^::btowc$;"
60 "^::c16rtomb$;"
61 "^::c32rtomb$;"
62 "^::calloc$;"
63 "^::clock$;"
64 "^::cnd_broadcast$;"
65 "^::cnd_init$;"
66 "^::cnd_signal$;"
67 "^::cnd_timedwait$;"
68 "^::cnd_wait$;"
69 "^::ctime_s$;"
70 "^::fclose$;"
71 "^::fflush$;"
72 "^::fgetc$;"
73 "^::fgetpos$;"
74 "^::fgets$;"
75 "^::fgetwc$;"
76 "^::fopen$;"
77 "^::fopen_s$;"
78 "^::fprintf$;"
79 "^::fprintf_s$;"
80 "^::fputc$;"
81 "^::fputs$;"
82 "^::fputwc$;"
83 "^::fputws$;"
84 "^::fread$;"
85 "^::freopen$;"
86 "^::freopen_s$;"
87 "^::fscanf$;"
88 "^::fscanf_s$;"
89 "^::fseek$;"
90 "^::fsetpos$;"
91 "^::ftell$;"
92 "^::fwprintf$;"
93 "^::fwprintf_s$;"
94 "^::fwrite$;"
95 "^::fwscanf$;"
96 "^::fwscanf_s$;"
97 "^::getc$;"
98 "^::getchar$;"
99 "^::getenv$;"
100 "^::getenv_s$;"
101 "^::gets_s$;"
102 "^::getwc$;"
103 "^::getwchar$;"
104 "^::gmtime$;"
105 "^::gmtime_s$;"
106 "^::localtime$;"
107 "^::localtime_s$;"
108 "^::malloc$;"
109 "^::mbrtoc16$;"
110 "^::mbrtoc32$;"
111 "^::mbsrtowcs$;"
112 "^::mbsrtowcs_s$;"
113 "^::mbstowcs$;"
114 "^::mbstowcs_s$;"
115 "^::memchr$;"
116 "^::mktime$;"
117 "^::mtx_init$;"
118 "^::mtx_lock$;"
119 "^::mtx_timedlock$;"
120 "^::mtx_trylock$;"
121 "^::mtx_unlock$;"
122 "^::printf_s$;"
123 "^::putc$;"
124 "^::putwc$;"
125 "^::raise$;"
126 "^::realloc$;"
127 "^::remove$;"
128 "^::rename$;"
129 "^::scanf$;"
130 "^::scanf_s$;"
131 "^::setlocale$;"
132 "^::setvbuf$;"
133 "^::signal$;"
134 "^::snprintf$;"
135 "^::snprintf_s$;"
136 "^::sprintf$;"
137 "^::sprintf_s$;"
138 "^::sscanf$;"
139 "^::sscanf_s$;"
140 "^::strchr$;"
141 "^::strerror_s$;"
142 "^::strftime$;"
143 "^::strpbrk$;"
144 "^::strrchr$;"
145 "^::strstr$;"
146 "^::strtod$;"
147 "^::strtof$;"
148 "^::strtoimax$;"
149 "^::strtok$;"
150 "^::strtok_s$;"
151 "^::strtol$;"
152 "^::strtold$;"
153 "^::strtoll$;"
154 "^::strtoul$;"
155 "^::strtoull$;"
156 "^::strtoumax$;"
157 "^::strxfrm$;"
158 "^::swprintf$;"
159 "^::swprintf_s$;"
160 "^::swscanf$;"
161 "^::swscanf_s$;"
162 "^::thrd_create$;"
163 "^::thrd_detach$;"
164 "^::thrd_join$;"
165 "^::thrd_sleep$;"
166 "^::time$;"
167 "^::timespec_get$;"
168 "^::tmpfile$;"
169 "^::tmpfile_s$;"
170 "^::tmpnam$;"
171 "^::tmpnam_s$;"
172 "^::tss_create$;"
173 "^::tss_get$;"
174 "^::tss_set$;"
175 "^::ungetc$;"
176 "^::ungetwc$;"
177 "^::vfprintf$;"
178 "^::vfprintf_s$;"
179 "^::vfscanf$;"
180 "^::vfscanf_s$;"
181 "^::vfwprintf$;"
182 "^::vfwprintf_s$;"
183 "^::vfwscanf$;"
184 "^::vfwscanf_s$;"
185 "^::vprintf_s$;"
186 "^::vscanf$;"
187 "^::vscanf_s$;"
188 "^::vsnprintf$;"
189 "^::vsnprintf_s$;"
190 "^::vsprintf$;"
191 "^::vsprintf_s$;"
192 "^::vsscanf$;"
193 "^::vsscanf_s$;"
194 "^::vswprintf$;"
195 "^::vswprintf_s$;"
196 "^::vswscanf$;"
197 "^::vswscanf_s$;"
198 "^::vwprintf_s$;"
199 "^::vwscanf$;"
200 "^::vwscanf_s$;"
201 "^::wcrtomb$;"
202 "^::wcschr$;"
203 "^::wcsftime$;"
204 "^::wcspbrk$;"
205 "^::wcsrchr$;"
206 "^::wcsrtombs$;"
207 "^::wcsrtombs_s$;"
208 "^::wcsstr$;"
209 "^::wcstod$;"
210 "^::wcstof$;"
211 "^::wcstoimax$;"
212 "^::wcstok$;"
213 "^::wcstok_s$;"
214 "^::wcstol$;"
215 "^::wcstold$;"
216 "^::wcstoll$;"
217 "^::wcstombs$;"
218 "^::wcstombs_s$;"
219 "^::wcstoul$;"
220 "^::wcstoull$;"
221 "^::wcstoumax$;"
222 "^::wcsxfrm$;"
223 "^::wctob$;"
224 "^::wctrans$;"
225 "^::wctype$;"
226 "^::wmemchr$;"
227 "^::wprintf_s$;"
228 "^::wscanf$;"
229 "^::wscanf_s$;";
230
231} // namespace
232
233namespace clang::tidy {
234namespace cert {
235
237public:
238 void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
239 // C++ checkers
240 // CON
242 "cert-con54-cpp");
243 // CTR
244 CheckFactories
246 "cert-ctr56-cpp");
247 // DCL
249 "cert-dcl50-cpp");
251 "cert-dcl51-cpp");
253 "cert-dcl54-cpp");
254 CheckFactories.registerCheck<DontModifyStdNamespaceCheck>("cert-dcl58-cpp");
256 "cert-dcl59-cpp");
257 // ERR
259 "cert-err09-cpp");
261 "cert-err52-cpp");
263 "cert-err58-cpp");
264 CheckFactories.registerCheck<ThrownExceptionTypeCheck>("cert-err60-cpp");
266 "cert-err61-cpp");
267 // MEM
269 "cert-mem57-cpp");
270 // MSC
271 CheckFactories.registerCheck<LimitedRandomnessCheck>("cert-msc50-cpp");
273 "cert-msc51-cpp");
275 "cert-msc54-cpp");
276 // OOP
278 "cert-oop11-cpp");
280 "cert-oop54-cpp");
282 "cert-oop57-cpp");
283 CheckFactories.registerCheck<MutatingCopyCheck>("cert-oop58-cpp");
284
285 // C checkers
286 // ARR
288 "cert-arr39-c");
289 // CON
291 "cert-con36-c");
292 // DCL
293 CheckFactories.registerCheck<misc::StaticAssertCheck>("cert-dcl03-c");
295 "cert-dcl16-c");
297 "cert-dcl37-c");
298 // ENV
300 "cert-env33-c");
301 // ERR
303 "cert-err33-c");
304 CheckFactories
306 "cert-err34-c");
307 // EXP
309 "cert-exp42-c");
310 // FLP
311 CheckFactories.registerCheck<FloatLoopCounter>("cert-flp30-c");
313 "cert-flp37-c");
314 // FIO
315 CheckFactories.registerCheck<misc::NonCopyableObjectsCheck>("cert-fio38-c");
316 // INT
318 "cert-int09-c");
319 // MSC
321 "cert-msc24-c");
322 CheckFactories.registerCheck<LimitedRandomnessCheck>("cert-msc30-c");
324 "cert-msc32-c");
326 "cert-msc33-c");
327 // POS
329 "cert-pos44-c");
330 CheckFactories
332 "cert-pos47-c");
333 // SIG
334 CheckFactories.registerCheck<bugprone::SignalHandlerCheck>("cert-sig30-c");
335 // STR
337 "cert-str34-c");
338 }
339
341 ClangTidyOptions Options;
343 Opts["cert-arr39-c.WarnOnSizeOfConstant"] = "false";
344 Opts["cert-arr39-c.WarnOnSizeOfIntegerExpression"] = "false";
345 Opts["cert-arr39-c.WarnOnSizeOfThis"] = "false";
346 Opts["cert-arr39-c.WarnOnSizeOfCompareToConstant"] = "false";
347 Opts["cert-arr39-c.WarnOnSizeOfPointer"] = "false";
348 Opts["cert-arr39-c.WarnOnSizeOfPointerToAggregate"] = "false";
349 Opts["cert-dcl16-c.NewSuffixes"] = "L;LL;LU;LLU";
350 Opts["cert-err33-c.CheckedFunctions"] = CertErr33CCheckedFunctions;
351 Opts["cert-err33-c.AllowCastToVoid"] = "true";
352 Opts["cert-oop54-cpp.WarnOnlyIfThisHasSuspiciousField"] = "false";
353 Opts["cert-str34-c.DiagnoseSignedUnsignedCharComparisons"] = "false";
354 return Options;
355 }
356};
357
358} // namespace cert
359
360// Register the MiscTidyModule using this statically initialized variable.
361static ClangTidyModuleRegistry::Add<cert::CERTModule>
362 X("cert-module",
363 "Adds lint checks corresponding to CERT secure coding guidelines.");
364
365// This anchor is used to force the linker to link in the generated object file
366// and thus register the CERTModule.
367volatile int CERTModuleAnchorSource = 0; // NOLINT(misc-use-internal-linkage)
368
369} // namespace clang::tidy
A collection of ClangTidyCheckFactory instances.
void registerCheck(llvm::StringRef CheckName)
Registers the CheckType with the name Name.
A clang-tidy module groups a number of ClangTidyChecks and gives them a prefixed name.
Finds pthread_kill function calls when thread is terminated by / SIGTERM signal.
Execution of a command processor can lead to security vulnerabilities, and is generally not required.
Finds pointer arithmetic performed on classes that contain a virtual function.
Checks for usages of identifiers reserved for use by the implementation.
Checker for signal handler functions.
Finds those signed char -> integer conversions which might indicate a / programming error.
Find suspicious usages of sizeof expressions.
Finds cnd_wait, cnd_timedwait, wait, wait_for, or / wait_until function calls when the function is no...
Finds potentially incorrect calls to memcmp() based on properties of the arguments.
Checks whether the constructor for a static or thread_local object will throw.
Guards against use of string conversion functions that do not have reasonable error handling for conv...
Finds user-defined copy assignment operators which do not protect the code against self-assignment ei...
Checks for functions that have safer, more secure replacements available, or are considered deprecate...
Detects function calls where the return value is unused.
void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override
Implement this function in order to register all CheckFactories belonging to this module.
ClangTidyOptions getModuleOptions() override
Gets default options for checks defined in this module.
Checks if an object of type with extended alignment is allocated by using the default operator new.
Modification of the std or posix namespace can result in undefined behavior.
This check diagnoses when the loop induction expression of a for loop has floating-point type.
Pseudorandom number generators are not genuinely random.
Finds assignments to the copied object and its direct or indirect members in copy constructors and co...
Flags use of the C standard library functions 'memset', 'memcpy' and 'memcmp' and similar derivatives...
Checks whether a thrown object is nothrow copy constructible.
Finds pthread_setcanceltype function calls where a thread's cancellation type is set to asynchronous.
The check flags dereferences and non-pointer declarations of objects that are not meant to be passed ...
Replaces assert() with static_assert() if the condition is evaluatable at compile time.
Guards against use of setjmp/longjmp in C++ code.
Find all function definitions of C-style variadic functions.
The check flags user-defined move constructors that have a ctor-initializer initializing a member or ...
Enforces consistent style for enumerators' initialization, covering three styles: none,...
Detects when the integral literal or floating point literal has non-uppercase suffix,...
static ClangTidyModuleRegistry::Add< altera::AlteraModule > X("altera-module", "Adds Altera FPGA OpenCL lint checks.")
volatile int CERTModuleAnchorSource
Contains options for clang-tidy.
OptionMap CheckOptions
Key-value mapping used to store check-specific options.
llvm::StringMap< ClangTidyValue > OptionMap