1 //===- TypeName.h -----------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_SUPPORT_TYPENAME_H
11 #define LLVM_SUPPORT_TYPENAME_H
13 #include "llvm/ADT/StringRef.h"
17 /// We provide a function which tries to compute the (demangled) name of a type
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.
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>
28 inline StringRef getTypeName() {
29 #if defined(__clang__) || defined(__GNUC__)
30 StringRef Name = __PRETTY_FUNCTION__;
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());
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__;
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());
47 for (StringRef Prefix : {"class ", "struct ", "union ", "enum "})
48 if (Name.startswith(Prefix)) {
49 Name = Name.drop_front(Prefix.size());
53 auto AnglePos = Name.rfind('>');
54 assert(AnglePos != StringRef::npos && "Unable to find the closing '>'!");
55 return Name.substr(0, AnglePos);
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";