Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions cpp2rust/converter/translation_rule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,6 @@ TypeRule ParseTypeRuleJSON(const llvm::json::Object &obj) {
return rule;
}

bool TargetOSMatchesHost(llvm::StringRef target_os) {
#if defined(__linux__)
return target_os == "linux";
#elif defined(__APPLE__)
return target_os == "macos";
#else
return false;
#endif
}

void LoadTgtFromIR(ExprRules &exprs, TypeRules &types,
const std::filesystem::path &json_path) {
auto buf = llvm::MemoryBuffer::getFile(json_path.string());
Expand All @@ -167,11 +157,6 @@ void LoadTgtFromIR(ExprRules &exprs, TypeRules &types,
if (!obj)
continue;

if (auto target_os = obj->getString("target_os");
target_os && !TargetOSMatchesHost(*target_os)) {
continue;
}

auto name = entry_name.str();
if (name[0] == 'f') {
exprs[std::move(name)] = ParseExprRuleJSON(*obj);
Expand Down
2 changes: 1 addition & 1 deletion rule-preprocessor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2024"
[dependencies]
libcc2rs = { path = "../libcc2rs" }
rules = { path = "../rules" }
cfg-expr = "0.20"
ra_ap_syntax = "0.0.266"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
syn = "2"
2 changes: 0 additions & 2 deletions rule-preprocessor/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ pub struct FnIr {
pub params: Option<BTreeMap<String, TypeInfo>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub return_type: Option<TypeInfo>,
#[serde(skip_serializing_if = "Option::is_none")]
pub target_os: Option<String>,
}

impl FnIr {
Expand Down
64 changes: 31 additions & 33 deletions rule-preprocessor/src/syntactic.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

use ra_ap_syntax::ast::{HasGenericParams, HasName, HasTypeBounds};
use cfg_expr::Expression;
use cfg_expr::expr::{Predicate, TargetPredicate};
use ra_ap_syntax::ast::{HasAttrs, HasGenericParams, HasName, HasTypeBounds};
use ra_ap_syntax::{AstNode, SyntaxKind, ast, match_ast};
use std::collections::{BTreeMap, HashMap};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -38,6 +40,31 @@ fn pointer_flags(ty: &ast::Type) -> (bool, bool) {
flags
}

fn cfg_matches_host(fn_item: &ast::Fn) -> bool {
for attr in fn_item.attrs() {
let Some(meta) = attr.meta() else { continue };
let Some(path) = meta.path() else { continue };
if path.syntax().text() != "cfg" {
continue;
}
let meta_text = meta.syntax().text().to_string();
let expr = Expression::parse(&meta_text)
.unwrap_or_else(|e| panic!("failed to parse `{meta_text}`: {e}"));
let matches = expr.eval(|pred| match pred {
Predicate::Target(TargetPredicate::Os(os)) => match os.as_str() {
"linux" => cfg!(target_os = "linux"),
"macos" => cfg!(target_os = "macos"),
other => panic!("unsupported target_os in cfg: {other}"),
},
_ => panic!("unsupported cfg predicate in `{meta_text}`"),
});
if !matches {
return false;
}
}
true
}

pub struct SyntacticAnalysis;

impl SyntacticAnalysis {
Expand All @@ -49,12 +76,6 @@ impl SyntacticAnalysis {
let source = std::fs::read_to_string(rule_file).unwrap();
let file_ir = Self::parse_rule_file(&source, rule_file);

assert!(
!file_ir.is_empty(),
"Rule file {} produced no IR",
rule_file.display()
);

let canonical = rule_file
.canonicalize()
.unwrap_or_else(|_| rule_file.clone())
Expand Down Expand Up @@ -103,6 +124,9 @@ impl SyntacticAnalysis {
file_ir.insert(name, RuleIr::Type(ir));
}
} else if fn_name.starts_with('f') {
if !cfg_matches_host(&fn_item) {
continue;
}
file_ir.insert(
fn_name.clone(),
RuleIr::Fn(FnIrBuilder::new(&fn_item).build(path)),
Expand Down Expand Up @@ -253,31 +277,6 @@ impl<'a> FnIrBuilder<'a> {
Self { fn_item }
}

fn get_target_os(&self) -> Option<String> {
use ast::HasAttrs;
for attr in self.fn_item.attrs() {
let meta_text = attr.meta()?.syntax().text().to_string();
let syn::Meta::List(list) = syn::parse_str(&meta_text).ok()? else {
continue;
};
if !list.path.is_ident("cfg") {
continue;
}
let mut found = None;
let _ = list.parse_nested_meta(|nested| {
if nested.path.is_ident("target_os") {
let lit: syn::LitStr = nested.value()?.parse()?;
found = Some(lit.value());
}
Ok(())
});
if found.is_some() {
return found;
}
}
None
}

fn params(&self) -> Vec<ParamInfo> {
let mut params = Vec::new();
let Some(param_list) = self.fn_item.param_list() else {
Expand Down Expand Up @@ -512,7 +511,6 @@ impl<'a> FnIrBuilder<'a> {
},
multi_statement,
body,
target_os: self.get_target_os(),
};
ir.validate(&format!("{}:{}", path.display(), fn_name));
ir
Expand Down
10 changes: 10 additions & 0 deletions rules/ip/src.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,13 @@ int f2() {
int f3() {
return IPPROTO_IP;
}

int f4() {
return IPPROTO_IPV6;
}

#if defined(__linux__)
int f5() {
return IPPROTO_MPTCP;
}
#endif
9 changes: 9 additions & 0 deletions rules/ip/tgt_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ unsafe fn f2() -> i32 {
unsafe fn f3() -> i32 {
libc::IPPROTO_IP
}

unsafe fn f4() -> i32 {
libc::IPPROTO_IPV6
}

#[cfg(target_os = "linux")]
unsafe fn f5() -> i32 {
libc::IPPROTO_MPTCP
}
3 changes: 2 additions & 1 deletion tests/unit/ipproto_macros.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ int main() {
int tcp = IPPROTO_TCP;
int udp = IPPROTO_UDP;
int ip = IPPROTO_IP;
return tcp + udp + ip;
int ip6 = IPPROTO_IPV6;
return tcp + udp + ip + ip6;
}
3 changes: 2 additions & 1 deletion tests/unit/out/refcount/ipproto_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ fn main_0() -> i32 {
let tcp: Value<i32> = Rc::new(RefCell::new(libc::IPPROTO_TCP));
let udp: Value<i32> = Rc::new(RefCell::new(libc::IPPROTO_UDP));
let ip: Value<i32> = Rc::new(RefCell::new(libc::IPPROTO_IP));
return (((*tcp.borrow()) + (*udp.borrow())) + (*ip.borrow()));
let ip6: Value<i32> = Rc::new(RefCell::new(libc::IPPROTO_IPV6));
return ((((*tcp.borrow()) + (*udp.borrow())) + (*ip.borrow())) + (*ip6.borrow()));
}
3 changes: 2 additions & 1 deletion tests/unit/out/unsafe/ipproto_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ unsafe fn main_0() -> i32 {
let mut tcp: i32 = libc::IPPROTO_TCP;
let mut udp: i32 = libc::IPPROTO_UDP;
let mut ip: i32 = libc::IPPROTO_IP;
return (((tcp) + (udp)) + (ip));
let mut ip6: i32 = libc::IPPROTO_IPV6;
return ((((tcp) + (udp)) + (ip)) + (ip6));
}
Loading