Rui Ueyama | 53fe469 | 2017-11-28 02:15:26 +0000 | [diff] [blame] | 1 | //===- Strings.cpp -------------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Rui Ueyama | 53fe469 | 2017-11-28 02:15:26 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "lld/Common/Strings.h" |
Rui Ueyama | ee17371 | 2018-02-28 17:38:19 +0000 | [diff] [blame] | 10 | #include "lld/Common/ErrorHandler.h" |
| 11 | #include "lld/Common/LLVM.h" |
Rui Ueyama | 53fe469 | 2017-11-28 02:15:26 +0000 | [diff] [blame] | 12 | #include "llvm/Demangle/Demangle.h" |
Rui Ueyama | ee17371 | 2018-02-28 17:38:19 +0000 | [diff] [blame] | 13 | #include "llvm/Support/GlobPattern.h" |
| 14 | #include <algorithm> |
| 15 | #include <mutex> |
| 16 | #include <vector> |
| 17 | |
Rui Ueyama | 53fe469 | 2017-11-28 02:15:26 +0000 | [diff] [blame] | 18 | using namespace llvm; |
| 19 | using namespace lld; |
| 20 | |
Martin Storsjo | 5ebab1f | 2019-09-27 12:24:18 +0000 | [diff] [blame] | 21 | // Returns the demangled C++ symbol name for name. |
| 22 | std::string lld::demangleItanium(StringRef name) { |
Rui Ueyama | 53fe469 | 2017-11-28 02:15:26 +0000 | [diff] [blame] | 23 | // itaniumDemangle can be used to demangle strings other than symbol |
| 24 | // names which do not necessarily start with "_Z". Name can be |
Martin Storsjo | dd71b2d | 2019-09-27 12:24:03 +0000 | [diff] [blame] | 25 | // either a C or C++ symbol. Don't call demangle if the name |
Rui Ueyama | 53fe469 | 2017-11-28 02:15:26 +0000 | [diff] [blame] | 26 | // 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 Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 28 | if (!name.startswith("_Z")) |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 29 | return std::string(name); |
Rui Ueyama | 53fe469 | 2017-11-28 02:15:26 +0000 | [diff] [blame] | 30 | |
Benjamin Kramer | adcd026 | 2020-01-28 20:23:46 +0100 | [diff] [blame] | 31 | return demangle(std::string(name)); |
Rui Ueyama | 53fe469 | 2017-11-28 02:15:26 +0000 | [diff] [blame] | 32 | } |
Rui Ueyama | ee17371 | 2018-02-28 17:38:19 +0000 | [diff] [blame] | 33 | |
Thomas Preud'homme | c42fe24 | 2020-01-10 16:56:07 +0000 | [diff] [blame] | 34 | SingleStringMatcher::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 Ueyama | ee17371 | 2018-02-28 17:38:19 +0000 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | |
Thomas Preud'homme | c42fe24 | 2020-01-10 16:56:07 +0000 | [diff] [blame] | 50 | bool SingleStringMatcher::match(StringRef s) const { |
| 51 | return ExactMatch ? (ExactPattern == s) : GlobPatternMatcher.match(s); |
| 52 | } |
| 53 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 54 | bool StringMatcher::match(StringRef s) const { |
Thomas Preud'homme | c42fe24 | 2020-01-10 16:56:07 +0000 | [diff] [blame] | 55 | for (const SingleStringMatcher &pat : patterns) |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 56 | if (pat.match(s)) |
Rui Ueyama | ee17371 | 2018-02-28 17:38:19 +0000 | [diff] [blame] | 57 | return true; |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | // Converts a hex string (e.g. "deadbeef") to a vector. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 62 | std::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 Ueyama | ee17371 | 2018-02-28 17:38:19 +0000 | [diff] [blame] | 70 | return {}; |
| 71 | } |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 72 | hex.push_back(h); |
Rui Ueyama | ee17371 | 2018-02-28 17:38:19 +0000 | [diff] [blame] | 73 | } |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 74 | return hex; |
Rui Ueyama | ee17371 | 2018-02-28 17:38:19 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | // Returns true if S is valid as a C language identifier. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 78 | bool 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 Ueyama | ee17371 | 2018-02-28 17:38:19 +0000 | [diff] [blame] | 82 | } |
Sam Clegg | 3ad27e9 | 2018-05-22 20:20:25 +0000 | [diff] [blame] | 83 | |
| 84 | // Write the contents of the a buffer to a file |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 85 | void lld::saveBuffer(StringRef buffer, const Twine &path) { |
| 86 | std::error_code ec; |
Fangrui Song | d9b948b | 2019-08-05 05:43:48 +0000 | [diff] [blame] | 87 | raw_fd_ostream os(path.str(), ec, sys::fs::OpenFlags::OF_None); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 88 | if (ec) |
| 89 | error("cannot create " + path + ": " + ec.message()); |
| 90 | os << buffer; |
Sam Clegg | 3ad27e9 | 2018-05-22 20:20:25 +0000 | [diff] [blame] | 91 | } |