Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_TypeNameTraits.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
11 #include "Teuchos_Assert.hpp"
12 
13 // Define this if you want to force name demangling if supported
14 //#define HAVE_TEUCHOS_DEMANGLE
15 
16 #if defined(HAVE_GCC_ABI_DEMANGLE) && defined(HAVE_TEUCHOS_DEMANGLE)
17 # include <cxxabi.h>
18 #endif
19 
20 
21 std::string Teuchos::demangleName( const std::string &mangledName )
22 {
23 #if defined(HAVE_GCC_ABI_DEMANGLE) && defined(HAVE_TEUCHOS_DEMANGLE)
24  int status;
25  char* _demangledName = abi::__cxa_demangle (mangledName.c_str (), 0, 0, &status);
26  if (status != 0 || 0 == _demangledName) {
27 #ifdef TEUCHOS_DEBUG
28  // In a debug build, we check if demangling succeeded.
29  std::string nullstr ("NULL");
30  const char* demangle_output = _demangledName ? _demangledName : nullstr.c_str ();
31  using std::endl;
32 #endif // TEUCHOS_DEBUG
33  if (_demangledName != NULL) {
34  // The C library standard requires that free() do the right
35  // thing with NULL input, but it doesn't hurt to check.
36  free (_demangledName);
37  }
38 #ifdef TEUCHOS_DEBUG
40  true, std::logic_error,
41  "Error, name demangling with g++ has been enabled but the function "
42  "abi::__cxa_demangle(" << mangledName << ") returned returnVal = " <<
43  demangle_output <<" and status = " << status << "." << endl << "This "
44  "suggests that name demangling may be broken on this platform." << endl
45  << "You may prevent this exception from being thrown in the future by "
46  "turning off name demangling for this build at configure time." << endl
47  << "Do this by setting the CMake configuration option "
48  "Teuchos_ENABLE_GCC_DEMANGLE to OFF." << endl << "Add the following to "
49  "your list of CMake options:" << endl << endl <<
50  " -D Teuchos_ENABLE_GCC_DEMANGLE:BOOL=OFF" << endl);
51 #else // NOT TEUCHOS_DEBUG
52  return (mangledName + "<demangle-failed>");
53 #endif // TEUCHOS_DEBUG
54  }
55  const std::string demangledName (_demangledName);
56  free (_demangledName); // We have to free this before we return!
57  return demangledName;
58 #else
59  return mangledName;
60 #endif
61 }
TEUCHOSCORE_LIB_DLL_EXPORT std::string demangleName(const std::string &mangledName)
Demangle a C++ name if valid.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
Defines basic traits returning the name of a type in a portable and readable way. ...