12 #ifndef TOOLS_PP_EXTRACT_PP_EXTRACT_H_
13 #define TOOLS_PP_EXTRACT_PP_EXTRACT_H_
15 #include "Utils/ADT/kvec.h"
16 #include "Utils/ADT/kvec_ext.h"
17 #include "Utils/StringUtils.h"
18 #include <llvm/IR/DebugInfo.h>
19 #include <llvm/Support/Allocator.h>
20 #include <unordered_map>
26 BumpPtrAllocator string_allocator;
29 StringRef synthesize_file_path(StringRef File, StringRef Dir,
30 kvec_str<initN> &path_result) {
33 StringRef path_have_root_name;
35 if (!sys::path::is_absolute(File)) {
38 path_result.fill(Dir.data(), Dir.size());
39 path_have_root_name = Dir;
41 path_have_root_name = File;
46 char input_bc_path_sep =
'/';
47 if (path_have_root_name.size() >= 3 && (path_have_root_name[1] ==
':') &&
48 (path_have_root_name[2] ==
'\\')) {
49 input_bc_path_sep =
'\\';
52 bool first_comp =
true;
53 for (sys::path::const_iterator it = sys::path::begin(File),
54 ie = sys::path::end(File);
55 it != ie; ++it, first_comp =
false) {
56 StringRef component = *it;
58 if (component ==
"..") {
59 int pos = path_result.rfind(input_bc_path_sep);
61 path_result.erase_substr(pos);
65 }
else if (component !=
".") {
73 if (component == sys::path::root_name(File)) {
80 size_t last_bslash = component.rfind(
"/");
81 if (last_bslash != StringRef::npos) {
82 component = component.substr(last_bslash);
95 if (!component.startswith(StringRef(&input_bc_path_sep, 1)) &&
96 (path_result.empty() || path_result.back() != input_bc_path_sep))
97 path_result.push_back(input_bc_path_sep);
100 path_result.append_str(component.data(), component.size());
106 StringRef file_name(path_result.c_str(), path_result.size());
107 file_name = hooked_format_str(string_allocator,
"%s", path_result.c_str());
111 std::unique_ptr<std::unordered_map<const Function *, StringRef>>
112 getFunctionSrcFiles(Module &M) {
114 auto pFuncSrcFileCache =
115 std::unique_ptr<std::unordered_map<const Function *, StringRef>>(
116 new std::unordered_map<const Function *, StringRef>());
119 DebugInfoFinder DbgFinder;
120 DbgFinder.processModule(M);
121 kvec_str<512> tmp_src_path;
124 auto it_func_range = DbgFinder.subprograms();
125 for (
auto it = it_func_range.begin(), ie = it_func_range.end(); it != ie;
127 const DISubprogram &f_loc = *it;
129 Function *F = f_loc.getFunction();
130 StringRef File = f_loc.getFilename();
131 StringRef Dir = f_loc.getDirectory();
132 if (File.data() && Dir.data()) {
133 (*pFuncSrcFileCache)[F] = synthesize_file_path(File, Dir, tmp_src_path);
135 (*pFuncSrcFileCache)[F] =
"";
138 return pFuncSrcFileCache;