aboutsummaryrefslogtreecommitdiff
path: root/openmp/libomptarget/include/Shared/SourceInfo.h
blob: 7ce5fd43efc07fe6970a84302582bd5f57aed056 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//===-- SharedSourceInfo.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 OMPTARGET_SHARED_SOURCE_INFO_H
#define OMPTARGET_SHARED_SOURCE_INFO_H

#include <string>

#ifdef _WIN32
constexpr bool OSWindows = true;
#else
constexpr bool OSWindows = false;
#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 *;

/// 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;
};

/// Struct to hold source individual location information.
class SourceInfo {
  /// Underlying string copy of the original source information.
  const std::string SourceStr;

  /// Location fields extracted from the source information string.
  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));
  }

  /// Get n-th substring in an expression separated by ;.
  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);
  };

  /// Get the filename from a full path.
  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); }
};

/// Standalone function for getting the variable name of a mapping.
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 // OMPTARGET_SHARED_SOURCE_INFO_H