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/SignedCharMisuseCheck.h"
15 #include "../bugprone/SpuriouslyWakeUpFunctionsCheck.h"
16 #include "../bugprone/UnhandledSelfAssignmentCheck.h"
17 #include "../google/UnnamedNamespaceInHeaderCheck.h"
18 #include "../misc/NewDeleteOverloadsCheck.h"
19 #include "../misc/NonCopyableObjects.h"
20 #include "../misc/StaticAssertCheck.h"
21 #include "../misc/ThrowByValueCatchByReferenceCheck.h"
22 #include "../performance/MoveConstructorInitCheck.h"
23 #include "../readability/UppercaseLiteralSuffixCheck.h"
24 #include "CommandProcessorCheck.h"
25 #include "DefaultOperatorNewAlignmentCheck.h"
26 #include "DontModifyStdNamespaceCheck.h"
27 #include "FloatLoopCounter.h"
28 #include "LimitedRandomnessCheck.h"
29 #include "MutatingCopyCheck.h"
30 #include "NonTrivialTypesLibcMemoryCallsCheck.h"
31 #include "PostfixOperatorCheck.h"
32 #include "ProperlySeededRandomGeneratorCheck.h"
33 #include "SetLongJmpCheck.h"
34 #include "StaticObjectExceptionCheck.h"
35 #include "StrToNumCheck.h"
36 #include "ThrownExceptionTypeCheck.h"
37 #include "VariadicFunctionDefCheck.h"
38 
39 namespace clang {
40 namespace tidy {
41 namespace cert {
42 
43 class CERTModule : public ClangTidyModule {
44 public:
addCheckFactories(ClangTidyCheckFactories & CheckFactories)45   void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
46     // C++ checkers
47     // CON
48     CheckFactories.registerCheck<bugprone::SpuriouslyWakeUpFunctionsCheck>(
49         "cert-con54-cpp");
50     // DCL
51     CheckFactories.registerCheck<PostfixOperatorCheck>(
52         "cert-dcl21-cpp");
53     CheckFactories.registerCheck<VariadicFunctionDefCheck>("cert-dcl50-cpp");
54     CheckFactories.registerCheck<bugprone::ReservedIdentifierCheck>(
55         "cert-dcl51-cpp");
56     CheckFactories.registerCheck<misc::NewDeleteOverloadsCheck>(
57         "cert-dcl54-cpp");
58     CheckFactories.registerCheck<DontModifyStdNamespaceCheck>(
59         "cert-dcl58-cpp");
60     CheckFactories.registerCheck<google::build::UnnamedNamespaceInHeaderCheck>(
61         "cert-dcl59-cpp");
62     // ERR
63     CheckFactories.registerCheck<misc::ThrowByValueCatchByReferenceCheck>(
64         "cert-err09-cpp");
65     CheckFactories.registerCheck<SetLongJmpCheck>("cert-err52-cpp");
66     CheckFactories.registerCheck<StaticObjectExceptionCheck>("cert-err58-cpp");
67     CheckFactories.registerCheck<ThrownExceptionTypeCheck>("cert-err60-cpp");
68     CheckFactories.registerCheck<misc::ThrowByValueCatchByReferenceCheck>(
69         "cert-err61-cpp");
70     // MEM
71     CheckFactories.registerCheck<DefaultOperatorNewAlignmentCheck>(
72         "cert-mem57-cpp");
73     // MSC
74     CheckFactories.registerCheck<LimitedRandomnessCheck>("cert-msc50-cpp");
75     CheckFactories.registerCheck<ProperlySeededRandomGeneratorCheck>(
76         "cert-msc51-cpp");
77     // OOP
78     CheckFactories.registerCheck<performance::MoveConstructorInitCheck>(
79         "cert-oop11-cpp");
80     CheckFactories.registerCheck<bugprone::UnhandledSelfAssignmentCheck>(
81         "cert-oop54-cpp");
82     CheckFactories.registerCheck<NonTrivialTypesLibcMemoryCallsCheck>(
83         "cert-oop57-cpp");
84     CheckFactories.registerCheck<MutatingCopyCheck>(
85         "cert-oop58-cpp");
86 
87     // C checkers
88     // CON
89     CheckFactories.registerCheck<bugprone::SpuriouslyWakeUpFunctionsCheck>(
90         "cert-con36-c");
91     // DCL
92     CheckFactories.registerCheck<misc::StaticAssertCheck>("cert-dcl03-c");
93     CheckFactories.registerCheck<readability::UppercaseLiteralSuffixCheck>(
94         "cert-dcl16-c");
95     CheckFactories.registerCheck<bugprone::ReservedIdentifierCheck>(
96         "cert-dcl37-c");
97     // ENV
98     CheckFactories.registerCheck<CommandProcessorCheck>("cert-env33-c");
99     // FLP
100     CheckFactories.registerCheck<FloatLoopCounter>("cert-flp30-c");
101     // FIO
102     CheckFactories.registerCheck<misc::NonCopyableObjectsCheck>("cert-fio38-c");
103     // ERR
104     CheckFactories.registerCheck<StrToNumCheck>("cert-err34-c");
105     // MSC
106     CheckFactories.registerCheck<LimitedRandomnessCheck>("cert-msc30-c");
107     CheckFactories.registerCheck<ProperlySeededRandomGeneratorCheck>(
108         "cert-msc32-c");
109     // POS
110     CheckFactories.registerCheck<bugprone::BadSignalToKillThreadCheck>(
111         "cert-pos44-c");
112     // STR
113     CheckFactories.registerCheck<bugprone::SignedCharMisuseCheck>(
114         "cert-str34-c");
115   }
116 
getModuleOptions()117   ClangTidyOptions getModuleOptions() override {
118     ClangTidyOptions Options;
119     ClangTidyOptions::OptionMap &Opts = Options.CheckOptions;
120     Opts["cert-dcl16-c.NewSuffixes"] = "L;LL;LU;LLU";
121     Opts["cert-oop54-cpp.WarnOnlyIfThisHasSuspiciousField"] = "0";
122     Opts["cert-str34-c.DiagnoseSignedUnsignedCharComparisons"] = "0";
123     return Options;
124   }
125 };
126 
127 } // namespace cert
128 
129 // Register the MiscTidyModule using this statically initialized variable.
130 static ClangTidyModuleRegistry::Add<cert::CERTModule>
131     X("cert-module",
132       "Adds lint checks corresponding to CERT secure coding guidelines.");
133 
134 // This anchor is used to force the linker to link in the generated object file
135 // and thus register the CERTModule.
136 volatile int CERTModuleAnchorSource = 0;
137 
138 } // namespace tidy
139 } // namespace clang
140