2017-12-04 21:49:59 +08:00
|
|
|
//===--- Compiler.cpp -------------------------------------------*- C++-*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===---------------------------------------------------------------------===//
|
2017-12-15 05:22:03 +08:00
|
|
|
|
2017-12-04 21:49:59 +08:00
|
|
|
#include "Compiler.h"
|
2018-02-12 20:48:51 +08:00
|
|
|
#include "Logger.h"
|
2017-12-04 21:49:59 +08:00
|
|
|
#include "clang/Basic/TargetInfo.h"
|
|
|
|
#include "clang/Lex/PreprocessorOptions.h"
|
2018-02-12 20:48:51 +08:00
|
|
|
#include "llvm/Support/Format.h"
|
|
|
|
#include "llvm/Support/FormatVariadic.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) {
|
|
|
|
SmallString<64> Message;
|
|
|
|
Info.FormatDiagnostic(Message);
|
|
|
|
|
|
|
|
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] Upgrade logging facilities with levels and formatv.
Summary:
log() is split into four functions:
- elog()/log()/vlog() have different severity levels, allowing filtering
- dlog() is a lazy macro which uses LLVM_DEBUG - it logs to the logger, but
conditionally based on -debug-only flag and is omitted in release builds
All logging functions use formatv-style format strings now, e.g:
log("Could not resolve URI {0}: {1}", URI, Result.takeError());
Existing log sites have been split between elog/log/vlog by best guess.
This includes a workaround for passing Error to formatv that can be
simplified when D49170 or similar lands.
Subscribers: ilya-biryukov, javed.absar, ioeric, MaskRay, jkorous, cfe-commits
Differential Revision: https://reviews.llvm.org/D49008
llvm-svn: 336785
2018-07-11 18:35:11 +08:00
|
|
|
clangd::log("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);
|
|
|
|
}
|
|
|
|
|
2017-12-04 21:49:59 +08:00
|
|
|
std::unique_ptr<CompilerInstance>
|
|
|
|
prepareCompilerInstance(std::unique_ptr<clang::CompilerInvocation> CI,
|
|
|
|
const PrecompiledPreamble *Preamble,
|
|
|
|
std::unique_ptr<llvm::MemoryBuffer> Buffer,
|
|
|
|
std::shared_ptr<PCHContainerOperations> PCHs,
|
|
|
|
IntrusiveRefCntPtr<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) {
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Clang = llvm::make_unique<CompilerInstance>(PCHs);
|
|
|
|
Clang->setInvocation(std::move(CI));
|
|
|
|
Clang->createDiagnostics(&DiagsClient, false);
|
|
|
|
|
|
|
|
if (auto VFSWithRemapping = createVFSFromCompilerInvocation(
|
|
|
|
Clang->getInvocation(), Clang->getDiagnostics(), VFS))
|
|
|
|
VFS = VFSWithRemapping;
|
|
|
|
Clang->setVirtualFileSystem(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
|