Coverage Report

Created: 2019-05-19 14:56

/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
832
Optional<std::string> lld::demangleItanium(StringRef Name) {
23
832
  // itaniumDemangle can be used to demangle strings other than symbol
24
832
  // names which do not necessarily start with "_Z". Name can be
25
832
  // either a C or C++ symbol. Don't call itaniumDemangle if the name
26
832
  // does not look like a C++ symbol name to avoid getting unexpected
27
832
  // result for a C symbol that happens to match a mangled type name.
28
832
  if (!Name.startswith("_Z"))
29
766
    return None;
30
66
31
66
  char *Buf = itaniumDemangle(Name.str().c_str(), nullptr, nullptr, nullptr);
32
66
  if (!Buf)
33
0
    return None;
34
66
  std::string S(Buf);
35
66
  free(Buf);
36
66
  return S;
37
66
}
38
39
161
Optional<std::string> lld::demangleMSVC(StringRef Name) {
40
161
  std::string Prefix;
41
161
  if (Name.consume_front("__imp_"))
42
4
    Prefix = "__declspec(dllimport) ";
43
161
44
161
  // Demangle only C++ names.
45
161
  if (!Name.startswith("?"))
46
142
    return None;
47
19
48
19
  char *Buf = microsoftDemangle(Name.str().c_str(), nullptr, nullptr, nullptr);
49
19
  if (!Buf)
50
1
    return None;
51
18
  std::string S(Buf);
52
18
  free(Buf);
53
18
  return Prefix + S;
54
18
}
55
56
375k
StringMatcher::StringMatcher(ArrayRef<StringRef> Pat) {
57
375k
  for (StringRef S : Pat) {
58
375k
    Expected<GlobPattern> Pat = GlobPattern::create(S);
59
375k
    if (!Pat)
60
1
      error(toString(Pat.takeError()));
61
375k
    else
62
375k
      Patterns.push_back(*Pat);
63
375k
  }
64
375k
}
65
66
440k
bool StringMatcher::match(StringRef S) const {
67
440k
  for (const GlobPattern &Pat : Patterns)
68
293k
    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
310k
bool lld::isValidCIdentifier(StringRef S) {
91
310k
  return !S.empty() && 
(310k
isAlpha(S[0])310k
||
S[0] == '_'48.6k
) &&
92
310k
         std::all_of(S.begin() + 1, S.end(),
93
3.40M
                     [](char C) { return C == '_' || 
isAlnum(C)3.40M
; });
94
310k
}
95
96
// Write the contents of the a buffer to a file
97
86
void lld::saveBuffer(StringRef Buffer, const Twine &Path) {
98
86
  std::error_code EC;
99
86
  raw_fd_ostream OS(Path.str(), EC, sys::fs::OpenFlags::F_None);
100
86
  if (EC)
101
0
    error("cannot create " + Path + ": " + EC.message());
102
86
  OS << Buffer;
103
86
}