blob: 605d9de685edad602008aa52ea5c0e3a3e265406 [file] [log] [blame]
Rui Ueyama53fe4692017-11-28 02:15:26 +00001//===- Strings.cpp -------------------------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Rui Ueyama53fe4692017-11-28 02:15:26 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "lld/Common/Strings.h"
Rui Ueyamaee173712018-02-28 17:38:19 +000010#include "lld/Common/ErrorHandler.h"
11#include "lld/Common/LLVM.h"
Rui Ueyama53fe4692017-11-28 02:15:26 +000012#include "llvm/Demangle/Demangle.h"
Rui Ueyamaee173712018-02-28 17:38:19 +000013#include "llvm/Support/GlobPattern.h"
14#include <algorithm>
15#include <mutex>
16#include <vector>
17
Rui Ueyama53fe4692017-11-28 02:15:26 +000018using namespace llvm;
19using namespace lld;
20
Martin Storsjo5ebab1f2019-09-27 12:24:18 +000021// Returns the demangled C++ symbol name for name.
22std::string lld::demangleItanium(StringRef name) {
Rui Ueyama53fe4692017-11-28 02:15:26 +000023 // itaniumDemangle can be used to demangle strings other than symbol
24 // names which do not necessarily start with "_Z". Name can be
Martin Storsjodd71b2d2019-09-27 12:24:03 +000025 // either a C or C++ symbol. Don't call demangle if the name
Rui Ueyama53fe4692017-11-28 02:15:26 +000026 // does not look like a C++ symbol name to avoid getting unexpected
27 // result for a C symbol that happens to match a mangled type name.
Rui Ueyama136d27a2019-07-11 05:40:30 +000028 if (!name.startswith("_Z"))
Benjamin Krameradcd0262020-01-28 20:23:46 +010029 return std::string(name);
Rui Ueyama53fe4692017-11-28 02:15:26 +000030
Benjamin Krameradcd0262020-01-28 20:23:46 +010031 return demangle(std::string(name));
Rui Ueyama53fe4692017-11-28 02:15:26 +000032}
Rui Ueyamaee173712018-02-28 17:38:19 +000033
Thomas Preud'hommec42fe242020-01-10 16:56:07 +000034SingleStringMatcher::SingleStringMatcher(StringRef Pattern) {
35 if (Pattern.size() > 2 && Pattern.startswith("\"") &&
36 Pattern.endswith("\"")) {
37 ExactMatch = true;
38 ExactPattern = Pattern.substr(1, Pattern.size() - 2);
39 } else {
40 Expected<GlobPattern> Glob = GlobPattern::create(Pattern);
41 if (!Glob) {
42 error(toString(Glob.takeError()));
43 return;
44 }
45 ExactMatch = false;
46 GlobPatternMatcher = *Glob;
Rui Ueyamaee173712018-02-28 17:38:19 +000047 }
48}
49
Thomas Preud'hommec42fe242020-01-10 16:56:07 +000050bool SingleStringMatcher::match(StringRef s) const {
51 return ExactMatch ? (ExactPattern == s) : GlobPatternMatcher.match(s);
52}
53
Rui Ueyama136d27a2019-07-11 05:40:30 +000054bool StringMatcher::match(StringRef s) const {
Thomas Preud'hommec42fe242020-01-10 16:56:07 +000055 for (const SingleStringMatcher &pat : patterns)
Rui Ueyama136d27a2019-07-11 05:40:30 +000056 if (pat.match(s))
Rui Ueyamaee173712018-02-28 17:38:19 +000057 return true;
58 return false;
59}
60
61// Converts a hex string (e.g. "deadbeef") to a vector.
Rui Ueyama136d27a2019-07-11 05:40:30 +000062std::vector<uint8_t> lld::parseHex(StringRef s) {
63 std::vector<uint8_t> hex;
64 while (!s.empty()) {
65 StringRef b = s.substr(0, 2);
66 s = s.substr(2);
67 uint8_t h;
68 if (!to_integer(b, h, 16)) {
69 error("not a hexadecimal value: " + b);
Rui Ueyamaee173712018-02-28 17:38:19 +000070 return {};
71 }
Rui Ueyama136d27a2019-07-11 05:40:30 +000072 hex.push_back(h);
Rui Ueyamaee173712018-02-28 17:38:19 +000073 }
Rui Ueyama136d27a2019-07-11 05:40:30 +000074 return hex;
Rui Ueyamaee173712018-02-28 17:38:19 +000075}
76
77// Returns true if S is valid as a C language identifier.
Rui Ueyama136d27a2019-07-11 05:40:30 +000078bool lld::isValidCIdentifier(StringRef s) {
79 return !s.empty() && (isAlpha(s[0]) || s[0] == '_') &&
80 std::all_of(s.begin() + 1, s.end(),
81 [](char c) { return c == '_' || isAlnum(c); });
Rui Ueyamaee173712018-02-28 17:38:19 +000082}
Sam Clegg3ad27e92018-05-22 20:20:25 +000083
84// Write the contents of the a buffer to a file
Rui Ueyama136d27a2019-07-11 05:40:30 +000085void lld::saveBuffer(StringRef buffer, const Twine &path) {
86 std::error_code ec;
Fangrui Songd9b948b2019-08-05 05:43:48 +000087 raw_fd_ostream os(path.str(), ec, sys::fs::OpenFlags::OF_None);
Rui Ueyama136d27a2019-07-11 05:40:30 +000088 if (ec)
89 error("cannot create " + path + ": " + ec.message());
90 os << buffer;
Sam Clegg3ad27e92018-05-22 20:20:25 +000091}