1 | //===--- TidyProvider.cpp - create options for running 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 "TidyProvider.h" |
10 | #include "../clang-tidy/ClangTidyModuleRegistry.h" |
11 | #include "../clang-tidy/ClangTidyOptions.h" |
12 | #include "Config.h" |
13 | #include "support/FileCache.h" |
14 | #include "support/Logger.h" |
15 | #include "support/Path.h" |
16 | #include "support/ThreadsafeFS.h" |
17 | #include "llvm/ADT/STLExtras.h" |
18 | #include "llvm/ADT/SmallString.h" |
19 | #include "llvm/ADT/StringExtras.h" |
20 | #include "llvm/ADT/StringSet.h" |
21 | #include "llvm/Support/Allocator.h" |
22 | #include "llvm/Support/Process.h" |
23 | #include "llvm/Support/SourceMgr.h" |
24 | #include <memory> |
25 | #include <optional> |
26 | |
27 | namespace clang { |
28 | namespace clangd { |
29 | namespace { |
30 | |
31 | // Access to config from a .clang-tidy file, caching IO and parsing. |
32 | class DotClangTidyCache : private FileCache { |
33 | // We cache and expose shared_ptr to avoid copying the value on every lookup |
34 | // when we're ultimately just going to pass it to mergeWith. |
35 | mutable std::shared_ptr<const tidy::ClangTidyOptions> Value; |
36 | |
37 | public: |
38 | DotClangTidyCache(PathRef Path) : FileCache(Path) {} |
39 | |
40 | std::shared_ptr<const tidy::ClangTidyOptions> |
41 | get(const ThreadsafeFS &TFS, |
42 | std::chrono::steady_clock::time_point FreshTime) const { |
43 | std::shared_ptr<const tidy::ClangTidyOptions> Result; |
44 | read( |
45 | TFS, FreshTime, |
46 | Parse: [this](std::optional<llvm::StringRef> Data) { |
47 | Value.reset(); |
48 | if (Data && !Data->empty()) { |
49 | tidy::DiagCallback Diagnostics = [](const llvm::SMDiagnostic &D) { |
50 | switch (D.getKind()) { |
51 | case llvm::SourceMgr::DK_Error: |
52 | elog(Fmt: "tidy-config error at {0}:{1}:{2}: {3}" , Vals: D.getFilename(), |
53 | Vals: D.getLineNo(), Vals: D.getColumnNo(), Vals: D.getMessage()); |
54 | break; |
55 | case llvm::SourceMgr::DK_Warning: |
56 | log(Fmt: "tidy-config warning at {0}:{1}:{2}: {3}" , Vals: D.getFilename(), |
57 | Vals: D.getLineNo(), Vals: D.getColumnNo(), Vals: D.getMessage()); |
58 | break; |
59 | case llvm::SourceMgr::DK_Note: |
60 | case llvm::SourceMgr::DK_Remark: |
61 | vlog(Fmt: "tidy-config note at {0}:{1}:{2}: {3}" , Vals: D.getFilename(), |
62 | Vals: D.getLineNo(), Vals: D.getColumnNo(), Vals: D.getMessage()); |
63 | break; |
64 | } |
65 | }; |
66 | if (auto Parsed = tidy::parseConfigurationWithDiags( |
67 | Config: llvm::MemoryBufferRef(*Data, path()), Handler: Diagnostics)) |
68 | Value = std::make_shared<const tidy::ClangTidyOptions>( |
69 | args: std::move(*Parsed)); |
70 | else |
71 | elog(Fmt: "Error parsing clang-tidy configuration in {0}: {1}" , Vals: path(), |
72 | Vals: Parsed.getError().message()); |
73 | } |
74 | }, |
75 | Read: [&]() { Result = Value; }); |
76 | return Result; |
77 | } |
78 | }; |
79 | |
80 | // Access to combined config from .clang-tidy files governing a source file. |
81 | // Each config file is cached and the caches are shared for affected sources. |
82 | // |
83 | // FIXME: largely duplicates config::Provider::fromAncestorRelativeYAMLFiles. |
84 | // Potentially useful for compile_commands.json too. Extract? |
85 | class DotClangTidyTree { |
86 | const ThreadsafeFS &FS; |
87 | std::string RelPath; |
88 | std::chrono::steady_clock::duration MaxStaleness; |
89 | |
90 | mutable std::mutex Mu; |
91 | // Keys are the ancestor directory, not the actual config path within it. |
92 | // We only insert into this map, so pointers to values are stable forever. |
93 | // Mutex guards the map itself, not the values (which are threadsafe). |
94 | mutable llvm::StringMap<DotClangTidyCache> Cache; |
95 | |
96 | public: |
97 | DotClangTidyTree(const ThreadsafeFS &FS) |
98 | : FS(FS), RelPath(".clang-tidy" ), MaxStaleness(std::chrono::seconds(5)) {} |
99 | |
100 | void apply(tidy::ClangTidyOptions &Result, PathRef AbsPath) { |
101 | namespace path = llvm::sys::path; |
102 | assert(path::is_absolute(AbsPath)); |
103 | |
104 | // Compute absolute paths to all ancestors (substrings of P.Path). |
105 | // Ensure cache entries for each ancestor exist in the map. |
106 | llvm::SmallVector<DotClangTidyCache *> Caches; |
107 | { |
108 | std::lock_guard<std::mutex> Lock(Mu); |
109 | for (auto Ancestor = absoluteParent(Path: AbsPath); !Ancestor.empty(); |
110 | Ancestor = absoluteParent(Path: Ancestor)) { |
111 | auto It = Cache.find(Key: Ancestor); |
112 | // Assemble the actual config file path only if needed. |
113 | if (It == Cache.end()) { |
114 | llvm::SmallString<256> ConfigPath = Ancestor; |
115 | path::append(path&: ConfigPath, a: RelPath); |
116 | It = Cache.try_emplace(Key: Ancestor, Args: ConfigPath.str()).first; |
117 | } |
118 | Caches.push_back(Elt: &It->second); |
119 | } |
120 | } |
121 | // Finally query each individual file. |
122 | // This will take a (per-file) lock for each file that actually exists. |
123 | std::chrono::steady_clock::time_point FreshTime = |
124 | std::chrono::steady_clock::now() - MaxStaleness; |
125 | llvm::SmallVector<std::shared_ptr<const tidy::ClangTidyOptions>> |
126 | OptionStack; |
127 | for (const DotClangTidyCache *Cache : Caches) |
128 | if (auto Config = Cache->get(TFS: FS, FreshTime)) { |
129 | OptionStack.push_back(Elt: std::move(Config)); |
130 | if (!OptionStack.back()->InheritParentConfig.value_or(u: false)) |
131 | break; |
132 | } |
133 | unsigned Order = 1u; |
134 | for (auto &Option : llvm::reverse(C&: OptionStack)) |
135 | Result.mergeWith(Other: *Option, Order: Order++); |
136 | } |
137 | }; |
138 | |
139 | } // namespace |
140 | |
141 | static void mergeCheckList(std::optional<std::string> &Checks, |
142 | llvm::StringRef List) { |
143 | if (List.empty()) |
144 | return; |
145 | if (!Checks || Checks->empty()) { |
146 | Checks.emplace(args&: List); |
147 | return; |
148 | } |
149 | *Checks = llvm::join_items(Separator: "," , Items&: *Checks, Items&: List); |
150 | } |
151 | |
152 | TidyProviderRef provideEnvironment() { |
153 | static const std::optional<std::string> User = [] { |
154 | std::optional<std::string> Ret = llvm::sys::Process::GetEnv(name: "USER" ); |
155 | #ifdef _WIN32 |
156 | if (!Ret) |
157 | return llvm::sys::Process::GetEnv("USERNAME" ); |
158 | #endif |
159 | return Ret; |
160 | }(); |
161 | |
162 | if (User) |
163 | return |
164 | [](tidy::ClangTidyOptions &Opts, llvm::StringRef) { Opts.User = User; }; |
165 | // FIXME: Once function_ref and unique_function operator= operators handle |
166 | // null values, this can return null. |
167 | return [](tidy::ClangTidyOptions &, llvm::StringRef) {}; |
168 | } |
169 | |
170 | TidyProviderRef provideDefaultChecks() { |
171 | // These default checks are chosen for: |
172 | // - low false-positive rate |
173 | // - providing a lot of value |
174 | // - being reasonably efficient |
175 | static const std::string DefaultChecks = llvm::join_items( |
176 | Separator: "," , Items: "readability-misleading-indentation" , Items: "readability-deleted-default" , |
177 | Items: "bugprone-integer-division" , Items: "bugprone-sizeof-expression" , |
178 | Items: "bugprone-suspicious-missing-comma" , Items: "bugprone-unused-raii" , |
179 | Items: "bugprone-unused-return-value" , Items: "misc-unused-using-decls" , |
180 | Items: "misc-unused-alias-decls" , Items: "misc-definitions-in-headers" ); |
181 | return [](tidy::ClangTidyOptions &Opts, llvm::StringRef) { |
182 | if (!Opts.Checks || Opts.Checks->empty()) |
183 | Opts.Checks = DefaultChecks; |
184 | }; |
185 | } |
186 | |
187 | TidyProvider addTidyChecks(llvm::StringRef Checks, |
188 | llvm::StringRef WarningsAsErrors) { |
189 | return [Checks = std::string(Checks), |
190 | WarningsAsErrors = std::string(WarningsAsErrors)]( |
191 | tidy::ClangTidyOptions &Opts, llvm::StringRef) { |
192 | mergeCheckList(Checks&: Opts.Checks, List: Checks); |
193 | mergeCheckList(Checks&: Opts.WarningsAsErrors, List: WarningsAsErrors); |
194 | }; |
195 | } |
196 | |
197 | TidyProvider disableUnusableChecks(llvm::ArrayRef<std::string> ) { |
198 | constexpr llvm::StringLiteral Seperator("," ); |
199 | static const std::string BadChecks = llvm::join_items( |
200 | Separator: Seperator, |
201 | // We want this list to start with a seperator to |
202 | // simplify appending in the lambda. So including an |
203 | // empty string here will force that. |
204 | Items: "" , |
205 | // include-cleaner is directly integrated in IncludeCleaner.cpp |
206 | Items: "-misc-include-cleaner" , |
207 | |
208 | // ----- False Positives ----- |
209 | |
210 | // Check relies on seeing ifndef/define/endif directives, |
211 | // clangd doesn't replay those when using a preamble. |
212 | Items: "-llvm-header-guard" , Items: "-modernize-macro-to-enum" , |
213 | |
214 | // ----- Crashing Checks ----- |
215 | |
216 | // Check can choke on invalid (intermediate) c++ |
217 | // code, which is often the case when clangd |
218 | // tries to build an AST. |
219 | Items: "-bugprone-use-after-move" , |
220 | // Alias for bugprone-use-after-move. |
221 | Items: "-hicpp-invalid-access-moved" , |
222 | // Check uses dataflow analysis, which might hang/crash unexpectedly on |
223 | // incomplete code. |
224 | Items: "-bugprone-unchecked-optional-access" , |
225 | |
226 | // ----- Performance problems ----- |
227 | |
228 | // This check runs expensive analysis for each variable. |
229 | // It has been observed to increase reparse time by 10x. |
230 | Items: "-misc-const-correctness" ); |
231 | |
232 | size_t Size = BadChecks.size(); |
233 | for (const std::string &Str : ExtraBadChecks) { |
234 | if (Str.empty()) |
235 | continue; |
236 | Size += Seperator.size(); |
237 | if (LLVM_LIKELY(Str.front() != '-')) |
238 | ++Size; |
239 | Size += Str.size(); |
240 | } |
241 | std::string DisableGlob; |
242 | DisableGlob.reserve(res: Size); |
243 | DisableGlob += BadChecks; |
244 | for (const std::string &Str : ExtraBadChecks) { |
245 | if (Str.empty()) |
246 | continue; |
247 | DisableGlob += Seperator; |
248 | if (LLVM_LIKELY(Str.front() != '-')) |
249 | DisableGlob.push_back(c: '-'); |
250 | DisableGlob += Str; |
251 | } |
252 | |
253 | return [DisableList(std::move(DisableGlob))](tidy::ClangTidyOptions &Opts, |
254 | llvm::StringRef) { |
255 | if (Opts.Checks && !Opts.Checks->empty()) |
256 | Opts.Checks->append(str: DisableList); |
257 | }; |
258 | } |
259 | |
260 | TidyProviderRef provideClangdConfig() { |
261 | return [](tidy::ClangTidyOptions &Opts, llvm::StringRef) { |
262 | const auto &CurTidyConfig = Config::current().Diagnostics.ClangTidy; |
263 | if (!CurTidyConfig.Checks.empty()) |
264 | mergeCheckList(Checks&: Opts.Checks, List: CurTidyConfig.Checks); |
265 | |
266 | for (const auto &CheckOption : CurTidyConfig.CheckOptions) |
267 | Opts.CheckOptions.insert_or_assign(Key: CheckOption.getKey(), |
268 | Val: tidy::ClangTidyOptions::ClangTidyValue( |
269 | CheckOption.getValue(), 10000U)); |
270 | }; |
271 | } |
272 | |
273 | TidyProvider provideClangTidyFiles(ThreadsafeFS &TFS) { |
274 | return [Tree = std::make_unique<DotClangTidyTree>(args&: TFS)]( |
275 | tidy::ClangTidyOptions &Opts, llvm::StringRef Filename) { |
276 | Tree->apply(Result&: Opts, AbsPath: Filename); |
277 | }; |
278 | } |
279 | |
280 | TidyProvider combine(std::vector<TidyProvider> Providers) { |
281 | // FIXME: Once function_ref and unique_function operator= operators handle |
282 | // null values, we should filter out any Providers that are null. Right now we |
283 | // have to ensure we dont pass any providers that are null. |
284 | return [Providers(std::move(Providers))](tidy::ClangTidyOptions &Opts, |
285 | llvm::StringRef Filename) { |
286 | for (const auto &Provider : Providers) |
287 | Provider(Opts, Filename); |
288 | }; |
289 | } |
290 | |
291 | tidy::ClangTidyOptions getTidyOptionsForFile(TidyProviderRef Provider, |
292 | llvm::StringRef Filename) { |
293 | // getDefaults instantiates all check factories, which are registered at link |
294 | // time. So cache the results once. |
295 | static const auto *DefaultOpts = [] { |
296 | auto *Opts = new tidy::ClangTidyOptions; |
297 | *Opts = tidy::ClangTidyOptions::getDefaults(); |
298 | Opts->Checks->clear(); |
299 | return Opts; |
300 | }(); |
301 | auto Opts = *DefaultOpts; |
302 | if (Provider) |
303 | Provider(Opts, Filename); |
304 | return Opts; |
305 | } |
306 | |
307 | bool isRegisteredTidyCheck(llvm::StringRef Check) { |
308 | assert(!Check.empty()); |
309 | assert(!Check.contains('*') && !Check.contains(',') && |
310 | "isRegisteredCheck doesn't support globs" ); |
311 | assert(Check.ltrim().front() != '-'); |
312 | |
313 | static const llvm::StringSet<llvm::BumpPtrAllocator> AllChecks = [] { |
314 | llvm::StringSet<llvm::BumpPtrAllocator> Result; |
315 | tidy::ClangTidyCheckFactories Factories; |
316 | for (tidy::ClangTidyModuleRegistry::entry E : |
317 | tidy::ClangTidyModuleRegistry::entries()) |
318 | E.instantiate()->addCheckFactories(CheckFactories&: Factories); |
319 | for (const auto &Factory : Factories) |
320 | Result.insert(key: Factory.getKey()); |
321 | return Result; |
322 | }(); |
323 | |
324 | return AllChecks.contains(key: Check); |
325 | } |
326 | |
327 | std::optional<bool> isFastTidyCheck(llvm::StringRef Check) { |
328 | static auto &Fast = *new llvm::StringMap<bool>{ |
329 | #define FAST(CHECK, TIME) {#CHECK,true}, |
330 | #define SLOW(CHECK, TIME) {#CHECK,false}, |
331 | #include "TidyFastChecks.inc" |
332 | }; |
333 | if (auto It = Fast.find(Key: Check); It != Fast.end()) |
334 | return It->second; |
335 | return std::nullopt; |
336 | } |
337 | |
338 | } // namespace clangd |
339 | } // namespace clang |
340 | |