Skip to content

Commit 4afa3c8

Browse files
fix: add -arch flag to GCC flags to prevent argument loss on macOS ARM (#674)
* fix: add -arch flag to GCC flags to prevent argument loss On macOS, cc/gcc are Apple Clang and pass -arch arm64, but Bear classifies them as GCC. Since -arch was only defined in CLANG_FLAGS, the GCC parser treated -arch as an unknown standalone flag and misclassified the following argument (e.g. arm64) as a source file, which was then silently dropped from compile_commands.json. This caused clangd to report: Invalid arch name '-arch -Wall' Add -arch and -arch_only to GCC_FLAGS so the parser correctly consumes the flag-value pair regardless of which interpreter handles the command. Fixes #673 * fix: lint issue
1 parent cf6e92a commit 4afa3c8

File tree

1 file changed

+39
-0
lines changed
  • bear/src/semantic/interpreters/compilers

1 file changed

+39
-0
lines changed

bear/src/semantic/interpreters/compilers/gcc.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,11 @@ pub static GCC_FLAGS: std::sync::LazyLock<Vec<FlagRule>> = std::sync::LazyLock::
406406
ArgumentKind::Other(PassEffect::Configures(CompilerPass::Assembling)),
407407
),
408408
// Other/unclassified options
409+
FlagRule::new(
410+
FlagPattern::Exactly("-arch", 1),
411+
ArgumentKind::Other(PassEffect::Configures(CompilerPass::Compiling)),
412+
),
413+
FlagRule::new(FlagPattern::Exactly("-arch_only", 1), ArgumentKind::Other(PassEffect::None)),
409414
FlagRule::new(FlagPattern::Exactly("-ansi", 0), ArgumentKind::Other(PassEffect::None)),
410415
FlagRule::new(FlagPattern::Exactly("-aux-info", 1), ArgumentKind::Other(PassEffect::None)),
411416
// Compilation options
@@ -1484,4 +1489,38 @@ mod tests {
14841489
}
14851490
}
14861491
}
1492+
1493+
#[test]
1494+
fn test_arch_flag_preserves_argument() {
1495+
let interpreter = GccInterpreter::new();
1496+
1497+
// On macOS, cc/gcc are Apple Clang and pass -arch arm64.
1498+
// The -arch flag takes one argument; both must be preserved.
1499+
let execution =
1500+
create_execution("cc", vec!["cc", "-arch", "arm64", "-Wall", "-O2", "-c", "hello.c"], "/project");
1501+
1502+
let result = interpreter.recognize(&execution).unwrap();
1503+
1504+
if let Command::Compiler(cmd) = result {
1505+
// -arch arm64 must be recognised as a single two-token flag
1506+
let arch_arg = cmd.arguments.iter().find(|a| {
1507+
let tokens = a.as_arguments(&|p| Cow::Borrowed(p));
1508+
tokens.len() == 2 && tokens[0] == "-arch" && tokens[1] == "arm64"
1509+
});
1510+
assert!(arch_arg.is_some(), "-arch arm64 should be captured as a single argument pair");
1511+
assert_eq!(
1512+
arch_arg.unwrap().kind(),
1513+
ArgumentKind::Other(PassEffect::Configures(CompilerPass::Compiling))
1514+
);
1515+
1516+
// arm64 must NOT appear as a source file
1517+
let bad_source = cmd.arguments.iter().any(|a| {
1518+
let tokens = a.as_arguments(&|p| Cow::Borrowed(p));
1519+
tokens.len() == 1 && tokens[0] == "arm64"
1520+
});
1521+
assert!(!bad_source, "arm64 must not be misclassified as a source file");
1522+
} else {
1523+
panic!("Expected compiler command");
1524+
}
1525+
}
14871526
}

0 commit comments

Comments
 (0)