#ifndef OMPTARGET_SHARED_SOURCE_INFO_H
#define OMPTARGET_SHARED_SOURCE_INFO_H
#include <cstdint>
#include <string>
#ifdef _WIN32
constexpr bool OSWindows = true;
#else
constexpr bool OSWindows = false;
#endif
using map_var_info_t = void *;
struct ident_t {
int32_t reserved_1;
int32_t flags;
int32_t reserved_2;
int32_t reserved_3;
char const *psource;
};
class SourceInfo {
const std::string SourceStr;
const std::string Name;
const std::string Filename;
const int32_t Line;
const int32_t Column;
std::string initStr(const void *Name) {
if (!Name)
return ";unknown;unknown;0;0;;";
std::string Str = std::string(reinterpret_cast<const char *>(Name));
if (Str.find(';') == std::string::npos)
return ";" + Str + ";unknown;0;0;;";
return Str;
}
std::string initStr(const ident_t *Loc) {
if (!Loc)
return ";unknown;unknown;0;0;;";
return std::string(reinterpret_cast<const char *>(Loc->psource));
}
std::string getSubstring(const unsigned N) const {
std::size_t Begin = SourceStr.find(';');
std::size_t End = SourceStr.find(';', Begin + 1);
for (unsigned I = 0; I < N; I++) {
Begin = End;
End = SourceStr.find(';', Begin + 1);
}
return SourceStr.substr(Begin + 1, End - Begin - 1);
};
std::string removePath(const std::string &Path) const {
std::size_t Pos = (OSWindows) ? Path.rfind('\\') : Path.rfind('/');
return Path.substr(Pos + 1);
};
public:
SourceInfo(const ident_t *Loc)
: SourceStr(initStr(Loc)), Name(getSubstring(1)),
Filename(removePath(getSubstring(0))), Line(std::stoi(getSubstring(2))),
Column(std::stoi(getSubstring(3))) {}
SourceInfo(const map_var_info_t Name)
: SourceStr(initStr(Name)), Name(getSubstring(0)),
Filename(removePath(getSubstring(1))), Line(std::stoi(getSubstring(2))),
Column(std::stoi(getSubstring(3))) {}
const char *getName() const { return Name.c_str(); }
const char *getFilename() const { return Filename.c_str(); }
const char *getProfileLocation() const { return SourceStr.data(); }
int32_t getLine() const { return Line; }
int32_t getColumn() const { return Column; }
bool isAvailible() const { return (Line || Column); }
};
static inline std::string getNameFromMapping(const map_var_info_t Name) {
if (!Name)
return "unknown";
const std::string NameStr(reinterpret_cast<const char *>(Name));
std::size_t Begin = NameStr.find(';');
std::size_t End = NameStr.find(';', Begin + 1);
return NameStr.substr(Begin + 1, End - Begin - 1);
}
#endif