2020-11-19 04:38:25 +08:00
|
|
|
//===------- SourceInfo.h - Target independent OpenMP target RTL -- C++ -*-===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Methods used to describe source information in target regions
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef _SOURCE_INFO_H_
|
|
|
|
#define _SOURCE_INFO_H_
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2021-01-26 01:07:09 +08:00
|
|
|
constexpr bool OSWindows = true;
|
2020-11-19 04:38:25 +08:00
|
|
|
#else
|
2021-01-26 01:07:09 +08:00
|
|
|
constexpr bool OSWindows = false;
|
2020-11-19 04:38:25 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/// Type alias for source location information for variable mappings with
|
|
|
|
/// data layout ";name;filename;row;col;;\0" from clang.
|
|
|
|
using map_var_info_t = void *;
|
|
|
|
|
2020-11-20 00:56:59 +08:00
|
|
|
/// The ident structure that describes a source location from kmp.h. with
|
|
|
|
/// source location string data as ";filename;function;line;column;;\0".
|
|
|
|
struct ident_t {
|
|
|
|
// Ident_t flags described in kmp.h.
|
|
|
|
int32_t reserved_1;
|
|
|
|
int32_t flags;
|
|
|
|
int32_t reserved_2;
|
|
|
|
int32_t reserved_3;
|
|
|
|
char const *psource;
|
|
|
|
};
|
|
|
|
|
2020-11-19 04:38:25 +08:00
|
|
|
/// Struct to hold source individual location information.
|
|
|
|
class SourceInfo {
|
|
|
|
/// Underlying string copy of the original source information.
|
2021-01-26 01:07:09 +08:00
|
|
|
const std::string SourceStr;
|
2020-11-20 00:56:59 +08:00
|
|
|
|
|
|
|
/// Location fields extracted from the source information string.
|
2021-01-26 01:07:09 +08:00
|
|
|
const std::string Name;
|
|
|
|
const std::string Filename;
|
|
|
|
const int32_t Line;
|
|
|
|
const int32_t Column;
|
2020-11-19 04:38:25 +08:00
|
|
|
|
2021-01-26 01:07:09 +08:00
|
|
|
std::string initStr(const void *Name) {
|
|
|
|
if (!Name)
|
2020-11-19 04:38:25 +08:00
|
|
|
return ";unknown;unknown;0;0;;";
|
2022-04-25 20:28:30 +08:00
|
|
|
|
|
|
|
std::string Str = std::string(reinterpret_cast<const char *>(Name));
|
|
|
|
if (Str.find(';') == std::string::npos)
|
|
|
|
return ";" + Str + ";unknown;0;0;;";
|
|
|
|
return Str;
|
2020-11-19 04:38:25 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 01:07:09 +08:00
|
|
|
std::string initStr(const ident_t *Loc) {
|
|
|
|
if (!Loc)
|
2020-12-19 04:14:44 +08:00
|
|
|
return ";unknown;unknown;0;0;;";
|
2022-04-25 20:28:30 +08:00
|
|
|
return std::string(reinterpret_cast<const char *>(Loc->psource));
|
2020-12-19 04:14:44 +08:00
|
|
|
}
|
|
|
|
|
2020-11-19 04:38:25 +08:00
|
|
|
/// Get n-th substring in an expression separated by ;.
|
2021-01-26 01:07:09 +08:00
|
|
|
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);
|
2020-11-19 04:38:25 +08:00
|
|
|
}
|
2021-01-26 01:07:09 +08:00
|
|
|
return SourceStr.substr(Begin + 1, End - Begin - 1);
|
2020-11-19 04:38:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Get the filename from a full path.
|
2021-01-26 01:07:09 +08:00
|
|
|
std::string removePath(const std::string &Path) const {
|
|
|
|
std::size_t Pos = (OSWindows) ? Path.rfind('\\') : Path.rfind('/');
|
|
|
|
return Path.substr(Pos + 1);
|
2020-11-19 04:38:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2021-01-26 01:07:09 +08:00
|
|
|
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(); }
|
2021-01-23 09:04:44 +08:00
|
|
|
const char *getProfileLocation() const { return SourceStr.data(); }
|
2021-01-26 01:07:09 +08:00
|
|
|
int32_t getLine() const { return Line; }
|
|
|
|
int32_t getColumn() const { return Column; }
|
|
|
|
bool isAvailible() const { return (Line || Column); }
|
2020-11-19 04:38:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Standalone function for getting the variable name of a mapping.
|
2021-01-26 01:07:09 +08:00
|
|
|
static inline std::string getNameFromMapping(const map_var_info_t Name) {
|
|
|
|
if (!Name)
|
2020-11-19 04:38:25 +08:00
|
|
|
return "unknown";
|
|
|
|
|
2021-01-26 01:07:09 +08:00
|
|
|
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);
|
2020-11-19 04:38:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|