Coverage Report

Created: 2018-06-25 02:00

/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
261
Optional<std::string> lld::demangleItanium(StringRef Name) {
32
261
  // itaniumDemangle can be used to demangle strings other than symbol
33
261
  // names which do not necessarily start with "_Z". Name can be
34
261
  // either a C or C++ symbol. Don't call itaniumDemangle if the name
35
261
  // does not look like a C++ symbol name to avoid getting unexpected
36
261
  // result for a C symbol that happens to match a mangled type name.
37
261
  if (!Name.startswith("_Z"))
38
205
    return None;
39
56
40
56
  char *Buf = itaniumDemangle(Name.str().c_str(), nullptr, nullptr, nullptr);
41
56
  if (!Buf)
42
0
    return None;
43
56
  std::string S(Buf);
44
56
  free(Buf);
45
56
  return S;
46
56
}
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
232k
StringMatcher::StringMatcher(ArrayRef<StringRef> Pat) {
63
232k
  for (StringRef S : Pat) {
64
232k
    Expected<GlobPattern> Pat = GlobPattern::create(S);
65
232k
    if (!Pat)
66
1
      error(toString(Pat.takeError()));
67
232k
    else
68
232k
      Patterns.push_back(*Pat);
69
232k
  }
70
232k
}
71
72
432k
bool StringMatcher::match(StringRef S) const {
73
432k
  for (const GlobPattern &Pat : Patterns)
74
288k
    if (Pat.match(S))
75
144k
      return true;
76
432k
  
return false287k
;
77
432k
}
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
231k
bool lld::isValidCIdentifier(StringRef S) {
97
231k
  return !S.empty() && 
(231k
isAlpha(S[0])231k
||
S[0] == '_'35.7k
) &&
98
231k
         std::all_of(S.begin() + 1, S.end(),
99
2.55M
                     [](char C) { return C == '_' || 
isAlnum(C)2.55M
; });
100
231k
}
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
}