Skip to content

Commit 0aa03b3

Browse files
committed
[clang-cache] Introduce CLANG_CACHE_REDACT_TIME_MACROS environment variable
This can be used to get deterministic outputs for source that uses the `__DATE__`, `__TIMESTAMP__`, `__TIME__` macros. Related to rdar://100185558
1 parent 9ba2ffb commit 0aa03b3

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

clang/test/CAS/test-for-deterministic-outputs.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22

33
// This compiles twice with replay disabled, ensuring that we get the same outputs for the same key.
44

5-
// RUN: env LLVM_CACHE_CAS_PATH=%t/cas CLANG_CACHE_TEST_DETERMINISTIC_OUTPUTS=1 %clang-cache \
5+
// RUN: env LLVM_CACHE_CAS_PATH=%t/cas CLANG_CACHE_TEST_DETERMINISTIC_OUTPUTS=1 CLANG_CACHE_REDACT_TIME_MACROS=1 %clang-cache \
66
// RUN: %clang -target x86_64-apple-macos11 -c %s -o %t/t.o -Rcompile-job-cache 2> %t/out.txt
77
// RUN: FileCheck %s --check-prefix=CACHE-MISS --input-file=%t/out.txt
88

99
// CACHE-MISS: remark: compile job cache miss
1010
// CACHE-MISS: remark: compile job cache miss
11+
12+
void getit(const char **p1, const char **p2, const char **p3) {
13+
*p1 = __DATE__;
14+
*p2 = __TIMESTAMP__;
15+
*p3 = __TIME__;
16+
}

clang/tools/driver/CacheLauncherMode.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,13 @@ clang::handleClangCacheInvocation(SmallVectorImpl<const char *> &Args,
173173
}
174174
Args.append({"-greproducible"});
175175

176+
if (llvm::sys::Process::GetEnv("CLANG_CACHE_REDACT_TIME_MACROS")) {
177+
// Remove use of these macros to get reproducible outputs. This can
178+
// accompany CLANG_CACHE_TEST_DETERMINISTIC_OUTPUTS to avoid fatal errors
179+
// when the source uses these macros.
180+
Args.append({"-Wno-builtin-macro-redefined", "-D__DATE__=\"redacted\"",
181+
"-D__TIMESTAMP__=\"redacted\"", "-D__TIME__=\"redacted\""});
182+
}
176183
if (llvm::sys::Process::GetEnv("CLANG_CACHE_TEST_DETERMINISTIC_OUTPUTS")) {
177184
// Run the compilation twice, without replaying, to check that we get the
178185
// same compilation artifacts for the same key. If they are not the same

0 commit comments

Comments
 (0)