clang-tools  11.0.0
CanonicalIncludesTests.cpp
Go to the documentation of this file.
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 
10 #include "clang/Basic/LangOptions.h"
11 #include "gtest/gtest.h"
12 
13 namespace clang {
14 namespace clangd {
15 namespace {
16 
17 TEST(CanonicalIncludesTest, CStandardLibrary) {
18  CanonicalIncludes CI;
19  auto Language = LangOptions();
20  Language.C11 = true;
21  CI.addSystemHeadersMapping(Language);
22  // Usual standard library symbols are mapped correctly.
23  EXPECT_EQ("<stdio.h>", CI.mapHeader("path/stdio.h", "printf"));
24 }
25 
26 TEST(CanonicalIncludesTest, CXXStandardLibrary) {
27  CanonicalIncludes CI;
28  auto Language = LangOptions();
29  Language.CPlusPlus = true;
30  CI.addSystemHeadersMapping(Language);
31 
32  // Usual standard library symbols are mapped correctly.
33  EXPECT_EQ("<vector>", CI.mapHeader("path/vector.h", "std::vector"));
34  EXPECT_EQ("<cstdio>", CI.mapHeader("path/stdio.h", "std::printf"));
35  // std::move is ambiguous, currently mapped only based on path
36  EXPECT_EQ("<utility>", CI.mapHeader("libstdc++/bits/move.h", "std::move"));
37  EXPECT_EQ("path/utility.h", CI.mapHeader("path/utility.h", "std::move"));
38  // Unknown std symbols aren't mapped.
39  EXPECT_EQ("foo/bar.h", CI.mapHeader("foo/bar.h", "std::notathing"));
40  // iosfwd declares some symbols it doesn't own.
41  EXPECT_EQ("<ostream>", CI.mapHeader("iosfwd", "std::ostream"));
42  // And (for now) we assume it owns the others.
43  EXPECT_EQ("<iosfwd>", CI.mapHeader("iosfwd", "std::notwathing"));
44 }
45 
46 TEST(CanonicalIncludesTest, PathMapping) {
47  // As used for IWYU pragmas.
48  CanonicalIncludes CI;
49  CI.addMapping("foo/bar", "<baz>");
50 
51  EXPECT_EQ("<baz>", CI.mapHeader("foo/bar", "some::symbol"));
52  EXPECT_EQ("bar/bar", CI.mapHeader("bar/bar", "some::symbol"));
53 }
54 
55 TEST(CanonicalIncludesTest, SymbolMapping) {
56  // As used for standard library.
57  CanonicalIncludes CI;
58  LangOptions Language;
59  Language.CPlusPlus = true;
60  // Ensures 'std::vector' is mapped to '<vector>'.
61  CI.addSystemHeadersMapping(Language);
62 
63  EXPECT_EQ("<vector>", CI.mapHeader("foo/bar", "std::vector"));
64  EXPECT_EQ("foo/bar", CI.mapHeader("foo/bar", "other::symbol"));
65 }
66 
67 TEST(CanonicalIncludesTest, Precedence) {
68  CanonicalIncludes CI;
69  CI.addMapping("some/path", "<path>");
70  LangOptions Language;
71  Language.CPlusPlus = true;
72  CI.addSystemHeadersMapping(Language);
73 
74  // We added a mapping from some/path to <path>.
75  ASSERT_EQ("<path>", CI.mapHeader("some/path", ""));
76  // We should have a path from 'bits/stl_vector.h' to '<vector>'.
77  ASSERT_EQ("<vector>", CI.mapHeader("bits/stl_vector.h", ""));
78  // We should also have a symbol mapping from 'std::map' to '<map>'.
79  ASSERT_EQ("<map>", CI.mapHeader("some/header.h", "std::map"));
80 
81  // And the symbol mapping should take precedence over paths mapping.
82  EXPECT_EQ("<map>", CI.mapHeader("bits/stl_vector.h", "std::map"));
83  EXPECT_EQ("<map>", CI.mapHeader("some/path", "std::map"));
84 }
85 
86 } // namespace
87 } // namespace clangd
88 } // namespace clang
clang::clangd::TEST
TEST(BackgroundQueueTest, Priority)
Definition: BackgroundIndexTests.cpp:704
CI
std::unique_ptr< CompilerInvocation > CI
Definition: TUScheduler.cpp:320
CanonicalIncludes.h
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27