Coverage Report

Created: 2017-09-21 03:39

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/lld/ELF/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 "Strings.h"
11
#include "Config.h"
12
#include "Error.h"
13
#include "llvm/ADT/ArrayRef.h"
14
#include "llvm/ADT/StringRef.h"
15
#include "llvm/ADT/Twine.h"
16
#include "llvm/Demangle/Demangle.h"
17
#include <algorithm>
18
#include <cstring>
19
20
using namespace llvm;
21
using namespace lld;
22
using namespace lld::elf;
23
24
222k
StringMatcher::StringMatcher(ArrayRef<StringRef> Pat) {
25
222k
  for (StringRef S : Pat) {
26
222k
    Expected<GlobPattern> Pat = GlobPattern::create(S);
27
222k
    if (!Pat)
28
1
      error(toString(Pat.takeError()));
29
222k
    else
30
222k
      Patterns.push_back(*Pat);
31
222k
  }
32
222k
}
33
34
414k
bool StringMatcher::match(StringRef S) const {
35
414k
  for (const GlobPattern &Pat : Patterns)
36
276k
    
if (276k
Pat.match(S)276k
)
37
138k
      return true;
38
275k
  return false;
39
275k
}
40
41
// Converts a hex string (e.g. "deadbeef") to a vector.
42
1
std::vector<uint8_t> elf::parseHex(StringRef S) {
43
1
  std::vector<uint8_t> Hex;
44
5
  while (
!S.empty()5
) {
45
4
    StringRef B = S.substr(0, 2);
46
4
    S = S.substr(2);
47
4
    uint8_t H;
48
4
    if (
!to_integer(B, H, 16)4
) {
49
0
      error("not a hexadecimal value: " + B);
50
0
      return {};
51
0
    }
52
4
    Hex.push_back(H);
53
4
  }
54
1
  return Hex;
55
1
}
56
57
2.77M
static bool isAlpha(char C) {
58
2.77M
  return ('a' <= C && 
C <= 'z'2.74M
) ||
('A' <= C && 25.7k
C <= 'Z'78
) ||
C == '_'25.6k
;
59
2.77M
}
60
61
// Returns true if C is a valid letter, digit or underscore as defined in the
62
// "C" locale.
63
2.55M
bool elf::isAlnum(char C) 
{ return isAlpha(C) || 2.55M
('0' <= C && 87
C <= '9'17
); }
64
65
// Returns true if S is valid as a C language identifier.
66
221k
bool elf::isValidCIdentifier(StringRef S) {
67
221k
  return !S.empty() && isAlpha(S[0]) &&
68
195k
         std::all_of(S.begin() + 1, S.end(), isAlnum);
69
221k
}
70
71
// Returns the demangled C++ symbol name for Name.
72
179
Optional<std::string> elf::demangle(StringRef Name) {
73
179
  // itaniumDemangle can be used to demangle strings other than symbol
74
179
  // names which do not necessarily start with "_Z". Name can be
75
179
  // either a C or C++ symbol. Don't call itaniumDemangle if the name
76
179
  // does not look like a C++ symbol name to avoid getting unexpected
77
179
  // result for a C symbol that happens to match a mangled type name.
78
179
  if (!Name.startswith("_Z"))
79
127
    return None;
80
52
81
52
  char *Buf = itaniumDemangle(Name.str().c_str(), nullptr, nullptr, nullptr);
82
52
  if (!Buf)
83
0
    return None;
84
52
  std::string S(Buf);
85
52
  free(Buf);
86
52
  return S;
87
52
}