aboutsummaryrefslogtreecommitdiff
path: root/clang-tools-extra/clangd/unittests/CompilerTests.cpp
blob: ab9e85e983d1c6550bd8f8d561140d9ded927613 (plain)
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
113
114
115
116
117
118
//===-- CompilerTests.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 "Compiler.h"
#include "TestTU.h"
#include "clang/Frontend/DependencyOutputOptions.h"
#include "clang/Frontend/FrontendOptions.h"
#include "clang/Lex/PreprocessorOptions.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

namespace clang {
namespace clangd {
namespace {

using testing::IsEmpty;

TEST(BuildCompilerInvocation, DropsPCH) {
  MockFS FS;
  IgnoreDiagnostics Diags;
  TestTU TU;
  TU.AdditionalFiles["test.h.pch"] = "";

  TU.ExtraArgs = {"-include-pch", "test.h.pch"};
  EXPECT_THAT(buildCompilerInvocation(TU.inputs(FS), Diags)
                  ->getPreprocessorOpts()
                  .ImplicitPCHInclude,
              IsEmpty());

  // Transparent include translation
  TU.ExtraArgs = {"-include", "test.h"};
  EXPECT_THAT(buildCompilerInvocation(TU.inputs(FS), Diags)
                  ->getPreprocessorOpts()
                  .ImplicitPCHInclude,
              IsEmpty());

  // CL mode parsing.
  TU.AdditionalFiles["test.pch"] = "";
  TU.ExtraArgs = {"--driver-mode=cl"};
  TU.ExtraArgs.push_back("/Yutest.h");
  EXPECT_THAT(buildCompilerInvocation(TU.inputs(FS), Diags)
                  ->getPreprocessorOpts()
                  .ImplicitPCHInclude,
              IsEmpty());
  EXPECT_THAT(buildCompilerInvocation(TU.inputs(FS), Diags)
                  ->getPreprocessorOpts()
                  .PCHThroughHeader,
              IsEmpty());
}

TEST(BuildCompilerInvocation, PragmaDebugCrash) {
  TestTU TU = TestTU::withCode("#pragma clang __debug parser_crash");
  TU.build(); // no-crash
}

TEST(BuildCompilerInvocation, DropsShowIncludes) {
  MockFS FS;
  IgnoreDiagnostics Diags;
  TestTU TU;

  TU.ExtraArgs = {"-Xclang", "--show-includes"};
  EXPECT_THAT(buildCompilerInvocation(TU.inputs(FS), Diags)
                  ->getDependencyOutputOpts()
                  .ShowIncludesDest,
              ShowIncludesDestination::None);

  TU.ExtraArgs = {"/showIncludes", "--driver-mode=cl"};
  EXPECT_THAT(buildCompilerInvocation(TU.inputs(FS), Diags)
                  ->getDependencyOutputOpts()
                  .ShowIncludesDest,
              ShowIncludesDestination::None);

  TU.ExtraArgs = {"/showIncludes:user", "--driver-mode=cl"};
  EXPECT_THAT(buildCompilerInvocation(TU.inputs(FS), Diags)
                  ->getDependencyOutputOpts()
                  .ShowIncludesDest,
              ShowIncludesDestination::None);
}

TEST(BuildCompilerInvocation, DropsPlugins) {
  MockFS FS;
  IgnoreDiagnostics Diags;
  TestTU TU;

  TU.ExtraArgs = {"-Xclang", "-load",
                  "-Xclang", "plugins.so",
                  "-Xclang", "-plugin",
                  "-Xclang", "my_plugin",
                  "-Xclang", "-plugin-arg-my_plugin",
                  "-Xclang", "foo=bar",
                  "-Xclang", "-add-plugin",
                  "-Xclang", "my_plugin2"};
  auto Opts = buildCompilerInvocation(TU.inputs(FS), Diags)->getFrontendOpts();
  EXPECT_THAT(Opts.Plugins, IsEmpty());
  EXPECT_THAT(Opts.PluginArgs, IsEmpty());
  EXPECT_THAT(Opts.AddPluginActions, IsEmpty());
  EXPECT_EQ(Opts.ProgramAction, frontend::ActionKind::ParseSyntaxOnly);
  EXPECT_TRUE(Opts.ActionName.empty());
}

TEST(BuildCompilerInvocation, EmptyArgs) {
  MockFS FS;
  IgnoreDiagnostics Diags;
  TestTU TU;
  auto Inputs = TU.inputs(FS);
  Inputs.CompileCommand.CommandLine.clear();

  // No crash.
  EXPECT_EQ(buildCompilerInvocation(Inputs, Diags), nullptr);
}
} // namespace
} // namespace clangd
} // namespace clang