#include "Compiler.h"
#include "support/Logger.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Frontend/CompilerInvocation.h"
#include "clang/Lex/PreprocessorOptions.h"
#include "clang/Serialization/PCHContainerOperations.h"
#include "llvm/ADT/StringRef.h"
namespace clang {
namespace clangd {
void IgnoreDiagnostics::log(DiagnosticsEngine::Level DiagLevel,
const clang::Diagnostic &Info) {
llvm::SmallString<64> Message;
Info.FormatDiagnostic(Message);
llvm::SmallString<64> Location;
if (Info.hasSourceManager() && Info.getLocation().isValid()) {
auto &SourceMgr = Info.getSourceManager();
auto Loc = SourceMgr.getFileLoc(Info.getLocation());
llvm::raw_svector_ostream OS(Location);
Loc.print(OS, SourceMgr);
OS << ":";
}
clangd::vlog("Ignored diagnostic. {0}{1}", Location, Message);
}
void IgnoreDiagnostics::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
const clang::Diagnostic &Info) {
IgnoreDiagnostics::log(DiagLevel, Info);
}
static bool AllowCrashPragmasForTest = false;
void allowCrashPragmasForTest() { AllowCrashPragmasForTest = true; }
void disableUnsupportedOptions(CompilerInvocation &CI) {
CI.getDiagnosticOpts().VerifyDiagnostics = false;
CI.getDiagnosticOpts().ShowColors = false;
CI.getDependencyOutputOpts().ShowIncludesDest = ShowIncludesDestination::None;
CI.getDependencyOutputOpts().OutputFile.clear();
CI.getDependencyOutputOpts().HeaderIncludeOutputFile.clear();
CI.getDependencyOutputOpts().DOTOutputFile.clear();
CI.getDependencyOutputOpts().ModuleDependencyOutputDir.clear();
CI.getPreprocessorOpts().ImplicitPCHInclude.clear();
CI.getPreprocessorOpts().PrecompiledPreambleBytes = {0, false};
CI.getPreprocessorOpts().PCHThroughHeader.clear();
CI.getPreprocessorOpts().PCHWithHdrStop = false;
CI.getPreprocessorOpts().PCHWithHdrStopCreate = false;
if (!AllowCrashPragmasForTest)
CI.getPreprocessorOpts().DisablePragmaDebugCrash = true;
CI.getHeaderSearchOpts().ModuleFormat =
PCHContainerOperations().getRawReader().getFormats().front().str();
CI.getFrontendOpts().Plugins.clear();
CI.getFrontendOpts().AddPluginActions.clear();
CI.getFrontendOpts().PluginArgs.clear();
CI.getFrontendOpts().ProgramAction = frontend::ParseSyntaxOnly;
CI.getFrontendOpts().ActionName.clear();
CI.getLangOpts().NoSanitizeFiles.clear();
CI.getLangOpts().XRayAttrListFiles.clear();
CI.getLangOpts().ProfileListFiles.clear();
CI.getLangOpts().XRayAlwaysInstrumentFiles.clear();
CI.getLangOpts().XRayNeverInstrumentFiles.clear();
}
std::unique_ptr<CompilerInvocation>
buildCompilerInvocation(const ParseInputs &Inputs, clang::DiagnosticConsumer &D,
std::vector<std::string> *CC1Args) {
llvm::ArrayRef<std::string> Argv = Inputs.CompileCommand.CommandLine;
if (Argv.empty())
return nullptr;
std::vector<const char *> ArgStrs;
ArgStrs.reserve(Argv.size() + 1);
ArgStrs = {Argv.front().c_str(), "-Xclang", "-no-round-trip-args"};
for (const auto &S : Argv.drop_front())
ArgStrs.push_back(S.c_str());
CreateInvocationOptions CIOpts;
CIOpts.VFS = Inputs.TFS->view(Inputs.CompileCommand.Directory);
CIOpts.CC1Args = CC1Args;
CIOpts.RecoverOnError = true;
CIOpts.Diags =
CompilerInstance::createDiagnostics(new DiagnosticOptions, &D, false);
CIOpts.ProbePrecompiled = false;
std::unique_ptr<CompilerInvocation> CI = createInvocation(ArgStrs, CIOpts);
if (!CI)
return nullptr;
CI->getFrontendOpts().DisableFree = false;
CI->getLangOpts().CommentOpts.ParseAllComments = true;
CI->getLangOpts().RetainCommentsFromSystemHeaders = true;
disableUnsupportedOptions(*CI);
return CI;
}
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) {
assert(VFS && "VFS is null");
assert(!CI->getPreprocessorOpts().RetainRemappedFileBuffers &&
"Setting RetainRemappedFileBuffers to true will cause a memory leak "
"of ContentsBuffer");
if (Preamble) {
Preamble->OverridePreamble(*CI, VFS, Buffer.get());
} else {
CI->getPreprocessorOpts().addRemappedFile(
CI->getFrontendOpts().Inputs[0].getFile(), Buffer.get());
}
auto Clang = std::make_unique<CompilerInstance>(
std::make_shared<PCHContainerOperations>());
Clang->setInvocation(std::move(CI));
Clang->createDiagnostics(&DiagsClient, false);
if (auto VFSWithRemapping = createVFSFromCompilerInvocation(
Clang->getInvocation(), Clang->getDiagnostics(), VFS))
VFS = VFSWithRemapping;
Clang->createFileManager(VFS);
if (!Clang->createTarget())
return nullptr;
Buffer.release();
return Clang;
}
}
}