1 //===--- Compiler.cpp --------------------------------------------*- C++-*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "Compiler.h"
10 #include "Logger.h"
11 #include "clang/Basic/TargetInfo.h"
12 #include "clang/Lex/PreprocessorOptions.h"
13 #include "llvm/Support/Format.h"
14 #include "llvm/Support/FormatVariadic.h"
15 
16 namespace clang {
17 namespace clangd {
18 
19 void IgnoreDiagnostics::log(DiagnosticsEngine::Level DiagLevel,
20                             const clang::Diagnostic &Info) {
21   // FIXME: format lazily, in case vlog is off.
22   llvm::SmallString<64> Message;
23   Info.FormatDiagnostic(Message);
24 
25   llvm::SmallString<64> Location;
26   if (Info.hasSourceManager() && Info.getLocation().isValid()) {
27     auto &SourceMgr = Info.getSourceManager();
28     auto Loc = SourceMgr.getFileLoc(Info.getLocation());
29     llvm::raw_svector_ostream OS(Location);
30     Loc.print(OS, SourceMgr);
31     OS << ":";
32   }
33 
34   clangd::vlog("Ignored diagnostic. {0}{1}", Location, Message);
35 }
36 
37 void IgnoreDiagnostics::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
38                                          const clang::Diagnostic &Info) {
39   IgnoreDiagnostics::log(DiagLevel, Info);
40 }
41 
42 std::unique_ptr<CompilerInvocation>
43 buildCompilerInvocation(const ParseInputs &Inputs) {
44   std::vector<const char *> ArgStrs;
45   for (const auto &S : Inputs.CompileCommand.CommandLine)
46     ArgStrs.push_back(S.c_str());
47 
48   if (Inputs.FS->setCurrentWorkingDirectory(Inputs.CompileCommand.Directory)) {
49     log("Couldn't set working directory when creating compiler invocation.");
50     // We proceed anyway, our lit-tests rely on results for non-existing working
51     // dirs.
52   }
53 
54   // FIXME(ibiryukov): store diagnostics from CommandLine when we start
55   // reporting them.
56   IgnoreDiagnostics IgnoreDiagnostics;
57   llvm::IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine =
58       CompilerInstance::createDiagnostics(new DiagnosticOptions,
59                                           &IgnoreDiagnostics, false);
60   std::unique_ptr<CompilerInvocation> CI = createInvocationFromCommandLine(
61       ArgStrs, CommandLineDiagsEngine, Inputs.FS);
62   if (!CI)
63     return nullptr;
64   // createInvocationFromCommandLine sets DisableFree.
65   CI->getFrontendOpts().DisableFree = false;
66   CI->getLangOpts()->CommentOpts.ParseAllComments = true;
67   return CI;
68 }
69 
70 std::unique_ptr<CompilerInstance>
71 prepareCompilerInstance(std::unique_ptr<clang::CompilerInvocation> CI,
72                         const PrecompiledPreamble *Preamble,
73                         std::unique_ptr<llvm::MemoryBuffer> Buffer,
74                         std::shared_ptr<PCHContainerOperations> PCHs,
75                         llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
76                         DiagnosticConsumer &DiagsClient) {
77   assert(VFS && "VFS is null");
78   assert(!CI->getPreprocessorOpts().RetainRemappedFileBuffers &&
79          "Setting RetainRemappedFileBuffers to true will cause a memory leak "
80          "of ContentsBuffer");
81 
82   // NOTE: we use Buffer.get() when adding remapped files, so we have to make
83   // sure it will be released if no error is emitted.
84   if (Preamble) {
85     Preamble->OverridePreamble(*CI, VFS, Buffer.get());
86   } else {
87     CI->getPreprocessorOpts().addRemappedFile(
88         CI->getFrontendOpts().Inputs[0].getFile(), Buffer.get());
89   }
90 
91   auto Clang = llvm::make_unique<CompilerInstance>(PCHs);
92   Clang->setInvocation(std::move(CI));
93   Clang->createDiagnostics(&DiagsClient, false);
94 
95   if (auto VFSWithRemapping = createVFSFromCompilerInvocation(
96           Clang->getInvocation(), Clang->getDiagnostics(), VFS))
97     VFS = VFSWithRemapping;
98   Clang->setVirtualFileSystem(VFS);
99 
100   Clang->setTarget(TargetInfo::CreateTargetInfo(
101       Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
102   if (!Clang->hasTarget())
103     return nullptr;
104 
105   // RemappedFileBuffers will handle the lifetime of the Buffer pointer,
106   // release it.
107   Buffer.release();
108   return Clang;
109 }
110 
111 } // namespace clangd
112 } // namespace clang
113