2018-08-14 08:03:32 -08:00
|
|
|
//===--- Compiler.cpp --------------------------------------------*- C++-*-===//
|
2017-12-04 04:49:59 -09:00
|
|
|
//
|
2019-01-18 23:50:56 -09: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 04:49:59 -09:00
|
|
|
//
|
2018-08-14 08:03:32 -08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2017-12-14 12:22:03 -09:00
|
|
|
|
2017-12-04 04:49:59 -09: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 07:49:17 -08:00
|
|
|
#include "support/Logger.h"
|
2017-12-04 04:49:59 -09:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
2025-11-26 03:33:26 -09:00
|
|
|
#include "clang/Driver/CreateInvocationFromArgs.h"
|
2021-07-23 06:57:33 -08:00
|
|
|
#include "clang/Frontend/CompilerInvocation.h"
|
2017-12-04 04:49:59 -09:00
|
|
|
#include "clang/Lex/PreprocessorOptions.h"
|
2019-04-04 04:56:03 -08:00
|
|
|
#include "clang/Serialization/PCHContainerOperations.h"
|
2020-06-16 02:16:24 -08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2017-12-04 04:49:59 -09:00
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
|
namespace clangd {
|
|
|
|
|
|
2018-02-12 03:48:51 -09:00
|
|
|
void IgnoreDiagnostics::log(DiagnosticsEngine::Level DiagLevel,
|
|
|
|
|
const clang::Diagnostic &Info) {
|
2018-11-02 04:51:26 -08:00
|
|
|
// FIXME: format lazily, in case vlog is off.
|
2019-01-07 06:45:19 -09:00
|
|
|
llvm::SmallString<64> Message;
|
2018-02-12 03:48:51 -09:00
|
|
|
Info.FormatDiagnostic(Message);
|
|
|
|
|
|
2019-01-07 06:45:19 -09:00
|
|
|
llvm::SmallString<64> Location;
|
2018-02-12 03:48:51 -09:00
|
|
|
if (Info.hasSourceManager() && Info.getLocation().isValid()) {
|
|
|
|
|
auto &SourceMgr = Info.getSourceManager();
|
|
|
|
|
auto Loc = SourceMgr.getFileLoc(Info.getLocation());
|
2019-01-07 06:45:19 -09:00
|
|
|
llvm::raw_svector_ostream OS(Location);
|
2018-02-12 03:48:51 -09:00
|
|
|
Loc.print(OS, SourceMgr);
|
|
|
|
|
OS << ":";
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-02 04:51:26 -08:00
|
|
|
clangd::vlog("Ignored diagnostic. {0}{1}", Location, Message);
|
2018-02-12 03:48:51 -09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IgnoreDiagnostics::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
|
|
|
|
|
const clang::Diagnostic &Info) {
|
|
|
|
|
IgnoreDiagnostics::log(DiagLevel, Info);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-26 05:27:07 -08:00
|
|
|
static bool AllowCrashPragmasForTest = false;
|
|
|
|
|
void allowCrashPragmasForTest() { AllowCrashPragmasForTest = true; }
|
|
|
|
|
|
2021-07-23 06: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;
|
2026-06-29 12:30:10 -08:00
|
|
|
CI.getDiagnosticOpts().setShowColors(ShowColorsKind::Off);
|
2021-07-23 06:57:33 -08:00
|
|
|
|
|
|
|
|
// 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 05:27:07 -08:00
|
|
|
if (!AllowCrashPragmasForTest)
|
|
|
|
|
CI.getPreprocessorOpts().DisablePragmaDebugCrash = true;
|
2021-07-23 06: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 =
|
2023-05-02 12:55:46 -08:00
|
|
|
PCHContainerOperations().getRawReader().getFormats().front().str();
|
2021-07-23 06:57:33 -08:00
|
|
|
|
|
|
|
|
CI.getFrontendOpts().Plugins.clear();
|
|
|
|
|
CI.getFrontendOpts().AddPluginActions.clear();
|
|
|
|
|
CI.getFrontendOpts().PluginArgs.clear();
|
|
|
|
|
CI.getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
|
|
|
|
|
CI.getFrontendOpts().ActionName.clear();
|
2023-01-04 00:42:24 -09:00
|
|
|
|
|
|
|
|
// These options mostly affect codegen, and aren't relevant to clangd. And
|
|
|
|
|
// clang will die immediately when these files are not existed.
|
|
|
|
|
// Disable these uninteresting options to make clangd more robust.
|
2023-08-31 12:44:31 -08:00
|
|
|
CI.getLangOpts().NoSanitizeFiles.clear();
|
|
|
|
|
CI.getLangOpts().XRayAttrListFiles.clear();
|
|
|
|
|
CI.getLangOpts().ProfileListFiles.clear();
|
|
|
|
|
CI.getLangOpts().XRayAlwaysInstrumentFiles.clear();
|
|
|
|
|
CI.getLangOpts().XRayNeverInstrumentFiles.clear();
|
2021-07-23 06:57:33 -08:00
|
|
|
}
|
|
|
|
|
|
2019-01-22 00:58:53 -09:00
|
|
|
std::unique_ptr<CompilerInvocation>
|
2020-05-09 02:34:06 -08:00
|
|
|
buildCompilerInvocation(const ParseInputs &Inputs, clang::DiagnosticConsumer &D,
|
2019-11-28 09:22:50 -09:00
|
|
|
std::vector<std::string> *CC1Args) {
|
2022-05-07 07:31:52 -08:00
|
|
|
llvm::ArrayRef<std::string> Argv = Inputs.CompileCommand.CommandLine;
|
|
|
|
|
if (Argv.empty())
|
2021-09-16 07:43:15 -08:00
|
|
|
return nullptr;
|
2019-01-22 00:58:53 -09:00
|
|
|
std::vector<const char *> ArgStrs;
|
2022-05-07 07:31:52 -08:00
|
|
|
ArgStrs.reserve(Argv.size() + 1);
|
|
|
|
|
// In asserts builds, CompilerInvocation redundantly reads/parses cc1 args as
|
|
|
|
|
// a sanity test. This is not useful to clangd, and costs 10% of test time.
|
|
|
|
|
// To avoid mismatches between assert/production builds, disable it always.
|
|
|
|
|
ArgStrs = {Argv.front().c_str(), "-Xclang", "-no-round-trip-args"};
|
|
|
|
|
for (const auto &S : Argv.drop_front())
|
2019-01-22 00:58:53 -09:00
|
|
|
ArgStrs.push_back(S.c_str());
|
|
|
|
|
|
2022-05-04 16:15:24 -08:00
|
|
|
CreateInvocationOptions CIOpts;
|
|
|
|
|
CIOpts.VFS = Inputs.TFS->view(Inputs.CompileCommand.Directory);
|
|
|
|
|
CIOpts.CC1Args = CC1Args;
|
|
|
|
|
CIOpts.RecoverOnError = true;
|
2025-05-22 11:51:28 -08:00
|
|
|
DiagnosticOptions DiagOpts;
|
|
|
|
|
CIOpts.Diags =
|
|
|
|
|
CompilerInstance::createDiagnostics(*CIOpts.VFS, DiagOpts, &D, false);
|
[Driver] Make "upgrade" of -include to include-pch optional; disable in clangd
If clang is passed "-include foo.h", it will rewrite to "-include-pch foo.h.pch"
before passing it to cc1, if foo.h.pch exists.
Existence is checked, but validity is not. This is probably a reasonable
assumption for the compiler itself, but not for clang-based tools where the
actual compiler may be a different version of clang, or even GCC.
In the end, we lose our -include, we gain a -include-pch that can't be used,
and the file often fails to parse.
I would like to turn this off for all non-clang invocations (i.e.
createInvocationFromCommandLine), but we have explicit tests of this behavior
for libclang and I can't work out the implications of changing it.
Instead this patch:
- makes it optional in the driver, default on (no change)
- makes it optional in createInvocationFromCommandLine, default on (no change)
- changes driver to do IO through the VFS so it can be tested
- tests the option
- turns the option off in clangd where the problem was reported
Subsequent patches should make libclang opt in explicitly and flip the default
for all other tools. It's probably also time to extract an options struct
for createInvocationFromCommandLine.
Fixes https://github.com/clangd/clangd/issues/856
Fixes https://github.com/clangd/vscode-clangd/issues/324
Differential Revision: https://reviews.llvm.org/D124970
2022-05-04 15:15:28 -08:00
|
|
|
CIOpts.ProbePrecompiled = false;
|
2022-05-04 16:15:24 -08:00
|
|
|
std::unique_ptr<CompilerInvocation> CI = createInvocation(ArgStrs, CIOpts);
|
2019-01-22 00:58:53 -09:00
|
|
|
if (!CI)
|
|
|
|
|
return nullptr;
|
|
|
|
|
// createInvocationFromCommandLine sets DisableFree.
|
|
|
|
|
CI->getFrontendOpts().DisableFree = false;
|
2023-08-31 12:44:31 -08:00
|
|
|
CI->getLangOpts().CommentOpts.ParseAllComments = true;
|
|
|
|
|
CI->getLangOpts().RetainCommentsFromSystemHeaders = true;
|
2021-07-24 09:44:15 -08:00
|
|
|
|
2021-07-23 06:57:33 -08:00
|
|
|
disableUnsupportedOptions(*CI);
|
2019-01-22 00:58:53 -09:00
|
|
|
return CI;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-07 06:45:19 -09: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 04:49:59 -09: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 06:17:00 -09:00
|
|
|
Preamble->OverridePreamble(*CI, VFS, Buffer.get());
|
2017-12-04 04:49:59 -09:00
|
|
|
} else {
|
|
|
|
|
CI->getPreprocessorOpts().addRemappedFile(
|
|
|
|
|
CI->getFrontendOpts().Inputs[0].getFile(), Buffer.get());
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-01 06:31:30 -08:00
|
|
|
auto Clang = std::make_unique<CompilerInstance>(std::move(CI));
|
2025-09-16 07:21:06 -08:00
|
|
|
Clang->createVirtualFileSystem(VFS, &DiagsClient);
|
|
|
|
|
Clang->createDiagnostics(&DiagsClient, false);
|
|
|
|
|
Clang->createFileManager();
|
2021-04-06 07:21:19 -08:00
|
|
|
if (!Clang->createTarget())
|
2017-12-04 04:49:59 -09:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
|
|
// RemappedFileBuffers will handle the lifetime of the Buffer pointer,
|
|
|
|
|
// release it.
|
|
|
|
|
Buffer.release();
|
|
|
|
|
return Clang;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace clangd
|
|
|
|
|
} // namespace clang
|