Skip to content

Commit 94c6dfb

Browse files
committed
[clang] Implement setting crash_diagnostics_dir through env variable
This implements setting the equivalent of `-fcrash-diagnostics-dir` through the environment variable `CLANG_CRASH_DIAGNOSTICS_DIR`. If present, the flag still takes precedence. This helps integration with test frameworks and pipelines. With this feature, we change the libcxx bootstrapping build pipeline to produce clang crash reproducers as artifacts. Signed-off-by: Matheus Izvekov <[email protected]> Differential Revision: https://reviews.llvm.org/D133082
1 parent 27e7db5 commit 94c6dfb

File tree

5 files changed

+26
-8
lines changed

5 files changed

+26
-8
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ Improvements to Clang's diagnostics
135135

136136
Non-comprehensive list of changes in this release
137137
-------------------------------------------------
138+
- It's now possible to set the crash diagnostics directory through
139+
the environment variable ``CLANG_CRASH_DIAGNOSTICS_DIR``.
140+
The ``-fcrash-diagnostics-dir`` flag takes precedence.
138141

139142
New Compiler Flags
140143
------------------

clang/docs/UsersManual.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,11 @@ of generating a delta reduced test case.
687687
Specify where to write the crash diagnostics files; defaults to the
688688
usual location for temporary files.
689689

690+
.. envvar:: CLANG_CRASH_DIAGNOSTICS_DIR=<dir>
691+
692+
Like :option:`-fcrash-diagnostics-dir=<dir>`, specifies where to write the
693+
crash diagnostics files, but with lower precedence than the option.
694+
690695
Clang is also capable of generating preprocessed source file(s) and associated
691696
run script(s) even without a crash. This is specially useful when trying to
692697
generate a reproducer for warnings or errors while using modules.

clang/lib/Driver/Driver.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5413,15 +5413,18 @@ const char *Driver::CreateTempFile(Compilation &C, StringRef Prefix,
54135413
StringRef BoundArch) const {
54145414
SmallString<128> TmpName;
54155415
Arg *A = C.getArgs().getLastArg(options::OPT_fcrash_diagnostics_dir);
5416-
if (CCGenDiagnostics && A) {
5417-
SmallString<128> CrashDirectory(A->getValue());
5418-
if (!getVFS().exists(CrashDirectory))
5419-
llvm::sys::fs::create_directories(CrashDirectory);
5420-
llvm::sys::path::append(CrashDirectory, Prefix);
5416+
Optional<std::string> CrashDirectory =
5417+
CCGenDiagnostics && A
5418+
? std::string(A->getValue())
5419+
: llvm::sys::Process::GetEnv("CLANG_CRASH_DIAGNOSTICS_DIR");
5420+
if (CrashDirectory) {
5421+
if (!getVFS().exists(*CrashDirectory))
5422+
llvm::sys::fs::create_directories(*CrashDirectory);
5423+
SmallString<128> Path(*CrashDirectory);
5424+
llvm::sys::path::append(Path, Prefix);
54215425
const char *Middle = !Suffix.empty() ? "-%%%%%%." : "-%%%%%%";
5422-
std::error_code EC = llvm::sys::fs::createUniqueFile(
5423-
CrashDirectory + Middle + Suffix, TmpName);
5424-
if (EC) {
5426+
if (std::error_code EC =
5427+
llvm::sys::fs::createUniqueFile(Path + Middle + Suffix, TmpName)) {
54255428
Diag(clang::diag::err_unable_to_make_temp) << EC.message();
54265429
return "";
54275430
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// RUN: rm -rf %t
2+
// RUN: not env CLANG_CRASH_DIAGNOSTICS_DIR=%t %clang -c %s -o - 2>&1 | FileCheck %s
3+
#pragma clang __debug parser_crash
4+
// CHECK: Preprocessed source(s) and associated run script(s) are located at:
5+
// CHECK: diagnostic msg: {{.*}}{{/|\\}}crash-diagnostics-dir-3.c.tmp{{(/|\\).*}}.c

libcxx/utils/ci/buildkite-pipeline.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,10 +369,12 @@ steps:
369369
artifact_paths:
370370
- "**/test-results.xml"
371371
- "**/*.abilist"
372+
- "**/crash_diagnostics/*"
372373
env:
373374
CC: "clang-${LLVM_HEAD_VERSION}"
374375
CXX: "clang++-${LLVM_HEAD_VERSION}"
375376
LLVM_SYMBOLIZER_PATH: "/usr/bin/llvm-symbolizer-${LLVM_HEAD_VERSION}"
377+
CLANG_CRASH_DIAGNOSTICS_DIR: "crash_diagnostics"
376378
agents:
377379
queue: "libcxx-builders"
378380
os: "linux"

0 commit comments

Comments
 (0)