1 //===------- SourceInfo.h - Target independent OpenMP target RTL -- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // Methods used to describe source information in target regions
11 //===----------------------------------------------------------------------===//
13 #ifndef _SOURCE_INFO_H_
14 #define _SOURCE_INFO_H_
19 constexpr bool OSWindows
= true;
21 constexpr bool OSWindows
= false;
24 /// Type alias for source location information for variable mappings with
25 /// data layout ";name;filename;row;col;;\0" from clang.
26 using map_var_info_t
= void *;
28 /// The ident structure that describes a source location from kmp.h. with
29 /// source location string data as ";filename;function;line;column;;\0".
31 // Ident_t flags described in kmp.h.
39 /// Struct to hold source individual location information.
41 /// Underlying string copy of the original source information.
42 const std::string SourceStr
;
44 /// Location fields extracted from the source information string.
45 const std::string Name
;
46 const std::string Filename
;
50 std::string
initStr(const void *Name
) {
52 return ";unknown;unknown;0;0;;";
54 std::string Str
= std::string(reinterpret_cast<const char *>(Name
));
55 if (Str
.find(';') == std::string::npos
)
56 return ";" + Str
+ ";unknown;0;0;;";
60 std::string
initStr(const ident_t
*Loc
) {
62 return ";unknown;unknown;0;0;;";
63 return std::string(reinterpret_cast<const char *>(Loc
->psource
));
66 /// Get n-th substring in an expression separated by ;.
67 std::string
getSubstring(const unsigned N
) const {
68 std::size_t Begin
= SourceStr
.find(';');
69 std::size_t End
= SourceStr
.find(';', Begin
+ 1);
70 for (unsigned I
= 0; I
< N
; I
++) {
72 End
= SourceStr
.find(';', Begin
+ 1);
74 return SourceStr
.substr(Begin
+ 1, End
- Begin
- 1);
77 /// Get the filename from a full path.
78 std::string
removePath(const std::string
&Path
) const {
79 std::size_t Pos
= (OSWindows
) ? Path
.rfind('\\') : Path
.rfind('/');
80 return Path
.substr(Pos
+ 1);
84 SourceInfo(const ident_t
*Loc
)
85 : SourceStr(initStr(Loc
)), Name(getSubstring(1)),
86 Filename(removePath(getSubstring(0))), Line(std::stoi(getSubstring(2))),
87 Column(std::stoi(getSubstring(3))) {}
89 SourceInfo(const map_var_info_t Name
)
90 : SourceStr(initStr(Name
)), Name(getSubstring(0)),
91 Filename(removePath(getSubstring(1))), Line(std::stoi(getSubstring(2))),
92 Column(std::stoi(getSubstring(3))) {}
94 const char *getName() const { return Name
.c_str(); }
95 const char *getFilename() const { return Filename
.c_str(); }
96 const char *getProfileLocation() const { return SourceStr
.data(); }
97 int32_t getLine() const { return Line
; }
98 int32_t getColumn() const { return Column
; }
99 bool isAvailible() const { return (Line
|| Column
); }
102 /// Standalone function for getting the variable name of a mapping.
103 static inline std::string
getNameFromMapping(const map_var_info_t Name
) {
107 const std::string
NameStr(reinterpret_cast<const char *>(Name
));
108 std::size_t Begin
= NameStr
.find(';');
109 std::size_t End
= NameStr
.find(';', Begin
+ 1);
110 return NameStr
.substr(Begin
+ 1, End
- Begin
- 1);