================ @@ -0,0 +1,465 @@ +//===- ExamplePlugin.cpp - Example SSAF plugin ----------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// A loadable plugin that demonstrates the full SSAF analysis pipeline. +// +//===----------------------------------------------------------------------===// + +#include "clang/ScalableStaticAnalysisFramework/Core/Model/EntityId.h" +#include "clang/ScalableStaticAnalysisFramework/Core/Model/SummaryName.h" +#include "clang/ScalableStaticAnalysisFramework/Core/Serialization/JSONFormat.h" +#include "clang/ScalableStaticAnalysisFramework/Core/TUSummary/EntitySummary.h" +#include "clang/ScalableStaticAnalysisFramework/Core/WholeProgramAnalysis/AnalysisName.h" +#include "clang/ScalableStaticAnalysisFramework/Core/WholeProgramAnalysis/AnalysisRegistry.h" +#include "clang/ScalableStaticAnalysisFramework/Core/WholeProgramAnalysis/AnalysisResult.h" +#include "clang/ScalableStaticAnalysisFramework/Core/WholeProgramAnalysis/DerivedAnalysis.h" +#include "clang/ScalableStaticAnalysisFramework/Core/WholeProgramAnalysis/SummaryAnalysis.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/JSON.h" +#include "llvm/Support/Registry.h" +#include <algorithm> +#include <memory> +#include <string> +#include <utility> +#include <vector> + +using namespace clang::ssaf; +using namespace llvm; + +namespace { + +//===----------------------------------------------------------------------===// +// TagsEntitySummary +// +// Per-entity data: a list of string tags. Stored in the LU data section +// under summary_name "TagsEntitySummary". Serialized as: +// { "tags": ["tag1", "tag2", ...] } +//===----------------------------------------------------------------------===// + +struct TagsEntitySummary final : EntitySummary { + static SummaryName summaryName() { return SummaryName("TagsEntitySummary"); } + + SummaryName getSummaryName() const override { + return SummaryName("TagsEntitySummary"); + } + + std::vector<std::string> Tags; +}; + +json::Object serializeTagsEntitySummary(const EntitySummary &ES, + JSONFormat::EntityIdToJSONFn) { + const auto &S = static_cast<const TagsEntitySummary &>(ES); + json::Array TagsArray; + for (const auto &Tag : S.Tags) { + TagsArray.push_back(Tag); + } + return json::Object{{"tags", std::move(TagsArray)}}; +} + +Expected<std::unique_ptr<EntitySummary>> +deserializeTagsEntitySummary(const json::Object &Obj, EntityIdTable &, + JSONFormat::EntityIdFromJSONFn) { + const json::Array *TagsArray = Obj.getArray("tags"); + if (!TagsArray) { + return createStringError(inconvertibleErrorCode(), + "missing or invalid field 'tags'"); + } + + auto S = std::make_unique<TagsEntitySummary>(); + for (const auto &[Index, Val] : llvm::enumerate(*TagsArray)) { + auto Str = Val.getAsString(); + if (!Str) { + return createStringError(inconvertibleErrorCode(), + "tags element at index %zu is not a string", + Index); + } + S->Tags.push_back(Str->str()); + } + return std::move(S); +} + +struct TagsEntitySummaryFormatInfo final : JSONFormat::FormatInfo { + TagsEntitySummaryFormatInfo() + : JSONFormat::FormatInfo(SummaryName("TagsEntitySummary"), ---------------- aviralg wrote:
Yes, that's why I kept the names short. If it was a `unittest`, I would have added a prefix to the names to avoid clashes. https://github.com/llvm/llvm-project/pull/187403 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
