Skip to content

Basics: define a YAML-based blocklist format #65009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions include/swift/Basic/BlockList.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,26 @@
namespace swift {

enum class BlockListAction: uint8_t {
ShouldUseBinaryModule = 0,
Undefined = 0,
ShouldUseBinaryModule,
ShouldUseTextualModule,
};

enum class BlockListKeyKind: uint8_t {
Undefined = 0,
ModuleName,
ProjectName
};

class BlockListStore {
public:
struct Implementation;
void addConfigureFilePath(StringRef path);
bool hasBlockListAction(StringRef key, BlockListKeyKind keyKind,
BlockListAction action);
BlockListStore();
~BlockListStore();
private:
friend class ASTContext;
void addConfigureFilePath(StringRef path);
Implementation &Impl;
};

Expand Down
84 changes: 82 additions & 2 deletions lib/Basic/BlockList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,31 @@
#include "swift/Basic/SourceManager.h"

struct swift::BlockListStore::Implementation {
SourceManager SM;
llvm::StringMap<std::vector<BlockListAction>> ModuleActionDict;
llvm::StringMap<std::vector<BlockListAction>> ProjectActionDict;
void addConfigureFilePath(StringRef path);
bool hasBlockListAction(StringRef key, BlockListKeyKind keyKind,
BlockListAction action);
void collectBlockList(llvm::yaml::Node *N, BlockListAction action);

llvm::StringMap<std::vector<BlockListAction>> *getDictToUse(BlockListKeyKind kind) {
switch (kind) {
case BlockListKeyKind::ModuleName:
return &ModuleActionDict;
case BlockListKeyKind::ProjectName:
return &ProjectActionDict;
case BlockListKeyKind::Undefined:
return nullptr;
}
}
static std::string getScalaString(llvm::yaml::Node *N) {
llvm::SmallString<64> Buffer;
if (auto *scala = dyn_cast<llvm::yaml::ScalarNode>(N)) {
return scala->getValue(Buffer).str();
}
return std::string();
}
};

swift::BlockListStore::BlockListStore(): Impl(*new Implementation()) {}
Expand All @@ -40,14 +60,74 @@ void swift::BlockListStore::addConfigureFilePath(StringRef path) {

bool swift::BlockListStore::Implementation::hasBlockListAction(StringRef key,
BlockListKeyKind keyKind, BlockListAction action) {
auto *dict = keyKind == BlockListKeyKind::ModuleName ? &ModuleActionDict :
&ProjectActionDict;
auto *dict = getDictToUse(keyKind);
assert(dict);
auto it = dict->find(key);
if (it == dict->end())
return false;
return llvm::is_contained(it->second, action);
}

void swift::BlockListStore::Implementation::collectBlockList(llvm::yaml::Node *N,
BlockListAction action) {
namespace yaml = llvm::yaml;
auto *pair = dyn_cast<yaml::KeyValueNode>(N);
if (!pair)
return;
std::string rawKey = getScalaString(pair->getKey());
auto keyKind = llvm::StringSwitch<BlockListKeyKind>(rawKey)
#define CASE(X) .Case(#X, BlockListKeyKind::X)
CASE(ModuleName)
CASE(ProjectName)
#undef CASE
.Default(BlockListKeyKind::Undefined);
if (keyKind == BlockListKeyKind::Undefined)
return;
auto *dictToUse = getDictToUse(keyKind);
assert(dictToUse);
auto *seq = dyn_cast<yaml::SequenceNode>(pair->getValue());
if (!seq)
return;
for (auto &node: *seq) {
std::string name = getScalaString(&node);
dictToUse->insert({name, std::vector<BlockListAction>()})
.first->second.push_back(action);
}
}

void swift::BlockListStore::Implementation::addConfigureFilePath(StringRef path) {
namespace yaml = llvm::yaml;

// Load the input file.
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileBufOrErr =
vfs::getFileOrSTDIN(*SM.getFileSystem(), path,
/*FileSize*/-1, /*RequiresNullTerminator*/true,
/*IsVolatile*/false, /*RetryCount*/30);
if (!FileBufOrErr) {
return;
}
StringRef Buffer = FileBufOrErr->get()->getBuffer();
yaml::Stream Stream(llvm::MemoryBufferRef(Buffer, path),
SM.getLLVMSourceMgr());
for (auto DI = Stream.begin(); DI != Stream.end(); ++ DI) {
assert(DI != Stream.end() && "Failed to read a document");
yaml::Node *N = DI->getRoot();
for (auto &pair: *dyn_cast<yaml::MappingNode>(N)) {
std::string key = getScalaString(pair.getKey());
auto action = llvm::StringSwitch<BlockListAction>(key)
#define CASE(X) .Case(#X, BlockListAction::X)
CASE(ShouldUseBinaryModule)
CASE(ShouldUseTextualModule)
#undef CASE
.Default(BlockListAction::Undefined);
if (action == BlockListAction::Undefined)
continue;
auto *map = dyn_cast<yaml::MappingNode>(pair.getValue());
if (!map)
continue;
for (auto &innerPair: *map) {
collectBlockList(&innerPair, action);
}
}
}
}
117 changes: 117 additions & 0 deletions unittests/Basic/BlocklistTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
//===------- BlocklistTest.cpp --------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#include "gtest/gtest.h"
#include "swift/AST/SearchPathOptions.h"
#include "swift/Basic/Defer.h"
#include "swift/Basic/BlockList.h"

using namespace swift;

static std::string createFilename(StringRef base, StringRef name) {
SmallString<256> path = base;
llvm::sys::path::append(path, name);
return llvm::Twine(path).str();
}

static bool emitFileWithContents(StringRef path, StringRef contents,
std::string *pathOut = nullptr) {
int FD;
if (llvm::sys::fs::openFileForWrite(path, FD))
return true;
if (pathOut)
*pathOut = path.str();
llvm::raw_fd_ostream file(FD, /*shouldClose=*/true);
file << contents;
return false;
}

static bool emitFileWithContents(StringRef base, StringRef name,
StringRef contents,
std::string *pathOut = nullptr) {
return emitFileWithContents(createFilename(base, name), contents, pathOut);
}

TEST(BlocklistTest, testYamlParsing) {
SmallString<256> temp;
ASSERT_FALSE(llvm::sys::fs::createUniqueDirectory(
"BlocklistTest.testYamlParsing", temp));
SWIFT_DEFER { llvm::sys::fs::remove_directories(temp); };
BlockListStore store;
std::string path1, path2;
ASSERT_FALSE(emitFileWithContents(temp, "block1.yaml",
"---\n"
"ShouldUseBinaryModule:\n"
" ModuleName:\n"
" - M1 #rdar12345\n"
" - M2 #rdar12345\n"
" - M3\n"
" - M4\n"
" ProjectName:\n"
" - P1\n"
" - P2\n"
" - P3 #rdar12344\n"
" - P4\n"
"---\n"
"ShouldUseTextualModule:\n"
" ModuleName:\n"
" - M1_2 #rdar12345\n"
" - M2_2 #rdar12345\n"
" - M3_2\n"
" - M4_2\n"
" ProjectName:\n"
" - P1_2\n"
" - P2_2\n"
" - P3_2 #rdar12344\n"
" - P4_2\n",
&path1));
ASSERT_FALSE(emitFileWithContents(temp, "block2.yml",
"---\n"
"ShouldUseBinaryModule:\n"
" ModuleName:\n"
" - M1_block2 #rdar12345\n"
" ProjectName:\n"
" - P1_block2\n"
"---\n"
"ShouldUseTextualModule:\n"
" ModuleName:\n"
" - M1_2_block2 #rdar12345\n"
" ProjectName:\n"
" - P1_2_block2\n",
&path2));
store.addConfigureFilePath(path1);
store.addConfigureFilePath(path2);
ASSERT_TRUE(store.hasBlockListAction("M1", BlockListKeyKind::ModuleName, BlockListAction::ShouldUseBinaryModule));
ASSERT_TRUE(store.hasBlockListAction("M2", BlockListKeyKind::ModuleName, BlockListAction::ShouldUseBinaryModule));
ASSERT_TRUE(store.hasBlockListAction("P1", BlockListKeyKind::ProjectName, BlockListAction::ShouldUseBinaryModule));
ASSERT_TRUE(store.hasBlockListAction("P2", BlockListKeyKind::ProjectName, BlockListAction::ShouldUseBinaryModule));

ASSERT_TRUE(store.hasBlockListAction("M1_2", BlockListKeyKind::ModuleName, BlockListAction::ShouldUseTextualModule));
ASSERT_TRUE(store.hasBlockListAction("M2_2", BlockListKeyKind::ModuleName, BlockListAction::ShouldUseTextualModule));
ASSERT_TRUE(store.hasBlockListAction("P1_2", BlockListKeyKind::ProjectName, BlockListAction::ShouldUseTextualModule));
ASSERT_TRUE(store.hasBlockListAction("P2_2", BlockListKeyKind::ProjectName, BlockListAction::ShouldUseTextualModule));

ASSERT_FALSE(store.hasBlockListAction("P1", BlockListKeyKind::ModuleName, BlockListAction::ShouldUseBinaryModule));
ASSERT_FALSE(store.hasBlockListAction("P2", BlockListKeyKind::ModuleName, BlockListAction::ShouldUseBinaryModule));
ASSERT_FALSE(store.hasBlockListAction("M1", BlockListKeyKind::ProjectName, BlockListAction::ShouldUseBinaryModule));
ASSERT_FALSE(store.hasBlockListAction("M2", BlockListKeyKind::ProjectName, BlockListAction::ShouldUseBinaryModule));

ASSERT_TRUE(store.hasBlockListAction("M1_block2", BlockListKeyKind::ModuleName, BlockListAction::ShouldUseBinaryModule));
ASSERT_TRUE(store.hasBlockListAction("P1_block2", BlockListKeyKind::ProjectName, BlockListAction::ShouldUseBinaryModule));
ASSERT_TRUE(store.hasBlockListAction("M1_2_block2", BlockListKeyKind::ModuleName, BlockListAction::ShouldUseTextualModule));
ASSERT_TRUE(store.hasBlockListAction("P1_2_block2", BlockListKeyKind::ProjectName, BlockListAction::ShouldUseTextualModule));

ASSERT_FALSE(store.hasBlockListAction("M1_block2", BlockListKeyKind::ProjectName, BlockListAction::ShouldUseBinaryModule));
ASSERT_FALSE(store.hasBlockListAction("P1_block2", BlockListKeyKind::ModuleName, BlockListAction::ShouldUseBinaryModule));
ASSERT_FALSE(store.hasBlockListAction("M1_2_block2", BlockListKeyKind::ProjectName, BlockListAction::ShouldUseTextualModule));
ASSERT_FALSE(store.hasBlockListAction("P1_2_block2", BlockListKeyKind::ModuleName, BlockListAction::ShouldUseTextualModule));
}
1 change: 1 addition & 0 deletions unittests/Basic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ handle_gyb_sources(

add_swift_unittest(SwiftBasicTests
BlotMapVectorTest.cpp
BlocklistTest.cpp
CacheTest.cpp
ClangImporterOptionsTest.cpp
ClusteredBitVectorTest.cpp
Expand Down