CERTTidyModule.cpp 18 KB

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