clang-tools 10.0.0

Compiler.cpp
Go to the documentation of this file.
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 "clang/Serialization/PCHContainerOperations.h"
14 #include "llvm/Support/Format.h"
15 #include "llvm/Support/FormatVariadic.h"
16
17 namespace clang {
18 namespace clangd {
19
20 void IgnoreDiagnostics::log(DiagnosticsEngine::Level DiagLevel,
21 const clang::Diagnostic &Info) {
22 // FIXME: format lazily, in case vlog is off.
23 llvm::SmallString<64> Message;
24 Info.FormatDiagnostic(Message);
25
26 llvm::SmallString<64> Location;
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);
32 OS << ":";
33 }
34
35 clangd::vlog("Ignored diagnostic. {0}{1}", Location, Message);
36 }
37
38 void IgnoreDiagnostics::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
39 const clang::Diagnostic &Info) {
40 IgnoreDiagnostics::log(DiagLevel, Info);
41 }
42
43 std::unique_ptr<CompilerInvocation>
44 buildCompilerInvocation(const ParseInputs &Inputs,
45 clang::DiagnosticConsumer &D,
46 std::vector<std::string> *CC1Args) {
47 std::vector<const char *> ArgStrs;
48 for (const auto &S : Inputs.CompileCommand.CommandLine)
49 ArgStrs.push_back(S.c_str());
50
51 if (Inputs.FS->setCurrentWorkingDirectory(Inputs.CompileCommand.Directory)) {
52 log("Couldn't set working directory when creating compiler invocation.");
53 // We proceed anyway, our lit-tests rely on results for non-existing working
54 // dirs.
55 }
56
57 llvm::IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine =
58 CompilerInstance::createDiagnostics(new DiagnosticOptions, &D, false);
59 std::unique_ptr<CompilerInvocation> CI = createInvocationFromCommandLine(
60 ArgStrs, CommandLineDiagsEngine, Inputs.FS,
61 /*ShouldRecoverOnErrors=*/true, CC1Args);
62 if (!CI)
63 return nullptr;
64 // createInvocationFromCommandLine sets DisableFree.
65 CI->getFrontendOpts().DisableFree = false;
66 CI->getLangOpts()->CommentOpts.ParseAllComments = true;
67 CI->getLangOpts()->RetainCommentsFromSystemHeaders = true;
68 return CI;
69 }
70
71 std::unique_ptr<CompilerInstance>
72 prepareCompilerInstance(std::unique_ptr<clang::CompilerInvocation> CI,
73 const PrecompiledPreamble *Preamble,
74 std::unique_ptr<llvm::MemoryBuffer> Buffer,
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 = std::make_unique<CompilerInstance>(
92 std::make_shared<PCHContainerOperations>());
93 Clang->setInvocation(std::move(CI));
94 Clang->createDiagnostics(&DiagsClient, false);
95
96 if (auto VFSWithRemapping = createVFSFromCompilerInvocation(
97 Clang->getInvocation(), Clang->getDiagnostics(), VFS))
98 VFS = VFSWithRemapping;
99 Clang->createFileManager(VFS);
100
101 Clang->setTarget(TargetInfo::CreateTargetInfo(
102 Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
103 if (!Clang->hasTarget())
104 return nullptr;
105
106 // RemappedFileBuffers will handle the lifetime of the Buffer pointer,
107 // release it.
108 Buffer.release();
109 return Clang;
110 }
111
112 } // namespace clangd
113 } // namespace clang
DiagnosticConsumer
Loc
SourceLocation Loc
&#39;#&#39; location in the include directive
Definition: IncludeOrderCheck.cpp:37
clang::clangd::IgnoreDiagnostics::log
static void log(DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &Info)
Definition: Compiler.cpp:20
VFS
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS
Definition: CodeComplete.cpp:1023
clang::tidy::modernize::Message
constexpr llvm::StringLiteral Message
Definition: UseTrailingReturnTypeCheck.cpp:101
clang::clangd::prepareCompilerInstance
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)
Definition: Compiler.cpp:72
clang::clangd::vlog
void vlog(const char *Fmt, Ts &&... Vals)
Definition: Logger.h:67
Compiler.h
clang::clangd::ParseInputs::CompileCommand
tooling::CompileCommand CompileCommand
Definition: Compiler.h:45
clang::clangd::MessageType::Info
An information message.
Logger.h
clang::clangd::ParseInputs
Information required to run clang, e.g. to parse AST or do code completion.
Definition: Compiler.h:44
Preamble
const PreambleData * Preamble
Definition: CodeComplete.cpp:1020
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::buildCompilerInvocation
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.
Definition: Compiler.cpp:44
clang::clangd::IgnoreDiagnostics::HandleDiagnostic
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &Info) override
Definition: Compiler.cpp:38
clang::clangd::ParseInputs::FS
IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS
Definition: Compiler.h:46
clang::clangd::Location
Definition: Protocol.h:182