Skip to content

Commit 95cc408

Browse files
committed
Support src.cpp and src.c in the same rules dir
1 parent 959538b commit 95cc408

6 files changed

Lines changed: 125 additions & 16 deletions

File tree

CMakeLists.txt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,19 +146,20 @@ add_custom_target("check"
146146
DEPENDS check-libcc2rs check-unit
147147
)
148148

149-
file(GLOB rule_src_files
150-
${PROJECT_SOURCE_DIR}/rules/*/src.cpp
151-
${PROJECT_SOURCE_DIR}/rules/*/src.c)
149+
file(GLOB rule_subdirs ${PROJECT_SOURCE_DIR}/rules/*)
152150
set(cpp_rules_ir_outputs)
153151
set(rust_rules_ir_outputs)
154152
set(rust_rules_inputs)
155-
foreach(_src IN LISTS rule_src_files)
156-
get_filename_component(_rule_dir ${_src} DIRECTORY)
153+
foreach(_rule_dir IN LISTS rule_subdirs)
154+
file(GLOB _srcs ${_rule_dir}/src.c ${_rule_dir}/src.cpp)
155+
if(NOT _srcs)
156+
continue()
157+
endif()
157158
set(_out ${_rule_dir}/ir_src.json)
158159
add_custom_command(
159160
OUTPUT ${_out}
160-
COMMAND $<TARGET_FILE:cpp-rule-preprocessor> --file ${_src}
161-
DEPENDS ${_src} ${PROJECT_SOURCE_DIR}/cpp2rust/cpp_rule_preprocessor.cpp
161+
COMMAND $<TARGET_FILE:cpp-rule-preprocessor> --dir ${_rule_dir}
162+
DEPENDS ${_srcs} ${PROJECT_SOURCE_DIR}/cpp2rust/cpp_rule_preprocessor.cpp
162163
VERBATIM
163164
)
164165
list(APPEND cpp_rules_ir_outputs ${_out})

cpp2rust/cpp_rule_preprocessor.cpp

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -751,24 +751,40 @@ namespace {
751751
llvm::cl::OptionCategory cat("cpp-rule-preprocessor options");
752752

753753
llvm::cl::opt<std::string>
754-
SrcFile("file",
755-
llvm::cl::desc("Path to a rule's src.c or src.cpp. ir_src.json is "
756-
"written next to it"),
757-
llvm::cl::value_desc("src.c|src.cpp"), llvm::cl::Required,
758-
llvm::cl::cat(cat));
754+
SrcDir("dir",
755+
llvm::cl::desc("Path to a rule directory containing src.c and/or "
756+
"src.cpp. ir_src.json is written into this dir."),
757+
llvm::cl::value_desc("rule-dir"), llvm::cl::Required,
758+
llvm::cl::cat(cat));
759759

760760
} // namespace
761761

762762
int main(int argc, char *argv[]) {
763763
llvm::cl::HideUnrelatedOptions(cat);
764764
llvm::cl::ParseCommandLineOptions(argc, argv);
765765

766-
fs::path src = SrcFile.getValue();
767-
llvm::errs() << "Preprocessing " << src.string() << '\n';
766+
fs::path dir = SrcDir.getValue();
768767
llvm::json::Object root;
769-
cpp2rust::Extract(src, root);
768+
for (const char *name : {"src.c", "src.cpp"}) {
769+
auto path = dir / name;
770+
if (!fs::exists(path)) {
771+
continue;
772+
}
773+
llvm::errs() << "Preprocessing " << path.string() << '\n';
774+
llvm::json::Object file_root;
775+
cpp2rust::Extract(path, file_root);
776+
for (auto &[k, v] : file_root) {
777+
if (root.find(k) != root.end()) {
778+
llvm::errs() << "ERROR: rule name " << k.str()
779+
<< " defined in multiple files in " << dir.string()
780+
<< '\n';
781+
return EXIT_FAILURE;
782+
}
783+
root[k] = std::move(v);
784+
}
785+
}
770786

771-
auto out_path = src.parent_path() / "ir_src.json";
787+
auto out_path = dir / "ir_src.json";
772788
std::error_code ec;
773789
llvm::raw_fd_ostream out(out_path.string(), ec);
774790
if (ec) {

rules/cstring/ir_unsafe.json

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,5 +312,81 @@
312312
"type": "*mut u8",
313313
"is_unsafe_pointer": true
314314
}
315+
},
316+
"f5": {
317+
"body": [
318+
{
319+
"text": "libc::strchr("
320+
},
321+
{
322+
"placeholder": {
323+
"arg": 0,
324+
"access": "read"
325+
}
326+
},
327+
{
328+
"text": " as *const i8, "
329+
},
330+
{
331+
"placeholder": {
332+
"arg": 1,
333+
"access": "read"
334+
}
335+
},
336+
{
337+
"text": ") as *mut u8"
338+
}
339+
],
340+
"params": {
341+
"a0": {
342+
"type": "*const u8",
343+
"is_unsafe_pointer": true
344+
},
345+
"a1": {
346+
"type": "i32"
347+
}
348+
},
349+
"return_type": {
350+
"type": "*mut u8",
351+
"is_unsafe_pointer": true
352+
}
353+
},
354+
"f6": {
355+
"body": [
356+
{
357+
"text": "libc::strchr("
358+
},
359+
{
360+
"placeholder": {
361+
"arg": 0,
362+
"access": "read"
363+
}
364+
},
365+
{
366+
"text": " as *const i8, "
367+
},
368+
{
369+
"placeholder": {
370+
"arg": 1,
371+
"access": "read"
372+
}
373+
},
374+
{
375+
"text": ") as *const u8"
376+
}
377+
],
378+
"params": {
379+
"a0": {
380+
"type": "*const u8",
381+
"is_unsafe_pointer": true
382+
},
383+
"a1": {
384+
"type": "i32"
385+
}
386+
},
387+
"return_type": {
388+
"type": "*const u8",
389+
"is_unsafe_pointer": true
390+
}
315391
}
316392
}

rules/cstring/src.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ void *f2(void *dst, int c, size_t n) { return memset(dst, c, n); }
1010
int f3(const void *s1, const void *s2, size_t n) { return memcmp(s1, s2, n); }
1111

1212
void *f4(void *dst, const void *src, size_t n) { return memmove(dst, src, n); }
13+
14+
char *f5(const char *a0, int a1) { return strchr(a0, a1); }

rules/cstring/src.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
#include <cstring>
5+
6+
const char *f6(const char *a0, int a1) { return strchr(a0, a1); }

rules/cstring/tgt_unsafe.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,11 @@ unsafe fn f4(a0: *mut u8, a1: *const u8, a2: usize) -> *mut u8 {
3535
}
3636
a0
3737
}
38+
39+
unsafe fn f5(a0: *const u8, a1: i32) -> *mut u8 {
40+
libc::strchr(a0 as *const i8, a1) as *mut u8
41+
}
42+
43+
unsafe fn f6(a0: *const u8, a1: i32) -> *const u8 {
44+
libc::strchr(a0 as *const i8, a1) as *const u8
45+
}

0 commit comments

Comments
 (0)