Debian Codegraph @

src/ClazyStandaloneMain.cpp

raw
1 /*
2 SPDX-FileCopyrightText: 2017 Sergio Martins <[email protected]>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7 // clazy:excludeall=non-pod-global-static
8
9 #include "Clazy.h"
10 #include "ClazyContext.h"
11
12 #include "checks.json.h"
13
14 #include <clang/Tooling/CommonOptionsParser.h>
15 #include <clang/Tooling/Tooling.h>
16 #include <llvm/ADT/ArrayRef.h>
17 #include <llvm/ADT/StringRef.h>
18 #include <llvm/Support/CommandLine.h>
19
20 #include <iostream>
21 #include <string>
22
23 namespace clang
24 {
25 class FrontendAction;
26 } // namespace clang
27
28 using namespace clang;
29 using namespace clang::tooling;
30 using namespace llvm;
31
32 static llvm::cl::OptionCategory s_clazyCategory("clazy options");
33 static cl::opt<std::string> s_checks("checks", cl::desc("Comma-separated list of clazy checks. Default is level1"), cl::init(""), cl::cat(s_clazyCategory));
34
35 static cl::opt<std::string>
36 s_exportFixes("export-fixes",
37 cl::desc("YAML file to store suggested fixes in. The stored fixes can be applied to the input source code with clang-apply-replacements."),
38 cl::init(""),
39 cl::cat(s_clazyCategory));
40
41 static cl::opt<bool> s_onlyQt("only-qt",
42 cl::desc("Won't emit warnings for non-Qt files, or in other words, if -DQT_CORE_LIB is missing."),
43 cl::init(false),
44 cl::cat(s_clazyCategory));
45
46 static cl::opt<bool> s_qtDeveloper("qt-developer",
47 cl::desc("For running clazy on Qt itself, optional, but honours specific guidelines"),
48 cl::init(false),
49 cl::cat(s_clazyCategory));
50
51 static cl::opt<bool> s_visitImplicitCode(
52 "visit-implicit-code",
53 cl::desc(
54 "For visiting implicit code like compiler generated constructors. None of the built-in checks benefit from this, but can be useful for custom checks"),
55 cl::init(false),
56 cl::cat(s_clazyCategory));
57
58 static cl::opt<bool> s_ignoreIncludedFiles("ignore-included-files",
59 cl::desc("Only emit warnings for the current file being compiled and ignore any includes. "
60 "Useful for performance reasons. Have a look at a check's README*.md file to see "
61 "if it supports this feature or not."),
62 cl::init(false),
63 cl::cat(s_clazyCategory));
64
65 static cl::opt<std::string> s_headerFilter("header-filter",
66 cl::desc(R"(Regular expression matching the names of the
67 headers to output diagnostics from. Diagnostics
68 from the main file of each translation unit are
69 always displayed.)"),
70 cl::init(""),
71 cl::cat(s_clazyCategory));
72
73 static cl::opt<std::string> s_ignoreDirs("ignore-dirs",
74 cl::desc(R"(Regular expression matching the names of the
75 directories for which diagnostics should never be emitted. Useful for ignoring 3rdparty code.)"),
76 cl::init(""),
77 cl::cat(s_clazyCategory));
78
79 static cl::opt<bool> s_supportedChecks("supported-checks-json",
80 cl::desc("Dump meta information about supported checks in JSON format."),
81 cl::init(false),
82 cl::cat(s_clazyCategory));
83
84 static cl::opt<bool> s_listEnabledChecks("list-checks", cl::desc("List all enabled checks and exit."), cl::init(false), cl::cat(s_clazyCategory));
85
86 static cl::opt<std::string> s_vfsoverlay("vfsoverlay",
87 cl::desc("YAML file to overlay the virtual filesystem described by file over the real file system."),
88 cl::init(""),
89 cl::cat(s_clazyCategory));
90
91 static cl::extrahelp s_commonHelp(CommonOptionsParser::HelpMessage);
92
93 class ClazyToolActionFactory : public clang::tooling::FrontendActionFactory
94 {
95 public:
96 explicit ClazyToolActionFactory(std::vector<std::string> paths)
97 : FrontendActionFactory()
98 , m_paths(std::move(paths))
99 {
100 }
101
102 std::unique_ptr<FrontendAction> create() override
103 {
104 ClazyContext::ClazyOptions options = ClazyContext::ClazyOption_None;
105
106 if (!s_exportFixes.getValue().empty()) {
107 options |= ClazyContext::ClazyOption_ExportFixes;
108 }
109
110 if (s_qtDeveloper.getValue()) {
111 options |= ClazyContext::ClazyOption_QtDeveloper;
112 }
113
114 if (s_onlyQt.getValue()) {
115 options |= ClazyContext::ClazyOption_OnlyQt;
116 }
117
118 if (s_visitImplicitCode.getValue()) {
119 options |= ClazyContext::ClazyOption_VisitImplicitCode;
120 }
121
122 if (s_ignoreIncludedFiles.getValue()) {
123 options |= ClazyContext::ClazyOption_IgnoreIncludedFiles;
124 }
125 // TODO: We need to aggregate the fixes with previous run
126 return std::make_unique<ClazyStandaloneASTAction>(s_checks.getValue(),
127 s_headerFilter.getValue(),
128 s_ignoreDirs.getValue(),
129 s_exportFixes.getValue(),
130 m_paths,
131 options);
132 }
133
134 private:
135 std::vector<std::string> m_paths;
136 };
137
138 llvm::IntrusiveRefCntPtr<vfs::FileSystem> getVfsFromFile(const std::string &overlayFile, llvm::IntrusiveRefCntPtr<vfs::FileSystem> BaseFS)
139 {
140 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> buffer = BaseFS->getBufferForFile(overlayFile);
141 if (!buffer) {
142 llvm::errs() << "Can't load virtual filesystem overlay file '" << overlayFile << "': " << buffer.getError().message() << ".\n";
143 return nullptr;
144 }
145
146 IntrusiveRefCntPtr<vfs::FileSystem> fs = vfs::getVFSFromYAML(std::move(buffer.get()),
147 /*DiagHandler*/ nullptr,
148 overlayFile);
149 if (!fs) {
150 llvm::errs() << "Error: invalid virtual filesystem overlay file '" << overlayFile << "'.\n";
151 return nullptr;
152 }
153 return fs;
154 }
155
156 int main(int argc, const char **argv)
157 {
158 for (int i = 1; i < argc; i++) {
159 if (strcmp(argv[i], "--version") == 0) {
160 std::cout << "clazy version " << CLAZY_VERSION << "\n";
161 break;
162 }
163 }
164
165 auto expectedParser = CommonOptionsParser::create(argc, argv, s_clazyCategory, cl::ZeroOrMore);
166 if (!expectedParser) {
167 llvm::errs() << expectedParser.takeError();
168 return 1;
169 }
170
171 auto &optionsParser = expectedParser.get();
172 // llvm::errs() << optionsParser.getSourcePathList().size() << "\n";
173
174 if (s_supportedChecks.getValue()) {
175 std::cout << SUPPORTED_CHECKS_JSON_STR;
176 return 0;
177 }
178
179 if (s_listEnabledChecks.getValue()) {
180 std::string checksFromArgs = s_checks.getValue();
181 std::vector<std::string> checks = {checksFromArgs.empty() ? "level1" : checksFromArgs};
182 const RegisteredCheck::List enabledChecks = CheckManager::instance()->requestedChecks(checks);
183
184 if (!enabledChecks.empty()) {
185 llvm::outs() << "Enabled checks:";
186 for (const auto &check : enabledChecks) {
187 llvm::outs() << "\n " << check.name;
188 }
189 llvm::outs() << "\n";
190 }
191
192 return 0;
193 }
194
195 llvm::IntrusiveRefCntPtr<vfs::OverlayFileSystem> fs(new vfs::OverlayFileSystem(vfs::getRealFileSystem()));
196 const std::string &overlayFile = s_vfsoverlay.getValue();
197 if (!s_vfsoverlay.getValue().empty()) {
198 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> buffer = fs->getBufferForFile(overlayFile);
199 if (!buffer) {
200 llvm::errs() << "Can't load virtual filesystem overlay file '" << overlayFile << "': " << buffer.getError().message() << ".\n";
201 return 0;
202 }
203
204 IntrusiveRefCntPtr<vfs::FileSystem> vfso = vfs::getVFSFromYAML(std::move(buffer.get()),
205 /*DiagHandler*/ nullptr,
206 overlayFile);
207 if (!vfso) {
208 llvm::errs() << "Error: invalid virtual filesystem overlay file '" << overlayFile << "'.\n";
209 return 0;
210 }
211 fs->pushOverlay(vfso);
212 }
213
214 ClangTool tool(optionsParser.getCompilations(), optionsParser.getSourcePathList(), std::make_shared<PCHContainerOperations>(), fs);
215
216 return tool.run(new ClazyToolActionFactory(optionsParser.getSourcePathList()));
217 }