11 #include "clang/Basic/TargetInfo.h" 12 #include "clang/Lex/PreprocessorOptions.h" 13 #include "clang/Serialization/PCHContainerOperations.h" 14 #include "llvm/Support/Format.h" 15 #include "llvm/Support/FormatVariadic.h" 21 const clang::Diagnostic &Info) {
24 Info.FormatDiagnostic(Message);
27 if (Info.hasSourceManager() && Info.getLocation().isValid()) {
28 auto &SourceMgr = Info.getSourceManager();
29 auto Loc = SourceMgr.getFileLoc(Info.getLocation());
30 llvm::raw_svector_ostream OS(Location);
31 Loc.print(OS, SourceMgr);
35 clangd::vlog(
"Ignored diagnostic. {0}{1}", Location, Message);
39 const clang::Diagnostic &
Info) {
43 std::unique_ptr<CompilerInvocation>
45 clang::DiagnosticConsumer &D,
46 std::vector<std::string> *CC1Args) {
47 std::vector<const char *> ArgStrs;
49 ArgStrs.push_back(S.c_str());
51 if (Inputs.
FS->setCurrentWorkingDirectory(Inputs.
CompileCommand.Directory)) {
52 log(
"Couldn't set working directory when creating compiler invocation.");
57 llvm::IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine =
58 CompilerInstance::createDiagnostics(
new DiagnosticOptions, &D,
false);
59 std::unique_ptr<CompilerInvocation> CI = createInvocationFromCommandLine(
60 ArgStrs, CommandLineDiagsEngine, Inputs.
FS,
65 CI->getFrontendOpts().DisableFree =
false;
66 CI->getLangOpts()->CommentOpts.ParseAllComments =
true;
67 CI->getLangOpts()->RetainCommentsFromSystemHeaders =
true;
71 std::unique_ptr<CompilerInstance>
74 std::unique_ptr<llvm::MemoryBuffer> Buffer,
75 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
VFS,
77 assert(VFS &&
"VFS is null");
78 assert(!CI->getPreprocessorOpts().RetainRemappedFileBuffers &&
79 "Setting RetainRemappedFileBuffers to true will cause a memory leak " 85 Preamble->OverridePreamble(*CI, VFS, Buffer.get());
87 CI->getPreprocessorOpts().addRemappedFile(
88 CI->getFrontendOpts().Inputs[0].getFile(), Buffer.get());
91 auto Clang = std::make_unique<CompilerInstance>(
92 std::make_shared<PCHContainerOperations>());
93 Clang->setInvocation(std::move(CI));
94 Clang->createDiagnostics(&DiagsClient,
false);
96 if (
auto VFSWithRemapping = createVFSFromCompilerInvocation(
97 Clang->getInvocation(), Clang->getDiagnostics(),
VFS))
98 VFS = VFSWithRemapping;
99 Clang->createFileManager(VFS);
101 Clang->setTarget(TargetInfo::CreateTargetInfo(
102 Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
103 if (!Clang->hasTarget())
SourceLocation Loc
'#' location in the include directive
static void log(DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &Info)
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS
constexpr llvm::StringLiteral Message
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)
void vlog(const char *Fmt, Ts &&... Vals)
const PreambleData * Preamble
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
std::unique_ptr< CompilerInvocation > buildCompilerInvocation(const ParseInputs &Inputs, clang::DiagnosticConsumer &D, std::vector< std::string > *CC1Args)
Builds compiler invocation that could be used to build AST or preamble.
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &Info) override