Skip to content
This repository was archived by the owner on Jan 27, 2025. It is now read-only.

Commit 35e8afd

Browse files
committed
feat(utility): add location utility
1 parent 17a82e4 commit 35e8afd

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ target_compile_definitions(call-hook++ PUBLIC RUNTIME_HOME="${CMAKE_SOURCE_DIR}/
2222
target_compile_definitions(call-hook++ PUBLIC USE_CXX)
2323

2424
list(APPEND STRUCT_PARSER_SRCS ${BASE_SRCS} src/pass/pass_struct_parser.cc src/wrapper/wrapper_struct_parser.cc src/utility/struct_parser.cc)
25+
list(APPEND STRUCT_PARSER_SRCS src/utility/location.cc)
2526
add_executable(struct-parser ${STRUCT_PARSER_SRCS} src/main/struct_parser.cc)
2627
add_executable(struct-parser++ ${STRUCT_PARSER_SRCS} src/main/struct_parser.cc)
2728
target_compile_definitions(struct-parser++ PUBLIC USE_CXX)

src/pass/pass_struct_parser.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "common/logging.h"
66
#include "llvm/IR/Instructions.h"
77
#include "pass/pass_base.h"
8+
#include "utility/location.h"
89
#include "utility/struct_parser.h"
910

1011
using namespace llvm;
@@ -32,6 +33,7 @@ bool PassStructParser::run_on_load_inst(LoadInst& inst) {
3233
bool modified = false;
3334
auto name = this->struct_parser().get_field_name_from_value(inst);
3435
if (name.empty()) return modified;
36+
logger().info("at {}", get_inst_loc(inst));
3537
logger().info("{}: {}", name, inst);
3638
return modified;
3739
}
@@ -40,6 +42,7 @@ bool PassStructParser::run_on_get_element_ptr_inst(GetElementPtrInst& inst) {
4042
bool modified = false;
4143
auto name = this->struct_parser().get_field_name_from_address(inst);
4244
if (name.empty()) return modified;
45+
logger().info("at {}", get_inst_loc(inst));
4346
logger().info("{}: {}", name, inst);
4447
return modified;
4548
}

src/utility/location.cc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "location.h"
2+
3+
#include <sstream>
4+
#include <string>
5+
6+
#include "llvm/IR/DebugInfoMetadata.h"
7+
#include "llvm/IR/DebugLoc.h"
8+
#include "llvm/IR/Function.h"
9+
#include "llvm/IR/Instruction.h"
10+
#include "llvm/IR/Module.h"
11+
#include "llvm/Support/raw_ostream.h"
12+
13+
using namespace llvm;
14+
15+
std::string get_inst_loc(Instruction& inst) {
16+
std::string str;
17+
auto oss = raw_string_ostream(str);
18+
auto debug_loc = inst.getDebugLoc();
19+
if (debug_loc) {
20+
int line = debug_loc.getLine();
21+
int col = debug_loc.getCol();
22+
auto dir = debug_loc->getDirectory();
23+
auto file = debug_loc->getFilename();
24+
oss << dir.str() << "/" << file.str() << ":" << line << ":" << col;
25+
} else {
26+
auto mod = inst.getModule()->getName();
27+
auto func = inst.getFunction()->getName();
28+
auto opcode = inst.getOpcodeName();
29+
oss << mod.str() << ":" << func.str() << ":" << opcode;
30+
}
31+
return oss.str();
32+
}

src/utility/location.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef LOCATION_H_
2+
#define LOCATION_H_
3+
4+
#include <string>
5+
6+
#include "llvm/IR/Instruction.h"
7+
8+
std::string get_inst_loc(llvm::Instruction& inst);
9+
10+
#endif // LOCATION_H_

0 commit comments

Comments
 (0)