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"
21 const clang::Diagnostic &
Info) {
23 llvm::SmallString<64> Message;
24 Info.FormatDiagnostic(Message);
27 if (
Info.hasSourceManager() &&
Info.getLocation().isValid()) {
28 auto &SourceMgr =
Info.getSourceManager();
29 auto Loc = SourceMgr.getFileLoc(
Info.getLocation());
31 Loc.print(
OS, SourceMgr);
39 const clang::Diagnostic &
Info) {
50 CI.getDiagnosticOpts().VerifyDiagnostics =
false;
51 CI.getDiagnosticOpts().ShowColors =
false;
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();
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;
71 CI.getPreprocessorOpts().DisablePragmaDebugCrash =
true;
75 CI.getHeaderSearchOpts().ModuleFormat =
76 PCHContainerOperations().getRawReader().getFormats().front().str();
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();
87 CI.getLangOpts().NoSanitizeFiles.clear();
88 CI.getLangOpts().XRayAttrListFiles.clear();
89 CI.getLangOpts().ProfileListFiles.clear();
90 CI.getLangOpts().XRayAlwaysInstrumentFiles.clear();
91 CI.getLangOpts().XRayNeverInstrumentFiles.clear();
94std::unique_ptr<CompilerInvocation>
96 std::vector<std::string> *CC1Args) {
97 llvm::ArrayRef<std::string> Argv = Inputs.
CompileCommand.CommandLine;
100 std::vector<const char *> ArgStrs;
101 ArgStrs.reserve(Argv.size() + 1);
105 ArgStrs = {Argv.front().c_str(),
"-Xclang",
"-no-round-trip-args"};
106 for (
const auto &S : Argv.drop_front())
107 ArgStrs.push_back(S.c_str());
109 CreateInvocationOptions CIOpts;
111 CIOpts.CC1Args = CC1Args;
112 CIOpts.RecoverOnError =
true;
114 CompilerInstance::createDiagnostics(
new DiagnosticOptions, &D,
false);
115 CIOpts.ProbePrecompiled =
false;
116 std::unique_ptr<CompilerInvocation>
CI = createInvocation(ArgStrs, CIOpts);
120 CI->getFrontendOpts().DisableFree =
false;
121 CI->getLangOpts().CommentOpts.ParseAllComments =
true;
122 CI->getLangOpts().RetainCommentsFromSystemHeaders =
true;
128std::unique_ptr<CompilerInstance>
130 const PrecompiledPreamble *
Preamble,
131 std::unique_ptr<llvm::MemoryBuffer> Buffer,
132 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
134 assert(VFS &&
"VFS is null");
135 assert(!
CI->getPreprocessorOpts().RetainRemappedFileBuffers &&
136 "Setting RetainRemappedFileBuffers to true will cause a memory leak "
137 "of ContentsBuffer");
142 Preamble->OverridePreamble(*
CI, VFS, Buffer.get());
144 CI->getPreprocessorOpts().addRemappedFile(
145 CI->getFrontendOpts().Inputs[0].getFile(), Buffer.get());
148 auto Clang = std::make_unique<CompilerInstance>(
149 std::make_shared<PCHContainerOperations>());
150 Clang->setInvocation(std::move(
CI));
151 Clang->createDiagnostics(&DiagsClient,
false);
153 if (
auto VFSWithRemapping = createVFSFromCompilerInvocation(
154 Clang->getInvocation(),
Clang->getDiagnostics(), VFS))
155 VFS = VFSWithRemapping;
156 Clang->createFileManager(VFS);
158 if (!
Clang->createTarget())
std::unique_ptr< CompilerInvocation > CI
llvm::raw_string_ostream OS
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &Info) override
static void log(DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &Info)
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > view(std::nullopt_t CWD) const
Obtain a vfs::FileSystem with an arbitrary initial working directory.
@ Info
An information message.
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 vlog(const char *Fmt, Ts &&... Vals)
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 allowCrashPragmasForTest()
Respect #pragma clang __debug crash etc, which are usually disabled.
static bool AllowCrashPragmasForTest
void disableUnsupportedOptions(CompilerInvocation &CI)
Clears CI from options that are not supported by clangd, like codegen or plugins.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//