| 1 | //===-- CanonicalIncludesTests.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 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "TestFS.h" |
| 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" |
| 20 | |
| 21 | namespace clang { |
| 22 | namespace clangd { |
| 23 | namespace { |
| 24 | |
| 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()); |
| 30 | return *File; |
| 31 | } |
| 32 | |
| 33 | TEST(CanonicalIncludesTest, CStandardLibrary) { |
| 34 | CanonicalIncludes CI; |
| 35 | auto Language = LangOptions(); |
| 36 | Language.C11 = true; |
| 37 | CI.addSystemHeadersMapping(Language); |
| 38 | // Usual standard library symbols are mapped correctly. |
| 39 | EXPECT_EQ("<stdio.h>", CI.mapSymbol("printf")); |
| 40 | EXPECT_EQ("", CI.mapSymbol("unknown_symbol")); |
| 41 | } |
| 42 | |
| 43 | TEST(CanonicalIncludesTest, CXXStandardLibrary) { |
| 44 | CanonicalIncludes CI; |
| 45 | auto Language = LangOptions(); |
| 46 | Language.CPlusPlus = true; |
| 47 | CI.addSystemHeadersMapping(Language); |
| 48 | |
| 49 | // Usual standard library symbols are mapped correctly. |
| 50 | EXPECT_EQ("<vector>", CI.mapSymbol("std::vector")); |
| 51 | EXPECT_EQ("<cstdio>", CI.mapSymbol("std::printf")); |
| 52 | // std::move is ambiguous, currently always mapped to <utility> |
| 53 | EXPECT_EQ("<utility>", CI.mapSymbol("std::move")); |
| 54 | // Unknown std symbols aren't mapped. |
| 55 | EXPECT_EQ("", CI.mapSymbol("std::notathing")); |
| 56 | // iosfwd declares some symbols it doesn't own. |
| 57 | EXPECT_EQ("<ostream>", CI.mapSymbol("std::ostream")); |
| 58 | // And (for now) we assume it owns the others. |
| 59 | auto InMemFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>(); |
| 60 | FileManager Files(FileSystemOptions(), InMemFS); |
| 61 | auto File = addFile(*InMemFS, Files, testPath("iosfwd")); |
| 62 | EXPECT_EQ("<iosfwd>", CI.mapHeader(File)); |
| 63 | } |
| 64 | |
| 65 | TEST(CanonicalIncludesTest, PathMapping) { |
| 66 | auto InMemFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>(); |
| 67 | FileManager Files(FileSystemOptions(), InMemFS); |
| 68 | std::string BarPath = testPath("foo/bar"); |
| 69 | auto Bar = addFile(*InMemFS, Files, BarPath); |
| 70 | auto Other = addFile(*InMemFS, Files, testPath("foo/baz")); |
| 71 | // As used for IWYU pragmas. |
| 72 | CanonicalIncludes CI; |
| 73 | CI.addMapping(Bar, "<baz>"); |
| 74 | |
| 75 | // We added a mapping for baz. |
| 76 | EXPECT_EQ("<baz>", CI.mapHeader(Bar)); |
| 77 | // Other file doesn't have a mapping. |
| 78 | EXPECT_EQ("", CI.mapHeader(Other)); |
| 79 | |
| 80 | // Add hard link to "foo/bar" and check that it is also mapped to <baz>, hence |
| 81 | // does not depend on the header name. |
| 82 | std::string HardLinkPath = testPath("hard/link"); |
| 83 | InMemFS->addHardLink(HardLinkPath, BarPath); |
| 84 | auto HardLinkFile = Files.getFileRef(HardLinkPath); |
| 85 | ASSERT_THAT_EXPECTED(HardLinkFile, llvm::Succeeded()); |
| 86 | EXPECT_EQ("<baz>", CI.mapHeader(*HardLinkFile)); |
| 87 | } |
| 88 | |
| 89 | TEST(CanonicalIncludesTest, Precedence) { |
| 90 | auto InMemFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>(); |
| 91 | FileManager Files(FileSystemOptions(), InMemFS); |
| 92 | auto File = addFile(*InMemFS, Files, testPath("some/path")); |
| 93 | |
| 94 | CanonicalIncludes CI; |
| 95 | CI.addMapping(File, "<path>"); |
| 96 | LangOptions Language; |
| 97 | Language.CPlusPlus = true; |
| 98 | CI.addSystemHeadersMapping(Language); |
| 99 | |
| 100 | // We added a mapping from some/path to <path>. |
| 101 | ASSERT_EQ("<path>", CI.mapHeader(File)); |
| 102 | // We should have a path from 'bits/stl_vector.h' to '<vector>'. |
| 103 | // FIXME: The Standrad Library map in CanonicalIncludes expects forward |
| 104 | // slashes and Windows would use backward slashes instead, so the headers are |
| 105 | // not matched appropriately. |
| 106 | auto STLVectorFile = addFile(*InMemFS, Files, "bits/stl_vector.h"); |
| 107 | ASSERT_EQ("<vector>", CI.mapHeader(STLVectorFile)); |
| 108 | } |
| 109 | |
| 110 | } // namespace |
| 111 | } // namespace clangd |
| 112 | } // namespace clang |