1//===- ModelStringConversions.h ---------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Internal string conversion utilities for SSAF model types.
10//
11// These functions are shared by the model .cpp files (for operator<<) and
12// JSONFormat.cpp (for serialization). They are not part of the public API.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_CLANG_LIB_SCALABLESTATICANALYSISFRAMEWORK_CORE_MODELSTRINGCONVERSIONS_H
17#define LLVM_CLANG_LIB_SCALABLESTATICANALYSISFRAMEWORK_CORE_MODELSTRINGCONVERSIONS_H
18
19#include "clang/ScalableStaticAnalysisFramework/Core/Model/BuildNamespace.h"
20#include "clang/ScalableStaticAnalysisFramework/Core/Model/EntityLinkage.h"
21#include "llvm/ADT/StringRef.h"
22#include "llvm/Support/ErrorHandling.h"
23#include <optional>
24
25namespace clang::ssaf {
26
27//===----------------------------------------------------------------------===//
28// BuildNamespaceKind
29//===----------------------------------------------------------------------===//
30
31/// Returns the canonical string representation of \p BNK used for
32/// serialization and display (e.g. "CompilationUnit", "LinkUnit").
33inline llvm::StringRef buildNamespaceKindToString(BuildNamespaceKind BNK) {
34 switch (BNK) {
35 case BuildNamespaceKind::CompilationUnit:
36 return "CompilationUnit";
37 case BuildNamespaceKind::LinkUnit:
38 return "LinkUnit";
39 }
40 llvm_unreachable("Unhandled BuildNamespaceKind variant");
41}
42
43/// Parses a string produced by buildNamespaceKindToString(). Returns
44/// std::nullopt if \p Str does not match any known BuildNamespaceKind value.
45inline std::optional<BuildNamespaceKind>
46buildNamespaceKindFromString(llvm::StringRef Str) {
47 if (Str == "CompilationUnit")
48 return BuildNamespaceKind::CompilationUnit;
49 if (Str == "LinkUnit")
50 return BuildNamespaceKind::LinkUnit;
51 return std::nullopt;
52}
53
54//===----------------------------------------------------------------------===//
55// EntityLinkageType
56//===----------------------------------------------------------------------===//
57
58/// Returns the canonical string representation of \p LT used for
59/// serialization and display (e.g. "None", "Internal", "External").
60inline llvm::StringRef entityLinkageTypeToString(EntityLinkageType LT) {
61 switch (LT) {
62 case EntityLinkageType::None:
63 return "None";
64 case EntityLinkageType::Internal:
65 return "Internal";
66 case EntityLinkageType::External:
67 return "External";
68 }
69 llvm_unreachable("Unhandled EntityLinkageType variant");
70}
71
72/// Parses a string produced by entityLinkageTypeToString(). Returns
73/// std::nullopt if \p Str does not match any known EntityLinkageType value.
74inline std::optional<EntityLinkageType>
75entityLinkageTypeFromString(llvm::StringRef Str) {
76 if (Str == "None")
77 return EntityLinkageType::None;
78 if (Str == "Internal")
79 return EntityLinkageType::Internal;
80 if (Str == "External")
81 return EntityLinkageType::External;
82 return std::nullopt;
83}
84
85} // namespace clang::ssaf
86
87#endif // LLVM_CLANG_LIB_SCALABLESTATICANALYSISFRAMEWORK_CORE_MODELSTRINGCONVERSIONS_H
88