Skip to content

Commit 919d1ab

Browse files
authored
Redefine __FILE__ to use only the basename of the file (#121)
1 parent f560745 commit 919d1ab

10 files changed

Lines changed: 105 additions & 18 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,6 @@ bool Converter::VisitUsingType(clang::UsingType *type) {
275275
bool Converter::Convert(clang::Decl *decl) { return TraverseDecl(decl); }
276276

277277
bool Converter::VisitTranslationUnitDecl(clang::TranslationUnitDecl *decl) {
278-
if (auto main_file_name = GetMainFileName(ctx_); main_file_name != "input") {
279-
StrCat("\n//", main_file_name + ".rs\n");
280-
}
281278
for (auto *child : decl->decls()) {
282279
if (IsConvertibleDecl(child) &&
283280
(IsInMainFile(child) || !decl_ids_.contains(GetID(child)))) {

cpp2rust/cpp2rust_lib.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <clang/Tooling/CompilationDatabase.h>
88
#include <clang/Tooling/Tooling.h>
99

10+
#include <filesystem>
11+
1012
#include "compat/platform_flags.h"
1113
#include "frontend_action.h"
1214

@@ -23,8 +25,9 @@ std::string TranspileSrc(std::string_view cc_code, Model model,
2325

2426
std::string rs_code;
2527
clang::tooling::runToolOnCodeWithArgs(
26-
std::make_unique<FrontendAction>(rs_code, model, true, rules_dir),
27-
cc_code, tool_args, filename.ends_with(".c") ? "input.c" : "input.cpp",
28+
std::make_unique<FrontendAction>(rs_code, model, /*first=*/true,
29+
rules_dir),
30+
cc_code, tool_args, std::filesystem::path(filename).filename().string(),
2831
filename.ends_with(".c") ? CLANG_C_COMPILER : CLANG_CXX_COMPILER);
2932
return rs_code;
3033
}
@@ -49,6 +52,18 @@ std::string TranspileDir(std::string_view build_dir, Model model,
4952
clang::tooling::ArgumentInsertPosition::BEGIN));
5053
Tool.appendArgumentsAdjuster(clang::tooling::getInsertArgumentAdjuster(
5154
getPlatformClangEndFlags(), clang::tooling::ArgumentInsertPosition::END));
55+
// Redefine __FILE__ to use just the basename, so the generated code
56+
// doesn't contain system-specific absolute paths.
57+
Tool.appendArgumentsAdjuster(
58+
[](const clang::tooling::CommandLineArguments &args,
59+
llvm::StringRef filename) {
60+
auto result = args;
61+
auto basename =
62+
std::filesystem::path(filename.str()).filename().string();
63+
result.push_back("-Wno-builtin-macro-redefined");
64+
result.push_back("-D__FILE__=\"" + basename + "\"");
65+
return result;
66+
});
5267

5368
std::string rs_code;
5469
FrontendActionFactory factory(rs_code, model, rules_dir);

tests/lit/lit/formats/Cpp2RustTest.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,15 @@ def build_cpp(self):
184184
else os.environ.get("CXX", "clang++")
185185
)
186186
self.cpp_bin = self.tmp_dir / "cpp"
187-
cmd = [cc, "-O3", "-o", str(self.cpp_bin), str(self.cc_input)]
187+
cmd = [
188+
cc,
189+
"-O3",
190+
"-o",
191+
str(self.cpp_bin),
192+
str(self.cc_input),
193+
"-Wno-builtin-macro-redefined",
194+
'-D__FILE__="' + os.path.basename(self.cc_input) + '"',
195+
]
188196
_, _, rc = lit.util.executeCommand(cmd)
189197
if rc != 0:
190198
return (exp.fail_code, cc + " failed")

tests/multi-file/extern_functions/out/refcount/extern_functions.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ use std::io::prelude::*;
66
use std::io::{Read, Seek, Write};
77
use std::os::fd::AsFd;
88
use std::rc::{Rc, Weak};
9-
10-
// a.rs
119
pub fn main() {
1210
std::process::exit(main_0());
1311
}
@@ -21,7 +19,6 @@ fn main_0() -> i32 {
2119
);
2220
return 0;
2321
}
24-
// b.rs
2522
pub fn unrelated1_1() -> i32 {
2623
return 1;
2724
}

tests/multi-file/extern_functions/out/unsafe/extern_functions.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ use std::collections::BTreeMap;
66
use std::io::{Read, Seek, Write};
77
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
88
use std::rc::Rc;
9-
10-
// a.rs
119
pub fn main() {
1210
unsafe {
1311
std::process::exit(main_0() as i32);
@@ -23,7 +21,6 @@ unsafe fn main_0() -> i32 {
2321
);
2422
return 0;
2523
}
26-
// b.rs
2724
pub unsafe fn unrelated1_1() -> i32 {
2825
return 1;
2926
}

tests/multi-file/header_function/out/refcount/header_function.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ use std::io::prelude::*;
66
use std::io::{Read, Seek, Write};
77
use std::os::fd::AsFd;
88
use std::rc::{Rc, Weak};
9-
10-
// a.rs
119
pub fn main() {
1210
std::process::exit(main_0());
1311
}
@@ -21,7 +19,6 @@ fn main_0() -> i32 {
2119
);
2220
return 0;
2321
}
24-
// b.rs
2522
pub fn unrelated1_1() -> i32 {
2623
return 1;
2724
}

tests/multi-file/header_function/out/unsafe/header_function.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ use std::collections::BTreeMap;
66
use std::io::{Read, Seek, Write};
77
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
88
use std::rc::Rc;
9-
10-
// a.rs
119
pub fn main() {
1210
unsafe {
1311
std::process::exit(main_0() as i32);
@@ -23,7 +21,6 @@ unsafe fn main_0() -> i32 {
2321
);
2422
return 0;
2523
}
26-
// b.rs
2724
pub unsafe fn unrelated1_1() -> i32 {
2825
return 1;
2926
}

tests/unit/macros.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <stdio.h>
2+
3+
void log(const char *file, int line, const char *func) {
4+
printf("%s %d %s\n", file, line, func);
5+
}
6+
7+
int main() {
8+
printf("%s %d %s\n", __FILE__, __LINE__, __FUNCTION__);
9+
log(__FILE__, __LINE__, __FUNCTION__);
10+
return 0;
11+
}

tests/unit/out/refcount/macros.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
pub fn log_0(file: Ptr<u8>, line: i32, func: Ptr<u8>) {
10+
let file: Value<Ptr<u8>> = Rc::new(RefCell::new(file));
11+
let line: Value<i32> = Rc::new(RefCell::new(line));
12+
let func: Value<Ptr<u8>> = Rc::new(RefCell::new(func));
13+
println!(
14+
"{} {} {}",
15+
(*file.borrow()),
16+
(*line.borrow()),
17+
(*func.borrow())
18+
);
19+
}
20+
pub fn main() {
21+
std::process::exit(main_0());
22+
}
23+
fn main_0() -> i32 {
24+
println!(
25+
"{} {} {}",
26+
Ptr::from_string_literal("macros.cpp"),
27+
8,
28+
Ptr::from_string_literal("main")
29+
);
30+
({
31+
let _file: Ptr<u8> = Ptr::from_string_literal("macros.cpp");
32+
let _line: i32 = 9;
33+
let _func: Ptr<u8> = Ptr::from_string_literal("main");
34+
log_0(_file, _line, _func)
35+
});
36+
return 0;
37+
}

tests/unit/out/unsafe/macros.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
pub unsafe fn log_0(mut file: *const u8, mut line: i32, mut func: *const u8) {
10+
printf(b"%s %d %s\n\0".as_ptr() as *const i8, file, line, func);
11+
}
12+
pub fn main() {
13+
unsafe {
14+
std::process::exit(main_0() as i32);
15+
}
16+
}
17+
unsafe fn main_0() -> i32 {
18+
printf(
19+
b"%s %d %s\n\0".as_ptr() as *const i8,
20+
b"macros.cpp\0".as_ptr(),
21+
8,
22+
b"main\0".as_ptr(),
23+
);
24+
(unsafe {
25+
let _file: *const u8 = b"macros.cpp\0".as_ptr();
26+
let _line: i32 = 9;
27+
let _func: *const u8 = b"main\0".as_ptr();
28+
log_0(_file, _line, _func)
29+
});
30+
return 0;
31+
}

0 commit comments

Comments
 (0)