Coverage Report

Created: 2018-08-19 21:11

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/Common/Strings.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- Strings.cpp -------------------------------------------------------===//
2
//
3
//                             The LLVM Linker
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 "lld/Common/Strings.h"
11
#include "lld/Common/ErrorHandler.h"
12
#include "lld/Common/LLVM.h"
13
#include "llvm/Demangle/Demangle.h"
14
#include "llvm/Support/GlobPattern.h"
15
#include <algorithm>
16
#include <mutex>
17
#include <vector>
18
19
#if defined(_MSC_VER)
20
#include <Windows.h>
21
22
// DbgHelp.h must be included after Windows.h.
23
#include <DbgHelp.h>
24
#pragma comment(lib, "dbghelp.lib")
25
#endif
26
27
using namespace llvm;
28
using namespace lld;
29
30
// Returns the demangled C++ symbol name for Name.
31
267
Optional<std::string> lld::demangleItanium(StringRef Name) {
32
267
  // itaniumDemangle can be used to demangle strings other than symbol
33
267
  // names which do not necessarily start with "_Z". Name can be
34
267
  // either a C or C++ symbol. Don't call itaniumDemangle if the name
35
267
  // does not look like a C++ symbol name to avoid getting unexpected
36
267
  // result for a C symbol that happens to match a mangled type name.
37
267
  if (!Name.startswith("_Z"))
38
212
    return None;
39
55
40
55
  char *Buf = itaniumDemangle(Name.str().c_str(), nullptr, nullptr, nullptr);
41
55
  if (!Buf)
42
0
    return None;
43
55
  std::string S(Buf);
44
55
  free(Buf);
45
55
  return S;
46
55
}
47
48
33
Optional<std::string> lld::demangleMSVC(StringRef S) {
49
#if defined(_MSC_VER)
50
  // UnDecorateSymbolName is not thread-safe, so we need a mutex.
51
  static std::mutex Mu;
52
  std::lock_guard<std::mutex> Lock(Mu);
53
54
  char Buf[4096];
55
  if (S.startswith("?"))
56
    if (size_t Len = UnDecorateSymbolName(S.str().c_str(), Buf, sizeof(Buf), 0))
57
      return std::string(Buf, Len);
58
#endif
59
  return None;
60
33
}
61
62
369k
StringMatcher::StringMatcher(ArrayRef<StringRef> Pat) {
63
369k
  for (StringRef S : Pat) {
64
369k
    Expected<GlobPattern> Pat = GlobPattern::create(S);
65
369k
    if (!Pat)
66
1
      error(toString(Pat.takeError()));
67
369k
    else
68
369k
      Patterns.push_back(*Pat);
69
369k
  }
70
369k
}
71
72
435k
bool StringMatcher::match(StringRef S) const {
73
435k
  for (const GlobPattern &Pat : Patterns)
74
290k
    if (Pat.match(S))
75
146k
      return true;
76
435k
  
return false289k
;
77
435k
}
78
79
// Converts a hex string (e.g. "deadbeef") to a vector.
80
3
std::vector<uint8_t> lld::parseHex(StringRef S) {
81
3
  std::vector<uint8_t> Hex;
82
15
  while (!S.empty()) {
83
12
    StringRef B = S.substr(0, 2);
84
12
    S = S.substr(2);
85
12
    uint8_t H;
86
12
    if (!to_integer(B, H, 16)) {
87
0
      error("not a hexadecimal value: " + B);
88
0
      return {};
89
0
    }
90
12
    Hex.push_back(H);
91
12
  }
92
3
  return Hex;
93
3
}
94
95
// Returns true if S is valid as a C language identifier.
96
305k
bool lld::isValidCIdentifier(StringRef S) {
97
305k
  return !S.empty() && 
(305k
isAlpha(S[0])305k
||
S[0] == '_'43.6k
) &&
98
305k
         std::all_of(S.begin() + 1, S.end(),
99
3.40M
                     [](char C) { return C == '_' || 
isAlnum(C)3.40M
; });
100
305k
}
101
102
// Write the contents of the a buffer to a file
103
65
void lld::saveBuffer(StringRef Buffer, const Twine &Path) {
104
65
  std::error_code EC;
105
65
  raw_fd_ostream OS(Path.str(), EC, sys::fs::OpenFlags::F_None);
106
65
  if (EC)
107
0
    error("cannot create " + Path + ": " + EC.message());
108
65
  OS << Buffer;
109
65
}