LLVM  8.0.1
TypeName.h
Go to the documentation of this file.
1 //===- TypeName.h -----------------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_SUPPORT_TYPENAME_H
11 #define LLVM_SUPPORT_TYPENAME_H
12 
13 #include "llvm/ADT/StringRef.h"
14 
15 namespace llvm {
16 
17 /// We provide a function which tries to compute the (demangled) name of a type
18 /// statically.
19 ///
20 /// This routine may fail on some platforms or for particularly unusual types.
21 /// Do not use it for anything other than logging and debugging aids. It isn't
22 /// portable or dependendable in any real sense.
23 ///
24 /// The returned StringRef will point into a static storage duration string.
25 /// However, it may not be null terminated and may be some strangely aligned
26 /// inner substring of a larger string.
27 template <typename DesiredTypeName>
29 #if defined(__clang__) || defined(__GNUC__)
30  StringRef Name = __PRETTY_FUNCTION__;
31 
32  StringRef Key = "DesiredTypeName = ";
33  Name = Name.substr(Name.find(Key));
34  assert(!Name.empty() && "Unable to find the template parameter!");
35  Name = Name.drop_front(Key.size());
36 
37  assert(Name.endswith("]") && "Name doesn't end in the substitution key!");
38  return Name.drop_back(1);
39 #elif defined(_MSC_VER)
40  StringRef Name = __FUNCSIG__;
41 
42  StringRef Key = "getTypeName<";
43  Name = Name.substr(Name.find(Key));
44  assert(!Name.empty() && "Unable to find the function name!");
45  Name = Name.drop_front(Key.size());
46 
47  for (StringRef Prefix : {"class ", "struct ", "union ", "enum "})
48  if (Name.startswith(Prefix)) {
49  Name = Name.drop_front(Prefix.size());
50  break;
51  }
52 
53  auto AnglePos = Name.rfind('>');
54  assert(AnglePos != StringRef::npos && "Unable to find the closing '>'!");
55  return Name.substr(0, AnglePos);
56 #else
57  // No known technique for statically extracting a type name on this compiler.
58  // We return a string that is unlikely to look like any type in LLVM.
59  return "UNKNOWN_TYPE";
60 #endif
61 }
62 
63 }
64 
65 #endif
This class represents lattice values for constants.
Definition: AllocatorList.h:24
LLVM_NODISCARD size_t rfind(char C, size_t From=npos) const
Search for the last character C in the string.
Definition: StringRef.h:360
StringRef getTypeName()
We provide a function which tries to compute the (demangled) name of a type statically.
Definition: TypeName.h:28
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
amdgpu Simplify well known AMD library false Value Value const Twine & Name
Key
PAL metadata keys.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:598
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef drop_front(size_t N=1) const
Return a StringRef equal to &#39;this&#39; but with the first N elements dropped.
Definition: StringRef.h:645
static const size_t npos
Definition: StringRef.h:51
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:298