clang-tools 20.0.0git
TidyProviderTests.cpp
Go to the documentation of this file.
1//===-- TidyProviderTests.cpp - Clang tidy configuration provider tests ---===//
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 "Feature.h"
10#include "TestFS.h"
11#include "TidyProvider.h"
12#include "llvm/Testing/Support/SupportHelpers.h"
13#include "gtest/gtest.h"
14
15namespace clang {
16namespace clangd {
17
18namespace {
19
20TEST(TidyProvider, NestedDirectories) {
21 MockFS FS;
22 FS.Files[testPath(".clang-tidy")] = R"yaml(
23 Checks: 'llvm-*'
24 CheckOptions:
25 TestKey: 1
26)yaml";
27 FS.Files[testPath("sub1/.clang-tidy")] = R"yaml(
28 Checks: 'misc-*'
29 CheckOptions:
30 TestKey: 2
31)yaml";
32 FS.Files[testPath("sub1/sub2/.clang-tidy")] = R"yaml(
33 Checks: 'bugprone-*'
34 CheckOptions:
35 TestKey: 3
36 InheritParentConfig: true
37)yaml";
38
40
41 auto BaseOptions = getTidyOptionsForFile(Provider, testPath("File.cpp"));
42 ASSERT_TRUE(BaseOptions.Checks.has_value());
43 EXPECT_EQ(*BaseOptions.Checks, "llvm-*");
44 EXPECT_EQ(BaseOptions.CheckOptions.lookup("TestKey").Value, "1");
45
46 auto Sub1Options = getTidyOptionsForFile(Provider, testPath("sub1/File.cpp"));
47 ASSERT_TRUE(Sub1Options.Checks.has_value());
48 EXPECT_EQ(*Sub1Options.Checks, "misc-*");
49 EXPECT_EQ(Sub1Options.CheckOptions.lookup("TestKey").Value, "2");
50
51 auto Sub2Options =
52 getTidyOptionsForFile(Provider, testPath("sub1/sub2/File.cpp"));
53 ASSERT_TRUE(Sub2Options.Checks.has_value());
54 EXPECT_EQ(*Sub2Options.Checks, "misc-*,bugprone-*");
55 EXPECT_EQ(Sub2Options.CheckOptions.lookup("TestKey").Value, "3");
56}
57
58TEST(TidyProvider, IsFastTidyCheck) {
59 EXPECT_THAT(isFastTidyCheck("misc-const-correctness"), llvm::ValueIs(false));
60 EXPECT_THAT(isFastTidyCheck("bugprone-suspicious-include"),
61 llvm::ValueIs(true));
62 // Linked in (ParsedASTTests.cpp) but not measured.
63 EXPECT_EQ(isFastTidyCheck("replay-preamble-check"), std::nullopt);
64}
65
66#if CLANGD_TIDY_CHECKS
67TEST(TidyProvider, IsValidCheck) {
68 EXPECT_TRUE(isRegisteredTidyCheck("bugprone-argument-comment"));
69 EXPECT_FALSE(isRegisteredTidyCheck("bugprone-argument-clinic"));
70}
71#endif
72
73} // namespace
74} // namespace clangd
75} // namespace clang
bool isRegisteredTidyCheck(llvm::StringRef Check)
Returns if Check is a registered clang-tidy check.
std::optional< bool > isFastTidyCheck(llvm::StringRef Check)
Returns if Check is known-fast, known-slow, or its speed is unknown.
std::string testPath(PathRef File, llvm::sys::path::Style Style)
Definition: TestFS.cpp:93
TEST(BackgroundQueueTest, Priority)
TidyProvider provideClangTidyFiles(ThreadsafeFS &TFS)
Provider that searches for .clang-tidy configuration files in the directory tree.
tidy::ClangTidyOptions getTidyOptionsForFile(TidyProviderRef Provider, llvm::StringRef Filename)
llvm::unique_function< void(tidy::ClangTidyOptions &, llvm::StringRef) const > TidyProvider
A factory to modify a tidy::ClangTidyOptions.
Definition: TidyProvider.h:23
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//