llvm-project/clang/lib/Analysis/Scalable/Serialization/SerializationFormatRegistry.cpp
Balázs Benics 77740f5577
[clang][ssaf] Add SerializationFormatRegistry [1/2] (#179516)
Add a registry infrastructure for SerializationFormat implementations,
enabling registration and instantiation of different serialization
formats.

For example:
```c++
  static SerializationFormatRegistry::Add<MyFormat>
    RegisterFormat("MyFormat", "Description");
```

Formats can then be instantiated by name using `makeFormat()`.

The patch also updates the SerializationFormat base class to accept
FileSystem for virtualising reading inputs eg. by using file overlays in
the future.

Assisted-by: claude
rdar://169192127
2026-02-05 16:34:54 +00:00

37 lines
1.3 KiB
C++

//===- SerializationFormatRegistry.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 "clang/Analysis/Scalable/Serialization/SerializationFormatRegistry.h"
#include <memory>
using namespace clang;
using namespace ssaf;
// FIXME: LLVM_INSTANTIATE_REGISTRY can't be used here because it drops extra
// type parameters.
template class CLANG_EXPORT_TEMPLATE
llvm::Registry<clang::ssaf::SerializationFormat,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>>;
bool ssaf::isFormatRegistered(llvm::StringRef FormatName) {
for (const auto &Entry : SerializationFormatRegistry::entries())
if (Entry.getName() == FormatName)
return true;
return false;
}
std::unique_ptr<SerializationFormat>
ssaf::makeFormat(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
llvm::StringRef FormatName) {
for (const auto &Entry : SerializationFormatRegistry::entries())
if (Entry.getName() == FormatName)
return Entry.instantiate(std::move(FS));
assert(false && "Unknown SerializationFormat name");
return nullptr;
}