• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- CppCoreGuidelinesTidyModule.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 "../misc/NonPrivateMemberVariablesInClassesCheck.h"
13 #include "../misc/UnconventionalAssignOperatorCheck.h"
14 #include "../modernize/AvoidCArraysCheck.h"
15 #include "../modernize/UseOverrideCheck.h"
16 #include "../readability/MagicNumbersCheck.h"
17 #include "AvoidGotoCheck.h"
18 #include "AvoidNonConstGlobalVariablesCheck.h"
19 #include "InitVariablesCheck.h"
20 #include "InterfacesGlobalInitCheck.h"
21 #include "MacroUsageCheck.h"
22 #include "NarrowingConversionsCheck.h"
23 #include "NoMallocCheck.h"
24 #include "OwningMemoryCheck.h"
25 #include "PreferMemberInitializerCheck.h"
26 #include "ProBoundsArrayToPointerDecayCheck.h"
27 #include "ProBoundsConstantArrayIndexCheck.h"
28 #include "ProBoundsPointerArithmeticCheck.h"
29 #include "ProTypeConstCastCheck.h"
30 #include "ProTypeCstyleCastCheck.h"
31 #include "ProTypeMemberInitCheck.h"
32 #include "ProTypeReinterpretCastCheck.h"
33 #include "ProTypeStaticCastDowncastCheck.h"
34 #include "ProTypeUnionAccessCheck.h"
35 #include "ProTypeVarargCheck.h"
36 #include "SlicingCheck.h"
37 #include "SpecialMemberFunctionsCheck.h"
38 
39 namespace clang {
40 namespace tidy {
41 namespace cppcoreguidelines {
42 
43 /// A module containing checks of the C++ Core Guidelines
44 class CppCoreGuidelinesModule : public ClangTidyModule {
45 public:
addCheckFactories(ClangTidyCheckFactories & CheckFactories)46   void addCheckFactories(ClangTidyCheckFactories &CheckFactories) override {
47     CheckFactories.registerCheck<modernize::AvoidCArraysCheck>(
48         "cppcoreguidelines-avoid-c-arrays");
49     CheckFactories.registerCheck<AvoidGotoCheck>(
50         "cppcoreguidelines-avoid-goto");
51     CheckFactories.registerCheck<readability::MagicNumbersCheck>(
52         "cppcoreguidelines-avoid-magic-numbers");
53     CheckFactories.registerCheck<AvoidNonConstGlobalVariablesCheck>(
54         "cppcoreguidelines-avoid-non-const-global-variables");
55     CheckFactories.registerCheck<modernize::UseOverrideCheck>(
56         "cppcoreguidelines-explicit-virtual-functions");
57     CheckFactories.registerCheck<InitVariablesCheck>(
58         "cppcoreguidelines-init-variables");
59     CheckFactories.registerCheck<InterfacesGlobalInitCheck>(
60         "cppcoreguidelines-interfaces-global-init");
61     CheckFactories.registerCheck<MacroUsageCheck>(
62         "cppcoreguidelines-macro-usage");
63     CheckFactories.registerCheck<NarrowingConversionsCheck>(
64         "cppcoreguidelines-narrowing-conversions");
65     CheckFactories.registerCheck<NoMallocCheck>("cppcoreguidelines-no-malloc");
66     CheckFactories.registerCheck<misc::NonPrivateMemberVariablesInClassesCheck>(
67         "cppcoreguidelines-non-private-member-variables-in-classes");
68     CheckFactories.registerCheck<OwningMemoryCheck>(
69         "cppcoreguidelines-owning-memory");
70     CheckFactories.registerCheck<PreferMemberInitializerCheck>(
71         "cppcoreguidelines-prefer-member-initializer");
72     CheckFactories.registerCheck<ProBoundsArrayToPointerDecayCheck>(
73         "cppcoreguidelines-pro-bounds-array-to-pointer-decay");
74     CheckFactories.registerCheck<ProBoundsConstantArrayIndexCheck>(
75         "cppcoreguidelines-pro-bounds-constant-array-index");
76     CheckFactories.registerCheck<ProBoundsPointerArithmeticCheck>(
77         "cppcoreguidelines-pro-bounds-pointer-arithmetic");
78     CheckFactories.registerCheck<ProTypeConstCastCheck>(
79         "cppcoreguidelines-pro-type-const-cast");
80     CheckFactories.registerCheck<ProTypeCstyleCastCheck>(
81         "cppcoreguidelines-pro-type-cstyle-cast");
82     CheckFactories.registerCheck<ProTypeMemberInitCheck>(
83         "cppcoreguidelines-pro-type-member-init");
84     CheckFactories.registerCheck<ProTypeReinterpretCastCheck>(
85         "cppcoreguidelines-pro-type-reinterpret-cast");
86     CheckFactories.registerCheck<ProTypeStaticCastDowncastCheck>(
87         "cppcoreguidelines-pro-type-static-cast-downcast");
88     CheckFactories.registerCheck<ProTypeUnionAccessCheck>(
89         "cppcoreguidelines-pro-type-union-access");
90     CheckFactories.registerCheck<ProTypeVarargCheck>(
91         "cppcoreguidelines-pro-type-vararg");
92     CheckFactories.registerCheck<SpecialMemberFunctionsCheck>(
93         "cppcoreguidelines-special-member-functions");
94     CheckFactories.registerCheck<SlicingCheck>("cppcoreguidelines-slicing");
95     CheckFactories.registerCheck<misc::UnconventionalAssignOperatorCheck>(
96         "cppcoreguidelines-c-copy-assignment-signature");
97   }
98 
getModuleOptions()99   ClangTidyOptions getModuleOptions() override {
100     ClangTidyOptions Options;
101     ClangTidyOptions::OptionMap &Opts = Options.CheckOptions;
102 
103     Opts["cppcoreguidelines-non-private-member-variables-in-classes."
104          "IgnoreClassesWithAllMemberVariablesBeingPublic"] = "1";
105 
106     Opts["cppcoreguidelines-explicit-virtual-functions."
107          "IgnoreDestructors"] = "1";
108 
109     return Options;
110   }
111 };
112 
113 // Register the LLVMTidyModule using this statically initialized variable.
114 static ClangTidyModuleRegistry::Add<CppCoreGuidelinesModule>
115     X("cppcoreguidelines-module", "Adds checks for the C++ Core Guidelines.");
116 
117 } // namespace cppcoreguidelines
118 
119 // This anchor is used to force the linker to link in the generated object file
120 // and thus register the CppCoreGuidelinesModule.
121 volatile int CppCoreGuidelinesModuleAnchorSource = 0;
122 
123 } // namespace tidy
124 } // namespace clang
125