46 changes: 46 additions & 0 deletions clang-tools-extra/clangd/unittests/CompilerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#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"
Expand Down Expand Up @@ -56,6 +58,50 @@ TEST(BuildCompilerInvocation, PragmaDebugCrash) {
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());
}
} // namespace
} // namespace clangd
} // namespace clang
4 changes: 4 additions & 0 deletions clang-tools-extra/clangd/unittests/TestTU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "TestTU.h"
#include "CompileCommands.h"
#include "Compiler.h"
#include "Diagnostics.h"
#include "TestFS.h"
Expand Down Expand Up @@ -56,6 +57,9 @@ ParseInputs TestTU::inputs(MockFS &FS) const {
// Put the file name at the end -- this allows the extra arg (-xc++) to
// override the language setting.
Argv.push_back(FullFilename);

auto Mangler = CommandMangler::forTests();
Mangler.adjust(Inputs.CompileCommand.CommandLine, FullFilename);
Inputs.CompileCommand.Filename = FullFilename;
Inputs.CompileCommand.Directory = testRoot();
Inputs.Contents = Code;
Expand Down
11 changes: 8 additions & 3 deletions clang/lib/Frontend/CreateInvocationFromCommandLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@
//
//===----------------------------------------------------------------------===//

#include "clang/Frontend/Utils.h"
#include "clang/Basic/DiagnosticOptions.h"
#include "clang/Driver/Action.h"
#include "clang/Driver/Compilation.h"
#include "clang/Driver/Driver.h"
#include "clang/Driver/Action.h"
#include "clang/Driver/Options.h"
#include "clang/Driver/Tool.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendDiagnostic.h"
#include "clang/Frontend/Utils.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Option/ArgList.h"
#include "llvm/Support/Host.h"
using namespace clang;
Expand All @@ -37,7 +39,10 @@ std::unique_ptr<CompilerInvocation> clang::createInvocationFromCommandLine(
SmallVector<const char *, 16> Args(ArgList.begin(), ArgList.end());

// FIXME: Find a cleaner way to force the driver into restricted modes.
Args.push_back("-fsyntax-only");
Args.insert(
llvm::find_if(
Args, [](const char *Elem) { return llvm::StringRef(Elem) == "--"; }),
"-fsyntax-only");

// FIXME: We shouldn't have to pass in the path info.
driver::Driver TheDriver(Args[0], llvm::sys::getDefaultTargetTriple(), *Diags,
Expand Down