[AMDGPU][AsmParser][NFC] Translate parsed MIMG instructions to MCInsts automatically.
[llvm-project.git] / clang-tools-extra / clangd / unittests / CanonicalIncludesTests.cpp
blob0d6731ca59581f7d896057ad7bcfc0c6711722c3
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 //===----------------------------------------------------------------------===//
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"
21 namespace clang {
22 namespace clangd {
23 namespace {
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;
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", Language));
40 EXPECT_EQ("", CI.mapSymbol("", "unknown_symbol", Language));
43 TEST(CanonicalIncludesTest, CXXStandardLibrary) {
44 CanonicalIncludes CI;
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.
73 CanonicalIncludes CI;
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"));
95 CanonicalIncludes CI;
96 CI.addMapping(File, "<path>");
97 LangOptions Language;
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));
111 } // namespace
112 } // namespace clangd
113 } // namespace clang