#include "clang/Driver/Compilation.h"
#include "clang/Basic/LLVM.h"
#include "clang/Driver/Action.h"
#include "clang/Driver/Driver.h"
#include "clang/Driver/DriverDiagnostic.h"
#include "clang/Driver/Job.h"
#include "clang/Driver/Options.h"
#include "clang/Driver/ToolChain.h"
#include "clang/Driver/Util.h"
#include "llvm/ADT/None.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Option/ArgList.h"
#include "llvm/Option/OptSpecifier.h"
#include "llvm/Option/Option.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/raw_ostream.h"
#include <cassert>
#include <string>
#include <system_error>
#include <utility>
using namespace clang;
using namespace driver;
using namespace llvm::opt;
Compilation::Compilation(const Driver &D, const ToolChain &_DefaultToolChain,
InputArgList *_Args, DerivedArgList *_TranslatedArgs,
bool ContainsError)
: TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args),
TranslatedArgs(_TranslatedArgs), ContainsError(ContainsError) {
OrderedOffloadingToolchains.insert(
std::make_pair(Action::OFK_Host, &DefaultToolChain));
}
Compilation::~Compilation() {
if (!TheDriver.isSaveTempsEnabled() && !ForceKeepTempFiles)
CleanupFileList(TempFiles);
delete TranslatedArgs;
delete Args;
for (auto Arg : TCArgs)
if (Arg.second != TranslatedArgs)
delete Arg.second;
}
const DerivedArgList &
Compilation::getArgsForToolChain(const ToolChain *TC, StringRef BoundArch,
Action::OffloadKind DeviceOffloadKind) {
if (!TC)
TC = &DefaultToolChain;
DerivedArgList *&Entry = TCArgs[{TC, BoundArch, DeviceOffloadKind}];
if (!Entry) {
SmallVector<Arg *, 4> AllocatedArgs;
DerivedArgList *OpenMPArgs = nullptr;
if (DeviceOffloadKind == Action::OFK_OpenMP) {
const ToolChain *HostTC = getSingleOffloadToolChain<Action::OFK_Host>();
bool SameTripleAsHost = (TC->getTriple() == HostTC->getTriple());
OpenMPArgs = TC->TranslateOpenMPTargetArgs(
*TranslatedArgs, SameTripleAsHost, AllocatedArgs);
}
DerivedArgList *NewDAL = nullptr;
if (!OpenMPArgs) {
NewDAL = TC->TranslateXarchArgs(*TranslatedArgs, BoundArch,
DeviceOffloadKind, &AllocatedArgs);
} else {
NewDAL = TC->TranslateXarchArgs(*OpenMPArgs, BoundArch, DeviceOffloadKind,
&AllocatedArgs);
if (!NewDAL)
NewDAL = OpenMPArgs;
else
delete OpenMPArgs;
}
if (!NewDAL) {
Entry = TC->TranslateArgs(*TranslatedArgs, BoundArch, DeviceOffloadKind);
if (!Entry)
Entry = TranslatedArgs;
} else {
Entry = TC->TranslateArgs(*NewDAL, BoundArch, DeviceOffloadKind);
if (!Entry)
Entry = NewDAL;
else
delete NewDAL;
}
for (auto ArgPtr : AllocatedArgs)
Entry->AddSynthesizedArg(ArgPtr);
}
return *Entry;
}
bool Compilation::CleanupFile(const char *File, bool IssueErrors) const {
if (!llvm::sys::fs::can_write(File) || !llvm::sys::fs::is_regular_file(File))
return true;
if (std::error_code EC = llvm::sys::fs::remove(File)) {
if (IssueErrors)
getDriver().Diag(diag::err_drv_unable_to_remove_file)
<< EC.message();
return false;
}
return true;
}
bool Compilation::CleanupFileList(const llvm::opt::ArgStringList &Files,
bool IssueErrors) const {
bool Success = true;
for (const auto &File: Files)
Success &= CleanupFile(File, IssueErrors);
return Success;
}
bool Compilation::CleanupFileMap(const ArgStringMap &Files,
const JobAction *JA,
bool IssueErrors) const {
bool Success = true;
for (const auto &File : Files) {
if (JA && File.first != JA)
continue;
Success &= CleanupFile(File.second, IssueErrors);
}
return Success;
}
int Compilation::ExecuteCommand(const Command &C,
const Command *&FailingCommand,
bool LogOnly) const {
if ((getDriver().CCPrintOptions ||
getArgs().hasArg(options::OPT_v)) && !getDriver().CCGenDiagnostics) {
raw_ostream *OS = &llvm::errs();
std::unique_ptr<llvm::raw_fd_ostream> OwnedStream;
if (getDriver().CCPrintOptions &&
!getDriver().CCPrintOptionsFilename.empty()) {
std::error_code EC;
OwnedStream.reset(new llvm::raw_fd_ostream(
getDriver().CCPrintOptionsFilename, EC,
llvm::sys::fs::OF_Append | llvm::sys::fs::OF_TextWithCRLF));
if (EC) {
getDriver().Diag(diag::err_drv_cc_print_options_failure)
<< EC.message();
FailingCommand = &C;
return 1;
}
OS = OwnedStream.get();
}
if (getDriver().CCPrintOptions)
*OS << "[Logging clang options]\n";
C.Print(*OS, "\n", getDriver().CCPrintOptions);
}
if (LogOnly)
return 0;
std::string Error;
bool ExecutionFailed;
int Res = C.Execute(Redirects, &Error, &ExecutionFailed);
if (PostCallback)
PostCallback(C, Res);
if (!Error.empty()) {
assert(Res && "Error string set with 0 result code!");
getDriver().Diag(diag::err_drv_command_failure) << Error;
}
if (Res)
FailingCommand = &C;
return ExecutionFailed ? 1 : Res;
}
using FailingCommandList = SmallVectorImpl<std::pair<int, const Command *>>;
static bool ActionFailed(const Action *A,
const FailingCommandList &FailingCommands) {
if (FailingCommands.empty())
return false;
if (A->isOffloading(Action::OFK_Cuda) || A->isOffloading(Action::OFK_HIP))
return true;
for (const auto &CI : FailingCommands)
if (A == &(CI.second->getSource()))
return true;
for (const auto *AI : A->inputs())
if (ActionFailed(AI, FailingCommands))
return true;
return false;
}
static bool InputsOk(const Command &C,
const FailingCommandList &FailingCommands) {
return !ActionFailed(&C.getSource(), FailingCommands);
}
void Compilation::ExecuteJobs(const JobList &Jobs,
FailingCommandList &FailingCommands,
bool LogOnly) const {
for (const auto &Job : Jobs) {
if (!InputsOk(Job, FailingCommands))
continue;
const Command *FailingCommand = nullptr;
if (int Res = ExecuteCommand(Job, FailingCommand, LogOnly)) {
FailingCommands.push_back(std::make_pair(Res, FailingCommand));
if (TheDriver.IsCLMode())
return;
}
}
}
void Compilation::initCompilationForDiagnostics() {
ForDiagnostics = true;
Actions.clear();
AllActions.clear();
Jobs.clear();
if (!TheDriver.isSaveTempsEnabled() && !ForceKeepTempFiles)
CleanupFileList(TempFiles);
TempFiles.clear();
ResultFiles.clear();
FailureResultFiles.clear();
OptSpecifier OutputOpts[] = {
options::OPT_o, options::OPT_MD, options::OPT_MMD, options::OPT_M,
options::OPT_MM, options::OPT_MF, options::OPT_MG, options::OPT_MJ,
options::OPT_MQ, options::OPT_MT, options::OPT_MV};
for (unsigned i = 0, e = llvm::array_lengthof(OutputOpts); i != e; ++i) {
if (TranslatedArgs->hasArg(OutputOpts[i]))
TranslatedArgs->eraseArg(OutputOpts[i]);
}
TranslatedArgs->ClaimAllArgs();
for (auto Arg : TCArgs)
if (Arg.second != TranslatedArgs)
delete Arg.second;
TCArgs.clear();
Redirects = {None, {""}, {""}};
ForceKeepTempFiles = true;
}
StringRef Compilation::getSysRoot() const {
return getDriver().SysRoot;
}
void Compilation::Redirect(ArrayRef<Optional<StringRef>> Redirects) {
this->Redirects = Redirects;
}