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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Methods used to describe source information in target regions
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef OMPTARGET_SHARED_SOURCE_INFO_H
14 #define OMPTARGET_SHARED_SOURCE_INFO_H
15
16 #include <cstdint>
17 #include <string>
18
19 #ifdef _WIN32
20 constexpr bool OSWindows = true;
21 #else
22 constexpr bool OSWindows = false;
23 #endif
24
25 /// Type alias for source location information for variable mappings with
26 /// data layout ";name;filename;row;col;;\0" from clang.
27 using map_var_info_t = void *;
28
29 /// The ident structure that describes a source location from kmp.h. with
30 /// source location string data as ";filename;function;line;column;;\0".
31 struct 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.
41 class 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
initStr(const void * Name)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
initStr(const ident_t * Loc)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 ;.
getSubstring(const unsigned N)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.
removePath(const std::string & 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
84 public:
SourceInfo(const ident_t * Loc)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
SourceInfo(const map_var_info_t Name)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
getName()95 const char *getName() const { return Name.c_str(); }
getFilename()96 const char *getFilename() const { return Filename.c_str(); }
getProfileLocation()97 const char *getProfileLocation() const { return SourceStr.data(); }
getLine()98 int32_t getLine() const { return Line; }
getColumn()99 int32_t getColumn() const { return Column; }
isAvailible()100 bool isAvailible() const { return (Line || Column); }
101 };
102
103 /// Standalone function for getting the variable name of a mapping.
getNameFromMapping(const map_var_info_t Name)104 static 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_H
115