22 const clang::Diagnostic &
Info) {
24 llvm::SmallString<64> Message;
25 Info.FormatDiagnostic(Message);
28 if (
Info.hasSourceManager() &&
Info.getLocation().isValid()) {
29 auto &SourceMgr =
Info.getSourceManager();
30 auto Loc = SourceMgr.getFileLoc(
Info.getLocation());
31 llvm::raw_svector_ostream OS(
Location);
32 Loc.print(OS, SourceMgr);
51 CI.getDiagnosticOpts().VerifyDiagnostics =
false;
52 CI.getDiagnosticOpts().ShowColors =
false;
56 CI.getDependencyOutputOpts().ShowIncludesDest = ShowIncludesDestination::None;
57 CI.getDependencyOutputOpts().OutputFile.clear();
58 CI.getDependencyOutputOpts().HeaderIncludeOutputFile.clear();
59 CI.getDependencyOutputOpts().DOTOutputFile.clear();
60 CI.getDependencyOutputOpts().ModuleDependencyOutputDir.clear();
65 CI.getPreprocessorOpts().ImplicitPCHInclude.clear();
66 CI.getPreprocessorOpts().PrecompiledPreambleBytes = {0,
false};
67 CI.getPreprocessorOpts().PCHThroughHeader.clear();
68 CI.getPreprocessorOpts().PCHWithHdrStop =
false;
69 CI.getPreprocessorOpts().PCHWithHdrStopCreate =
false;
72 CI.getPreprocessorOpts().DisablePragmaDebugCrash =
true;
76 CI.getHeaderSearchOpts().ModuleFormat =
77 PCHContainerOperations().getRawReader().getFormats().front().str();
79 CI.getFrontendOpts().Plugins.clear();
80 CI.getFrontendOpts().AddPluginActions.clear();
81 CI.getFrontendOpts().PluginArgs.clear();
82 CI.getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
83 CI.getFrontendOpts().ActionName.clear();
88 CI.getLangOpts().NoSanitizeFiles.clear();
89 CI.getLangOpts().XRayAttrListFiles.clear();
90 CI.getLangOpts().ProfileListFiles.clear();
91 CI.getLangOpts().XRayAlwaysInstrumentFiles.clear();
92 CI.getLangOpts().XRayNeverInstrumentFiles.clear();
97 std::vector<std::string> *CC1Args) {
98 llvm::ArrayRef<std::string> Argv = Inputs.
CompileCommand.CommandLine;
101 std::vector<const char *> ArgStrs;
102 ArgStrs.reserve(Argv.size() + 1);
106 ArgStrs = {Argv.front().c_str(),
"-Xclang",
"-no-round-trip-args"};
107 for (
const auto &S : Argv.drop_front())
108 ArgStrs.push_back(S.c_str());
110 CreateInvocationOptions CIOpts;
112 CIOpts.CC1Args = CC1Args;
113 CIOpts.RecoverOnError =
true;
114 DiagnosticOptions DiagOpts;
116 CompilerInstance::createDiagnostics(*CIOpts.VFS, DiagOpts, &D,
false);
117 CIOpts.ProbePrecompiled =
false;
118 std::unique_ptr<CompilerInvocation> CI = createInvocation(ArgStrs, CIOpts);
122 CI->getFrontendOpts().DisableFree =
false;
123 CI->getLangOpts().CommentOpts.ParseAllComments =
true;
124 CI->getLangOpts().RetainCommentsFromSystemHeaders =
true;
132 const PrecompiledPreamble *
Preamble,
133 std::unique_ptr<llvm::MemoryBuffer> Buffer,
134 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
136 assert(VFS &&
"VFS is null");
137 assert(!CI->getPreprocessorOpts().RetainRemappedFileBuffers &&
138 "Setting RetainRemappedFileBuffers to true will cause a memory leak "
139 "of ContentsBuffer");
144 Preamble->OverridePreamble(*CI, VFS, Buffer.get());
146 CI->getPreprocessorOpts().addRemappedFile(
147 CI->getFrontendOpts().Inputs[0].getFile(), Buffer.get());
150 auto Clang = std::make_unique<CompilerInstance>(std::move(CI));
151 Clang->createVirtualFileSystem(VFS, &DiagsClient);
152 Clang->createDiagnostics(&DiagsClient,
false);
153 Clang->createFileManager();
154 if (!Clang->createTarget())
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.
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)