Skip to content

Commit

Permalink
fix(src/compiler/c): take common_args into account during preprocesso…
Browse files Browse the repository at this point in the history
…r hashing (#2039)

Signed-off-by: Gavin Zhao <git@gzgz.dev>
  • Loading branch information
GZGavinZhao authored Jan 18, 2024
1 parent 13b018f commit 8d3cd35
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/compiler/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ where
// use in creating a hash key
let mut preprocessor_and_arch_args = parsed_args.preprocessor_args.clone();
preprocessor_and_arch_args.extend(parsed_args.arch_args.to_vec());
// common_args is used in preprocessing too
preprocessor_and_arch_args.extend(parsed_args.common_args.to_vec());

let absolute_input_path: Cow<'_, _> = if parsed_args.input.is_absolute() {
Cow::Borrowed(&parsed_args.input)
Expand Down
63 changes: 63 additions & 0 deletions src/compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1726,6 +1726,69 @@ LLVM version: 6.0",
assert_ne!(results[0].key, results[1].key);
}

#[test_case(true ; "with preprocessor cache")]
#[test_case(false ; "without preprocessor cache")]
fn test_common_args_affects_hash(preprocessor_cache_mode: bool) {
let f = TestFixture::new();
let creator = new_creator();
let runtime = single_threaded_runtime();
let pool = runtime.handle();
let output = "compiler_id=clang\ncompiler_version=\"16.0.0\"";
let arguments = vec![
ovec!["-c", "foo.c", "-o", "foo.o", "-DHELLO"],
ovec!["-c", "foo.c", "-o", "foo.o", "-DHI"],
ovec!["-c", "foo.c", "-o", "foo.o"],
];
let cwd = f.tempdir.path();
// Write a dummy input file so the preprocessor cache mode can work
std::fs::write(f.tempdir.path().join("foo.c"), "whatever").unwrap();

let results: Vec<_> = arguments
.iter()
.map(|argument| {
next_command(&creator, Ok(MockChild::new(exit_status(0), output, "")));
let c = detect_compiler(
creator.clone(),
&f.bins[0],
f.tempdir.path(),
&[],
&[],
pool,
None,
)
.wait()
.unwrap()
.0;
next_command(
&creator,
Ok(MockChild::new(exit_status(0), "preprocessor output", "")),
);
let hasher = match c.parse_arguments(argument, ".".as_ref(), &[]) {
CompilerArguments::Ok(h) => h,
o => panic!("Bad result from parse_arguments: {:?}", o),
};
hasher
.generate_hash_key(
&creator,
cwd.to_path_buf(),
vec![],
false,
pool,
false,
Arc::new(MockStorage::new(None, preprocessor_cache_mode)),
CacheControl::Default,
)
.wait()
.unwrap()
})
.collect();

assert_eq!(results.len(), 3);
assert_ne!(results[0].key, results[1].key);
assert_ne!(results[1].key, results[2].key);
assert_ne!(results[0].key, results[2].key);
}

#[test]
fn test_get_compiler_info() {
let creator = new_creator();
Expand Down

0 comments on commit 8d3cd35

Please sign in to comment.