Bump version to 19.1.0git
[llvm-project.git] / clang-tools-extra / include-cleaner / unittests / TypesTest.cpp
blob609563da488e3b500d95365506a02cc3b6fdf4b9
1 //===-- RecordTest.cpp ----------------------------------------------------===//
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 //===----------------------------------------------------------------------===//
9 #include "clang-include-cleaner/Types.h"
10 #include "clang/Basic/FileManager.h"
11 #include "clang/Basic/FileSystemOptions.h"
12 #include "clang/Tooling/Inclusions/StandardLibrary.h"
13 #include "llvm/ADT/IntrusiveRefCntPtr.h"
14 #include "llvm/Support/VirtualFileSystem.h"
15 #include "gmock/gmock.h"
16 #include "gtest/gtest.h"
18 namespace clang::include_cleaner {
19 namespace {
20 using testing::ElementsAre;
21 using testing::IsEmpty;
22 using testing::UnorderedElementsAre;
24 // Matches an Include* on the specified line;
25 MATCHER_P(line, N, "") { return arg->Line == (unsigned)N; }
27 TEST(RecordedIncludesTest, Match) {
28 // We're using synthetic data, but need a FileManager to obtain FileEntry*s.
29 // Ensure it doesn't do any actual IO.
30 auto FS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
31 FileManager FM(FileSystemOptions{});
32 FileEntryRef A = FM.getVirtualFileRef("/path/a", /*Size=*/0, time_t{});
33 FileEntryRef B = FM.getVirtualFileRef("/path/b", /*Size=*/0, time_t{});
35 Includes Inc;
36 Inc.add(Include{"a", A, SourceLocation(), 1});
37 Inc.add(Include{"a2", A, SourceLocation(), 2});
38 Inc.add(Include{"b", B, SourceLocation(), 3});
39 Inc.add(Include{"vector", B, SourceLocation(), 4});
40 Inc.add(Include{"vector", B, SourceLocation(), 5});
41 Inc.add(Include{"missing", std::nullopt, SourceLocation(), 6});
43 EXPECT_THAT(Inc.match(A), ElementsAre(line(1), line(2)));
44 EXPECT_THAT(Inc.match(B), ElementsAre(line(3), line(4), line(5)));
45 EXPECT_THAT(Inc.match(*tooling::stdlib::Header::named("<vector>")),
46 ElementsAre(line(4), line(5)));
49 TEST(RecordedIncludesTest, MatchVerbatim) {
50 auto FS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
51 FileManager FM(FileSystemOptions{});
52 Includes Inc;
54 // By default, a verbatim header only matches includes with the same spelling.
55 auto Foo =
56 FM.getVirtualFileRef("repo/lib/include/rel/foo.h", /*Size=*/0, time_t{});
57 Inc.add(Include{"lib/include/rel/foo.h", Foo, SourceLocation(), 1});
58 Inc.add(Include{"rel/foo.h", Foo, SourceLocation(), 2});
59 EXPECT_THAT(Inc.match(Header("<rel/foo.h>")), ElementsAre(line(2)));
61 // A verbatim header can match another spelling if the search path
62 // suggests it's equivalent.
63 auto Bar =
64 FM.getVirtualFileRef("repo/lib/include/rel/bar.h", /*Size=*/0, time_t{});
65 Inc.addSearchDirectory("repo/");
66 Inc.addSearchDirectory("repo/lib/include");
67 Inc.add(Include{"lib/include/rel/bar.h", Bar, SourceLocation(), 3});
68 Inc.add(Include{"rel/bar.h", Bar, SourceLocation(), 4});
69 EXPECT_THAT(Inc.match(Header("<rel/bar.h>")),
70 UnorderedElementsAre(line(3), line(4)));
72 // We don't apply this logic to system headers, though.
73 auto Vector =
74 FM.getVirtualFileRef("repo/lib/include/vector", /*Size=*/0, time_t{});
75 Inc.add(Include{"lib/include/vector", Vector, SourceLocation(), 5});
76 EXPECT_THAT(Inc.match(Header(*tooling::stdlib::Header::named("<vector>"))),
77 IsEmpty());
80 TEST(RecordedIncludesTest, MatchVerbatimMixedAbsoluteRelative) {
81 auto FS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
82 FS->setCurrentWorkingDirectory("/working");
83 FileManager FM(FileSystemOptions{});
84 Includes Inc;
86 auto Foo =
87 FM.getVirtualFileRef("/working/rel1/rel2/foo.h", /*Size=*/0, time_t{});
88 Inc.addSearchDirectory("rel1");
89 Inc.addSearchDirectory("rel1/rel2");
90 Inc.add(Include{"rel2/foo.h", Foo, SourceLocation(), 1});
91 EXPECT_THAT(Inc.match(Header("<foo.h>")), IsEmpty());
93 Inc = Includes{};
94 auto Bar = FM.getVirtualFileRef("rel1/rel2/bar.h", /*Size=*/0, time_t{});
95 Inc.addSearchDirectory("/working/rel1");
96 Inc.addSearchDirectory("/working/rel1/rel2");
97 Inc.add(Include{"rel2/bar.h", Bar, SourceLocation(), 1});
98 EXPECT_THAT(Inc.match(Header("<bar.h>")), IsEmpty());
101 } // namespace
102 } // namespace clang::include_cleaner