Files
sdk/runtime/llvm_codegen/bit/bit.h
T
Jake Ehrlich 773f8d53ed Reland "[llvm] Add initial scaffolding"
This is a reland of b71d2d9996

Original change's description:
> [llvm] Add initial scaffolding
>
> This change adds the gclient and GN changes needed to build
> an executable using LLVM in the Dart tree as well as a basic
> testing framework based on llvm-lit.
>
> Change-Id: I9009a98ff95043cc3754966f31697ba7f1712310
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/106434
> Commit-Queue: Jake Ehrlich <jakehehrlich@google.com>
> Reviewed-by: Alexander Thomas <athom@google.com>
> Reviewed-by: Vyacheslav Egorov <vegorov@google.com>

Change-Id: I73cd24455c373bcc4d0f5675af6a3b1e0f947f67
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/110683
Reviewed-by: Vyacheslav Egorov <vegorov@google.com>
2019-07-30 20:34:31 +00:00

77 lines
2.2 KiB
C++

// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include <stdio.h>
#include <map>
#include <regex>
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/LineIterator.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Regex.h"
#include "llvm/Support/WithColor.h"
namespace {
using namespace llvm;
struct Config {
StringRef filename;
StringRef out_dir;
};
StringMap<std::string> GetSubstitutions(const Config& config) {
// Compute all of our strings needed for substitutions.
StringRef test_dir = sys::path::parent_path(config.filename);
StringRef basename = sys::path::filename(config.filename);
SmallString<128> tmp_file;
sys::path::append(tmp_file, sys::path::Style::native, config.out_dir,
basename + ".tmp");
SmallString<128> codegen;
sys::path::append(codegen, sys::path::Style::native, LIT_BINARY_DIR,
"codegen");
SmallString<128> bit;
sys::path::append(bit, sys::path::Style::native, LIT_BINARY_DIR, "bit");
// Set up our substitutions.
StringMap<std::string> subs;
subs["s"] = config.filename.str();
subs["p"] = test_dir.str();
subs["P"] = test_dir.str();
subs["t"] = tmp_file.str().str();
subs["codegen"] = codegen.str().str();
subs["bit"] = bit.str().str();
return subs;
}
std::string PerformSubstitutions(const StringMap<std::string>& subs,
StringRef string) {
std::string out = string.str();
for (const auto& sub : subs) {
std::string key = (Twine("%") + sub.getKeyData()).str();
size_t pos = 0;
while ((pos = out.find(key, pos)) != std::string::npos) {
if (pos != 0 && out[pos - 1] == '%') {
pos += key.size();
continue;
}
out.replace(pos, key.size(), sub.getValue());
pos += sub.second.size();
}
}
return out;
}
Optional<std::string> GetCommand(StringRef line) {
static Regex run_line("^;[ ]*RUN:[ ]*(.*)$");
SmallVector<StringRef, 2> cmd;
if (!run_line.match(line, &cmd)) return Optional<std::string>{};
assert(cmd.size() == 2);
return cmd[1].str();
}
} // namespace