1//===---- ObjCModuleTest.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 "ClangTidyOptions.h"
10#include "clang/Basic/LLVM.h"
11#include "llvm/Support/MemoryBuffer.h"
12#include "llvm/Support/VirtualFileSystem.h"
13#include "gtest/gtest.h"
14
15namespace clang {
16namespace tidy {
17namespace test {
18
19TEST(ClangTidyOptionsProvider, InMemoryFileSystems) {
20 llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> FileSystem(
21 new llvm::vfs::InMemoryFileSystem);
22
23 StringRef BaseClangTidy = R"(
24 Checks: -*,clang-diagnostic-*
25 )";
26 StringRef Sub1ClangTidy = R"(
27 Checks: readability-*
28 InheritParentConfig: true
29 )";
30 StringRef Sub2ClangTidy = R"(
31 Checks: bugprone-*,misc-*,clang-diagnostic-*
32 InheritParentConfig: false
33 )";
34 FileSystem->addFile(Path: "ProjectRoot/.clang-tidy", ModificationTime: 0,
35 Buffer: llvm::MemoryBuffer::getMemBuffer(InputData: BaseClangTidy));
36 FileSystem->addFile(Path: "ProjectRoot/SubDir1/.clang-tidy", ModificationTime: 0,
37 Buffer: llvm::MemoryBuffer::getMemBuffer(InputData: Sub1ClangTidy));
38 FileSystem->addFile(Path: "ProjectRoot/SubDir1/File.cpp", ModificationTime: 0,
39 Buffer: llvm::MemoryBuffer::getMemBuffer(InputData: ""));
40 FileSystem->addFile(Path: "ProjectRoot/SubDir1/SubDir2/.clang-tidy", ModificationTime: 0,
41 Buffer: llvm::MemoryBuffer::getMemBuffer(InputData: Sub2ClangTidy));
42 FileSystem->addFile(Path: "ProjectRoot/SubDir1/SubDir2/File.cpp", ModificationTime: 0,
43 Buffer: llvm::MemoryBuffer::getMemBuffer(InputData: ""));
44 FileSystem->addFile(Path: "ProjectRoot/SubDir1/SubDir2/SubDir3/File.cpp", ModificationTime: 0,
45 Buffer: llvm::MemoryBuffer::getMemBuffer(InputData: ""));
46
47 FileOptionsProvider FileOpt({}, {}, {}, FileSystem);
48
49 ClangTidyOptions File1Options =
50 FileOpt.getOptions(FileName: "ProjectRoot/SubDir1/File.cpp");
51 ClangTidyOptions File2Options =
52 FileOpt.getOptions(FileName: "ProjectRoot/SubDir1/SubDir2/File.cpp");
53 ClangTidyOptions File3Options =
54 FileOpt.getOptions(FileName: "ProjectRoot/SubDir1/SubDir2/SubDir3/File.cpp");
55
56 ASSERT_TRUE(File1Options.Checks.has_value());
57 EXPECT_EQ(*File1Options.Checks, "-*,clang-diagnostic-*,readability-*");
58 ASSERT_TRUE(File2Options.Checks.has_value());
59 EXPECT_EQ(*File2Options.Checks, "bugprone-*,misc-*,clang-diagnostic-*");
60
61 // 2 and 3 should use the same config so these should also be the same.
62 EXPECT_EQ(File2Options.Checks, File3Options.Checks);
63}
64
65} // namespace test
66} // namespace tidy
67} // namespace clang
68

source code of clang-tools-extra/unittests/clang-tidy/OptionsProviderTest.cpp