Coverage Report

Created: 2018-10-20 12:32

/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
using namespace llvm;
20
using namespace lld;
21
22
// Returns the demangled C++ symbol name for Name.
23
767
Optional<std::string> lld::demangleItanium(StringRef Name) {
24
767
  // itaniumDemangle can be used to demangle strings other than symbol
25
767
  // names which do not necessarily start with "_Z". Name can be
26
767
  // either a C or C++ symbol. Don't call itaniumDemangle if the name
27
767
  // does not look like a C++ symbol name to avoid getting unexpected
28
767
  // result for a C symbol that happens to match a mangled type name.
29
767
  if (!Name.startswith("_Z"))
30
705
    return None;
31
62
32
62
  char *Buf = itaniumDemangle(Name.str().c_str(), nullptr, nullptr, nullptr);
33
62
  if (!Buf)
34
0
    return None;
35
62
  std::string S(Buf);
36
62
  free(Buf);
37
62
  return S;
38
62
}
39
40
102
Optional<std::string> lld::demangleMSVC(StringRef Name) {
41
102
  std::string Prefix;
42
102
  if (Name.consume_front("__imp_"))
43
3
    Prefix = "__declspec(dllimport) ";
44
102
45
102
  // Demangle only C++ names.
46
102
  if (!Name.startswith("?"))
47
95
    return None;
48
7
49
7
  char *Buf = microsoftDemangle(Name.str().c_str(), nullptr, nullptr, nullptr);
50
7
  if (!Buf)
51
1
    return None;
52
6
  std::string S(Buf);
53
6
  free(Buf);
54
6
  return Prefix + S;
55
6
}
56
57
371k
StringMatcher::StringMatcher(ArrayRef<StringRef> Pat) {
58
371k
  for (StringRef S : Pat) {
59
371k
    Expected<GlobPattern> Pat = GlobPattern::create(S);
60
371k
    if (!Pat)
61
1
      error(toString(Pat.takeError()));
62
371k
    else
63
371k
      Patterns.push_back(*Pat);
64
371k
  }
65
371k
}
66
67
436k
bool StringMatcher::match(StringRef S) const {
68
436k
  for (const GlobPattern &Pat : Patterns)
69
291k
    if (Pat.match(S))
70
146k
      return true;
71
436k
  
return false289k
;
72
436k
}
73
74
// Converts a hex string (e.g. "deadbeef") to a vector.
75
3
std::vector<uint8_t> lld::parseHex(StringRef S) {
76
3
  std::vector<uint8_t> Hex;
77
15
  while (!S.empty()) {
78
12
    StringRef B = S.substr(0, 2);
79
12
    S = S.substr(2);
80
12
    uint8_t H;
81
12
    if (!to_integer(B, H, 16)) {
82
0
      error("not a hexadecimal value: " + B);
83
0
      return {};
84
0
    }
85
12
    Hex.push_back(H);
86
12
  }
87
3
  return Hex;
88
3
}
89
90
// Returns true if S is valid as a C language identifier.
91
306k
bool lld::isValidCIdentifier(StringRef S) {
92
306k
  return !S.empty() && 
(306k
isAlpha(S[0])306k
||
S[0] == '_'45.0k
) &&
93
306k
         std::all_of(S.begin() + 1, S.end(),
94
3.40M
                     [](char C) { return C == '_' || 
isAlnum(C)3.40M
; });
95
306k
}
96
97
// Write the contents of the a buffer to a file
98
79
void lld::saveBuffer(StringRef Buffer, const Twine &Path) {
99
79
  std::error_code EC;
100
79
  raw_fd_ostream OS(Path.str(), EC, sys::fs::OpenFlags::F_None);
101
79
  if (EC)
102
0
    error("cannot create " + Path + ": " + EC.message());
103
79
  OS << Buffer;
104
79
}