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_ScalableStaticAnalysis_CORE_MODELSTRINGCONVERSIONS_H
17#define LLVM_CLANG_LIB_ScalableStaticAnalysis_CORE_MODELSTRINGCONVERSIONS_H
18
19#include "clang/ScalableStaticAnalysis/Core/Model/BuildNamespace.h"
20#include "clang/ScalableStaticAnalysis/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 case BuildNamespaceKind::StaticLibrary:
40 return "StaticLibrary";
41 case BuildNamespaceKind::MultiArchStaticLibrary:
42 return "MultiArchStaticLibrary";
43 }
44 llvm_unreachable("Unhandled BuildNamespaceKind variant");
45}
46
47/// Parses a string produced by buildNamespaceKindToString(). Returns
48/// std::nullopt if \p Str does not match any known BuildNamespaceKind value.
49inline std::optional<BuildNamespaceKind>
50buildNamespaceKindFromString(llvm::StringRef Str) {
51 if (Str == "CompilationUnit")
52 return BuildNamespaceKind::CompilationUnit;
53 if (Str == "LinkUnit")
54 return BuildNamespaceKind::LinkUnit;
55 if (Str == "StaticLibrary")
56 return BuildNamespaceKind::StaticLibrary;
57 if (Str == "MultiArchStaticLibrary")
58 return BuildNamespaceKind::MultiArchStaticLibrary;
59 return std::nullopt;
60}
61
62//===----------------------------------------------------------------------===//
63// EntityLinkageType
64//===----------------------------------------------------------------------===//
65
66/// Returns the canonical string representation of \p LT used for
67/// serialization and display (e.g. "None", "Internal", "External").
68inline llvm::StringRef entityLinkageTypeToString(EntityLinkageType LT) {
69 switch (LT) {
70 case EntityLinkageType::None:
71 return "None";
72 case EntityLinkageType::Internal:
73 return "Internal";
74 case EntityLinkageType::External:
75 return "External";
76 }
77 llvm_unreachable("Unhandled EntityLinkageType variant");
78}
79
80/// Parses a string produced by entityLinkageTypeToString(). Returns
81/// std::nullopt if \p Str does not match any known EntityLinkageType value.
82inline std::optional<EntityLinkageType>
83entityLinkageTypeFromString(llvm::StringRef Str) {
84 if (Str == "None")
85 return EntityLinkageType::None;
86 if (Str == "Internal")
87 return EntityLinkageType::Internal;
88 if (Str == "External")
89 return EntityLinkageType::External;
90 return std::nullopt;
91}
92
93} // namespace clang::ssaf
94
95#endif // LLVM_CLANG_LIB_ScalableStaticAnalysis_CORE_MODELSTRINGCONVERSIONS_H
96