Skip to content

Commit f28716d

Browse files
committed
Redefine __FILE__ to use only the basename of the file
1 parent f560745 commit f28716d

637 files changed

Lines changed: 1287 additions & 5 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

cpp2rust/converter/converter.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,7 @@ 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-
}
278+
StrCat("\n//", GetMainFileName(ctx_) + ".rs\n");
281279
for (auto *child : decl->decls()) {
282280
if (IsConvertibleDecl(child) &&
283281
(IsInMainFile(child) || !decl_ids_.contains(GetID(child)))) {
@@ -294,7 +292,7 @@ bool Converter::VisitFunctionDecl(clang::FunctionDecl *decl) {
294292
if (!IsConvertibleFunctionDecl(decl)) {
295293
return false;
296294
}
297-
if (!IsInMainFile(decl) && !decl_ids_.insert(GetID(decl)).second) {
295+
if (!decl_ids_.insert(GetID(decl)).second) {
298296
return false;
299297
}
300298
decl->dump(log());

cpp2rust/cpp2rust_lib.cpp

Lines changed: 15 additions & 1 deletion
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

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

5367
std::string rs_code;
5468
FrontendActionFactory factory(rs_code, model, rules_dir);

tests/benchmarks/out/refcount/array_sum.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ 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+
// array_sum.rs
911
pub fn main() {
1012
std::process::exit(main_0());
1113
}

tests/benchmarks/out/refcount/bfs.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ 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+
// bfs.rs
911
#[derive(Default)]
1012
pub struct Queue {
1113
pub elems: Value<Ptr<u32>>,

tests/benchmarks/out/refcount/bst.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ 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+
// bst.rs
911
#[derive(Default)]
1012
pub struct node_t {
1113
pub left: Value<Ptr<node_t>>,

tests/benchmarks/out/refcount/fibonacci.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ 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+
// fibonacci.rs
911
pub fn fib_0(n: u64) -> u64 {
1012
let n: Value<u64> = Rc::new(RefCell::new(n));
1113
return if ((*n.borrow()) == 0_u64) || ((*n.borrow()) == 1_u64) {

tests/benchmarks/out/refcount/prime.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ 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+
// prime.rs
911
pub fn is_prime_0(x: i32) -> bool {
1012
let x: Value<i32> = Rc::new(RefCell::new(x));
1113
let i: Value<i32> = Rc::new(RefCell::new(2));

tests/benchmarks/out/refcount/ptr_fibonacci.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ 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+
// ptr_fibonacci.rs
911
pub fn fib_0(n: Ptr<u64>) {
1012
let n: Value<Ptr<u64>> = Rc::new(RefCell::new(n));
1113
if (((*n.borrow()).read()) == 0_u64) || (((*n.borrow()).read()) == 1_u64) {

tests/benchmarks/out/refcount/ref_array_sum.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ 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+
// ref_array_sum.rs
911
pub fn initialize_0(array: Ptr<Option<Value<Box<[i32]>>>>, N: i32) {
1012
let N: Value<i32> = Rc::new(RefCell::new(N));
1113
let i: Value<i32> = Rc::new(RefCell::new(0));

tests/benchmarks/out/refcount/ref_fibonacci.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ 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+
// ref_fibonacci.rs
911
pub fn fib_0(n: Ptr<u64>) {
1012
if ((n.read()) == 0_u64) || ((n.read()) == 1_u64) {
1113
return;

0 commit comments

Comments
 (0)