aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/clang16/tools/extra/clang-tidy/cert/CERTTidyModule.cpp
blob: c3be9d66d090af59c4685396d663971778357f46 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
//===--- CERTTidyModule.cpp - clang-tidy ----------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "../ClangTidy.h"
#include "../ClangTidyModule.h"
#include "../ClangTidyModuleRegistry.h"
#include "../bugprone/BadSignalToKillThreadCheck.h"
#include "../bugprone/ReservedIdentifierCheck.h"
#include "../bugprone/SignalHandlerCheck.h"
#include "../bugprone/SignedCharMisuseCheck.h"
#include "../bugprone/SpuriouslyWakeUpFunctionsCheck.h"
#include "../bugprone/SuspiciousMemoryComparisonCheck.h"
#include "../bugprone/UnhandledSelfAssignmentCheck.h"
#include "../bugprone/UnusedReturnValueCheck.h"
#include "../concurrency/ThreadCanceltypeAsynchronousCheck.h"
#include "../google/UnnamedNamespaceInHeaderCheck.h"
#include "../misc/NewDeleteOverloadsCheck.h"
#include "../misc/NonCopyableObjects.h"
#include "../misc/StaticAssertCheck.h"
#include "../misc/ThrowByValueCatchByReferenceCheck.h"
#include "../performance/MoveConstructorInitCheck.h"
#include "../readability/UppercaseLiteralSuffixCheck.h"
#include "CommandProcessorCheck.h"
#include "DefaultOperatorNewAlignmentCheck.h"
#include "DontModifyStdNamespaceCheck.h"
#include "FloatLoopCounter.h"
#include "LimitedRandomnessCheck.h"
#include "MutatingCopyCheck.h"
#include "NonTrivialTypesLibcMemoryCallsCheck.h"
#include "PostfixOperatorCheck.h"
#include "ProperlySeededRandomGeneratorCheck.h"
#include "SetLongJmpCheck.h"
#include "StaticObjectExceptionCheck.h"
#include "StrToNumCheck.h"
#include "ThrownExceptionTypeCheck.h"
#include "VariadicFunctionDefCheck.h"

namespace {

// Checked functions for cert-err33-c.
// The following functions are deliberately excluded because they can be called
// with NULL argument and in this case the check is not applicable:
// `mblen, mbrlen, mbrtowc, mbtowc, wctomb, wctomb_s`.
// FIXME: The check can be improved to handle such cases.
const llvm::StringRef CertErr33CCheckedFunctions = "::aligned_alloc;"
                                                   "::asctime_s;"
                                                   "::at_quick_exit;"
                                                   "::atexit;"
                                                   "::bsearch;"
                                                   "::bsearch_s;"
                                                   "::btowc;"
                                                   "::c16rtomb;"
                                                   "::c32rtomb;"
                                                   "::calloc;"
                                                   "::clock;"
                                                   "::cnd_broadcast;"
                                                   "::cnd_init;"
                                                   "::cnd_signal;"
                                                   "::cnd_timedwait;"
                                                   "::cnd_wait;"
                                                   "::ctime_s;"
                                                   "::fclose;"
                                                   "::fflush;"
                                                   "::fgetc;"
                                                   "::fgetpos;"
                                                   "::fgets;"
                                                   "::fgetwc;"
                                                   "::fopen;"
                                                   "::fopen_s;"
                                                   "::fprintf;"
                                                   "::fprintf_s;"
                                                   "::fputc;"
                                                   "::fputs;"
                                                   "::fputwc;"
                                                   "::fputws;"
                                                   "::fread;"
                                                   "::freopen;"
                                                   "::freopen_s;"
                                                   "::fscanf;"
                                                   "::fscanf_s;"
                                                   "::fseek;"
                                                   "::fsetpos;"
                                                   "::ftell;"
                                                   "::fwprintf;"
                                                   "::fwprintf_s;"
                                                   "::fwrite;"
                                                   "::fwscanf;"
                                                   "::fwscanf_s;"
                                                   "::getc;"
                                                   "::getchar;"
                                                   "::getenv;"
                                                   "::getenv_s;"
                                                   "::gets_s;"
                                                   "::getwc;"
                                                   "::getwchar;"
                                                   "::gmtime;"
                                                   "::gmtime_s;"
                                                   "::localtime;"
                                                   "::localtime_s;"
                                                   "::malloc;"
                                                   "::mbrtoc16;"
                                                   "::mbrtoc32;"
                                                   "::mbsrtowcs;"
                                                   "::mbsrtowcs_s;"
                                                   "::mbstowcs;"
                                                   "::mbstowcs_s;"
                                                   "::memchr;"
                                                   "::mktime;"
                                                   "::mtx_init;"
                                                   "::mtx_lock;"
                                                   "::mtx_timedlock;"
                                                   "::mtx_trylock;"
                                                   "::mtx_unlock;"
                                                   "::printf_s;"
                                                   "::putc;"
                                                   "::putwc;"
                                                   "::raise;"
                                                   "::realloc;"
                                                   "::remove;"
                                                   "::rename;"
                                                   "::scanf;"
                                                   "::scanf_s;"
                                                   "::setlocale;"
                                                   "::setvbuf;"
                                                   "::signal;"
                                                   "::snprintf;"
                                                   "::snprintf_s;"
                                                   "::sprintf;"
                                                   "::sprintf_s;"
                                                   "::sscanf;"
                                                   "::sscanf_s;"
                                                   "::strchr;"
                                                   "::strerror_s;"
                                                   "::strftime;"
                                                   "::strpbrk;"
                                                   "::strrchr;"
                                                   "::strstr;"
                                                   "::strtod;"
                                                   "::strtof;"
                                                   "::strtoimax;"
                                                   "::strtok;"
                                                   "::strtok_s;"
                                                   "::strtol;"
                                                   "::strtold;"
                                                   "::strtoll;"
                                                   "::strtoul;"
                                                   "::strtoull;"
                                                   "::strtoumax;"
                                                   "::strxfrm;"
                                                   "::swprintf;"
                                                   "::swprintf_s;"
                                                   "::swscanf;"
                                                   "::swscanf_s;"
                                                   "::thrd_create;"
                                                   "::thrd_detach;"
                                                   "::thrd_join;"
                                                   "::thrd_sleep;"
                                                   "::time;"
                                                   "::timespec_get;"
                                                   "::tmpfile;"
                                                   "::tmpfile_s;"
                                                   "::tmpnam;"
                                                   "::tmpnam_s;"
                                                   "::tss_create;"
                                                   "::tss_get;"
                                                   "::tss_set;"
                                                   "::ungetc;"
                                                   "::ungetwc;"
                                                   "::vfprintf;"
                                                   "::vfprintf_s;"
                                                   "::vfscanf;"
                                                   "::vfscanf_s;"
                                                   "::vfwprintf;"
                                                   "::vfwprintf_s;"
                                                   "::vfwscanf;"
                                                   "::vfwscanf_s;"
                                                   "::vprintf_s;"
                                                   "::vscanf;"
                                                   "::vscanf_s;"
                                                   "::vsnprintf;"
                                                   "::vsnprintf_s;"
                                                   "::vsprintf;"
                                                   "::vsprintf_s;"
                                                   "::vsscanf;"
                                                   "::vsscanf_s;"
                                                   "::vswprintf;"
                                                   "::vswprintf_s;"
                                                   "::vswscanf;"
                                                   "::vswscanf_s;"
                                                   "::vwprintf_s;"
                                                   "::vwscanf;"
                                                   "::vwscanf_s;"
                                                   "::wcrtomb;"
                                                   "::wcschr;"
                                                   "::wcsftime;"
                                                   "::wcspbrk;"
                                                   "::wcsrchr;"
                                                   "::wcsrtombs;"
                                                   "::wcsrtombs_s;"
                                                   "::wcsstr;"
                                                   "::wcstod;"
                                                   "::wcstof;"
                                                   "::wcstoimax;"
                                                   "::wcstok;"
                                                   "::wcstok_s;"
                                                   "::wcstol;"
                                                   "::wcstold;"
                                                   "::wcstoll;"
                                                   "::wcstombs;"
                                                   "::wcstombs_s;"
                                                   "::wcstoul;"
                                                   "::wcstoull;"
                                                   "::wcstoumax;"
                                                   "::wcsxfrm;"
                                                   "::wctob;"
                                                   "::wctrans;"
                                                   "::wctype;"
                                                   "::wmemchr;"
                                                   "::wprintf_s;"
                                                   "::wscanf;"
                                                   "::wscanf_s;";

} // namespace

namespace clang::tidy {
namespace cert {

class CERTModule : public ClangTidyModule {
public:
  void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
    // C++ checkers
    // CON
    CheckFactories.registerCheck<bugprone::SpuriouslyWakeUpFunctionsCheck>(
        "cert-con54-cpp");
    // DCL
    CheckFactories.registerCheck<PostfixOperatorCheck>(
        "cert-dcl21-cpp");
    CheckFactories.registerCheck<VariadicFunctionDefCheck>("cert-dcl50-cpp");
    CheckFactories.registerCheck<bugprone::ReservedIdentifierCheck>(
        "cert-dcl51-cpp");
    CheckFactories.registerCheck<misc::NewDeleteOverloadsCheck>(
        "cert-dcl54-cpp");
    CheckFactories.registerCheck<DontModifyStdNamespaceCheck>(
        "cert-dcl58-cpp");
    CheckFactories.registerCheck<google::build::UnnamedNamespaceInHeaderCheck>(
        "cert-dcl59-cpp");
    // ERR
    CheckFactories.registerCheck<misc::ThrowByValueCatchByReferenceCheck>(
        "cert-err09-cpp");
    CheckFactories.registerCheck<SetLongJmpCheck>("cert-err52-cpp");
    CheckFactories.registerCheck<StaticObjectExceptionCheck>("cert-err58-cpp");
    CheckFactories.registerCheck<ThrownExceptionTypeCheck>("cert-err60-cpp");
    CheckFactories.registerCheck<misc::ThrowByValueCatchByReferenceCheck>(
        "cert-err61-cpp");
    // MEM
    CheckFactories.registerCheck<DefaultOperatorNewAlignmentCheck>(
        "cert-mem57-cpp");
    // MSC
    CheckFactories.registerCheck<LimitedRandomnessCheck>("cert-msc50-cpp");
    CheckFactories.registerCheck<ProperlySeededRandomGeneratorCheck>(
        "cert-msc51-cpp");
    CheckFactories.registerCheck<bugprone::SignalHandlerCheck>(
        "cert-msc54-cpp");
    // OOP
    CheckFactories.registerCheck<performance::MoveConstructorInitCheck>(
        "cert-oop11-cpp");
    CheckFactories.registerCheck<bugprone::UnhandledSelfAssignmentCheck>(
        "cert-oop54-cpp");
    CheckFactories.registerCheck<NonTrivialTypesLibcMemoryCallsCheck>(
        "cert-oop57-cpp");
    CheckFactories.registerCheck<MutatingCopyCheck>(
        "cert-oop58-cpp");

    // C checkers
    // CON
    CheckFactories.registerCheck<bugprone::SpuriouslyWakeUpFunctionsCheck>(
        "cert-con36-c");
    // DCL
    CheckFactories.registerCheck<misc::StaticAssertCheck>("cert-dcl03-c");
    CheckFactories.registerCheck<readability::UppercaseLiteralSuffixCheck>(
        "cert-dcl16-c");
    CheckFactories.registerCheck<bugprone::ReservedIdentifierCheck>(
        "cert-dcl37-c");
    // ENV
    CheckFactories.registerCheck<CommandProcessorCheck>("cert-env33-c");
    // ERR
    CheckFactories.registerCheck<bugprone::UnusedReturnValueCheck>(
        "cert-err33-c");
    CheckFactories.registerCheck<StrToNumCheck>("cert-err34-c");
    // EXP
    CheckFactories.registerCheck<bugprone::SuspiciousMemoryComparisonCheck>(
        "cert-exp42-c");
    // FLP
    CheckFactories.registerCheck<FloatLoopCounter>("cert-flp30-c");
    CheckFactories.registerCheck<bugprone::SuspiciousMemoryComparisonCheck>(
        "cert-flp37-c");
    // FIO
    CheckFactories.registerCheck<misc::NonCopyableObjectsCheck>("cert-fio38-c");
    // MSC
    CheckFactories.registerCheck<LimitedRandomnessCheck>("cert-msc30-c");
    CheckFactories.registerCheck<ProperlySeededRandomGeneratorCheck>(
        "cert-msc32-c");
    // POS
    CheckFactories.registerCheck<bugprone::BadSignalToKillThreadCheck>(
        "cert-pos44-c");
    CheckFactories
        .registerCheck<concurrency::ThreadCanceltypeAsynchronousCheck>(
            "cert-pos47-c");
    // SIG
    CheckFactories.registerCheck<bugprone::SignalHandlerCheck>("cert-sig30-c");
    // STR
    CheckFactories.registerCheck<bugprone::SignedCharMisuseCheck>(
        "cert-str34-c");
  }

  ClangTidyOptions getModuleOptions() override {
    ClangTidyOptions Options;
    ClangTidyOptions::OptionMap &Opts = Options.CheckOptions;
    Opts["cert-dcl16-c.NewSuffixes"] = "L;LL;LU;LLU";
    Opts["cert-err33-c.CheckedFunctions"] = CertErr33CCheckedFunctions;
    Opts["cert-oop54-cpp.WarnOnlyIfThisHasSuspiciousField"] = "false";
    Opts["cert-str34-c.DiagnoseSignedUnsignedCharComparisons"] = "false";
    return Options;
  }
};

} // namespace cert

// Register the MiscTidyModule using this statically initialized variable.
static ClangTidyModuleRegistry::Add<cert::CERTModule>
    X("cert-module",
      "Adds lint checks corresponding to CERT secure coding guidelines.");

// This anchor is used to force the linker to link in the generated object file
// and thus register the CERTModule.
volatile int CERTModuleAnchorSource = 0;

} // namespace clang::tidy