1 //===-- CanonicalIncludesTests.cpp - --------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
10 #include "index/CanonicalIncludes.h"
11 #include "clang/Basic/FileEntry.h"
12 #include "clang/Basic/FileManager.h"
13 #include "clang/Basic/LangOptions.h"
14 #include "llvm/ADT/IntrusiveRefCntPtr.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Support/Error.h"
17 #include "llvm/Support/VirtualFileSystem.h"
18 #include "llvm/Testing/Support/Error.h"
19 #include "gtest/gtest.h"
25 FileEntryRef
addFile(llvm::vfs::InMemoryFileSystem
&FS
, FileManager
&FM
,
26 llvm::StringRef Filename
) {
27 FS
.addFile(Filename
, 0, llvm::MemoryBuffer::getMemBuffer(""));
28 auto File
= FM
.getFileRef(Filename
);
29 EXPECT_THAT_EXPECTED(File
, llvm::Succeeded());
33 TEST(CanonicalIncludesTest
, CStandardLibrary
) {
35 auto Language
= LangOptions();
37 CI
.addSystemHeadersMapping(Language
);
38 // Usual standard library symbols are mapped correctly.
39 EXPECT_EQ("<stdio.h>", CI
.mapSymbol("", "printf", Language
));
40 EXPECT_EQ("", CI
.mapSymbol("", "unknown_symbol", Language
));
43 TEST(CanonicalIncludesTest
, CXXStandardLibrary
) {
45 auto Language
= LangOptions();
46 Language
.CPlusPlus
= true;
47 CI
.addSystemHeadersMapping(Language
);
49 // Usual standard library symbols are mapped correctly.
50 EXPECT_EQ("<vector>", CI
.mapSymbol("std::", "vector", Language
));
51 EXPECT_EQ("<cstdio>", CI
.mapSymbol("std::", "printf", Language
));
52 // std::move is ambiguous, currently always mapped to <utility>
53 EXPECT_EQ("<utility>", CI
.mapSymbol("std::", "move", Language
));
54 EXPECT_EQ("<cstddef>", CI
.mapSymbol("std::", "size_t", Language
));
55 // Unknown std symbols aren't mapped.
56 EXPECT_EQ("", CI
.mapSymbol("std::", "notathing", Language
));
57 // iosfwd declares some symbols it doesn't own.
58 EXPECT_EQ("<ostream>", CI
.mapSymbol("std::", "ostream", Language
));
59 // And (for now) we assume it owns the others.
60 auto InMemFS
= llvm::makeIntrusiveRefCnt
<llvm::vfs::InMemoryFileSystem
>();
61 FileManager
Files(FileSystemOptions(), InMemFS
);
62 auto File
= addFile(*InMemFS
, Files
, testPath("iosfwd"));
63 EXPECT_EQ("<iosfwd>", CI
.mapHeader(File
));
66 TEST(CanonicalIncludesTest
, PathMapping
) {
67 auto InMemFS
= llvm::makeIntrusiveRefCnt
<llvm::vfs::InMemoryFileSystem
>();
68 FileManager
Files(FileSystemOptions(), InMemFS
);
69 std::string BarPath
= testPath("foo/bar");
70 auto Bar
= addFile(*InMemFS
, Files
, BarPath
);
71 auto Other
= addFile(*InMemFS
, Files
, testPath("foo/baz"));
72 // As used for IWYU pragmas.
74 CI
.addMapping(Bar
, "<baz>");
76 // We added a mapping for baz.
77 EXPECT_EQ("<baz>", CI
.mapHeader(Bar
));
78 // Other file doesn't have a mapping.
79 EXPECT_EQ("", CI
.mapHeader(Other
));
81 // Add hard link to "foo/bar" and check that it is also mapped to <baz>, hence
82 // does not depend on the header name.
83 std::string HardLinkPath
= testPath("hard/link");
84 InMemFS
->addHardLink(HardLinkPath
, BarPath
);
85 auto HardLinkFile
= Files
.getFileRef(HardLinkPath
);
86 ASSERT_THAT_EXPECTED(HardLinkFile
, llvm::Succeeded());
87 EXPECT_EQ("<baz>", CI
.mapHeader(*HardLinkFile
));
90 TEST(CanonicalIncludesTest
, Precedence
) {
91 auto InMemFS
= llvm::makeIntrusiveRefCnt
<llvm::vfs::InMemoryFileSystem
>();
92 FileManager
Files(FileSystemOptions(), InMemFS
);
93 auto File
= addFile(*InMemFS
, Files
, testPath("some/path"));
96 CI
.addMapping(File
, "<path>");
98 Language
.CPlusPlus
= true;
99 CI
.addSystemHeadersMapping(Language
);
101 // We added a mapping from some/path to <path>.
102 ASSERT_EQ("<path>", CI
.mapHeader(File
));
103 // We should have a path from 'bits/stl_vector.h' to '<vector>'.
104 // FIXME: The Standrad Library map in CanonicalIncludes expects forward
105 // slashes and Windows would use backward slashes instead, so the headers are
106 // not matched appropriately.
107 auto STLVectorFile
= addFile(*InMemFS
, Files
, "bits/stl_vector.h");
108 ASSERT_EQ("<vector>", CI
.mapHeader(STLVectorFile
));
112 } // namespace clangd