• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
15 namespace clang {
16 namespace tidy {
17 namespace test {
18 
TEST(ClangTidyOptionsProvider,InMemoryFileSystems)19 TEST(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("ProjectRoot/.clang-tidy", 0,
35                       llvm::MemoryBuffer::getMemBuffer(BaseClangTidy));
36   FileSystem->addFile("ProjectRoot/SubDir1/.clang-tidy", 0,
37                       llvm::MemoryBuffer::getMemBuffer(Sub1ClangTidy));
38   FileSystem->addFile("ProjectRoot/SubDir1/File.cpp", 0,
39                       llvm::MemoryBuffer::getMemBuffer(""));
40   FileSystem->addFile("ProjectRoot/SubDir1/SubDir2/.clang-tidy", 0,
41                       llvm::MemoryBuffer::getMemBuffer(Sub2ClangTidy));
42   FileSystem->addFile("ProjectRoot/SubDir1/SubDir2/File.cpp", 0,
43                       llvm::MemoryBuffer::getMemBuffer(""));
44   FileSystem->addFile("ProjectRoot/SubDir1/SubDir2/SubDir3/File.cpp", 0,
45                       llvm::MemoryBuffer::getMemBuffer(""));
46 
47   FileOptionsProvider FileOpt({}, {}, {}, FileSystem);
48 
49   ClangTidyOptions File1Options =
50       FileOpt.getOptions("ProjectRoot/SubDir1/File.cpp");
51   ClangTidyOptions File2Options =
52       FileOpt.getOptions("ProjectRoot/SubDir1/SubDir2/File.cpp");
53   ClangTidyOptions File3Options =
54       FileOpt.getOptions("ProjectRoot/SubDir1/SubDir2/SubDir3/File.cpp");
55 
56   ASSERT_TRUE(File1Options.Checks.hasValue());
57   EXPECT_EQ(*File1Options.Checks, "-*,clang-diagnostic-*,readability-*");
58   ASSERT_TRUE(File2Options.Checks.hasValue());
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