llvm/clang-tools-extra/clangd/Compiler.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

138 lines
5.3 KiB
C++
Raw Normal View History

//===--- Compiler.cpp --------------------------------------------*- C++-*-===//
//
// 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 "support/Logger.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Lex/PreprocessorOptions.h"
#include "clang/Serialization/PCHContainerOperations.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/FormatVariadic.h"
namespace clang {
namespace clangd {
void IgnoreDiagnostics::log(DiagnosticsEngine::Level DiagLevel,
const clang::Diagnostic &Info) {
// FIXME: format lazily, in case vlog is off.
llvm::SmallString<64> Message;
Info.FormatDiagnostic(Message);
llvm::SmallString<64> Location;
if (Info.hasSourceManager() && Info.getLocation().isValid()) {
auto &SourceMgr = Info.getSourceManager();
auto Loc = SourceMgr.getFileLoc(Info.getLocation());
llvm::raw_svector_ostream OS(Location);
Loc.print(OS, SourceMgr);
OS << ":";
}
clangd::vlog("Ignored diagnostic. {0}{1}", Location, Message);
}
void IgnoreDiagnostics::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
const clang::Diagnostic &Info) {
IgnoreDiagnostics::log(DiagLevel, Info);
}
std::unique_ptr<CompilerInvocation>
buildCompilerInvocation(const ParseInputs &Inputs, clang::DiagnosticConsumer &D,
std::vector<std::string> *CC1Args) {
std::vector<const char *> ArgStrs;
for (const auto &S : Inputs.CompileCommand.CommandLine)
ArgStrs.push_back(S.c_str());
auto VFS = Inputs.FSProvider->getFileSystem();
if (VFS->setCurrentWorkingDirectory(Inputs.CompileCommand.Directory)) {
log("Couldn't set working directory when creating compiler invocation.");
// We proceed anyway, our lit-tests rely on results for non-existing working
// dirs.
}
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 11:24:55 +02:00
CompilerInstance::createDiagnostics(new DiagnosticOptions, &D, false);
std::unique_ptr<CompilerInvocation> CI = createInvocationFromCommandLine(
ArgStrs, CommandLineDiagsEngine, std::move(VFS),
/*ShouldRecoverOnErrors=*/true, CC1Args);
if (!CI)
return nullptr;
// createInvocationFromCommandLine sets DisableFree.
CI->getFrontendOpts().DisableFree = false;
CI->getLangOpts()->CommentOpts.ParseAllComments = true;
CI->getLangOpts()->RetainCommentsFromSystemHeaders = true;
// 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;
// Recovery expression currently only works for C++.
if (CI->getLangOpts()->CPlusPlus) {
CI->getLangOpts()->RecoveryAST = Inputs.Opts.BuildRecoveryAST;
CI->getLangOpts()->RecoveryASTType = Inputs.Opts.PreserveRecoveryASTType;
}
return CI;
}
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) {
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) {
Preamble->OverridePreamble(*CI, VFS, Buffer.get());
} else {
CI->getPreprocessorOpts().addRemappedFile(
CI->getFrontendOpts().Inputs[0].getFile(), Buffer.get());
}
auto Clang = std::make_unique<CompilerInstance>(
std::make_shared<PCHContainerOperations>());
Clang->setInvocation(std::move(CI));
Clang->createDiagnostics(&DiagsClient, false);
if (auto VFSWithRemapping = createVFSFromCompilerInvocation(
Clang->getInvocation(), Clang->getDiagnostics(), VFS))
VFS = VFSWithRemapping;
Clang->createFileManager(VFS);
Clang->setTarget(TargetInfo::CreateTargetInfo(
Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
if (!Clang->hasTarget())
return nullptr;
// RemappedFileBuffers will handle the lifetime of the Buffer pointer,
// release it.
Buffer.release();
return Clang;
}
} // namespace clangd
} // namespace clang