115 lines · c
1//===-- SharedSourceInfo.h - Target independent OpenMP target RTL - C++ -*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// Methods used to describe source information in target regions10//11//===----------------------------------------------------------------------===//12 13#ifndef OMPTARGET_SHARED_SOURCE_INFO_H14#define OMPTARGET_SHARED_SOURCE_INFO_H15 16#include <cstdint>17#include <string>18 19#ifdef _WIN3220constexpr bool OSWindows = true;21#else22constexpr bool OSWindows = false;23#endif24 25/// Type alias for source location information for variable mappings with26/// data layout ";name;filename;row;col;;\0" from clang.27using map_var_info_t = void *;28 29/// The ident structure that describes a source location from kmp.h. with30/// source location string data as ";filename;function;line;column;;\0".31struct ident_t {32 // Ident_t flags described in kmp.h.33 int32_t reserved_1;34 int32_t flags;35 int32_t reserved_2;36 int32_t reserved_3;37 char const *psource;38};39 40/// Struct to hold source individual location information.41class SourceInfo {42 /// Underlying string copy of the original source information.43 const std::string SourceStr;44 45 /// Location fields extracted from the source information string.46 const std::string Name;47 const std::string Filename;48 const int32_t Line;49 const int32_t Column;50 51 std::string initStr(const void *Name) {52 if (!Name)53 return ";unknown;unknown;0;0;;";54 55 std::string Str = std::string(reinterpret_cast<const char *>(Name));56 if (Str.find(';') == std::string::npos)57 return ";" + Str + ";unknown;0;0;;";58 return Str;59 }60 61 std::string initStr(const ident_t *Loc) {62 if (!Loc)63 return ";unknown;unknown;0;0;;";64 return std::string(reinterpret_cast<const char *>(Loc->psource));65 }66 67 /// Get n-th substring in an expression separated by ;.68 std::string getSubstring(const unsigned N) const {69 std::size_t Begin = SourceStr.find(';');70 std::size_t End = SourceStr.find(';', Begin + 1);71 for (unsigned I = 0; I < N; I++) {72 Begin = End;73 End = SourceStr.find(';', Begin + 1);74 }75 return SourceStr.substr(Begin + 1, End - Begin - 1);76 };77 78 /// Get the filename from a full path.79 std::string removePath(const std::string &Path) const {80 std::size_t Pos = (OSWindows) ? Path.rfind('\\') : Path.rfind('/');81 return Path.substr(Pos + 1);82 };83 84public:85 SourceInfo(const ident_t *Loc)86 : SourceStr(initStr(Loc)), Name(getSubstring(1)),87 Filename(removePath(getSubstring(0))), Line(std::stoi(getSubstring(2))),88 Column(std::stoi(getSubstring(3))) {}89 90 SourceInfo(const map_var_info_t Name)91 : SourceStr(initStr(Name)), Name(getSubstring(0)),92 Filename(removePath(getSubstring(1))), Line(std::stoi(getSubstring(2))),93 Column(std::stoi(getSubstring(3))) {}94 95 const char *getName() const { return Name.c_str(); }96 const char *getFilename() const { return Filename.c_str(); }97 const char *getProfileLocation() const { return SourceStr.data(); }98 int32_t getLine() const { return Line; }99 int32_t getColumn() const { return Column; }100 bool isAvailible() const { return (Line || Column); }101};102 103/// Standalone function for getting the variable name of a mapping.104static inline std::string getNameFromMapping(const map_var_info_t Name) {105 if (!Name)106 return "unknown";107 108 const std::string NameStr(reinterpret_cast<const char *>(Name));109 std::size_t Begin = NameStr.find(';');110 std::size_t End = NameStr.find(';', Begin + 1);111 return NameStr.substr(Begin + 1, End - Begin - 1);112}113 114#endif // OMPTARGET_SHARED_SOURCE_INFO_H115