mirror of
https://github.com/llvm/llvm-project.git
synced 2025-05-15 15:26:08 +00:00

- Not all tools give good error messages, and sometimes the tool can fail w/o any error (for example, when signalled). - We suppress this message when the failing command is the compiler and it failed normally (exit code == 1), under the assumption that it gave a good diagnostic. For example, for a linker failure we now get: -- ddunbar@lordcrumb:tmp$ clang a.c b.c ld: duplicate symbol _x in /var/folders/cl/clrOX6SaG+moCeRKEI4PtU+++TI/-Tmp-/cc-bXYITq.o and /var/folders/cl/clrOX6SaG+moCeRKEI4PtU+++TI/-Tmp-/cc-6uK4jD.o clang: error: linker command failed with exit code 1 (use -v to see invocation) -- For a compiler crash we get: -- ddunbar@lordcrumb:tmp$ clang t.i Assertion failed: (CGT.getTargetData().getTypeAllocSizeInBits(STy) == RL.getSize()), function layoutStructFields, file CodeGenTypes.cpp, line 573. 0 clang-cc 0x0000000100f1f1f1 PrintStackTrace(void*) + 38 ... stack trace and virtual stack trace follow ... clang: error: compiler command failed due to signal 6 (use -v to see invocation) -- But for a regular compilation failure we get the usual: -- ddunbar@lordcrumb:tmp$ clang c.c c.c:1:6: error: invalid token after top level declarator int x ^ 1 diagnostic generated. -- - No test case, not amenable to non-executable testing. :/ - <rdar://problem/6945613> llvm-svn: 74629
206 lines
6.7 KiB
C++
206 lines
6.7 KiB
C++
//===--- Compilation.cpp - Compilation Task Implementation --------------*-===//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "clang/Driver/Compilation.h"
|
|
|
|
#include "clang/Driver/Action.h"
|
|
#include "clang/Driver/ArgList.h"
|
|
#include "clang/Driver/Driver.h"
|
|
#include "clang/Driver/DriverDiagnostic.h"
|
|
#include "clang/Driver/ToolChain.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
#include "llvm/System/Program.h"
|
|
#include <sys/stat.h>
|
|
#include <errno.h>
|
|
using namespace clang::driver;
|
|
|
|
Compilation::Compilation(Driver &D,
|
|
ToolChain &_DefaultToolChain,
|
|
InputArgList *_Args)
|
|
: TheDriver(D), DefaultToolChain(_DefaultToolChain), Args(_Args) {
|
|
}
|
|
|
|
Compilation::~Compilation() {
|
|
delete Args;
|
|
|
|
// Free any derived arg lists.
|
|
for (llvm::DenseMap<const ToolChain*, DerivedArgList*>::iterator
|
|
it = TCArgs.begin(), ie = TCArgs.end(); it != ie; ++it)
|
|
delete it->second;
|
|
|
|
// Free the actions, if built.
|
|
for (ActionList::iterator it = Actions.begin(), ie = Actions.end();
|
|
it != ie; ++it)
|
|
delete *it;
|
|
}
|
|
|
|
const DerivedArgList &Compilation::getArgsForToolChain(const ToolChain *TC) {
|
|
if (!TC)
|
|
TC = &DefaultToolChain;
|
|
|
|
DerivedArgList *&Entry = TCArgs[TC];
|
|
if (!Entry)
|
|
Entry = TC->TranslateArgs(*Args);
|
|
|
|
return *Entry;
|
|
}
|
|
|
|
void Compilation::PrintJob(llvm::raw_ostream &OS, const Job &J,
|
|
const char *Terminator, bool Quote) const {
|
|
if (const Command *C = dyn_cast<Command>(&J)) {
|
|
OS << " \"" << C->getExecutable() << '"';
|
|
for (ArgStringList::const_iterator it = C->getArguments().begin(),
|
|
ie = C->getArguments().end(); it != ie; ++it) {
|
|
if (Quote)
|
|
OS << " \"" << *it << '"';
|
|
else
|
|
OS << ' ' << *it;
|
|
}
|
|
OS << Terminator;
|
|
} else if (const PipedJob *PJ = dyn_cast<PipedJob>(&J)) {
|
|
for (PipedJob::const_iterator
|
|
it = PJ->begin(), ie = PJ->end(); it != ie; ++it)
|
|
PrintJob(OS, **it, (it + 1 != PJ->end()) ? " |\n" : "\n", Quote);
|
|
} else {
|
|
const JobList *Jobs = cast<JobList>(&J);
|
|
for (JobList::const_iterator
|
|
it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
|
|
PrintJob(OS, **it, Terminator, Quote);
|
|
}
|
|
}
|
|
|
|
bool Compilation::CleanupFileList(const ArgStringList &Files,
|
|
bool IssueErrors) const {
|
|
bool Success = true;
|
|
|
|
for (ArgStringList::const_iterator
|
|
it = Files.begin(), ie = Files.end(); it != ie; ++it) {
|
|
llvm::sys::Path P(*it);
|
|
std::string Error;
|
|
|
|
if (P.eraseFromDisk(false, &Error)) {
|
|
// Failure is only failure if the file doesn't exist. There is a
|
|
// race condition here due to the limited interface of
|
|
// llvm::sys::Path, we want to know if the removal gave E_NOENT.
|
|
|
|
// FIXME: Grumble, P.exists() is broken. PR3837.
|
|
struct stat buf;
|
|
if (::stat(P.c_str(), &buf) == 0
|
|
|| errno != ENOENT) {
|
|
if (IssueErrors)
|
|
getDriver().Diag(clang::diag::err_drv_unable_to_remove_file)
|
|
<< Error;
|
|
Success = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
return Success;
|
|
}
|
|
|
|
int Compilation::ExecuteCommand(const Command &C,
|
|
const Command *&FailingCommand) const {
|
|
llvm::sys::Path Prog(C.getExecutable());
|
|
const char **Argv = new const char*[C.getArguments().size() + 2];
|
|
Argv[0] = C.getExecutable();
|
|
std::copy(C.getArguments().begin(), C.getArguments().end(), Argv+1);
|
|
Argv[C.getArguments().size() + 1] = 0;
|
|
|
|
if (getDriver().CCCEcho || getArgs().hasArg(options::OPT_v))
|
|
PrintJob(llvm::errs(), C, "\n", false);
|
|
|
|
std::string Error;
|
|
int Res =
|
|
llvm::sys::Program::ExecuteAndWait(Prog, Argv,
|
|
/*env*/0, /*redirects*/0,
|
|
/*secondsToWait*/0, /*memoryLimit*/0,
|
|
&Error);
|
|
if (!Error.empty()) {
|
|
assert(Res && "Error string set with 0 result code!");
|
|
getDriver().Diag(clang::diag::err_drv_command_failure) << Error;
|
|
}
|
|
|
|
if (Res)
|
|
FailingCommand = &C;
|
|
|
|
delete[] Argv;
|
|
return Res;
|
|
}
|
|
|
|
int Compilation::ExecuteJob(const Job &J,
|
|
const Command *&FailingCommand) const {
|
|
if (const Command *C = dyn_cast<Command>(&J)) {
|
|
return ExecuteCommand(*C, FailingCommand);
|
|
} else if (const PipedJob *PJ = dyn_cast<PipedJob>(&J)) {
|
|
// Piped commands with a single job are easy.
|
|
if (PJ->size() == 1)
|
|
return ExecuteCommand(**PJ->begin(), FailingCommand);
|
|
|
|
FailingCommand = *PJ->begin();
|
|
getDriver().Diag(clang::diag::err_drv_unsupported_opt) << "-pipe";
|
|
return 1;
|
|
} else {
|
|
const JobList *Jobs = cast<JobList>(&J);
|
|
for (JobList::const_iterator
|
|
it = Jobs->begin(), ie = Jobs->end(); it != ie; ++it)
|
|
if (int Res = ExecuteJob(**it, FailingCommand))
|
|
return Res;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
int Compilation::Execute() const {
|
|
// Just print if -### was present.
|
|
if (getArgs().hasArg(options::OPT__HASH_HASH_HASH)) {
|
|
PrintJob(llvm::errs(), Jobs, "\n", true);
|
|
return 0;
|
|
}
|
|
|
|
// If there were errors building the compilation, quit now.
|
|
if (getDriver().getDiags().getNumErrors())
|
|
return 1;
|
|
|
|
const Command *FailingCommand = 0;
|
|
int Res = ExecuteJob(Jobs, FailingCommand);
|
|
|
|
// Remove temp files.
|
|
CleanupFileList(TempFiles);
|
|
|
|
// If the compilation failed, remove result files as well.
|
|
if (Res != 0 && !getArgs().hasArg(options::OPT_save_temps))
|
|
CleanupFileList(ResultFiles, true);
|
|
|
|
// Print extra information about abnormal failures, if possible.
|
|
if (Res) {
|
|
// This is ad-hoc, but we don't want to be excessively noisy. If the result
|
|
// status was 1, assume the command failed normally. In particular, if it
|
|
// was the compiler then assume it gave a reasonable error code. Failures in
|
|
// other tools are less common, and they generally have worse diagnostics,
|
|
// so always print the diagnostic there.
|
|
const Action &Source = FailingCommand->getSource();
|
|
bool IsFriendlyTool = (isa<PreprocessJobAction>(Source) ||
|
|
isa<PrecompileJobAction>(Source) ||
|
|
isa<AnalyzeJobAction>(Source) ||
|
|
isa<CompileJobAction>(Source));
|
|
|
|
if (!IsFriendlyTool || Res != 1) {
|
|
// FIXME: See FIXME above regarding result code interpretation.
|
|
if (Res < 0)
|
|
getDriver().Diag(clang::diag::err_drv_command_signalled)
|
|
<< Source.getClassName() << -Res;
|
|
else
|
|
getDriver().Diag(clang::diag::err_drv_command_failed)
|
|
<< Source.getClassName() << Res;
|
|
}
|
|
}
|
|
|
|
return Res;
|
|
}
|