clang-tools 23.0.0git
CompilerTests.cpp
Go to the documentation of this file.
1//===-- CompilerTests.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 "Compiler.h"
10#include "Config.h"
11#include "Diagnostics.h"
12#include "TestTU.h"
13#include "clang/Frontend/DependencyOutputOptions.h"
14#include "clang/Frontend/FrontendOptions.h"
15#include "clang/Lex/PreprocessorOptions.h"
16#include "gmock/gmock.h"
17#include "gtest/gtest.h"
18
19namespace clang {
20namespace clangd {
21namespace {
22
23using testing::IsEmpty;
24
25TEST(BuildCompilerInvocation, DropsPCH) {
26 MockFS FS;
28 TestTU TU;
29 TU.AdditionalFiles["test.h.pch"] = "";
30
31 TU.ExtraArgs = {"-include-pch", "test.h.pch"};
32 EXPECT_THAT(buildCompilerInvocation(TU.inputs(FS), Diags)
33 ->getPreprocessorOpts()
34 .ImplicitPCHInclude,
35 IsEmpty());
36
37 // Transparent include translation
38 TU.ExtraArgs = {"-include", "test.h"};
39 EXPECT_THAT(buildCompilerInvocation(TU.inputs(FS), Diags)
40 ->getPreprocessorOpts()
41 .ImplicitPCHInclude,
42 IsEmpty());
43
44 // CL mode parsing.
45 TU.AdditionalFiles["test.pch"] = "";
46 TU.ExtraArgs = {"--driver-mode=cl"};
47 TU.ExtraArgs.push_back("/Yutest.h");
48 EXPECT_THAT(buildCompilerInvocation(TU.inputs(FS), Diags)
49 ->getPreprocessorOpts()
50 .ImplicitPCHInclude,
51 IsEmpty());
52 EXPECT_THAT(buildCompilerInvocation(TU.inputs(FS), Diags)
53 ->getPreprocessorOpts()
54 .PCHThroughHeader,
55 IsEmpty());
56}
57
58TEST(BuildCompilerInvocation, PragmaDebugCrash) {
59 TestTU TU = TestTU::withCode("#pragma clang __debug parser_crash");
60 TU.build(); // no-crash
61}
62
63TEST(BuildCompilerInvocation, DropsShowIncludes) {
64 MockFS FS;
66 TestTU TU;
67
68 TU.ExtraArgs = {"-Xclang", "--show-includes"};
69 EXPECT_THAT(buildCompilerInvocation(TU.inputs(FS), Diags)
70 ->getDependencyOutputOpts()
71 .ShowIncludesDest,
72 ShowIncludesDestination::None);
73
74 TU.ExtraArgs = {"/showIncludes", "--driver-mode=cl"};
75 EXPECT_THAT(buildCompilerInvocation(TU.inputs(FS), Diags)
76 ->getDependencyOutputOpts()
77 .ShowIncludesDest,
78 ShowIncludesDestination::None);
79
80 TU.ExtraArgs = {"/showIncludes:user", "--driver-mode=cl"};
81 EXPECT_THAT(buildCompilerInvocation(TU.inputs(FS), Diags)
82 ->getDependencyOutputOpts()
83 .ShowIncludesDest,
84 ShowIncludesDestination::None);
85}
86
87TEST(BuildCompilerInvocation, DropsPlugins) {
88 MockFS FS;
90 TestTU TU;
91
92 TU.ExtraArgs = {"-Xclang", "-load",
93 "-Xclang", "plugins.so",
94 "-Xclang", "-plugin",
95 "-Xclang", "my_plugin",
96 "-Xclang", "-plugin-arg-my_plugin",
97 "-Xclang", "foo=bar",
98 "-Xclang", "-add-plugin",
99 "-Xclang", "my_plugin2"};
100 auto Opts = buildCompilerInvocation(TU.inputs(FS), Diags)->getFrontendOpts();
101 EXPECT_THAT(Opts.Plugins, IsEmpty());
102 EXPECT_THAT(Opts.PluginArgs, IsEmpty());
103 EXPECT_THAT(Opts.AddPluginActions, IsEmpty());
104 EXPECT_EQ(Opts.ProgramAction, frontend::ActionKind::ParseSyntaxOnly);
105 EXPECT_TRUE(Opts.ActionName.empty());
106}
107
108TEST(BuildCompilerInvocation, EmptyArgs) {
109 MockFS FS;
110 IgnoreDiagnostics Diags;
111 TestTU TU;
112 auto Inputs = TU.inputs(FS);
113 Inputs.CompileCommand.CommandLine.clear();
114
115 // No crash.
116 EXPECT_EQ(buildCompilerInvocation(Inputs, Diags), nullptr);
117}
118TEST(BuildCompilerInvocation, SuppressDiags) {
119 MockFS FS;
120 StoreDiags Diags;
121 TestTU TU;
122 TU.ExtraArgs = {"-funknown-arg"};
123 auto Inputs = TU.inputs(FS);
124
125 Config Cfg;
126 Cfg.Diagnostics.Suppress = {"drv_unknown_argument"};
127 WithContextValue SuppressFilterWithCfg(Config::Key, std::move(Cfg));
128 EXPECT_NE(buildCompilerInvocation(Inputs, Diags), nullptr);
129 EXPECT_THAT(Diags.take(), IsEmpty());
130}
131} // namespace
132} // namespace clangd
133} // namespace clang
StoreDiags collects the diagnostics that can later be reported by clangd.
WithContextValue extends Context::current() with a single value.
Definition Context.h:200
FIXME: Skip testing on windows temporarily due to the different escaping code mode.
Definition AST.cpp:44
std::unique_ptr< CompilerInvocation > buildCompilerInvocation(const ParseInputs &Inputs, clang::DiagnosticConsumer &D, std::vector< std::string > *CC1Args)
Builds compiler invocation that could be used to build AST or preamble.
Definition Compiler.cpp:96
TEST(BackgroundQueueTest, Priority)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Settings that express user/project preferences and control clangd behavior.
Definition Config.h:44
static clangd::Key< Config > Key
Context key which can be used to set the current Config.
Definition Config.h:48
llvm::StringSet Suppress
Definition Config.h:105
struct clang::clangd::Config::@224206046260313204212274150166346126315121140114 Diagnostics
Controls warnings and errors when parsing code.
tooling::CompileCommand CompileCommand
Definition Compiler.h:50
std::vector< std::string > ExtraArgs
Definition TestTU.h:60
ParseInputs inputs(MockFS &FS) const
Definition TestTU.cpp:27
static TestTU withCode(llvm::StringRef Code)
Definition TestTU.h:36
llvm::StringMap< std::string > AdditionalFiles
Definition TestTU.h:57