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);
0 commit comments