2018-08-15 00:03:32 +08:00
|
|
|
//===--- Compiler.cpp --------------------------------------------*- C++-*-===//
|
2017-12-04 21:49:59 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2017-12-04 21:49:59 +08:00
|
|
|
//
|
2018-08-15 00:03:32 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2017-12-15 05:22:03 +08:00
|
|
|
|
2017-12-04 21:49:59 +08:00
|
|
|
#include "Compiler.h"
|
[clangd] Move non-clang base pieces into separate support/ lib. NFCI
Summary:
This enforces layering, reduces a sprawling clangd/ directory, and makes life
easier for embedders.
Reviewers: kbobyrev
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D79014
2020-04-28 23:49:17 +08:00
|
|
|
#include "support/Logger.h"
|
2017-12-04 21:49:59 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2021-07-23 22:57:33 +08:00
|
|
|
#include "clang/Frontend/CompilerInvocation.h"
|
2017-12-04 21:49:59 +08:00
|
|
|
#include "clang/Lex/PreprocessorOptions.h"
|
2019-04-04 20:56:03 +08:00
|
|
|
#include "clang/Serialization/PCHContainerOperations.h"
|
2020-06-16 18:16:24 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2017-12-04 21:49:59 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
|
2018-02-12 20:48:51 +08:00
|
|
|
void IgnoreDiagnostics::log(DiagnosticsEngine::Level DiagLevel,
|
|
|
|
const clang::Diagnostic &Info) {
|
2018-11-02 20:51:26 +08:00
|
|
|
// FIXME: format lazily, in case vlog is off.
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::SmallString<64> Message;
|
2018-02-12 20:48:51 +08:00
|
|
|
Info.FormatDiagnostic(Message);
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::SmallString<64> Location;
|
2018-02-12 20:48:51 +08:00
|
|
|
if (Info.hasSourceManager() && Info.getLocation().isValid()) {
|
|
|
|
auto &SourceMgr = Info.getSourceManager();
|
|
|
|
auto Loc = SourceMgr.getFileLoc(Info.getLocation());
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::raw_svector_ostream OS(Location);
|
2018-02-12 20:48:51 +08:00
|
|
|
Loc.print(OS, SourceMgr);
|
|
|
|
OS << ":";
|
|
|
|
}
|
|
|
|
|
2018-11-02 20:51:26 +08:00
|
|
|
clangd::vlog("Ignored diagnostic. {0}{1}", Location, Message);
|
2018-02-12 20:48:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void IgnoreDiagnostics::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
|
|
|
|
const clang::Diagnostic &Info) {
|
|
|
|
IgnoreDiagnostics::log(DiagLevel, Info);
|
|
|
|
}
|
|
|
|
|
2021-10-26 21:27:07 +08:00
|
|
|
static bool AllowCrashPragmasForTest = false;
|
|
|
|
void allowCrashPragmasForTest() { AllowCrashPragmasForTest = true; }
|
|
|
|
|
2021-07-23 22:57:33 +08:00
|
|
|
void disableUnsupportedOptions(CompilerInvocation &CI) {
|
|
|
|
// Disable "clang -verify" diagnostics, they are rarely useful in clangd, and
|
|
|
|
// our compiler invocation set-up doesn't seem to work with it (leading
|
|
|
|
// assertions in VerifyDiagnosticConsumer).
|
|
|
|
CI.getDiagnosticOpts().VerifyDiagnostics = false;
|
|
|
|
CI.getDiagnosticOpts().ShowColors = false;
|
|
|
|
|
|
|
|
// Disable any dependency outputting, we don't want to generate files or write
|
|
|
|
// to stdout/stderr.
|
|
|
|
CI.getDependencyOutputOpts().ShowIncludesDest = ShowIncludesDestination::None;
|
|
|
|
CI.getDependencyOutputOpts().OutputFile.clear();
|
|
|
|
CI.getDependencyOutputOpts().HeaderIncludeOutputFile.clear();
|
|
|
|
CI.getDependencyOutputOpts().DOTOutputFile.clear();
|
|
|
|
CI.getDependencyOutputOpts().ModuleDependencyOutputDir.clear();
|
|
|
|
|
|
|
|
// Disable any pch generation/usage operations. Since serialized preamble
|
|
|
|
// format is unstable, using an incompatible one might result in unexpected
|
|
|
|
// behaviours, including crashes.
|
|
|
|
CI.getPreprocessorOpts().ImplicitPCHInclude.clear();
|
|
|
|
CI.getPreprocessorOpts().PrecompiledPreambleBytes = {0, false};
|
|
|
|
CI.getPreprocessorOpts().PCHThroughHeader.clear();
|
|
|
|
CI.getPreprocessorOpts().PCHWithHdrStop = false;
|
|
|
|
CI.getPreprocessorOpts().PCHWithHdrStopCreate = false;
|
|
|
|
// Don't crash on `#pragma clang __debug parser_crash`
|
2021-10-26 21:27:07 +08:00
|
|
|
if (!AllowCrashPragmasForTest)
|
|
|
|
CI.getPreprocessorOpts().DisablePragmaDebugCrash = true;
|
2021-07-23 22:57:33 +08:00
|
|
|
|
|
|
|
// Always default to raw container format as clangd doesn't registry any other
|
|
|
|
// and clang dies when faced with unknown formats.
|
|
|
|
CI.getHeaderSearchOpts().ModuleFormat =
|
|
|
|
PCHContainerOperations().getRawReader().getFormat().str();
|
|
|
|
|
|
|
|
CI.getFrontendOpts().Plugins.clear();
|
|
|
|
CI.getFrontendOpts().AddPluginActions.clear();
|
|
|
|
CI.getFrontendOpts().PluginArgs.clear();
|
|
|
|
CI.getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
|
|
|
|
CI.getFrontendOpts().ActionName.clear();
|
|
|
|
}
|
|
|
|
|
2019-01-22 17:58:53 +08:00
|
|
|
std::unique_ptr<CompilerInvocation>
|
2020-05-09 18:34:06 +08:00
|
|
|
buildCompilerInvocation(const ParseInputs &Inputs, clang::DiagnosticConsumer &D,
|
2019-11-29 02:22:50 +08:00
|
|
|
std::vector<std::string> *CC1Args) {
|
2021-09-16 23:43:15 +08:00
|
|
|
if (Inputs.CompileCommand.CommandLine.empty())
|
|
|
|
return nullptr;
|
2019-01-22 17:58:53 +08:00
|
|
|
std::vector<const char *> ArgStrs;
|
|
|
|
for (const auto &S : Inputs.CompileCommand.CommandLine)
|
|
|
|
ArgStrs.push_back(S.c_str());
|
|
|
|
|
2020-06-18 00:09:54 +08:00
|
|
|
auto VFS = Inputs.TFS->view(Inputs.CompileCommand.Directory);
|
2019-01-22 17:58:53 +08:00
|
|
|
llvm::IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine =
|
[clangd] Surface errors from command-line parsing
Summary:
Those errors are exposed at the first character of a file,
for a lack of a better place.
Previously, all errors were stored inside the AST and report
accordingly. However, errors in command-line argument parsing could
result in failure to produce the AST, so we need an alternative ways to
report those errors.
We take the following approach in this patch:
- buildCompilerInvocation() now requires an explicit DiagnosticConsumer.
- TUScheduler and TestTU now collect the diagnostics produced when
parsing command line arguments.
If pasing of the AST failed, diagnostics are reported via a new
ParsingCallbacks::onFailedAST method.
If parsing of the AST succeeded, any errors produced during
command-line parsing are stored alongside the AST inside the
ParsedAST instance and reported as previously by calling the
ParsingCallbacks::onMainAST method;
- The client code that uses ClangdServer's DiagnosticConsumer
does not need to change, it will receive new diagnostics in the
onDiagnosticsReady() callback
Errors produced when parsing command-line arguments are collected using
the same StoreDiags class that is used to collect all other errors. They
are recognized by their location being invalid. IIUC, the location is
invalid as there is no source manager at this point, it is created at a
later stage.
Although technically we might also get diagnostics that mention the
command-line arguments FileID with after the source manager was created
(and they have valid source locations), we choose to not handle those
and they are dropped as not coming from the main file. AFAICT, those
diagnostics should always be notes, therefore it's safe to drop them
without loosing too much information.
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, javed.absar, MaskRay, jkorous, arphaman, cfe-commits, gribozavr
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66759
llvm-svn: 370177
2019-08-28 17:24:55 +08:00
|
|
|
CompilerInstance::createDiagnostics(new DiagnosticOptions, &D, false);
|
2019-01-22 17:58:53 +08:00
|
|
|
std::unique_ptr<CompilerInvocation> CI = createInvocationFromCommandLine(
|
2020-06-05 00:26:52 +08:00
|
|
|
ArgStrs, CommandLineDiagsEngine, std::move(VFS),
|
2019-11-29 02:22:50 +08:00
|
|
|
/*ShouldRecoverOnErrors=*/true, CC1Args);
|
2019-01-22 17:58:53 +08:00
|
|
|
if (!CI)
|
|
|
|
return nullptr;
|
|
|
|
// createInvocationFromCommandLine sets DisableFree.
|
|
|
|
CI->getFrontendOpts().DisableFree = false;
|
|
|
|
CI->getLangOpts()->CommentOpts.ParseAllComments = true;
|
2019-11-07 16:53:07 +08:00
|
|
|
CI->getLangOpts()->RetainCommentsFromSystemHeaders = true;
|
2021-07-25 01:44:15 +08:00
|
|
|
|
2021-07-23 22:57:33 +08:00
|
|
|
disableUnsupportedOptions(*CI);
|
2019-01-22 17:58:53 +08:00
|
|
|
return CI;
|
|
|
|
}
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
std::unique_ptr<CompilerInstance>
|
|
|
|
prepareCompilerInstance(std::unique_ptr<clang::CompilerInvocation> CI,
|
|
|
|
const PrecompiledPreamble *Preamble,
|
|
|
|
std::unique_ptr<llvm::MemoryBuffer> Buffer,
|
|
|
|
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
|
|
|
|
DiagnosticConsumer &DiagsClient) {
|
2017-12-04 21:49:59 +08:00
|
|
|
assert(VFS && "VFS is null");
|
|
|
|
assert(!CI->getPreprocessorOpts().RetainRemappedFileBuffers &&
|
|
|
|
"Setting RetainRemappedFileBuffers to true will cause a memory leak "
|
|
|
|
"of ContentsBuffer");
|
|
|
|
|
|
|
|
// NOTE: we use Buffer.get() when adding remapped files, so we have to make
|
|
|
|
// sure it will be released if no error is emitted.
|
|
|
|
if (Preamble) {
|
2018-01-18 23:17:00 +08:00
|
|
|
Preamble->OverridePreamble(*CI, VFS, Buffer.get());
|
2017-12-04 21:49:59 +08:00
|
|
|
} else {
|
|
|
|
CI->getPreprocessorOpts().addRemappedFile(
|
|
|
|
CI->getFrontendOpts().Inputs[0].getFile(), Buffer.get());
|
|
|
|
}
|
|
|
|
|
2019-08-15 07:52:23 +08:00
|
|
|
auto Clang = std::make_unique<CompilerInstance>(
|
2019-04-04 20:56:03 +08:00
|
|
|
std::make_shared<PCHContainerOperations>());
|
2017-12-04 21:49:59 +08:00
|
|
|
Clang->setInvocation(std::move(CI));
|
|
|
|
Clang->createDiagnostics(&DiagsClient, false);
|
|
|
|
|
|
|
|
if (auto VFSWithRemapping = createVFSFromCompilerInvocation(
|
|
|
|
Clang->getInvocation(), Clang->getDiagnostics(), VFS))
|
|
|
|
VFS = VFSWithRemapping;
|
2019-03-27 06:18:52 +08:00
|
|
|
Clang->createFileManager(VFS);
|
2017-12-04 21:49:59 +08:00
|
|
|
|
2021-04-06 23:21:19 +08:00
|
|
|
if (!Clang->createTarget())
|
2017-12-04 21:49:59 +08:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// RemappedFileBuffers will handle the lifetime of the Buffer pointer,
|
|
|
|
// release it.
|
|
|
|
Buffer.release();
|
|
|
|
return Clang;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|