mirror of
https://github.com/Gericom/teak-llvm.git
synced 2025-06-19 03:25:54 -04:00

Summary: Linker scripts allow filenames to be put in double quotes to prevent characters in filenames that are part of the linker script syntax from having their special meaning. Case in point the * wildcard character. Availability of double quoting filenames also allows to fix a failure in ELF/linkerscript/filename-spec.s when the path contain a @ which the lexer consider as a special characters and thus break up a filename containing it. This may happens under Jenkins which createspath such as pipeline@2. To avoid the need for escaping GlobPattern metacharacters in filename in double quotes, GlobPattern::create is augmented with a new parameter to request literal matching instead of relying on the presence of a wildcard character in the pattern. Reviewers: jhenderson, MaskRay, evgeny777, espindola, alexshap Reviewed By: MaskRay Subscribers: peter.smith, grimar, ruiu, emaste, arichardson, hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D72517
92 lines
2.8 KiB
C++
92 lines
2.8 KiB
C++
//===- Strings.cpp -------------------------------------------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "lld/Common/Strings.h"
|
|
#include "lld/Common/ErrorHandler.h"
|
|
#include "lld/Common/LLVM.h"
|
|
#include "llvm/Demangle/Demangle.h"
|
|
#include "llvm/Support/GlobPattern.h"
|
|
#include <algorithm>
|
|
#include <mutex>
|
|
#include <vector>
|
|
|
|
using namespace llvm;
|
|
using namespace lld;
|
|
|
|
// Returns the demangled C++ symbol name for name.
|
|
std::string lld::demangleItanium(StringRef name) {
|
|
// itaniumDemangle can be used to demangle strings other than symbol
|
|
// names which do not necessarily start with "_Z". Name can be
|
|
// either a C or C++ symbol. Don't call demangle if the name
|
|
// does not look like a C++ symbol name to avoid getting unexpected
|
|
// result for a C symbol that happens to match a mangled type name.
|
|
if (!name.startswith("_Z"))
|
|
return name;
|
|
|
|
return demangle(name);
|
|
}
|
|
|
|
SingleStringMatcher::SingleStringMatcher(StringRef Pattern) {
|
|
if (Pattern.size() > 2 && Pattern.startswith("\"") &&
|
|
Pattern.endswith("\"")) {
|
|
ExactMatch = true;
|
|
ExactPattern = Pattern.substr(1, Pattern.size() - 2);
|
|
} else {
|
|
Expected<GlobPattern> Glob = GlobPattern::create(Pattern);
|
|
if (!Glob) {
|
|
error(toString(Glob.takeError()));
|
|
return;
|
|
}
|
|
ExactMatch = false;
|
|
GlobPatternMatcher = *Glob;
|
|
}
|
|
}
|
|
|
|
bool SingleStringMatcher::match(StringRef s) const {
|
|
return ExactMatch ? (ExactPattern == s) : GlobPatternMatcher.match(s);
|
|
}
|
|
|
|
bool StringMatcher::match(StringRef s) const {
|
|
for (const SingleStringMatcher &pat : patterns)
|
|
if (pat.match(s))
|
|
return true;
|
|
return false;
|
|
}
|
|
|
|
// Converts a hex string (e.g. "deadbeef") to a vector.
|
|
std::vector<uint8_t> lld::parseHex(StringRef s) {
|
|
std::vector<uint8_t> hex;
|
|
while (!s.empty()) {
|
|
StringRef b = s.substr(0, 2);
|
|
s = s.substr(2);
|
|
uint8_t h;
|
|
if (!to_integer(b, h, 16)) {
|
|
error("not a hexadecimal value: " + b);
|
|
return {};
|
|
}
|
|
hex.push_back(h);
|
|
}
|
|
return hex;
|
|
}
|
|
|
|
// Returns true if S is valid as a C language identifier.
|
|
bool lld::isValidCIdentifier(StringRef s) {
|
|
return !s.empty() && (isAlpha(s[0]) || s[0] == '_') &&
|
|
std::all_of(s.begin() + 1, s.end(),
|
|
[](char c) { return c == '_' || isAlnum(c); });
|
|
}
|
|
|
|
// Write the contents of the a buffer to a file
|
|
void lld::saveBuffer(StringRef buffer, const Twine &path) {
|
|
std::error_code ec;
|
|
raw_fd_ostream os(path.str(), ec, sys::fs::OpenFlags::OF_None);
|
|
if (ec)
|
|
error("cannot create " + path + ": " + ec.message());
|
|
os << buffer;
|
|
}
|