5 #ifndef CLEARBLUE_CB_SPLIT_H
6 #define CLEARBLUE_CB_SPLIT_H
8 #include "Utils/ADT/kvec.h"
9 #include "Utils/ADT/kvec_ext.h"
10 #include "Utils/StringUtils.h"
11 #include <llvm/IR/DebugInfo.h>
12 #include <llvm/Support/Allocator.h>
13 #include <llvm/Support/Path.h>
15 #include <unordered_map>
21 BumpPtrAllocator string_allocator;
24 StringRef synthesize_file_path(StringRef File, StringRef Dir,
25 kvec_str<initN> &path_result) {
28 StringRef path_have_root_name;
30 if (!sys::path::is_absolute(File)) {
33 path_result.fill(Dir.data(), Dir.size());
34 path_have_root_name = Dir;
36 path_have_root_name = File;
41 char input_bc_path_sep =
'/';
42 if ((path_have_root_name[1] ==
':') && (path_have_root_name[2] ==
'\\')) {
43 input_bc_path_sep =
'\\';
46 bool first_comp =
true;
47 for (sys::path::const_iterator it = sys::path::begin(File),
48 ie = sys::path::end(File);
49 it != ie; ++it, first_comp =
false) {
50 StringRef component = *it;
52 if (component ==
"..") {
53 int pos = path_result.rfind(input_bc_path_sep);
55 path_result.erase_substr(pos);
59 }
else if (component !=
".") {
67 if (component == sys::path::root_name(File)) {
74 size_t last_bslash = component.rfind(
"/");
75 if (last_bslash != StringRef::npos) {
76 component = component.substr(last_bslash);
89 if (!component.startswith(StringRef(&input_bc_path_sep, 1)) &&
90 (path_result.empty() || path_result.back() != input_bc_path_sep))
91 path_result.push_back(input_bc_path_sep);
94 path_result.append_str(component.data(), component.size());
100 StringRef file_name(path_result.c_str(), path_result.size());
101 file_name = hooked_format_str(string_allocator,
"%s", path_result.c_str());
105 std::unique_ptr<std::unordered_map<const Function *, StringRef>>
106 getFunctionSrcFiles(Module &M) {
108 auto pFuncSrcFileCache =
109 std::unique_ptr<std::unordered_map<const Function *, StringRef>>(
110 new std::unordered_map<const Function *, StringRef>());
113 DebugInfoFinder DbgFinder;
114 DbgFinder.processModule(M);
115 kvec_str<512> tmp_src_path;
118 auto it_func_range = DbgFinder.subprograms();
119 for (
auto it = it_func_range.begin(), ie = it_func_range.end(); it != ie;
121 const DISubprogram &f_loc = *it;
123 Function *F = f_loc.getFunction();
124 StringRef File = f_loc.getFilename();
125 StringRef Dir = f_loc.getDirectory();
126 if (File.data() && Dir.data()) {
127 (*pFuncSrcFileCache)[F] = synthesize_file_path(File, Dir, tmp_src_path);
129 (*pFuncSrcFileCache)[F] =
"";
132 return pFuncSrcFileCache;
135 #endif // CLEARBLUE_CB_SPLIT_H