Coverage Report

Created: 2019-07-24 05:18

/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
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "lld/Common/Strings.h"
10
#include "lld/Common/ErrorHandler.h"
11
#include "lld/Common/LLVM.h"
12
#include "llvm/Demangle/Demangle.h"
13
#include "llvm/Support/GlobPattern.h"
14
#include <algorithm>
15
#include <mutex>
16
#include <vector>
17
18
using namespace llvm;
19
using namespace lld;
20
21
// Returns the demangled C++ symbol name for Name.
22
914
Optional<std::string> lld::demangleItanium(StringRef name) {
23
914
  // itaniumDemangle can be used to demangle strings other than symbol
24
914
  // names which do not necessarily start with "_Z". Name can be
25
914
  // either a C or C++ symbol. Don't call itaniumDemangle if the name
26
914
  // does not look like a C++ symbol name to avoid getting unexpected
27
914
  // result for a C symbol that happens to match a mangled type name.
28
914
  if (!name.startswith("_Z"))
29
849
    return None;
30
65
31
65
  char *buf = itaniumDemangle(name.str().c_str(), nullptr, nullptr, nullptr);
32
65
  if (!buf)
33
0
    return None;
34
65
  std::string s(buf);
35
65
  free(buf);
36
65
  return s;
37
65
}
38
39
342
Optional<std::string> lld::demangleMSVC(StringRef name) {
40
342
  std::string prefix;
41
342
  if (name.consume_front("__imp_"))
42
21
    prefix = "__declspec(dllimport) ";
43
342
44
342
  // Demangle only C++ names.
45
342
  if (!name.startswith("?"))
46
314
    return None;
47
28
48
28
  char *buf = microsoftDemangle(name.str().c_str(), nullptr, nullptr, nullptr);
49
28
  if (!buf)
50
1
    return None;
51
27
  std::string s(buf);
52
27
  free(buf);
53
27
  return prefix + s;
54
27
}
55
56
379k
StringMatcher::StringMatcher(ArrayRef<StringRef> pat) {
57
379k
  for (StringRef s : pat) {
58
379k
    Expected<GlobPattern> pat = GlobPattern::create(s);
59
379k
    if (!pat)
60
1
      error(toString(pat.takeError()));
61
379k
    else
62
379k
      patterns.push_back(*pat);
63
379k
  }
64
379k
}
65
66
440k
bool StringMatcher::match(StringRef s) const {
67
440k
  for (const GlobPattern &pat : patterns)
68
294k
    if (pat.match(s))
69
147k
      return true;
70
440k
  
return false292k
;
71
440k
}
72
73
// Converts a hex string (e.g. "deadbeef") to a vector.
74
3
std::vector<uint8_t> lld::parseHex(StringRef s) {
75
3
  std::vector<uint8_t> hex;
76
15
  while (!s.empty()) {
77
12
    StringRef b = s.substr(0, 2);
78
12
    s = s.substr(2);
79
12
    uint8_t h;
80
12
    if (!to_integer(b, h, 16)) {
81
0
      error("not a hexadecimal value: " + b);
82
0
      return {};
83
0
    }
84
12
    hex.push_back(h);
85
12
  }
86
3
  return hex;
87
3
}
88
89
// Returns true if S is valid as a C language identifier.
90
314k
bool lld::isValidCIdentifier(StringRef s) {
91
314k
  return !s.empty() && 
(314k
isAlpha(s[0])314k
||
s[0] == '_'53.0k
) &&
92
314k
         std::all_of(s.begin() + 1, s.end(),
93
3.40M
                     [](char c) { return c == '_' || 
isAlnum(c)3.40M
; });
94
314k
}
95
96
// Write the contents of the a buffer to a file
97
90
void lld::saveBuffer(StringRef buffer, const Twine &path) {
98
90
  std::error_code ec;
99
90
  raw_fd_ostream os(path.str(), ec, sys::fs::OpenFlags::F_None);
100
90
  if (ec)
101
0
    error("cannot create " + path + ": " + ec.message());
102
90
  os << buffer;
103
90
}