]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Host/common/ThisThread.cpp
Merge ^/head r314178 through r314269.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Host / common / ThisThread.cpp
1 //===-- ThisThread.cpp ------------------------------------------*- 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 #include "lldb/Host/ThisThread.h"
11 #include "lldb/Core/Error.h"
12 #include "lldb/Host/HostInfo.h"
13
14 #include "llvm/ADT/STLExtras.h"
15
16 #include <algorithm>
17
18 using namespace lldb;
19 using namespace lldb_private;
20
21 void ThisThread::SetName(llvm::StringRef name, int max_length) {
22   std::string truncated_name(name.data());
23
24   // Thread names are coming in like '<lldb.comm.debugger.edit>' and
25   // '<lldb.comm.debugger.editline>'.  So just chopping the end of the string
26   // off leads to a lot of similar named threads.  Go through the thread name
27   // and search for the last dot and use that.
28
29   if (max_length > 0 &&
30       truncated_name.length() > static_cast<size_t>(max_length)) {
31     // First see if we can get lucky by removing any initial or final braces.
32     std::string::size_type begin = truncated_name.find_first_not_of("(<");
33     std::string::size_type end = truncated_name.find_last_not_of(")>.");
34     if (end - begin > static_cast<size_t>(max_length)) {
35       // We're still too long.  Since this is a dotted component, use everything
36       // after the last
37       // dot, up to a maximum of |length| characters.
38       std::string::size_type last_dot = truncated_name.rfind('.');
39       if (last_dot != std::string::npos)
40         begin = last_dot + 1;
41
42       end = std::min(end, begin + max_length);
43     }
44
45     std::string::size_type count = end - begin + 1;
46     truncated_name = truncated_name.substr(begin, count);
47   }
48
49   SetName(truncated_name);
50 }