clang-tools  10.0.0
CompileCommandsTests.cpp
Go to the documentation of this file.
1 //===-- CompileCommandsTests.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 "CompileCommands.h"
10 #include "TestFS.h"
11 
12 #include "llvm/ADT/StringExtras.h"
13 
14 #include "gmock/gmock.h"
15 #include "gtest/gtest.h"
16 
17 namespace clang {
18 namespace clangd {
19 namespace {
20 
21 using ::testing::Contains;
22 using ::testing::ElementsAre;
23 using ::testing::HasSubstr;
24 using ::testing::Not;
25 
26 // Sadly, CommandMangler::detect(), which contains much of the logic, is
27 // a bunch of untested integration glue. We test the string manipulation here
28 // assuming its results are correct.
29 
30 // Make use of all features and assert the exact command we get out.
31 // Other tests just verify presence/absence of certain args.
32 TEST(CommandMangler, Everything) {
33  auto Mangler = CommandMangler::forTests();
34  Mangler.ClangPath = testPath("fake/clang");
35  Mangler.ResourceDir = testPath("fake/resources");
36  Mangler.Sysroot = testPath("fake/sysroot");
37  std::vector<std::string> Cmd = {"clang++", "-Xclang", "-load", "-Xclang",
38  "plugin", "-MF", "dep", "foo.cc"};
39  Mangler.adjust(Cmd);
40  EXPECT_THAT(Cmd, ElementsAre(testPath("fake/clang++"), "foo.cc",
41  "-fsyntax-only",
42  "-resource-dir=" + testPath("fake/resources"),
43  "-isysroot", testPath("fake/sysroot")));
44 }
45 
46 TEST(CommandMangler, ResourceDir) {
47  auto Mangler = CommandMangler::forTests();
48  Mangler.ResourceDir = testPath("fake/resources");
49  std::vector<std::string> Cmd = {"clang++", "foo.cc"};
50  Mangler.adjust(Cmd);
51  EXPECT_THAT(Cmd, Contains("-resource-dir=" + testPath("fake/resources")));
52 }
53 
54 TEST(CommandMangler, Sysroot) {
55  auto Mangler = CommandMangler::forTests();
56  Mangler.Sysroot = testPath("fake/sysroot");
57 
58  std::vector<std::string> Cmd = {"clang++", "foo.cc"};
59  Mangler.adjust(Cmd);
60  EXPECT_THAT(llvm::join(Cmd, " "),
61  HasSubstr("-isysroot " + testPath("fake/sysroot")));
62 }
63 
64 TEST(CommandMangler, StripPlugins) {
65  auto Mangler = CommandMangler::forTests();
66  std::vector<std::string> Cmd = {"clang++", "-Xclang", "-load",
67  "-Xclang", "plugin", "foo.cc"};
68  Mangler.adjust(Cmd);
69  for (const char* Stripped : {"-Xclang", "-load", "plugin"})
70  EXPECT_THAT(Cmd, Not(Contains(Stripped)));
71 }
72 
73 TEST(CommandMangler, StripOutput) {
74  auto Mangler = CommandMangler::forTests();
75  std::vector<std::string> Cmd = {"clang++", "-MF", "dependency", "-c",
76  "foo.cc"};
77  Mangler.adjust(Cmd);
78  for (const char* Stripped : {"-MF", "dependency"})
79  EXPECT_THAT(Cmd, Not(Contains(Stripped)));
80 }
81 
82 TEST(CommandMangler, ClangPath) {
83  auto Mangler = CommandMangler::forTests();
84  Mangler.ClangPath = testPath("fake/clang");
85 
86  std::vector<std::string> Cmd = {"clang++", "foo.cc"};
87  Mangler.adjust(Cmd);
88  EXPECT_EQ(testPath("fake/clang++"), Cmd.front());
89 
90  Cmd = {"unknown-binary", "foo.cc"};
91  Mangler.adjust(Cmd);
92  EXPECT_EQ("unknown-binary", Cmd.front());
93 
94  Cmd = {testPath("path/clang++"), "foo.cc"};
95  Mangler.adjust(Cmd);
96  EXPECT_EQ(testPath("path/clang++"), Cmd.front());
97 }
98 
99 } // namespace
100 } // namespace clangd
101 } // namespace clang
102 
static CommandMangler forTests()
TEST(BackgroundQueueTest, Priority)
std::string testPath(PathRef File)
Definition: TestFS.cpp:82
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static std::string join(ArrayRef< SpecialMemberFunctionsCheck::SpecialMemberFunctionKind > SMFS, llvm::StringRef AndOr)