File: CanonicalIncludesTests.cpp

package info (click to toggle)
llvm-toolchain-15 1%3A15.0.6-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,554,644 kB
  • sloc: cpp: 5,922,452; ansic: 1,012,136; asm: 674,362; python: 191,568; objc: 73,855; f90: 42,327; lisp: 31,913; pascal: 11,973; javascript: 10,144; sh: 9,421; perl: 7,447; ml: 5,527; awk: 3,523; makefile: 2,520; xml: 885; cs: 573; fortran: 567
file content (112 lines) | stat: -rw-r--r-- 4,233 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//===-- CanonicalIncludesTests.cpp - --------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "TestFS.h"
#include "index/CanonicalIncludes.h"
#include "clang/Basic/FileEntry.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/LangOptions.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h"

namespace clang {
namespace clangd {
namespace {

FileEntryRef addFile(llvm::vfs::InMemoryFileSystem &FS, FileManager &FM,
                     llvm::StringRef Filename) {
  FS.addFile(Filename, 0, llvm::MemoryBuffer::getMemBuffer(""));
  auto File = FM.getFileRef(Filename);
  EXPECT_THAT_EXPECTED(File, llvm::Succeeded());
  return *File;
}

TEST(CanonicalIncludesTest, CStandardLibrary) {
  CanonicalIncludes CI;
  auto Language = LangOptions();
  Language.C11 = true;
  CI.addSystemHeadersMapping(Language);
  // Usual standard library symbols are mapped correctly.
  EXPECT_EQ("<stdio.h>", CI.mapSymbol("printf"));
  EXPECT_EQ("", CI.mapSymbol("unknown_symbol"));
}

TEST(CanonicalIncludesTest, CXXStandardLibrary) {
  CanonicalIncludes CI;
  auto Language = LangOptions();
  Language.CPlusPlus = true;
  CI.addSystemHeadersMapping(Language);

  // Usual standard library symbols are mapped correctly.
  EXPECT_EQ("<vector>", CI.mapSymbol("std::vector"));
  EXPECT_EQ("<cstdio>", CI.mapSymbol("std::printf"));
  // std::move is ambiguous, currently always mapped to <utility>
  EXPECT_EQ("<utility>", CI.mapSymbol("std::move"));
  // Unknown std symbols aren't mapped.
  EXPECT_EQ("", CI.mapSymbol("std::notathing"));
  // iosfwd declares some symbols it doesn't own.
  EXPECT_EQ("<ostream>", CI.mapSymbol("std::ostream"));
  // And (for now) we assume it owns the others.
  auto InMemFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
  FileManager Files(FileSystemOptions(), InMemFS);
  auto File = addFile(*InMemFS, Files, testPath("iosfwd"));
  EXPECT_EQ("<iosfwd>", CI.mapHeader(File));
}

TEST(CanonicalIncludesTest, PathMapping) {
  auto InMemFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
  FileManager Files(FileSystemOptions(), InMemFS);
  std::string BarPath = testPath("foo/bar");
  auto Bar = addFile(*InMemFS, Files, BarPath);
  auto Other = addFile(*InMemFS, Files, testPath("foo/baz"));
  // As used for IWYU pragmas.
  CanonicalIncludes CI;
  CI.addMapping(Bar, "<baz>");

  // We added a mapping for baz.
  EXPECT_EQ("<baz>", CI.mapHeader(Bar));
  // Other file doesn't have a mapping.
  EXPECT_EQ("", CI.mapHeader(Other));

  // Add hard link to "foo/bar" and check that it is also mapped to <baz>, hence
  // does not depend on the header name.
  std::string HardLinkPath = testPath("hard/link");
  InMemFS->addHardLink(HardLinkPath, BarPath);
  auto HardLinkFile = Files.getFileRef(HardLinkPath);
  ASSERT_THAT_EXPECTED(HardLinkFile, llvm::Succeeded());
  EXPECT_EQ("<baz>", CI.mapHeader(*HardLinkFile));
}

TEST(CanonicalIncludesTest, Precedence) {
  auto InMemFS = llvm::makeIntrusiveRefCnt<llvm::vfs::InMemoryFileSystem>();
  FileManager Files(FileSystemOptions(), InMemFS);
  auto File = addFile(*InMemFS, Files, testPath("some/path"));

  CanonicalIncludes CI;
  CI.addMapping(File, "<path>");
  LangOptions Language;
  Language.CPlusPlus = true;
  CI.addSystemHeadersMapping(Language);

  // We added a mapping from some/path to <path>.
  ASSERT_EQ("<path>", CI.mapHeader(File));
  // We should have a path from 'bits/stl_vector.h' to '<vector>'.
  // FIXME: The Standrad Library map in CanonicalIncludes expects forward
  // slashes and Windows would use backward slashes instead, so the headers are
  // not matched appropriately.
  auto STLVectorFile = addFile(*InMemFS, Files, "bits/stl_vector.h");
  ASSERT_EQ("<vector>", CI.mapHeader(STLVectorFile));
}

} // namespace
} // namespace clangd
} // namespace clang