Skip to content

Commit

Permalink
Improve error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
Kobzol committed Jul 1, 2023
1 parent ce5ec54 commit fc61ad3
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 18 deletions.
6 changes: 3 additions & 3 deletions compiler/rustc_codegen_llvm/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ impl<'a> DiagnosticHandlers<'a> {
handler: &'a Handler,
llcx: &'a llvm::Context,
module: &ModuleCodegen<ModuleLlvm>,
section: CodegenDiagnosticsStage,
stage: CodegenDiagnosticsStage,
) -> Self {
let remark_passes_all: bool;
let remark_passes: Vec<CString>;
Expand All @@ -312,12 +312,12 @@ impl<'a> DiagnosticHandlers<'a> {
.as_ref()
// Use the .opt.yaml file suffix, which is supported by LLVM's opt-viewer.
.map(|dir| {
let section = match section {
let stage_suffix = match stage {
CodegenDiagnosticsStage::Codegen => "codegen",
CodegenDiagnosticsStage::Opt => "opt",
CodegenDiagnosticsStage::LTO => "lto",
};
dir.join(format!("{}.{section}.opt.yaml", module.name))
dir.join(format!("{}.{stage_suffix}.opt.yaml", module.name))
})
.and_then(|dir| dir.to_str().and_then(|p| CString::new(p).ok()));

Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_codegen_ssa/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ codegen_ssa_create_temp_dir = couldn't create a temp dir: {$error}
codegen_ssa_erroneous_constant = erroneous constant encountered
codegen_ssa_error_creating_remark_dir = failed to create remark directory: {$error}
codegen_ssa_expected_used_symbol = expected `used`, `used(compiler)` or `used(linker)`
codegen_ssa_extern_funcs_not_found = some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
Expand Down
10 changes: 6 additions & 4 deletions compiler/rustc_codegen_ssa/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use rustc_span::symbol::sym;
use rustc_span::{BytePos, FileName, InnerSpan, Pos, Span};
use rustc_target::spec::{MergeFunctions, SanitizerSet};

use crate::errors::ErrorCreatingRemarkDir;
use std::any::Any;
use std::borrow::Cow;
use std::fs;
Expand Down Expand Up @@ -1046,10 +1047,11 @@ fn start_executing_work<B: ExtraBackendMethods>(
let backend_features = tcx.global_backend_features(());

let remark_dir = if let Some(ref dir) = sess.opts.unstable_opts.remark_dir {
// Should this be here?
// Can this conflict with a parallel attempt to create this directory?
fs::create_dir_all(dir).expect("Cannot create remark directory");
Some(dir.canonicalize().expect("Cannot canonicalize remark directory"))
let result = fs::create_dir_all(dir).and_then(|_| dir.canonicalize());
match result {
Ok(dir) => Some(dir),
Err(error) => sess.emit_fatal(ErrorCreatingRemarkDir { error }),
}
} else {
None
};
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_codegen_ssa/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1023,3 +1023,9 @@ pub struct TargetFeatureSafeTrait {
#[label(codegen_ssa_label_def)]
pub def: Span,
}

#[derive(Diagnostic)]
#[diag(codegen_ssa_error_creating_remark_dir)]
pub struct ErrorCreatingRemarkDir {
pub error: std::io::Error,
}
28 changes: 17 additions & 11 deletions compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1877,17 +1877,17 @@ extern "C" void LLVMRustContextConfigureDiagnosticHandler(
void *DiagnosticHandlerContext,
bool RemarkAllPasses,
std::vector<std::string> RemarkPasses,
std::unique_ptr<ToolOutputFile> RemarksFile,
std::unique_ptr<llvm::remarks::RemarkStreamer> RemarkStreamer,
std::unique_ptr<LLVMRemarkStreamer> LlvmRemarkStreamer,
std::unique_ptr<ToolOutputFile> RemarksFile
std::unique_ptr<LLVMRemarkStreamer> LlvmRemarkStreamer
)
: DiagnosticHandlerCallback(DiagnosticHandlerCallback),
DiagnosticHandlerContext(DiagnosticHandlerContext),
RemarkAllPasses(RemarkAllPasses),
RemarkPasses(std::move(RemarkPasses)),
RemarksFile(std::move(RemarksFile)),
RemarkStreamer(std::move(RemarkStreamer)),
LlvmRemarkStreamer(std::move(LlvmRemarkStreamer)),
RemarksFile(std::move(RemarksFile)) {}
LlvmRemarkStreamer(std::move(LlvmRemarkStreamer)) {}

virtual bool handleDiagnostics(const DiagnosticInfo &DI) override {
if (this->LlvmRemarkStreamer) {
Expand Down Expand Up @@ -1939,11 +1939,11 @@ extern "C" void LLVMRustContextConfigureDiagnosticHandler(
bool RemarkAllPasses = false;
std::vector<std::string> RemarkPasses;

// Since LlvmRemarkStreamer contains a pointer to RemarkStreamer, the ordering of the two
// Since LlvmRemarkStreamer contains a pointer to RemarkStreamer, the ordering of the three
// members below is important.
std::unique_ptr<ToolOutputFile> RemarksFile;
std::unique_ptr<llvm::remarks::RemarkStreamer> RemarkStreamer;
std::unique_ptr<LLVMRemarkStreamer> LlvmRemarkStreamer;
std::unique_ptr<ToolOutputFile> RemarksFile;
};

std::vector<std::string> Passes;
Expand All @@ -1953,9 +1953,9 @@ extern "C" void LLVMRustContextConfigureDiagnosticHandler(
}

// We need to hold onto both the streamers and the opened file
std::unique_ptr<ToolOutputFile> RemarkFile;
std::unique_ptr<llvm::remarks::RemarkStreamer> RemarkStreamer;
std::unique_ptr<LLVMRemarkStreamer> LlvmRemarkStreamer;
std::unique_ptr<ToolOutputFile> RemarkFile;

if (RemarkFilePath != nullptr) {
std::error_code EC;
Expand All @@ -1964,6 +1964,12 @@ extern "C" void LLVMRustContextConfigureDiagnosticHandler(
EC,
llvm::sys::fs::OF_TextWithCRLF
);
if (EC) {
std::string Error = std::string("Cannot create remark file: ") +
toString(errorCodeToError(EC));
report_fatal_error(Twine(Error));
}

// Do not delete the file after we gather remarks
RemarkFile->keep();

Expand All @@ -1974,8 +1980,8 @@ extern "C" void LLVMRustContextConfigureDiagnosticHandler(
);
if (Error E = RemarkSerializer.takeError())
{
// Is this OK?
report_fatal_error("Cannot create remark serializer");
std::string Error = std::string("Cannot create remark serializer: ") + toString(std::move(E));
report_fatal_error(Twine(Error));
}
RemarkStreamer = std::make_unique<llvm::remarks::RemarkStreamer>(std::move(*RemarkSerializer));
LlvmRemarkStreamer = std::make_unique<LLVMRemarkStreamer>(*RemarkStreamer);
Expand All @@ -1986,9 +1992,9 @@ extern "C" void LLVMRustContextConfigureDiagnosticHandler(
DiagnosticHandlerContext,
RemarkAllPasses,
Passes,
std::move(RemarkFile),
std::move(RemarkStreamer),
std::move(LlvmRemarkStreamer),
std::move(RemarkFile)
std::move(LlvmRemarkStreamer)
));
}

Expand Down

0 comments on commit fc61ad3

Please sign in to comment.