clang-tools  15.0.0git
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 "support/Logger.h"
11 #include "clang/Basic/TargetInfo.h"
12 #include "clang/Frontend/CompilerInvocation.h"
13 #include "clang/Lex/PreprocessorOptions.h"
14 #include "clang/Serialization/PCHContainerOperations.h"
15 #include "llvm/ADT/StringRef.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 static bool AllowCrashPragmasForTest = false;
45 
46 void disableUnsupportedOptions(CompilerInvocation &CI) {
47  // Disable "clang -verify" diagnostics, they are rarely useful in clangd, and
48  // our compiler invocation set-up doesn't seem to work with it (leading
49  // assertions in VerifyDiagnosticConsumer).
50  CI.getDiagnosticOpts().VerifyDiagnostics = false;
51  CI.getDiagnosticOpts().ShowColors = false;
52 
53  // Disable any dependency outputting, we don't want to generate files or write
54  // to stdout/stderr.
55  CI.getDependencyOutputOpts().ShowIncludesDest = ShowIncludesDestination::None;
56  CI.getDependencyOutputOpts().OutputFile.clear();
57  CI.getDependencyOutputOpts().HeaderIncludeOutputFile.clear();
58  CI.getDependencyOutputOpts().DOTOutputFile.clear();
59  CI.getDependencyOutputOpts().ModuleDependencyOutputDir.clear();
60 
61  // Disable any pch generation/usage operations. Since serialized preamble
62  // format is unstable, using an incompatible one might result in unexpected
63  // behaviours, including crashes.
64  CI.getPreprocessorOpts().ImplicitPCHInclude.clear();
65  CI.getPreprocessorOpts().PrecompiledPreambleBytes = {0, false};
66  CI.getPreprocessorOpts().PCHThroughHeader.clear();
67  CI.getPreprocessorOpts().PCHWithHdrStop = false;
68  CI.getPreprocessorOpts().PCHWithHdrStopCreate = false;
69  // Don't crash on `#pragma clang __debug parser_crash`
71  CI.getPreprocessorOpts().DisablePragmaDebugCrash = true;
72 
73  // Always default to raw container format as clangd doesn't registry any other
74  // and clang dies when faced with unknown formats.
75  CI.getHeaderSearchOpts().ModuleFormat =
76  PCHContainerOperations().getRawReader().getFormat().str();
77 
78  CI.getFrontendOpts().Plugins.clear();
79  CI.getFrontendOpts().AddPluginActions.clear();
80  CI.getFrontendOpts().PluginArgs.clear();
81  CI.getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
82  CI.getFrontendOpts().ActionName.clear();
83 }
84 
85 std::unique_ptr<CompilerInvocation>
86 buildCompilerInvocation(const ParseInputs &Inputs, clang::DiagnosticConsumer &D,
87  std::vector<std::string> *CC1Args) {
88  llvm::ArrayRef<std::string> Argv = Inputs.CompileCommand.CommandLine;
89  if (Argv.empty())
90  return nullptr;
91  std::vector<const char *> ArgStrs;
92  ArgStrs.reserve(Argv.size() + 1);
93  // In asserts builds, CompilerInvocation redundantly reads/parses cc1 args as
94  // a sanity test. This is not useful to clangd, and costs 10% of test time.
95  // To avoid mismatches between assert/production builds, disable it always.
96  ArgStrs = {Argv.front().c_str(), "-Xclang", "-no-round-trip-args"};
97  for (const auto &S : Argv.drop_front())
98  ArgStrs.push_back(S.c_str());
99 
100  CreateInvocationOptions CIOpts;
101  CIOpts.VFS = Inputs.TFS->view(Inputs.CompileCommand.Directory);
102  CIOpts.CC1Args = CC1Args;
103  CIOpts.RecoverOnError = true;
104  CIOpts.Diags =
105  CompilerInstance::createDiagnostics(new DiagnosticOptions, &D, false);
106  CIOpts.ProbePrecompiled = false;
107  std::unique_ptr<CompilerInvocation> CI = createInvocation(ArgStrs, CIOpts);
108  if (!CI)
109  return nullptr;
110  // createInvocationFromCommandLine sets DisableFree.
111  CI->getFrontendOpts().DisableFree = false;
112  CI->getLangOpts()->CommentOpts.ParseAllComments = true;
113  CI->getLangOpts()->RetainCommentsFromSystemHeaders = true;
114 
116  return CI;
117 }
118 
119 std::unique_ptr<CompilerInstance>
120 prepareCompilerInstance(std::unique_ptr<clang::CompilerInvocation> CI,
121  const PrecompiledPreamble *Preamble,
122  std::unique_ptr<llvm::MemoryBuffer> Buffer,
123  llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
124  DiagnosticConsumer &DiagsClient) {
125  assert(VFS && "VFS is null");
126  assert(!CI->getPreprocessorOpts().RetainRemappedFileBuffers &&
127  "Setting RetainRemappedFileBuffers to true will cause a memory leak "
128  "of ContentsBuffer");
129 
130  // NOTE: we use Buffer.get() when adding remapped files, so we have to make
131  // sure it will be released if no error is emitted.
132  if (Preamble) {
133  Preamble->OverridePreamble(*CI, VFS, Buffer.get());
134  } else {
135  CI->getPreprocessorOpts().addRemappedFile(
136  CI->getFrontendOpts().Inputs[0].getFile(), Buffer.get());
137  }
138 
139  auto Clang = std::make_unique<CompilerInstance>(
140  std::make_shared<PCHContainerOperations>());
141  Clang->setInvocation(std::move(CI));
142  Clang->createDiagnostics(&DiagsClient, false);
143 
144  if (auto VFSWithRemapping = createVFSFromCompilerInvocation(
145  Clang->getInvocation(), Clang->getDiagnostics(), VFS))
146  VFS = VFSWithRemapping;
147  Clang->createFileManager(VFS);
148 
149  if (!Clang->createTarget())
150  return nullptr;
151 
152  // RemappedFileBuffers will handle the lifetime of the Buffer pointer,
153  // release it.
154  Buffer.release();
155  return Clang;
156 }
157 
158 } // namespace clangd
159 } // namespace clang
Loc
SourceLocation Loc
Definition: KernelNameRestrictionCheck.cpp:45
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:120
DiagnosticConsumer
clang::clangd::Location
Definition: Protocol.h:208
clang::clangd::disableUnsupportedOptions
void disableUnsupportedOptions(CompilerInvocation &CI)
Clears CI from options that are not supported by clangd, like codegen or plugins.
Definition: Compiler.cpp:46
CI
std::unique_ptr< CompilerInvocation > CI
Definition: TUScheduler.cpp:491
clang::tidy::bugprone::Message
static const char Message[]
Definition: ReservedIdentifierCheck.cpp:31
clang::clangd::AllowCrashPragmasForTest
static bool AllowCrashPragmasForTest
Definition: Compiler.cpp:43
SourceMgr
llvm::SourceMgr * SourceMgr
Definition: ConfigCompile.cpp:101
clang::clangd::ParseInputs::CompileCommand
tooling::CompileCommand CompileCommand
Definition: Compiler.h:47
Preamble
const PreambleData & Preamble
Definition: CodeComplete.cpp:1192
Inputs
ParseInputs Inputs
Definition: TUScheduler.cpp:492
clang::clangd::ParseInputs
Information required to run clang, e.g. to parse AST or do code completion.
Definition: Compiler.h:46
ns1::ns2::D
@ D
Definition: CategoricalFeature.h:3
Diagnostic
DiagnosticCallback Diagnostic
Definition: ConfigCompile.cpp:100
clang::clangd::IgnoreDiagnostics::HandleDiagnostic
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &Info) override
Definition: Compiler.cpp:38
clang::clangd::ParseInputs::TFS
const ThreadsafeFS * TFS
Definition: Compiler.h:48
Logger.h
clang::clangd::vlog
void vlog(const char *Fmt, Ts &&... Vals)
Definition: Logger.h:72
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:86
Compiler.h
Info
FunctionInfo Info
Definition: FunctionSizeCheck.cpp:121
clang::clangd::allowCrashPragmasForTest
void allowCrashPragmasForTest()
Respect #pragma clang __debug crash etc, which are usually disabled.
Definition: Compiler.cpp:44
clang::clangd::ThreadsafeFS::view
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > view(llvm::NoneType CWD) const
Obtain a vfs::FileSystem with an arbitrary initial working directory.
Definition: ThreadsafeFS.h:33
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
OS
llvm::raw_string_ostream OS
Definition: TraceTests.cpp:160
clang::clangd::IgnoreDiagnostics::log
static void log(DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &Info)
Definition: Compiler.cpp:20
Clang
std::unique_ptr< CompilerInstance > Clang
Definition: HeadersTests.cpp:138