Bug Summary

File:clang-tools-extra/clangd/index/YAMLSerialization.cpp
Warning:line 127, column 5
1st function call argument is an uninitialized value

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name YAMLSerialization.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -relaxed-aliasing -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/build-llvm/tools/clang/stage2-bins -resource-dir /usr/lib/llvm-14/lib/clang/14.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I tools/clang/tools/extra/clangd -I /build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/clang-tools-extra/clangd -I tools/clang/tools/extra/clangd/../clang-tidy -I /build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/clang/include -I tools/clang/include -I include -I /build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/llvm/include -D _FORTIFY_SOURCE=2 -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-14/lib/clang/14.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fmacro-prefix-map=/build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/= -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/= -O3 -Wno-unused-command-line-argument -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/build-llvm/tools/clang/stage2-bins=build-llvm/tools/clang/stage2-bins -fdebug-prefix-map=/build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/= -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2022-01-25-232935-20746-1 -x c++ /build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/clang-tools-extra/clangd/index/YAMLSerialization.cpp

/build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/clang-tools-extra/clangd/index/YAMLSerialization.cpp

1//===-- YAMLSerialization.cpp ------------------------------------*- 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// A YAML index file is a sequence of tagged entries.
10// Each entry either encodes a Symbol or the list of references to a symbol
11// (a "ref bundle").
12//
13//===----------------------------------------------------------------------===//
14
15#include "Index.h"
16#include "Relation.h"
17#include "Serialization.h"
18#include "SymbolLocation.h"
19#include "SymbolOrigin.h"
20#include "dex/Dex.h"
21#include "support/Logger.h"
22#include "support/Trace.h"
23#include "llvm/ADT/Optional.h"
24#include "llvm/ADT/SmallVector.h"
25#include "llvm/ADT/StringRef.h"
26#include "llvm/Support/Allocator.h"
27#include "llvm/Support/Errc.h"
28#include "llvm/Support/MemoryBuffer.h"
29#include "llvm/Support/StringSaver.h"
30#include "llvm/Support/YAMLTraits.h"
31#include "llvm/Support/raw_ostream.h"
32#include <cstdint>
33
34LLVM_YAML_IS_SEQUENCE_VECTOR(clang::clangd::Symbol::IncludeHeaderWithReferences)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<clang::clangd::Symbol::IncludeHeaderWithReferences>::value
&& !std::is_same<clang::clangd::Symbol::IncludeHeaderWithReferences
, std::string>::value && !std::is_same<clang::clangd
::Symbol::IncludeHeaderWithReferences, llvm::StringRef>::value
, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<clang::clangd
::Symbol::IncludeHeaderWithReferences> { static const bool
flow = false; }; } }
35LLVM_YAML_IS_SEQUENCE_VECTOR(clang::clangd::Ref)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<clang::clangd::Ref>::value && !std::is_same<
clang::clangd::Ref, std::string>::value && !std::is_same
<clang::clangd::Ref, llvm::StringRef>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<clang::clangd
::Ref> { static const bool flow = false; }; } }
36
37namespace {
38using RefBundle =
39 std::pair<clang::clangd::SymbolID, std::vector<clang::clangd::Ref>>;
40// This is a pale imitation of std::variant<Symbol, RefBundle, Relation>
41struct VariantEntry {
42 llvm::Optional<clang::clangd::Symbol> Symbol;
43 llvm::Optional<RefBundle> Refs;
44 llvm::Optional<clang::clangd::Relation> Relation;
45 llvm::Optional<clang::clangd::IncludeGraphNode> Source;
46 llvm::Optional<clang::tooling::CompileCommand> Cmd;
47};
48// A class helps YAML to serialize the 32-bit encoded position (Line&Column),
49// as YAMLIO can't directly map bitfields.
50struct YPosition {
51 uint32_t Line;
52 uint32_t Column;
53};
54
55// avoid ODR violation of specialization for non-owned CompileCommand
56struct CompileCommandYAML : clang::tooling::CompileCommand {};
57
58} // namespace
59namespace llvm {
60namespace yaml {
61
62using clang::clangd::FileDigest;
63using clang::clangd::IncludeGraph;
64using clang::clangd::IncludeGraphNode;
65using clang::clangd::Ref;
66using clang::clangd::RefKind;
67using clang::clangd::Relation;
68using clang::clangd::RelationKind;
69using clang::clangd::Symbol;
70using clang::clangd::SymbolID;
71using clang::clangd::SymbolLocation;
72using clang::index::SymbolInfo;
73using clang::index::SymbolKind;
74using clang::index::SymbolLanguage;
75using clang::tooling::CompileCommand;
76
77// Helper to (de)serialize the SymbolID. We serialize it as a hex string.
78struct NormalizedSymbolID {
79 NormalizedSymbolID(IO &) {}
80 NormalizedSymbolID(IO &, const SymbolID &ID) {
81 llvm::raw_string_ostream OS(HexString);
82 OS << ID;
83 }
84
85 SymbolID denormalize(IO &I) {
86 auto ID = SymbolID::fromStr(HexString);
87 if (!ID) {
88 I.setError(llvm::toString(ID.takeError()));
89 return SymbolID();
90 }
91 return *ID;
92 }
93
94 std::string HexString;
95};
96
97struct NormalizedSymbolFlag {
98 NormalizedSymbolFlag(IO &) {}
99 NormalizedSymbolFlag(IO &, Symbol::SymbolFlag F) {
100 Flag = static_cast<uint8_t>(F);
101 }
102
103 Symbol::SymbolFlag denormalize(IO &) {
104 return static_cast<Symbol::SymbolFlag>(Flag);
105 }
106
107 uint8_t Flag = 0;
108};
109
110template <> struct MappingTraits<YPosition> {
111 static void mapping(IO &IO, YPosition &Value) {
112 IO.mapRequired("Line", Value.Line);
113 IO.mapRequired("Column", Value.Column);
114 }
115};
116
117struct NormalizedPosition {
118 using Position = clang::clangd::SymbolLocation::Position;
119 NormalizedPosition(IO &) {}
22
Returning without writing to 'this->P.Line'
120 NormalizedPosition(IO &, const Position &Pos) {
121 P.Line = Pos.line();
122 P.Column = Pos.column();
123 }
124
125 Position denormalize(IO &) {
126 Position Pos;
127 Pos.setLine(P.Line);
37
1st function call argument is an uninitialized value
128 Pos.setColumn(P.Column);
129 return Pos;
130 }
131 YPosition P;
132};
133
134struct NormalizedFileURI {
135 NormalizedFileURI(IO &) {}
136 NormalizedFileURI(IO &, const char *FileURI) { URI = FileURI; }
137
138 const char *denormalize(IO &IO) {
139 assert(IO.getContext() &&(static_cast <bool> (IO.getContext() && "Expecting an UniqueStringSaver to allocate data"
) ? void (0) : __assert_fail ("IO.getContext() && \"Expecting an UniqueStringSaver to allocate data\""
, "clang-tools-extra/clangd/index/YAMLSerialization.cpp", 140
, __extension__ __PRETTY_FUNCTION__))
140 "Expecting an UniqueStringSaver to allocate data")(static_cast <bool> (IO.getContext() && "Expecting an UniqueStringSaver to allocate data"
) ? void (0) : __assert_fail ("IO.getContext() && \"Expecting an UniqueStringSaver to allocate data\""
, "clang-tools-extra/clangd/index/YAMLSerialization.cpp", 140
, __extension__ __PRETTY_FUNCTION__))
;
141 return static_cast<llvm::UniqueStringSaver *>(IO.getContext())
142 ->save(URI)
143 .data();
144 }
145
146 std::string URI;
147};
148
149template <> struct MappingTraits<SymbolLocation> {
150 static void mapping(IO &IO, SymbolLocation &Value) {
151 MappingNormalization<NormalizedFileURI, const char *> NFile(IO,
152 Value.FileURI);
153 IO.mapRequired("FileURI", NFile->URI);
154 MappingNormalization<NormalizedPosition, SymbolLocation::Position> NStart(
155 IO, Value.Start);
156 IO.mapRequired("Start", NStart->P);
157 MappingNormalization<NormalizedPosition, SymbolLocation::Position> NEnd(
16
Calling constructor for 'MappingNormalization<llvm::yaml::NormalizedPosition, clang::clangd::SymbolLocation::Position>'
24
Returning from constructor for 'MappingNormalization<llvm::yaml::NormalizedPosition, clang::clangd::SymbolLocation::Position>'
158 IO, Value.End);
159 IO.mapRequired("End", NEnd->P);
25
Calling 'IO::mapRequired'
32
Returning from 'IO::mapRequired'
160 }
33
Calling '~MappingNormalization'
161};
162
163template <> struct MappingTraits<SymbolInfo> {
164 static void mapping(IO &io, SymbolInfo &SymInfo) {
165 // FIXME: expose other fields?
166 io.mapRequired("Kind", SymInfo.Kind);
167 io.mapRequired("Lang", SymInfo.Lang);
168 }
169};
170
171template <>
172struct MappingTraits<clang::clangd::Symbol::IncludeHeaderWithReferences> {
173 static void mapping(IO &io,
174 clang::clangd::Symbol::IncludeHeaderWithReferences &Inc) {
175 io.mapRequired("Header", Inc.IncludeHeader);
176 io.mapRequired("References", Inc.References);
177 }
178};
179
180template <> struct MappingTraits<Symbol> {
181 static void mapping(IO &IO, Symbol &Sym) {
182 MappingNormalization<NormalizedSymbolID, SymbolID> NSymbolID(IO, Sym.ID);
183 MappingNormalization<NormalizedSymbolFlag, Symbol::SymbolFlag> NSymbolFlag(
184 IO, Sym.Flags);
185 IO.mapRequired("ID", NSymbolID->HexString);
186 IO.mapRequired("Name", Sym.Name);
187 IO.mapRequired("Scope", Sym.Scope);
188 IO.mapRequired("SymInfo", Sym.SymInfo);
189 IO.mapOptional("CanonicalDeclaration", Sym.CanonicalDeclaration,
190 SymbolLocation());
191 IO.mapOptional("Definition", Sym.Definition, SymbolLocation());
192 IO.mapOptional("References", Sym.References, 0u);
193 IO.mapOptional("Flags", NSymbolFlag->Flag);
194 IO.mapOptional("Signature", Sym.Signature);
195 IO.mapOptional("TemplateSpecializationArgs",
196 Sym.TemplateSpecializationArgs);
197 IO.mapOptional("CompletionSnippetSuffix", Sym.CompletionSnippetSuffix);
198 IO.mapOptional("Documentation", Sym.Documentation);
199 IO.mapOptional("ReturnType", Sym.ReturnType);
200 IO.mapOptional("Type", Sym.Type);
201 IO.mapOptional("IncludeHeaders", Sym.IncludeHeaders);
202 }
203};
204
205template <> struct ScalarEnumerationTraits<SymbolLanguage> {
206 static void enumeration(IO &IO, SymbolLanguage &Value) {
207 IO.enumCase(Value, "C", SymbolLanguage::C);
208 IO.enumCase(Value, "Cpp", SymbolLanguage::CXX);
209 IO.enumCase(Value, "ObjC", SymbolLanguage::ObjC);
210 IO.enumCase(Value, "Swift", SymbolLanguage::Swift);
211 }
212};
213
214template <> struct ScalarEnumerationTraits<SymbolKind> {
215 static void enumeration(IO &IO, SymbolKind &Value) {
216#define DEFINE_ENUM(name) IO.enumCase(Value, #name, SymbolKind::name)
217
218 DEFINE_ENUM(Unknown);
219 DEFINE_ENUM(Function);
220 DEFINE_ENUM(Module);
221 DEFINE_ENUM(Namespace);
222 DEFINE_ENUM(NamespaceAlias);
223 DEFINE_ENUM(Macro);
224 DEFINE_ENUM(Enum);
225 DEFINE_ENUM(Struct);
226 DEFINE_ENUM(Class);
227 DEFINE_ENUM(Protocol);
228 DEFINE_ENUM(Extension);
229 DEFINE_ENUM(Union);
230 DEFINE_ENUM(TypeAlias);
231 DEFINE_ENUM(Function);
232 DEFINE_ENUM(Variable);
233 DEFINE_ENUM(Field);
234 DEFINE_ENUM(EnumConstant);
235 DEFINE_ENUM(InstanceMethod);
236 DEFINE_ENUM(ClassMethod);
237 DEFINE_ENUM(StaticMethod);
238 DEFINE_ENUM(InstanceProperty);
239 DEFINE_ENUM(ClassProperty);
240 DEFINE_ENUM(StaticProperty);
241 DEFINE_ENUM(Constructor);
242 DEFINE_ENUM(Destructor);
243 DEFINE_ENUM(ConversionFunction);
244 DEFINE_ENUM(Parameter);
245 DEFINE_ENUM(Using);
246
247#undef DEFINE_ENUM
248 }
249};
250
251template <> struct MappingTraits<RefBundle> {
252 static void mapping(IO &IO, RefBundle &Refs) {
253 MappingNormalization<NormalizedSymbolID, SymbolID> NSymbolID(IO,
254 Refs.first);
255 IO.mapRequired("ID", NSymbolID->HexString);
256 IO.mapRequired("References", Refs.second);
257 }
258};
259
260struct NormalizedRefKind {
261 NormalizedRefKind(IO &) {}
262 NormalizedRefKind(IO &, RefKind O) { Kind = static_cast<uint8_t>(O); }
263
264 RefKind denormalize(IO &) { return static_cast<RefKind>(Kind); }
265
266 uint8_t Kind = 0;
267};
268
269template <> struct MappingTraits<Ref> {
270 static void mapping(IO &IO, Ref &R) {
271 MappingNormalization<NormalizedRefKind, RefKind> NKind(IO, R.Kind);
272 IO.mapRequired("Kind", NKind->Kind);
273 IO.mapRequired("Location", R.Location);
8
Calling 'IO::mapRequired'
274 }
275};
276
277struct NormalizedSymbolRole {
278 NormalizedSymbolRole(IO &) {}
279 NormalizedSymbolRole(IO &IO, RelationKind R) {
280 Kind = static_cast<uint8_t>(R);
281 }
282
283 RelationKind denormalize(IO &IO) { return static_cast<RelationKind>(Kind); }
284
285 uint8_t Kind = 0;
286};
287
288template <> struct MappingTraits<SymbolID> {
289 static void mapping(IO &IO, SymbolID &ID) {
290 MappingNormalization<NormalizedSymbolID, SymbolID> NSymbolID(IO, ID);
291 IO.mapRequired("ID", NSymbolID->HexString);
292 }
293};
294
295template <> struct MappingTraits<Relation> {
296 static void mapping(IO &IO, Relation &Relation) {
297 MappingNormalization<NormalizedSymbolRole, RelationKind> NRole(
298 IO, Relation.Predicate);
299 IO.mapRequired("Subject", Relation.Subject);
300 IO.mapRequired("Predicate", NRole->Kind);
301 IO.mapRequired("Object", Relation.Object);
302 }
303};
304
305struct NormalizedSourceFlag {
306 NormalizedSourceFlag(IO &) {}
307 NormalizedSourceFlag(IO &, IncludeGraphNode::SourceFlag O) {
308 Flag = static_cast<uint8_t>(O);
309 }
310
311 IncludeGraphNode::SourceFlag denormalize(IO &) {
312 return static_cast<IncludeGraphNode::SourceFlag>(Flag);
313 }
314
315 uint8_t Flag = 0;
316};
317
318struct NormalizedFileDigest {
319 NormalizedFileDigest(IO &) {}
320 NormalizedFileDigest(IO &, const FileDigest &Digest) {
321 HexString = llvm::toHex(Digest);
322 }
323
324 FileDigest denormalize(IO &I) {
325 FileDigest Digest;
326 if (HexString.size() == Digest.size() * 2 &&
327 llvm::all_of(HexString, llvm::isHexDigit)) {
328 memcpy(Digest.data(), llvm::fromHex(HexString).data(), Digest.size());
329 } else {
330 I.setError(std::string("Bad hex file digest: ") + HexString);
331 }
332 return Digest;
333 }
334
335 std::string HexString;
336};
337
338template <> struct MappingTraits<IncludeGraphNode> {
339 static void mapping(IO &IO, IncludeGraphNode &Node) {
340 IO.mapRequired("URI", Node.URI);
341 MappingNormalization<NormalizedSourceFlag, IncludeGraphNode::SourceFlag>
342 NSourceFlag(IO, Node.Flags);
343 IO.mapRequired("Flags", NSourceFlag->Flag);
344 MappingNormalization<NormalizedFileDigest, FileDigest> NDigest(IO,
345 Node.Digest);
346 IO.mapRequired("Digest", NDigest->HexString);
347 IO.mapRequired("DirectIncludes", Node.DirectIncludes);
348 }
349};
350
351template <> struct MappingTraits<CompileCommandYAML> {
352 static void mapping(IO &IO, CompileCommandYAML &Cmd) {
353 IO.mapRequired("Directory", Cmd.Directory);
354 IO.mapRequired("CommandLine", Cmd.CommandLine);
355 }
356};
357
358template <> struct MappingTraits<VariantEntry> {
359 static void mapping(IO &IO, VariantEntry &Variant) {
360 if (IO.mapTag("!Symbol", Variant.Symbol.hasValue())) {
361 if (!IO.outputting())
362 Variant.Symbol.emplace();
363 MappingTraits<Symbol>::mapping(IO, *Variant.Symbol);
364 } else if (IO.mapTag("!Refs", Variant.Refs.hasValue())) {
365 if (!IO.outputting())
366 Variant.Refs.emplace();
367 MappingTraits<RefBundle>::mapping(IO, *Variant.Refs);
368 } else if (IO.mapTag("!Relations", Variant.Relation.hasValue())) {
369 if (!IO.outputting())
370 Variant.Relation.emplace();
371 MappingTraits<Relation>::mapping(IO, *Variant.Relation);
372 } else if (IO.mapTag("!Source", Variant.Source.hasValue())) {
373 if (!IO.outputting())
374 Variant.Source.emplace();
375 MappingTraits<IncludeGraphNode>::mapping(IO, *Variant.Source);
376 } else if (IO.mapTag("!Cmd", Variant.Cmd.hasValue())) {
377 if (!IO.outputting())
378 Variant.Cmd.emplace();
379 MappingTraits<CompileCommandYAML>::mapping(
380 IO, static_cast<CompileCommandYAML &>(*Variant.Cmd));
381 }
382 }
383};
384
385} // namespace yaml
386} // namespace llvm
387
388namespace clang {
389namespace clangd {
390
391void writeYAML(const IndexFileOut &O, llvm::raw_ostream &OS) {
392 llvm::yaml::Output Yout(OS);
393 for (const auto &Sym : *O.Symbols) {
394 VariantEntry Entry;
395 Entry.Symbol = Sym;
396 Yout << Entry;
397 }
398 if (O.Refs)
399 for (auto &Sym : *O.Refs) {
400 VariantEntry Entry;
401 Entry.Refs = Sym;
402 Yout << Entry;
403 }
404 if (O.Relations)
405 for (auto &R : *O.Relations) {
406 VariantEntry Entry;
407 Entry.Relation = R;
408 Yout << Entry;
409 }
410 if (O.Sources) {
411 for (const auto &Source : *O.Sources) {
412 VariantEntry Entry;
413 Entry.Source = Source.getValue();
414 Yout << Entry;
415 }
416 }
417 if (O.Cmd) {
418 VariantEntry Entry;
419 Entry.Cmd = *O.Cmd;
420 Yout << Entry;
421 }
422}
423
424llvm::Expected<IndexFileIn> readYAML(llvm::StringRef Data,
425 SymbolOrigin Origin) {
426 SymbolSlab::Builder Symbols;
427 RefSlab::Builder Refs;
428 RelationSlab::Builder Relations;
429 llvm::BumpPtrAllocator
430 Arena; // store the underlying data of Position::FileURI.
431 llvm::UniqueStringSaver Strings(Arena);
432 llvm::yaml::Input Yin(Data, &Strings);
433 IncludeGraph Sources;
434 llvm::Optional<tooling::CompileCommand> Cmd;
435 while (Yin.setCurrentDocument()) {
436 llvm::yaml::EmptyContext Ctx;
437 VariantEntry Variant;
438 yamlize(Yin, Variant, true, Ctx);
439 if (Yin.error())
440 return llvm::errorCodeToError(Yin.error());
441
442 if (Variant.Symbol) {
443 Variant.Symbol->Origin = Origin;
444 Symbols.insert(*Variant.Symbol);
445 }
446 if (Variant.Refs)
447 for (const auto &Ref : Variant.Refs->second)
448 Refs.insert(Variant.Refs->first, Ref);
449 if (Variant.Relation)
450 Relations.insert(*Variant.Relation);
451 if (Variant.Source) {
452 auto &IGN = Variant.Source.getValue();
453 auto Entry = Sources.try_emplace(IGN.URI).first;
454 Entry->getValue() = std::move(IGN);
455 // Fixup refs to refer to map keys which will live on
456 Entry->getValue().URI = Entry->getKey();
457 for (auto &Include : Entry->getValue().DirectIncludes)
458 Include = Sources.try_emplace(Include).first->getKey();
459 }
460 if (Variant.Cmd)
461 Cmd = *Variant.Cmd;
462 Yin.nextDocument();
463 }
464
465 IndexFileIn Result;
466 Result.Symbols.emplace(std::move(Symbols).build());
467 Result.Refs.emplace(std::move(Refs).build());
468 Result.Relations.emplace(std::move(Relations).build());
469 if (Sources.size())
470 Result.Sources = std::move(Sources);
471 Result.Cmd = std::move(Cmd);
472 return std::move(Result);
473}
474
475std::string toYAML(const Symbol &S) {
476 std::string Buf;
477 {
478 llvm::raw_string_ostream OS(Buf);
479 llvm::yaml::Output Yout(OS);
480 Symbol Sym = S; // copy: Yout<< requires mutability.
481 Yout << Sym;
482 }
483 return Buf;
484}
485
486std::string toYAML(const std::pair<SymbolID, llvm::ArrayRef<Ref>> &Data) {
487 RefBundle Refs = {Data.first, Data.second};
488 std::string Buf;
489 {
490 llvm::raw_string_ostream OS(Buf);
491 llvm::yaml::Output Yout(OS);
492 Yout << Refs;
493 }
494 return Buf;
495}
496
497std::string toYAML(const Relation &R) {
498 std::string Buf;
499 {
500 llvm::raw_string_ostream OS(Buf);
501 llvm::yaml::Output Yout(OS);
502 Relation Rel = R; // copy: Yout<< requires mutability.
503 Yout << Rel;
504 }
505 return Buf;
506}
507
508std::string toYAML(const Ref &R) {
509 std::string Buf;
510 {
511 llvm::raw_string_ostream OS(Buf);
512 llvm::yaml::Output Yout(OS);
513 Ref Reference = R; // copy: Yout<< requires mutability.
514 Yout << Reference;
1
Calling 'operator<<<clang::clangd::Ref>'
515 }
516 return Buf;
517}
518
519} // namespace clangd
520} // namespace clang

/build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/llvm/include/llvm/Support/YAMLTraits.h

1//===- llvm/Support/YAMLTraits.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#ifndef LLVM_SUPPORT_YAMLTRAITS_H
10#define LLVM_SUPPORT_YAMLTRAITS_H
11
12#include "llvm/ADT/Optional.h"
13#include "llvm/ADT/SmallVector.h"
14#include "llvm/ADT/StringExtras.h"
15#include "llvm/ADT/StringMap.h"
16#include "llvm/ADT/StringRef.h"
17#include "llvm/ADT/Twine.h"
18#include "llvm/Support/AlignOf.h"
19#include "llvm/Support/Allocator.h"
20#include "llvm/Support/Endian.h"
21#include "llvm/Support/SMLoc.h"
22#include "llvm/Support/SourceMgr.h"
23#include "llvm/Support/VersionTuple.h"
24#include "llvm/Support/YAMLParser.h"
25#include "llvm/Support/raw_ostream.h"
26#include <cassert>
27#include <cctype>
28#include <map>
29#include <memory>
30#include <new>
31#include <string>
32#include <system_error>
33#include <type_traits>
34#include <vector>
35
36namespace llvm {
37namespace yaml {
38
39enum class NodeKind : uint8_t {
40 Scalar,
41 Map,
42 Sequence,
43};
44
45struct EmptyContext {};
46
47/// This class should be specialized by any type that needs to be converted
48/// to/from a YAML mapping. For example:
49///
50/// struct MappingTraits<MyStruct> {
51/// static void mapping(IO &io, MyStruct &s) {
52/// io.mapRequired("name", s.name);
53/// io.mapRequired("size", s.size);
54/// io.mapOptional("age", s.age);
55/// }
56/// };
57template<class T>
58struct MappingTraits {
59 // Must provide:
60 // static void mapping(IO &io, T &fields);
61 // Optionally may provide:
62 // static std::string validate(IO &io, T &fields);
63 //
64 // The optional flow flag will cause generated YAML to use a flow mapping
65 // (e.g. { a: 0, b: 1 }):
66 // static const bool flow = true;
67};
68
69/// This class is similar to MappingTraits<T> but allows you to pass in
70/// additional context for each map operation. For example:
71///
72/// struct MappingContextTraits<MyStruct, MyContext> {
73/// static void mapping(IO &io, MyStruct &s, MyContext &c) {
74/// io.mapRequired("name", s.name);
75/// io.mapRequired("size", s.size);
76/// io.mapOptional("age", s.age);
77/// ++c.TimesMapped;
78/// }
79/// };
80template <class T, class Context> struct MappingContextTraits {
81 // Must provide:
82 // static void mapping(IO &io, T &fields, Context &Ctx);
83 // Optionally may provide:
84 // static std::string validate(IO &io, T &fields, Context &Ctx);
85 //
86 // The optional flow flag will cause generated YAML to use a flow mapping
87 // (e.g. { a: 0, b: 1 }):
88 // static const bool flow = true;
89};
90
91/// This class should be specialized by any integral type that converts
92/// to/from a YAML scalar where there is a one-to-one mapping between
93/// in-memory values and a string in YAML. For example:
94///
95/// struct ScalarEnumerationTraits<Colors> {
96/// static void enumeration(IO &io, Colors &value) {
97/// io.enumCase(value, "red", cRed);
98/// io.enumCase(value, "blue", cBlue);
99/// io.enumCase(value, "green", cGreen);
100/// }
101/// };
102template <typename T, typename Enable = void> struct ScalarEnumerationTraits {
103 // Must provide:
104 // static void enumeration(IO &io, T &value);
105};
106
107/// This class should be specialized by any integer type that is a union
108/// of bit values and the YAML representation is a flow sequence of
109/// strings. For example:
110///
111/// struct ScalarBitSetTraits<MyFlags> {
112/// static void bitset(IO &io, MyFlags &value) {
113/// io.bitSetCase(value, "big", flagBig);
114/// io.bitSetCase(value, "flat", flagFlat);
115/// io.bitSetCase(value, "round", flagRound);
116/// }
117/// };
118template <typename T, typename Enable = void> struct ScalarBitSetTraits {
119 // Must provide:
120 // static void bitset(IO &io, T &value);
121};
122
123/// Describe which type of quotes should be used when quoting is necessary.
124/// Some non-printable characters need to be double-quoted, while some others
125/// are fine with simple-quoting, and some don't need any quoting.
126enum class QuotingType { None, Single, Double };
127
128/// This class should be specialized by type that requires custom conversion
129/// to/from a yaml scalar. For example:
130///
131/// template<>
132/// struct ScalarTraits<MyType> {
133/// static void output(const MyType &val, void*, llvm::raw_ostream &out) {
134/// // stream out custom formatting
135/// out << llvm::format("%x", val);
136/// }
137/// static StringRef input(StringRef scalar, void*, MyType &value) {
138/// // parse scalar and set `value`
139/// // return empty string on success, or error string
140/// return StringRef();
141/// }
142/// static QuotingType mustQuote(StringRef) { return QuotingType::Single; }
143/// };
144template <typename T, typename Enable = void> struct ScalarTraits {
145 // Must provide:
146 //
147 // Function to write the value as a string:
148 // static void output(const T &value, void *ctxt, llvm::raw_ostream &out);
149 //
150 // Function to convert a string to a value. Returns the empty
151 // StringRef on success or an error string if string is malformed:
152 // static StringRef input(StringRef scalar, void *ctxt, T &value);
153 //
154 // Function to determine if the value should be quoted.
155 // static QuotingType mustQuote(StringRef);
156};
157
158/// This class should be specialized by type that requires custom conversion
159/// to/from a YAML literal block scalar. For example:
160///
161/// template <>
162/// struct BlockScalarTraits<MyType> {
163/// static void output(const MyType &Value, void*, llvm::raw_ostream &Out)
164/// {
165/// // stream out custom formatting
166/// Out << Value;
167/// }
168/// static StringRef input(StringRef Scalar, void*, MyType &Value) {
169/// // parse scalar and set `value`
170/// // return empty string on success, or error string
171/// return StringRef();
172/// }
173/// };
174template <typename T>
175struct BlockScalarTraits {
176 // Must provide:
177 //
178 // Function to write the value as a string:
179 // static void output(const T &Value, void *ctx, llvm::raw_ostream &Out);
180 //
181 // Function to convert a string to a value. Returns the empty
182 // StringRef on success or an error string if string is malformed:
183 // static StringRef input(StringRef Scalar, void *ctxt, T &Value);
184 //
185 // Optional:
186 // static StringRef inputTag(T &Val, std::string Tag)
187 // static void outputTag(const T &Val, raw_ostream &Out)
188};
189
190/// This class should be specialized by type that requires custom conversion
191/// to/from a YAML scalar with optional tags. For example:
192///
193/// template <>
194/// struct TaggedScalarTraits<MyType> {
195/// static void output(const MyType &Value, void*, llvm::raw_ostream
196/// &ScalarOut, llvm::raw_ostream &TagOut)
197/// {
198/// // stream out custom formatting including optional Tag
199/// Out << Value;
200/// }
201/// static StringRef input(StringRef Scalar, StringRef Tag, void*, MyType
202/// &Value) {
203/// // parse scalar and set `value`
204/// // return empty string on success, or error string
205/// return StringRef();
206/// }
207/// static QuotingType mustQuote(const MyType &Value, StringRef) {
208/// return QuotingType::Single;
209/// }
210/// };
211template <typename T> struct TaggedScalarTraits {
212 // Must provide:
213 //
214 // Function to write the value and tag as strings:
215 // static void output(const T &Value, void *ctx, llvm::raw_ostream &ScalarOut,
216 // llvm::raw_ostream &TagOut);
217 //
218 // Function to convert a string to a value. Returns the empty
219 // StringRef on success or an error string if string is malformed:
220 // static StringRef input(StringRef Scalar, StringRef Tag, void *ctxt, T
221 // &Value);
222 //
223 // Function to determine if the value should be quoted.
224 // static QuotingType mustQuote(const T &Value, StringRef Scalar);
225};
226
227/// This class should be specialized by any type that needs to be converted
228/// to/from a YAML sequence. For example:
229///
230/// template<>
231/// struct SequenceTraits<MyContainer> {
232/// static size_t size(IO &io, MyContainer &seq) {
233/// return seq.size();
234/// }
235/// static MyType& element(IO &, MyContainer &seq, size_t index) {
236/// if ( index >= seq.size() )
237/// seq.resize(index+1);
238/// return seq[index];
239/// }
240/// };
241template<typename T, typename EnableIf = void>
242struct SequenceTraits {
243 // Must provide:
244 // static size_t size(IO &io, T &seq);
245 // static T::value_type& element(IO &io, T &seq, size_t index);
246 //
247 // The following is option and will cause generated YAML to use
248 // a flow sequence (e.g. [a,b,c]).
249 // static const bool flow = true;
250};
251
252/// This class should be specialized by any type for which vectors of that
253/// type need to be converted to/from a YAML sequence.
254template<typename T, typename EnableIf = void>
255struct SequenceElementTraits {
256 // Must provide:
257 // static const bool flow;
258};
259
260/// This class should be specialized by any type that needs to be converted
261/// to/from a list of YAML documents.
262template<typename T>
263struct DocumentListTraits {
264 // Must provide:
265 // static size_t size(IO &io, T &seq);
266 // static T::value_type& element(IO &io, T &seq, size_t index);
267};
268
269/// This class should be specialized by any type that needs to be converted
270/// to/from a YAML mapping in the case where the names of the keys are not known
271/// in advance, e.g. a string map.
272template <typename T>
273struct CustomMappingTraits {
274 // static void inputOne(IO &io, StringRef key, T &elem);
275 // static void output(IO &io, T &elem);
276};
277
278/// This class should be specialized by any type that can be represented as
279/// a scalar, map, or sequence, decided dynamically. For example:
280///
281/// typedef std::unique_ptr<MyBase> MyPoly;
282///
283/// template<>
284/// struct PolymorphicTraits<MyPoly> {
285/// static NodeKind getKind(const MyPoly &poly) {
286/// return poly->getKind();
287/// }
288/// static MyScalar& getAsScalar(MyPoly &poly) {
289/// if (!poly || !isa<MyScalar>(poly))
290/// poly.reset(new MyScalar());
291/// return *cast<MyScalar>(poly.get());
292/// }
293/// // ...
294/// };
295template <typename T> struct PolymorphicTraits {
296 // Must provide:
297 // static NodeKind getKind(const T &poly);
298 // static scalar_type &getAsScalar(T &poly);
299 // static map_type &getAsMap(T &poly);
300 // static sequence_type &getAsSequence(T &poly);
301};
302
303// Only used for better diagnostics of missing traits
304template <typename T>
305struct MissingTrait;
306
307// Test if ScalarEnumerationTraits<T> is defined on type T.
308template <class T>
309struct has_ScalarEnumerationTraits
310{
311 using Signature_enumeration = void (*)(class IO&, T&);
312
313 template <typename U>
314 static char test(SameType<Signature_enumeration, &U::enumeration>*);
315
316 template <typename U>
317 static double test(...);
318
319 static bool const value =
320 (sizeof(test<ScalarEnumerationTraits<T>>(nullptr)) == 1);
321};
322
323// Test if ScalarBitSetTraits<T> is defined on type T.
324template <class T>
325struct has_ScalarBitSetTraits
326{
327 using Signature_bitset = void (*)(class IO&, T&);
328
329 template <typename U>
330 static char test(SameType<Signature_bitset, &U::bitset>*);
331
332 template <typename U>
333 static double test(...);
334
335 static bool const value = (sizeof(test<ScalarBitSetTraits<T>>(nullptr)) == 1);
336};
337
338// Test if ScalarTraits<T> is defined on type T.
339template <class T>
340struct has_ScalarTraits
341{
342 using Signature_input = StringRef (*)(StringRef, void*, T&);
343 using Signature_output = void (*)(const T&, void*, raw_ostream&);
344 using Signature_mustQuote = QuotingType (*)(StringRef);
345
346 template <typename U>
347 static char test(SameType<Signature_input, &U::input> *,
348 SameType<Signature_output, &U::output> *,
349 SameType<Signature_mustQuote, &U::mustQuote> *);
350
351 template <typename U>
352 static double test(...);
353
354 static bool const value =
355 (sizeof(test<ScalarTraits<T>>(nullptr, nullptr, nullptr)) == 1);
356};
357
358// Test if BlockScalarTraits<T> is defined on type T.
359template <class T>
360struct has_BlockScalarTraits
361{
362 using Signature_input = StringRef (*)(StringRef, void *, T &);
363 using Signature_output = void (*)(const T &, void *, raw_ostream &);
364
365 template <typename U>
366 static char test(SameType<Signature_input, &U::input> *,
367 SameType<Signature_output, &U::output> *);
368
369 template <typename U>
370 static double test(...);
371
372 static bool const value =
373 (sizeof(test<BlockScalarTraits<T>>(nullptr, nullptr)) == 1);
374};
375
376// Test if TaggedScalarTraits<T> is defined on type T.
377template <class T> struct has_TaggedScalarTraits {
378 using Signature_input = StringRef (*)(StringRef, StringRef, void *, T &);
379 using Signature_output = void (*)(const T &, void *, raw_ostream &,
380 raw_ostream &);
381 using Signature_mustQuote = QuotingType (*)(const T &, StringRef);
382
383 template <typename U>
384 static char test(SameType<Signature_input, &U::input> *,
385 SameType<Signature_output, &U::output> *,
386 SameType<Signature_mustQuote, &U::mustQuote> *);
387
388 template <typename U> static double test(...);
389
390 static bool const value =
391 (sizeof(test<TaggedScalarTraits<T>>(nullptr, nullptr, nullptr)) == 1);
392};
393
394// Test if MappingContextTraits<T> is defined on type T.
395template <class T, class Context> struct has_MappingTraits {
396 using Signature_mapping = void (*)(class IO &, T &, Context &);
397
398 template <typename U>
399 static char test(SameType<Signature_mapping, &U::mapping>*);
400
401 template <typename U>
402 static double test(...);
403
404 static bool const value =
405 (sizeof(test<MappingContextTraits<T, Context>>(nullptr)) == 1);
406};
407
408// Test if MappingTraits<T> is defined on type T.
409template <class T> struct has_MappingTraits<T, EmptyContext> {
410 using Signature_mapping = void (*)(class IO &, T &);
411
412 template <typename U>
413 static char test(SameType<Signature_mapping, &U::mapping> *);
414
415 template <typename U> static double test(...);
416
417 static bool const value = (sizeof(test<MappingTraits<T>>(nullptr)) == 1);
418};
419
420// Test if MappingContextTraits<T>::validate() is defined on type T.
421template <class T, class Context> struct has_MappingValidateTraits {
422 using Signature_validate = std::string (*)(class IO &, T &, Context &);
423
424 template <typename U>
425 static char test(SameType<Signature_validate, &U::validate>*);
426
427 template <typename U>
428 static double test(...);
429
430 static bool const value =
431 (sizeof(test<MappingContextTraits<T, Context>>(nullptr)) == 1);
432};
433
434// Test if MappingTraits<T>::validate() is defined on type T.
435template <class T> struct has_MappingValidateTraits<T, EmptyContext> {
436 using Signature_validate = std::string (*)(class IO &, T &);
437
438 template <typename U>
439 static char test(SameType<Signature_validate, &U::validate> *);
440
441 template <typename U> static double test(...);
442
443 static bool const value = (sizeof(test<MappingTraits<T>>(nullptr)) == 1);
444};
445
446// Test if SequenceTraits<T> is defined on type T.
447template <class T>
448struct has_SequenceMethodTraits
449{
450 using Signature_size = size_t (*)(class IO&, T&);
451
452 template <typename U>
453 static char test(SameType<Signature_size, &U::size>*);
454
455 template <typename U>
456 static double test(...);
457
458 static bool const value = (sizeof(test<SequenceTraits<T>>(nullptr)) == 1);
459};
460
461// Test if CustomMappingTraits<T> is defined on type T.
462template <class T>
463struct has_CustomMappingTraits
464{
465 using Signature_input = void (*)(IO &io, StringRef key, T &v);
466
467 template <typename U>
468 static char test(SameType<Signature_input, &U::inputOne>*);
469
470 template <typename U>
471 static double test(...);
472
473 static bool const value =
474 (sizeof(test<CustomMappingTraits<T>>(nullptr)) == 1);
475};
476
477// has_FlowTraits<int> will cause an error with some compilers because
478// it subclasses int. Using this wrapper only instantiates the
479// real has_FlowTraits only if the template type is a class.
480template <typename T, bool Enabled = std::is_class<T>::value>
481class has_FlowTraits
482{
483public:
484 static const bool value = false;
485};
486
487// Some older gcc compilers don't support straight forward tests
488// for members, so test for ambiguity cause by the base and derived
489// classes both defining the member.
490template <class T>
491struct has_FlowTraits<T, true>
492{
493 struct Fallback { bool flow; };
494 struct Derived : T, Fallback { };
495
496 template<typename C>
497 static char (&f(SameType<bool Fallback::*, &C::flow>*))[1];
498
499 template<typename C>
500 static char (&f(...))[2];
501
502 static bool const value = sizeof(f<Derived>(nullptr)) == 2;
503};
504
505// Test if SequenceTraits<T> is defined on type T
506template<typename T>
507struct has_SequenceTraits : public std::integral_constant<bool,
508 has_SequenceMethodTraits<T>::value > { };
509
510// Test if DocumentListTraits<T> is defined on type T
511template <class T>
512struct has_DocumentListTraits
513{
514 using Signature_size = size_t (*)(class IO &, T &);
515
516 template <typename U>
517 static char test(SameType<Signature_size, &U::size>*);
518
519 template <typename U>
520 static double test(...);
521
522 static bool const value = (sizeof(test<DocumentListTraits<T>>(nullptr))==1);
523};
524
525template <class T> struct has_PolymorphicTraits {
526 using Signature_getKind = NodeKind (*)(const T &);
527
528 template <typename U>
529 static char test(SameType<Signature_getKind, &U::getKind> *);
530
531 template <typename U> static double test(...);
532
533 static bool const value = (sizeof(test<PolymorphicTraits<T>>(nullptr)) == 1);
534};
535
536inline bool isNumeric(StringRef S) {
537 const static auto skipDigits = [](StringRef Input) {
538 return Input.drop_front(
539 std::min(Input.find_first_not_of("0123456789"), Input.size()));
540 };
541
542 // Make S.front() and S.drop_front().front() (if S.front() is [+-]) calls
543 // safe.
544 if (S.empty() || S.equals("+") || S.equals("-"))
545 return false;
546
547 if (S.equals(".nan") || S.equals(".NaN") || S.equals(".NAN"))
548 return true;
549
550 // Infinity and decimal numbers can be prefixed with sign.
551 StringRef Tail = (S.front() == '-' || S.front() == '+') ? S.drop_front() : S;
552
553 // Check for infinity first, because checking for hex and oct numbers is more
554 // expensive.
555 if (Tail.equals(".inf") || Tail.equals(".Inf") || Tail.equals(".INF"))
556 return true;
557
558 // Section 10.3.2 Tag Resolution
559 // YAML 1.2 Specification prohibits Base 8 and Base 16 numbers prefixed with
560 // [-+], so S should be used instead of Tail.
561 if (S.startswith("0o"))
562 return S.size() > 2 &&
563 S.drop_front(2).find_first_not_of("01234567") == StringRef::npos;
564
565 if (S.startswith("0x"))
566 return S.size() > 2 && S.drop_front(2).find_first_not_of(
567 "0123456789abcdefABCDEF") == StringRef::npos;
568
569 // Parse float: [-+]? (\. [0-9]+ | [0-9]+ (\. [0-9]* )?) ([eE] [-+]? [0-9]+)?
570 S = Tail;
571
572 // Handle cases when the number starts with '.' and hence needs at least one
573 // digit after dot (as opposed by number which has digits before the dot), but
574 // doesn't have one.
575 if (S.startswith(".") &&
576 (S.equals(".") ||
577 (S.size() > 1 && std::strchr("0123456789", S[1]) == nullptr)))
578 return false;
579
580 if (S.startswith("E") || S.startswith("e"))
581 return false;
582
583 enum ParseState {
584 Default,
585 FoundDot,
586 FoundExponent,
587 };
588 ParseState State = Default;
589
590 S = skipDigits(S);
591
592 // Accept decimal integer.
593 if (S.empty())
594 return true;
595
596 if (S.front() == '.') {
597 State = FoundDot;
598 S = S.drop_front();
599 } else if (S.front() == 'e' || S.front() == 'E') {
600 State = FoundExponent;
601 S = S.drop_front();
602 } else {
603 return false;
604 }
605
606 if (State == FoundDot) {
607 S = skipDigits(S);
608 if (S.empty())
609 return true;
610
611 if (S.front() == 'e' || S.front() == 'E') {
612 State = FoundExponent;
613 S = S.drop_front();
614 } else {
615 return false;
616 }
617 }
618
619 assert(State == FoundExponent && "Should have found exponent at this point.")(static_cast <bool> (State == FoundExponent && "Should have found exponent at this point."
) ? void (0) : __assert_fail ("State == FoundExponent && \"Should have found exponent at this point.\""
, "llvm/include/llvm/Support/YAMLTraits.h", 619, __extension__
__PRETTY_FUNCTION__))
;
620 if (S.empty())
621 return false;
622
623 if (S.front() == '+' || S.front() == '-') {
624 S = S.drop_front();
625 if (S.empty())
626 return false;
627 }
628
629 return skipDigits(S).empty();
630}
631
632inline bool isNull(StringRef S) {
633 return S.equals("null") || S.equals("Null") || S.equals("NULL") ||
634 S.equals("~");
635}
636
637inline bool isBool(StringRef S) {
638 // FIXME: using parseBool is causing multiple tests to fail.
639 return S.equals("true") || S.equals("True") || S.equals("TRUE") ||
640 S.equals("false") || S.equals("False") || S.equals("FALSE");
641}
642
643// 5.1. Character Set
644// The allowed character range explicitly excludes the C0 control block #x0-#x1F
645// (except for TAB #x9, LF #xA, and CR #xD which are allowed), DEL #x7F, the C1
646// control block #x80-#x9F (except for NEL #x85 which is allowed), the surrogate
647// block #xD800-#xDFFF, #xFFFE, and #xFFFF.
648inline QuotingType needsQuotes(StringRef S) {
649 if (S.empty())
650 return QuotingType::Single;
651
652 QuotingType MaxQuotingNeeded = QuotingType::None;
653 if (isSpace(static_cast<unsigned char>(S.front())) ||
654 isSpace(static_cast<unsigned char>(S.back())))
655 MaxQuotingNeeded = QuotingType::Single;
656 if (isNull(S))
657 MaxQuotingNeeded = QuotingType::Single;
658 if (isBool(S))
659 MaxQuotingNeeded = QuotingType::Single;
660 if (isNumeric(S))
661 MaxQuotingNeeded = QuotingType::Single;
662
663 // 7.3.3 Plain Style
664 // Plain scalars must not begin with most indicators, as this would cause
665 // ambiguity with other YAML constructs.
666 static constexpr char Indicators[] = R"(-?:\,[]{}#&*!|>'"%@`)";
667 if (S.find_first_of(Indicators) == 0)
668 MaxQuotingNeeded = QuotingType::Single;
669
670 for (unsigned char C : S) {
671 // Alphanum is safe.
672 if (isAlnum(C))
673 continue;
674
675 switch (C) {
676 // Safe scalar characters.
677 case '_':
678 case '-':
679 case '^':
680 case '.':
681 case ',':
682 case ' ':
683 // TAB (0x9) is allowed in unquoted strings.
684 case 0x9:
685 continue;
686 // LF(0xA) and CR(0xD) may delimit values and so require at least single
687 // quotes. LLVM YAML parser cannot handle single quoted multiline so use
688 // double quoting to produce valid YAML.
689 case 0xA:
690 case 0xD:
691 return QuotingType::Double;
692 // DEL (0x7F) are excluded from the allowed character range.
693 case 0x7F:
694 return QuotingType::Double;
695 // Forward slash is allowed to be unquoted, but we quote it anyway. We have
696 // many tests that use FileCheck against YAML output, and this output often
697 // contains paths. If we quote backslashes but not forward slashes then
698 // paths will come out either quoted or unquoted depending on which platform
699 // the test is run on, making FileCheck comparisons difficult.
700 case '/':
701 default: {
702 // C0 control block (0x0 - 0x1F) is excluded from the allowed character
703 // range.
704 if (C <= 0x1F)
705 return QuotingType::Double;
706
707 // Always double quote UTF-8.
708 if ((C & 0x80) != 0)
709 return QuotingType::Double;
710
711 // The character is not safe, at least simple quoting needed.
712 MaxQuotingNeeded = QuotingType::Single;
713 }
714 }
715 }
716
717 return MaxQuotingNeeded;
718}
719
720template <typename T, typename Context>
721struct missingTraits
722 : public std::integral_constant<bool,
723 !has_ScalarEnumerationTraits<T>::value &&
724 !has_ScalarBitSetTraits<T>::value &&
725 !has_ScalarTraits<T>::value &&
726 !has_BlockScalarTraits<T>::value &&
727 !has_TaggedScalarTraits<T>::value &&
728 !has_MappingTraits<T, Context>::value &&
729 !has_SequenceTraits<T>::value &&
730 !has_CustomMappingTraits<T>::value &&
731 !has_DocumentListTraits<T>::value &&
732 !has_PolymorphicTraits<T>::value> {};
733
734template <typename T, typename Context>
735struct validatedMappingTraits
736 : public std::integral_constant<
737 bool, has_MappingTraits<T, Context>::value &&
738 has_MappingValidateTraits<T, Context>::value> {};
739
740template <typename T, typename Context>
741struct unvalidatedMappingTraits
742 : public std::integral_constant<
743 bool, has_MappingTraits<T, Context>::value &&
744 !has_MappingValidateTraits<T, Context>::value> {};
745
746// Base class for Input and Output.
747class IO {
748public:
749 IO(void *Ctxt = nullptr);
750 virtual ~IO();
751
752 virtual bool outputting() const = 0;
753
754 virtual unsigned beginSequence() = 0;
755 virtual bool preflightElement(unsigned, void *&) = 0;
756 virtual void postflightElement(void*) = 0;
757 virtual void endSequence() = 0;
758 virtual bool canElideEmptySequence() = 0;
759
760 virtual unsigned beginFlowSequence() = 0;
761 virtual bool preflightFlowElement(unsigned, void *&) = 0;
762 virtual void postflightFlowElement(void*) = 0;
763 virtual void endFlowSequence() = 0;
764
765 virtual bool mapTag(StringRef Tag, bool Default=false) = 0;
766 virtual void beginMapping() = 0;
767 virtual void endMapping() = 0;
768 virtual bool preflightKey(const char*, bool, bool, bool &, void *&) = 0;
769 virtual void postflightKey(void*) = 0;
770 virtual std::vector<StringRef> keys() = 0;
771
772 virtual void beginFlowMapping() = 0;
773 virtual void endFlowMapping() = 0;
774
775 virtual void beginEnumScalar() = 0;
776 virtual bool matchEnumScalar(const char*, bool) = 0;
777 virtual bool matchEnumFallback() = 0;
778 virtual void endEnumScalar() = 0;
779
780 virtual bool beginBitSetScalar(bool &) = 0;
781 virtual bool bitSetMatch(const char*, bool) = 0;
782 virtual void endBitSetScalar() = 0;
783
784 virtual void scalarString(StringRef &, QuotingType) = 0;
785 virtual void blockScalarString(StringRef &) = 0;
786 virtual void scalarTag(std::string &) = 0;
787
788 virtual NodeKind getNodeKind() = 0;
789
790 virtual void setError(const Twine &) = 0;
791 virtual void setAllowUnknownKeys(bool Allow);
792
793 template <typename T>
794 void enumCase(T &Val, const char* Str, const T ConstVal) {
795 if ( matchEnumScalar(Str, outputting() && Val == ConstVal) ) {
796 Val = ConstVal;
797 }
798 }
799
800 // allow anonymous enum values to be used with LLVM_YAML_STRONG_TYPEDEF
801 template <typename T>
802 void enumCase(T &Val, const char* Str, const uint32_t ConstVal) {
803 if ( matchEnumScalar(Str, outputting() && Val == static_cast<T>(ConstVal)) ) {
804 Val = ConstVal;
805 }
806 }
807
808 template <typename FBT, typename T>
809 void enumFallback(T &Val) {
810 if (matchEnumFallback()) {
811 EmptyContext Context;
812 // FIXME: Force integral conversion to allow strong typedefs to convert.
813 FBT Res = static_cast<typename FBT::BaseType>(Val);
814 yamlize(*this, Res, true, Context);
815 Val = static_cast<T>(static_cast<typename FBT::BaseType>(Res));
816 }
817 }
818
819 template <typename T>
820 void bitSetCase(T &Val, const char* Str, const T ConstVal) {
821 if ( bitSetMatch(Str, outputting() && (Val & ConstVal) == ConstVal) ) {
822 Val = static_cast<T>(Val | ConstVal);
823 }
824 }
825
826 // allow anonymous enum values to be used with LLVM_YAML_STRONG_TYPEDEF
827 template <typename T>
828 void bitSetCase(T &Val, const char* Str, const uint32_t ConstVal) {
829 if ( bitSetMatch(Str, outputting() && (Val & ConstVal) == ConstVal) ) {
830 Val = static_cast<T>(Val | ConstVal);
831 }
832 }
833
834 template <typename T>
835 void maskedBitSetCase(T &Val, const char *Str, T ConstVal, T Mask) {
836 if (bitSetMatch(Str, outputting() && (Val & Mask) == ConstVal))
837 Val = Val | ConstVal;
838 }
839
840 template <typename T>
841 void maskedBitSetCase(T &Val, const char *Str, uint32_t ConstVal,
842 uint32_t Mask) {
843 if (bitSetMatch(Str, outputting() && (Val & Mask) == ConstVal))
844 Val = Val | ConstVal;
845 }
846
847 void *getContext() const;
848 void setContext(void *);
849
850 template <typename T> void mapRequired(const char *Key, T &Val) {
851 EmptyContext Ctx;
852 this->processKey(Key, Val, true, Ctx);
9
Calling 'IO::processKey'
26
Calling 'IO::processKey'
30
Returning from 'IO::processKey'
853 }
31
Returning without writing to 'Val.Line'
854
855 template <typename T, typename Context>
856 void mapRequired(const char *Key, T &Val, Context &Ctx) {
857 this->processKey(Key, Val, true, Ctx);
858 }
859
860 template <typename T> void mapOptional(const char *Key, T &Val) {
861 EmptyContext Ctx;
862 mapOptionalWithContext(Key, Val, Ctx);
863 }
864
865 template <typename T, typename DefaultT>
866 void mapOptional(const char *Key, T &Val, const DefaultT &Default) {
867 EmptyContext Ctx;
868 mapOptionalWithContext(Key, Val, Default, Ctx);
869 }
870
871 template <typename T, typename Context>
872 std::enable_if_t<has_SequenceTraits<T>::value, void>
873 mapOptionalWithContext(const char *Key, T &Val, Context &Ctx) {
874 // omit key/value instead of outputting empty sequence
875 if (this->canElideEmptySequence() && !(Val.begin() != Val.end()))
876 return;
877 this->processKey(Key, Val, false, Ctx);
878 }
879
880 template <typename T, typename Context>
881 void mapOptionalWithContext(const char *Key, Optional<T> &Val, Context &Ctx) {
882 this->processKeyWithDefault(Key, Val, Optional<T>(), /*Required=*/false,
883 Ctx);
884 }
885
886 template <typename T, typename Context>
887 std::enable_if_t<!has_SequenceTraits<T>::value, void>
888 mapOptionalWithContext(const char *Key, T &Val, Context &Ctx) {
889 this->processKey(Key, Val, false, Ctx);
890 }
891
892 template <typename T, typename Context, typename DefaultT>
893 void mapOptionalWithContext(const char *Key, T &Val, const DefaultT &Default,
894 Context &Ctx) {
895 static_assert(std::is_convertible<DefaultT, T>::value,
896 "Default type must be implicitly convertible to value type!");
897 this->processKeyWithDefault(Key, Val, static_cast<const T &>(Default),
898 false, Ctx);
899 }
900
901private:
902 template <typename T, typename Context>
903 void processKeyWithDefault(const char *Key, Optional<T> &Val,
904 const Optional<T> &DefaultValue, bool Required,
905 Context &Ctx);
906
907 template <typename T, typename Context>
908 void processKeyWithDefault(const char *Key, T &Val, const T &DefaultValue,
909 bool Required, Context &Ctx) {
910 void *SaveInfo;
911 bool UseDefault;
912 const bool sameAsDefault = outputting() && Val == DefaultValue;
913 if ( this->preflightKey(Key, Required, sameAsDefault, UseDefault,
914 SaveInfo) ) {
915 yamlize(*this, Val, Required, Ctx);
916 this->postflightKey(SaveInfo);
917 }
918 else {
919 if ( UseDefault )
920 Val = DefaultValue;
921 }
922 }
923
924 template <typename T, typename Context>
925 void processKey(const char *Key, T &Val, bool Required, Context &Ctx) {
926 void *SaveInfo;
927 bool UseDefault;
928 if ( this->preflightKey(Key, Required, false, UseDefault, SaveInfo) ) {
10
Assuming the condition is true
11
Taking true branch
27
Assuming the condition is false
28
Taking false branch
929 yamlize(*this, Val, Required, Ctx);
12
Calling 'yamlize<clang::clangd::SymbolLocation, llvm::yaml::EmptyContext>'
930 this->postflightKey(SaveInfo);
931 }
932 }
29
Returning without writing to 'Val.Line'
933
934private:
935 void *Ctxt;
936};
937
938namespace detail {
939
940template <typename T, typename Context>
941void doMapping(IO &io, T &Val, Context &Ctx) {
942 MappingContextTraits<T, Context>::mapping(io, Val, Ctx);
943}
944
945template <typename T> void doMapping(IO &io, T &Val, EmptyContext &Ctx) {
946 MappingTraits<T>::mapping(io, Val);
7
Calling 'MappingTraits::mapping'
15
Calling 'MappingTraits::mapping'
947}
948
949} // end namespace detail
950
951template <typename T>
952std::enable_if_t<has_ScalarEnumerationTraits<T>::value, void>
953yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) {
954 io.beginEnumScalar();
955 ScalarEnumerationTraits<T>::enumeration(io, Val);
956 io.endEnumScalar();
957}
958
959template <typename T>
960std::enable_if_t<has_ScalarBitSetTraits<T>::value, void>
961yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) {
962 bool DoClear;
963 if ( io.beginBitSetScalar(DoClear) ) {
964 if ( DoClear )
965 Val = T();
966 ScalarBitSetTraits<T>::bitset(io, Val);
967 io.endBitSetScalar();
968 }
969}
970
971template <typename T>
972std::enable_if_t<has_ScalarTraits<T>::value, void> yamlize(IO &io, T &Val, bool,
973 EmptyContext &Ctx) {
974 if ( io.outputting() ) {
975 std::string Storage;
976 raw_string_ostream Buffer(Storage);
977 ScalarTraits<T>::output(Val, io.getContext(), Buffer);
978 StringRef Str = Buffer.str();
979 io.scalarString(Str, ScalarTraits<T>::mustQuote(Str));
980 }
981 else {
982 StringRef Str;
983 io.scalarString(Str, ScalarTraits<T>::mustQuote(Str));
984 StringRef Result = ScalarTraits<T>::input(Str, io.getContext(), Val);
985 if ( !Result.empty() ) {
986 io.setError(Twine(Result));
987 }
988 }
989}
990
991template <typename T>
992std::enable_if_t<has_BlockScalarTraits<T>::value, void>
993yamlize(IO &YamlIO, T &Val, bool, EmptyContext &Ctx) {
994 if (YamlIO.outputting()) {
995 std::string Storage;
996 raw_string_ostream Buffer(Storage);
997 BlockScalarTraits<T>::output(Val, YamlIO.getContext(), Buffer);
998 StringRef Str = Buffer.str();
999 YamlIO.blockScalarString(Str);
1000 } else {
1001 StringRef Str;
1002 YamlIO.blockScalarString(Str);
1003 StringRef Result =
1004 BlockScalarTraits<T>::input(Str, YamlIO.getContext(), Val);
1005 if (!Result.empty())
1006 YamlIO.setError(Twine(Result));
1007 }
1008}
1009
1010template <typename T>
1011std::enable_if_t<has_TaggedScalarTraits<T>::value, void>
1012yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) {
1013 if (io.outputting()) {
1014 std::string ScalarStorage, TagStorage;
1015 raw_string_ostream ScalarBuffer(ScalarStorage), TagBuffer(TagStorage);
1016 TaggedScalarTraits<T>::output(Val, io.getContext(), ScalarBuffer,
1017 TagBuffer);
1018 io.scalarTag(TagBuffer.str());
1019 StringRef ScalarStr = ScalarBuffer.str();
1020 io.scalarString(ScalarStr,
1021 TaggedScalarTraits<T>::mustQuote(Val, ScalarStr));
1022 } else {
1023 std::string Tag;
1024 io.scalarTag(Tag);
1025 StringRef Str;
1026 io.scalarString(Str, QuotingType::None);
1027 StringRef Result =
1028 TaggedScalarTraits<T>::input(Str, Tag, io.getContext(), Val);
1029 if (!Result.empty()) {
1030 io.setError(Twine(Result));
1031 }
1032 }
1033}
1034
1035template <typename T, typename Context>
1036std::enable_if_t<validatedMappingTraits<T, Context>::value, void>
1037yamlize(IO &io, T &Val, bool, Context &Ctx) {
1038 if (has_FlowTraits<MappingTraits<T>>::value)
1039 io.beginFlowMapping();
1040 else
1041 io.beginMapping();
1042 if (io.outputting()) {
1043 std::string Err = MappingTraits<T>::validate(io, Val);
1044 if (!Err.empty()) {
1045 errs() << Err << "\n";
1046 assert(Err.empty() && "invalid struct trying to be written as yaml")(static_cast <bool> (Err.empty() && "invalid struct trying to be written as yaml"
) ? void (0) : __assert_fail ("Err.empty() && \"invalid struct trying to be written as yaml\""
, "llvm/include/llvm/Support/YAMLTraits.h", 1046, __extension__
__PRETTY_FUNCTION__))
;
1047 }
1048 }
1049 detail::doMapping(io, Val, Ctx);
1050 if (!io.outputting()) {
1051 std::string Err = MappingTraits<T>::validate(io, Val);
1052 if (!Err.empty())
1053 io.setError(Err);
1054 }
1055 if (has_FlowTraits<MappingTraits<T>>::value)
1056 io.endFlowMapping();
1057 else
1058 io.endMapping();
1059}
1060
1061template <typename T, typename Context>
1062std::enable_if_t<unvalidatedMappingTraits<T, Context>::value, void>
1063yamlize(IO &io, T &Val, bool, Context &Ctx) {
1064 if (has_FlowTraits<MappingTraits<T>>::value
4.1
'value' is false
12.1
'value' is false
4.1
'value' is false
12.1
'value' is false
4.1
'value' is false
12.1
'value' is false
) {
5
Taking false branch
13
Taking false branch
1065 io.beginFlowMapping();
1066 detail::doMapping(io, Val, Ctx);
1067 io.endFlowMapping();
1068 } else {
1069 io.beginMapping();
1070 detail::doMapping(io, Val, Ctx);
6
Calling 'doMapping<clang::clangd::Ref>'
14
Calling 'doMapping<clang::clangd::SymbolLocation>'
1071 io.endMapping();
1072 }
1073}
1074
1075template <typename T>
1076std::enable_if_t<has_CustomMappingTraits<T>::value, void>
1077yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) {
1078 if ( io.outputting() ) {
1079 io.beginMapping();
1080 CustomMappingTraits<T>::output(io, Val);
1081 io.endMapping();
1082 } else {
1083 io.beginMapping();
1084 for (StringRef key : io.keys())
1085 CustomMappingTraits<T>::inputOne(io, key, Val);
1086 io.endMapping();
1087 }
1088}
1089
1090template <typename T>
1091std::enable_if_t<has_PolymorphicTraits<T>::value, void>
1092yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) {
1093 switch (io.outputting() ? PolymorphicTraits<T>::getKind(Val)
1094 : io.getNodeKind()) {
1095 case NodeKind::Scalar:
1096 return yamlize(io, PolymorphicTraits<T>::getAsScalar(Val), true, Ctx);
1097 case NodeKind::Map:
1098 return yamlize(io, PolymorphicTraits<T>::getAsMap(Val), true, Ctx);
1099 case NodeKind::Sequence:
1100 return yamlize(io, PolymorphicTraits<T>::getAsSequence(Val), true, Ctx);
1101 }
1102}
1103
1104template <typename T>
1105std::enable_if_t<missingTraits<T, EmptyContext>::value, void>
1106yamlize(IO &io, T &Val, bool, EmptyContext &Ctx) {
1107 char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
1108}
1109
1110template <typename T, typename Context>
1111std::enable_if_t<has_SequenceTraits<T>::value, void>
1112yamlize(IO &io, T &Seq, bool, Context &Ctx) {
1113 if ( has_FlowTraits< SequenceTraits<T>>::value ) {
1114 unsigned incnt = io.beginFlowSequence();
1115 unsigned count = io.outputting() ? SequenceTraits<T>::size(io, Seq) : incnt;
1116 for(unsigned i=0; i < count; ++i) {
1117 void *SaveInfo;
1118 if ( io.preflightFlowElement(i, SaveInfo) ) {
1119 yamlize(io, SequenceTraits<T>::element(io, Seq, i), true, Ctx);
1120 io.postflightFlowElement(SaveInfo);
1121 }
1122 }
1123 io.endFlowSequence();
1124 }
1125 else {
1126 unsigned incnt = io.beginSequence();
1127 unsigned count = io.outputting() ? SequenceTraits<T>::size(io, Seq) : incnt;
1128 for(unsigned i=0; i < count; ++i) {
1129 void *SaveInfo;
1130 if ( io.preflightElement(i, SaveInfo) ) {
1131 yamlize(io, SequenceTraits<T>::element(io, Seq, i), true, Ctx);
1132 io.postflightElement(SaveInfo);
1133 }
1134 }
1135 io.endSequence();
1136 }
1137}
1138
1139template<>
1140struct ScalarTraits<bool> {
1141 static void output(const bool &, void* , raw_ostream &);
1142 static StringRef input(StringRef, void *, bool &);
1143 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1144};
1145
1146template<>
1147struct ScalarTraits<StringRef> {
1148 static void output(const StringRef &, void *, raw_ostream &);
1149 static StringRef input(StringRef, void *, StringRef &);
1150 static QuotingType mustQuote(StringRef S) { return needsQuotes(S); }
1151};
1152
1153template<>
1154struct ScalarTraits<std::string> {
1155 static void output(const std::string &, void *, raw_ostream &);
1156 static StringRef input(StringRef, void *, std::string &);
1157 static QuotingType mustQuote(StringRef S) { return needsQuotes(S); }
1158};
1159
1160template<>
1161struct ScalarTraits<uint8_t> {
1162 static void output(const uint8_t &, void *, raw_ostream &);
1163 static StringRef input(StringRef, void *, uint8_t &);
1164 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1165};
1166
1167template<>
1168struct ScalarTraits<uint16_t> {
1169 static void output(const uint16_t &, void *, raw_ostream &);
1170 static StringRef input(StringRef, void *, uint16_t &);
1171 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1172};
1173
1174template<>
1175struct ScalarTraits<uint32_t> {
1176 static void output(const uint32_t &, void *, raw_ostream &);
1177 static StringRef input(StringRef, void *, uint32_t &);
1178 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1179};
1180
1181template<>
1182struct ScalarTraits<uint64_t> {
1183 static void output(const uint64_t &, void *, raw_ostream &);
1184 static StringRef input(StringRef, void *, uint64_t &);
1185 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1186};
1187
1188template<>
1189struct ScalarTraits<int8_t> {
1190 static void output(const int8_t &, void *, raw_ostream &);
1191 static StringRef input(StringRef, void *, int8_t &);
1192 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1193};
1194
1195template<>
1196struct ScalarTraits<int16_t> {
1197 static void output(const int16_t &, void *, raw_ostream &);
1198 static StringRef input(StringRef, void *, int16_t &);
1199 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1200};
1201
1202template<>
1203struct ScalarTraits<int32_t> {
1204 static void output(const int32_t &, void *, raw_ostream &);
1205 static StringRef input(StringRef, void *, int32_t &);
1206 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1207};
1208
1209template<>
1210struct ScalarTraits<int64_t> {
1211 static void output(const int64_t &, void *, raw_ostream &);
1212 static StringRef input(StringRef, void *, int64_t &);
1213 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1214};
1215
1216template<>
1217struct ScalarTraits<float> {
1218 static void output(const float &, void *, raw_ostream &);
1219 static StringRef input(StringRef, void *, float &);
1220 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1221};
1222
1223template<>
1224struct ScalarTraits<double> {
1225 static void output(const double &, void *, raw_ostream &);
1226 static StringRef input(StringRef, void *, double &);
1227 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1228};
1229
1230// For endian types, we use existing scalar Traits class for the underlying
1231// type. This way endian aware types are supported whenever the traits are
1232// defined for the underlying type.
1233template <typename value_type, support::endianness endian, size_t alignment>
1234struct ScalarTraits<support::detail::packed_endian_specific_integral<
1235 value_type, endian, alignment>,
1236 std::enable_if_t<has_ScalarTraits<value_type>::value>> {
1237 using endian_type =
1238 support::detail::packed_endian_specific_integral<value_type, endian,
1239 alignment>;
1240
1241 static void output(const endian_type &E, void *Ctx, raw_ostream &Stream) {
1242 ScalarTraits<value_type>::output(static_cast<value_type>(E), Ctx, Stream);
1243 }
1244
1245 static StringRef input(StringRef Str, void *Ctx, endian_type &E) {
1246 value_type V;
1247 auto R = ScalarTraits<value_type>::input(Str, Ctx, V);
1248 E = static_cast<endian_type>(V);
1249 return R;
1250 }
1251
1252 static QuotingType mustQuote(StringRef Str) {
1253 return ScalarTraits<value_type>::mustQuote(Str);
1254 }
1255};
1256
1257template <typename value_type, support::endianness endian, size_t alignment>
1258struct ScalarEnumerationTraits<
1259 support::detail::packed_endian_specific_integral<value_type, endian,
1260 alignment>,
1261 std::enable_if_t<has_ScalarEnumerationTraits<value_type>::value>> {
1262 using endian_type =
1263 support::detail::packed_endian_specific_integral<value_type, endian,
1264 alignment>;
1265
1266 static void enumeration(IO &io, endian_type &E) {
1267 value_type V = E;
1268 ScalarEnumerationTraits<value_type>::enumeration(io, V);
1269 E = V;
1270 }
1271};
1272
1273template <typename value_type, support::endianness endian, size_t alignment>
1274struct ScalarBitSetTraits<
1275 support::detail::packed_endian_specific_integral<value_type, endian,
1276 alignment>,
1277 std::enable_if_t<has_ScalarBitSetTraits<value_type>::value>> {
1278 using endian_type =
1279 support::detail::packed_endian_specific_integral<value_type, endian,
1280 alignment>;
1281 static void bitset(IO &io, endian_type &E) {
1282 value_type V = E;
1283 ScalarBitSetTraits<value_type>::bitset(io, V);
1284 E = V;
1285 }
1286};
1287
1288// Utility for use within MappingTraits<>::mapping() method
1289// to [de]normalize an object for use with YAML conversion.
1290template <typename TNorm, typename TFinal>
1291struct MappingNormalization {
1292 MappingNormalization(IO &i_o, TFinal &Obj)
1293 : io(i_o), BufPtr(nullptr), Result(Obj) {
1294 if ( io.outputting() ) {
17
Assuming the condition is false
18
Taking false branch
1295 BufPtr = new (&Buffer) TNorm(io, Obj);
1296 }
1297 else {
1298 BufPtr = new (&Buffer) TNorm(io);
19
Calling 'operator new'
20
Returning from 'operator new'
21
Calling constructor for 'NormalizedPosition'
23
Returning from constructor for 'NormalizedPosition'
1299 }
1300 }
1301
1302 ~MappingNormalization() {
1303 if ( ! io.outputting() ) {
34
Assuming the condition is true
35
Taking true branch
1304 Result = BufPtr->denormalize(io);
36
Calling 'NormalizedPosition::denormalize'
1305 }
1306 BufPtr->~TNorm();
1307 }
1308
1309 TNorm* operator->() { return BufPtr; }
1310
1311private:
1312 using Storage = AlignedCharArrayUnion<TNorm>;
1313
1314 Storage Buffer;
1315 IO &io;
1316 TNorm *BufPtr;
1317 TFinal &Result;
1318};
1319
1320// Utility for use within MappingTraits<>::mapping() method
1321// to [de]normalize an object for use with YAML conversion.
1322template <typename TNorm, typename TFinal>
1323struct MappingNormalizationHeap {
1324 MappingNormalizationHeap(IO &i_o, TFinal &Obj, BumpPtrAllocator *allocator)
1325 : io(i_o), Result(Obj) {
1326 if ( io.outputting() ) {
1327 BufPtr = new (&Buffer) TNorm(io, Obj);
1328 }
1329 else if (allocator) {
1330 BufPtr = allocator->Allocate<TNorm>();
1331 new (BufPtr) TNorm(io);
1332 } else {
1333 BufPtr = new TNorm(io);
1334 }
1335 }
1336
1337 ~MappingNormalizationHeap() {
1338 if ( io.outputting() ) {
1339 BufPtr->~TNorm();
1340 }
1341 else {
1342 Result = BufPtr->denormalize(io);
1343 }
1344 }
1345
1346 TNorm* operator->() { return BufPtr; }
1347
1348private:
1349 using Storage = AlignedCharArrayUnion<TNorm>;
1350
1351 Storage Buffer;
1352 IO &io;
1353 TNorm *BufPtr = nullptr;
1354 TFinal &Result;
1355};
1356
1357///
1358/// The Input class is used to parse a yaml document into in-memory structs
1359/// and vectors.
1360///
1361/// It works by using YAMLParser to do a syntax parse of the entire yaml
1362/// document, then the Input class builds a graph of HNodes which wraps
1363/// each yaml Node. The extra layer is buffering. The low level yaml
1364/// parser only lets you look at each node once. The buffering layer lets
1365/// you search and interate multiple times. This is necessary because
1366/// the mapRequired() method calls may not be in the same order
1367/// as the keys in the document.
1368///
1369class Input : public IO {
1370public:
1371 // Construct a yaml Input object from a StringRef and optional
1372 // user-data. The DiagHandler can be specified to provide
1373 // alternative error reporting.
1374 Input(StringRef InputContent,
1375 void *Ctxt = nullptr,
1376 SourceMgr::DiagHandlerTy DiagHandler = nullptr,
1377 void *DiagHandlerCtxt = nullptr);
1378 Input(MemoryBufferRef Input,
1379 void *Ctxt = nullptr,
1380 SourceMgr::DiagHandlerTy DiagHandler = nullptr,
1381 void *DiagHandlerCtxt = nullptr);
1382 ~Input() override;
1383
1384 // Check if there was an syntax or semantic error during parsing.
1385 std::error_code error();
1386
1387private:
1388 bool outputting() const override;
1389 bool mapTag(StringRef, bool) override;
1390 void beginMapping() override;
1391 void endMapping() override;
1392 bool preflightKey(const char *, bool, bool, bool &, void *&) override;
1393 void postflightKey(void *) override;
1394 std::vector<StringRef> keys() override;
1395 void beginFlowMapping() override;
1396 void endFlowMapping() override;
1397 unsigned beginSequence() override;
1398 void endSequence() override;
1399 bool preflightElement(unsigned index, void *&) override;
1400 void postflightElement(void *) override;
1401 unsigned beginFlowSequence() override;
1402 bool preflightFlowElement(unsigned , void *&) override;
1403 void postflightFlowElement(void *) override;
1404 void endFlowSequence() override;
1405 void beginEnumScalar() override;
1406 bool matchEnumScalar(const char*, bool) override;
1407 bool matchEnumFallback() override;
1408 void endEnumScalar() override;
1409 bool beginBitSetScalar(bool &) override;
1410 bool bitSetMatch(const char *, bool ) override;
1411 void endBitSetScalar() override;
1412 void scalarString(StringRef &, QuotingType) override;
1413 void blockScalarString(StringRef &) override;
1414 void scalarTag(std::string &) override;
1415 NodeKind getNodeKind() override;
1416 void setError(const Twine &message) override;
1417 bool canElideEmptySequence() override;
1418
1419 class HNode {
1420 virtual void anchor();
1421
1422 public:
1423 HNode(Node *n) : _node(n) { }
1424 virtual ~HNode() = default;
1425
1426 static bool classof(const HNode *) { return true; }
1427
1428 Node *_node;
1429 };
1430
1431 class EmptyHNode : public HNode {
1432 void anchor() override;
1433
1434 public:
1435 EmptyHNode(Node *n) : HNode(n) { }
1436
1437 static bool classof(const HNode *n) { return NullNode::classof(n->_node); }
1438
1439 static bool classof(const EmptyHNode *) { return true; }
1440 };
1441
1442 class ScalarHNode : public HNode {
1443 void anchor() override;
1444
1445 public:
1446 ScalarHNode(Node *n, StringRef s) : HNode(n), _value(s) { }
1447
1448 StringRef value() const { return _value; }
1449
1450 static bool classof(const HNode *n) {
1451 return ScalarNode::classof(n->_node) ||
1452 BlockScalarNode::classof(n->_node);
1453 }
1454
1455 static bool classof(const ScalarHNode *) { return true; }
1456
1457 protected:
1458 StringRef _value;
1459 };
1460
1461 class MapHNode : public HNode {
1462 void anchor() override;
1463
1464 public:
1465 MapHNode(Node *n) : HNode(n) { }
1466
1467 static bool classof(const HNode *n) {
1468 return MappingNode::classof(n->_node);
1469 }
1470
1471 static bool classof(const MapHNode *) { return true; }
1472
1473 using NameToNodeAndLoc =
1474 StringMap<std::pair<std::unique_ptr<HNode>, SMRange>>;
1475
1476 NameToNodeAndLoc Mapping;
1477 SmallVector<std::string, 6> ValidKeys;
1478 };
1479
1480 class SequenceHNode : public HNode {
1481 void anchor() override;
1482
1483 public:
1484 SequenceHNode(Node *n) : HNode(n) { }
1485
1486 static bool classof(const HNode *n) {
1487 return SequenceNode::classof(n->_node);
1488 }
1489
1490 static bool classof(const SequenceHNode *) { return true; }
1491
1492 std::vector<std::unique_ptr<HNode>> Entries;
1493 };
1494
1495 std::unique_ptr<Input::HNode> createHNodes(Node *node);
1496 void setError(HNode *hnode, const Twine &message);
1497 void setError(Node *node, const Twine &message);
1498 void setError(const SMRange &Range, const Twine &message);
1499
1500 void reportWarning(HNode *hnode, const Twine &message);
1501 void reportWarning(Node *hnode, const Twine &message);
1502 void reportWarning(const SMRange &Range, const Twine &message);
1503
1504public:
1505 // These are only used by operator>>. They could be private
1506 // if those templated things could be made friends.
1507 bool setCurrentDocument();
1508 bool nextDocument();
1509
1510 /// Returns the current node that's being parsed by the YAML Parser.
1511 const Node *getCurrentNode() const;
1512
1513 void setAllowUnknownKeys(bool Allow) override;
1514
1515private:
1516 SourceMgr SrcMgr; // must be before Strm
1517 std::unique_ptr<llvm::yaml::Stream> Strm;
1518 std::unique_ptr<HNode> TopNode;
1519 std::error_code EC;
1520 BumpPtrAllocator StringAllocator;
1521 document_iterator DocIterator;
1522 std::vector<bool> BitValuesUsed;
1523 HNode *CurrentNode = nullptr;
1524 bool ScalarMatchFound = false;
1525 bool AllowUnknownKeys = false;
1526};
1527
1528///
1529/// The Output class is used to generate a yaml document from in-memory structs
1530/// and vectors.
1531///
1532class Output : public IO {
1533public:
1534 Output(raw_ostream &, void *Ctxt = nullptr, int WrapColumn = 70);
1535 ~Output() override;
1536
1537 /// Set whether or not to output optional values which are equal
1538 /// to the default value. By default, when outputting if you attempt
1539 /// to write a value that is equal to the default, the value gets ignored.
1540 /// Sometimes, it is useful to be able to see these in the resulting YAML
1541 /// anyway.
1542 void setWriteDefaultValues(bool Write) { WriteDefaultValues = Write; }
1543
1544 bool outputting() const override;
1545 bool mapTag(StringRef, bool) override;
1546 void beginMapping() override;
1547 void endMapping() override;
1548 bool preflightKey(const char *key, bool, bool, bool &, void *&) override;
1549 void postflightKey(void *) override;
1550 std::vector<StringRef> keys() override;
1551 void beginFlowMapping() override;
1552 void endFlowMapping() override;
1553 unsigned beginSequence() override;
1554 void endSequence() override;
1555 bool preflightElement(unsigned, void *&) override;
1556 void postflightElement(void *) override;
1557 unsigned beginFlowSequence() override;
1558 bool preflightFlowElement(unsigned, void *&) override;
1559 void postflightFlowElement(void *) override;
1560 void endFlowSequence() override;
1561 void beginEnumScalar() override;
1562 bool matchEnumScalar(const char*, bool) override;
1563 bool matchEnumFallback() override;
1564 void endEnumScalar() override;
1565 bool beginBitSetScalar(bool &) override;
1566 bool bitSetMatch(const char *, bool ) override;
1567 void endBitSetScalar() override;
1568 void scalarString(StringRef &, QuotingType) override;
1569 void blockScalarString(StringRef &) override;
1570 void scalarTag(std::string &) override;
1571 NodeKind getNodeKind() override;
1572 void setError(const Twine &message) override;
1573 bool canElideEmptySequence() override;
1574
1575 // These are only used by operator<<. They could be private
1576 // if that templated operator could be made a friend.
1577 void beginDocuments();
1578 bool preflightDocument(unsigned);
1579 void postflightDocument();
1580 void endDocuments();
1581
1582private:
1583 void output(StringRef s);
1584 void outputUpToEndOfLine(StringRef s);
1585 void newLineCheck(bool EmptySequence = false);
1586 void outputNewLine();
1587 void paddedKey(StringRef key);
1588 void flowKey(StringRef Key);
1589
1590 enum InState {
1591 inSeqFirstElement,
1592 inSeqOtherElement,
1593 inFlowSeqFirstElement,
1594 inFlowSeqOtherElement,
1595 inMapFirstKey,
1596 inMapOtherKey,
1597 inFlowMapFirstKey,
1598 inFlowMapOtherKey
1599 };
1600
1601 static bool inSeqAnyElement(InState State);
1602 static bool inFlowSeqAnyElement(InState State);
1603 static bool inMapAnyKey(InState State);
1604 static bool inFlowMapAnyKey(InState State);
1605
1606 raw_ostream &Out;
1607 int WrapColumn;
1608 SmallVector<InState, 8> StateStack;
1609 int Column = 0;
1610 int ColumnAtFlowStart = 0;
1611 int ColumnAtMapFlowStart = 0;
1612 bool NeedBitValueComma = false;
1613 bool NeedFlowSequenceComma = false;
1614 bool EnumerationMatchFound = false;
1615 bool WriteDefaultValues = false;
1616 StringRef Padding;
1617 StringRef PaddingBeforeContainer;
1618};
1619
1620template <typename T, typename Context>
1621void IO::processKeyWithDefault(const char *Key, Optional<T> &Val,
1622 const Optional<T> &DefaultValue, bool Required,
1623 Context &Ctx) {
1624 assert(DefaultValue.hasValue() == false &&(static_cast <bool> (DefaultValue.hasValue() == false &&
"Optional<T> shouldn't have a value!") ? void (0) : __assert_fail
("DefaultValue.hasValue() == false && \"Optional<T> shouldn't have a value!\""
, "llvm/include/llvm/Support/YAMLTraits.h", 1625, __extension__
__PRETTY_FUNCTION__))
1625 "Optional<T> shouldn't have a value!")(static_cast <bool> (DefaultValue.hasValue() == false &&
"Optional<T> shouldn't have a value!") ? void (0) : __assert_fail
("DefaultValue.hasValue() == false && \"Optional<T> shouldn't have a value!\""
, "llvm/include/llvm/Support/YAMLTraits.h", 1625, __extension__
__PRETTY_FUNCTION__))
;
1626 void *SaveInfo;
1627 bool UseDefault = true;
1628 const bool sameAsDefault = outputting() && !Val.hasValue();
1629 if (!outputting() && !Val.hasValue())
1630 Val = T();
1631 if (Val.hasValue() &&
1632 this->preflightKey(Key, Required, sameAsDefault, UseDefault, SaveInfo)) {
1633
1634 // When reading an Optional<X> key from a YAML description, we allow the
1635 // special "<none>" value, which can be used to specify that no value was
1636 // requested, i.e. the DefaultValue will be assigned. The DefaultValue is
1637 // usually None.
1638 bool IsNone = false;
1639 if (!outputting())
1640 if (const auto *Node = dyn_cast<ScalarNode>(((Input *)this)->getCurrentNode()))
1641 // We use rtrim to ignore possible white spaces that might exist when a
1642 // comment is present on the same line.
1643 IsNone = Node->getRawValue().rtrim(' ') == "<none>";
1644
1645 if (IsNone)
1646 Val = DefaultValue;
1647 else
1648 yamlize(*this, Val.getValue(), Required, Ctx);
1649 this->postflightKey(SaveInfo);
1650 } else {
1651 if (UseDefault)
1652 Val = DefaultValue;
1653 }
1654}
1655
1656/// YAML I/O does conversion based on types. But often native data types
1657/// are just a typedef of built in intergral types (e.g. int). But the C++
1658/// type matching system sees through the typedef and all the typedefed types
1659/// look like a built in type. This will cause the generic YAML I/O conversion
1660/// to be used. To provide better control over the YAML conversion, you can
1661/// use this macro instead of typedef. It will create a class with one field
1662/// and automatic conversion operators to and from the base type.
1663/// Based on BOOST_STRONG_TYPEDEF
1664#define LLVM_YAML_STRONG_TYPEDEF(_base, _type)struct _type { _type() = default; _type(const _base v) : value
(v) {} _type(const _type &v) = default; _type &operator
=(const _type &rhs) = default; _type &operator=(const
_base &rhs) { value = rhs; return *this; } operator const
_base & () const { return value; } bool operator==(const
_type &rhs) const { return value == rhs.value; } bool operator
==(const _base &rhs) const { return value == rhs; } bool operator
<(const _type &rhs) const { return value < rhs.value
; } _base value; using BaseType = _base; };
\
1665 struct _type { \
1666 _type() = default; \
1667 _type(const _base v) : value(v) {} \
1668 _type(const _type &v) = default; \
1669 _type &operator=(const _type &rhs) = default; \
1670 _type &operator=(const _base &rhs) { value = rhs; return *this; } \
1671 operator const _base & () const { return value; } \
1672 bool operator==(const _type &rhs) const { return value == rhs.value; } \
1673 bool operator==(const _base &rhs) const { return value == rhs; } \
1674 bool operator<(const _type &rhs) const { return value < rhs.value; } \
1675 _base value; \
1676 using BaseType = _base; \
1677 };
1678
1679///
1680/// Use these types instead of uintXX_t in any mapping to have
1681/// its yaml output formatted as hexadecimal.
1682///
1683LLVM_YAML_STRONG_TYPEDEF(uint8_t, Hex8)struct Hex8 { Hex8() = default; Hex8(const uint8_t v) : value
(v) {} Hex8(const Hex8 &v) = default; Hex8 &operator=
(const Hex8 &rhs) = default; Hex8 &operator=(const uint8_t
&rhs) { value = rhs; return *this; } operator const uint8_t
& () const { return value; } bool operator==(const Hex8 &
rhs) const { return value == rhs.value; } bool operator==(const
uint8_t &rhs) const { return value == rhs; } bool operator
<(const Hex8 &rhs) const { return value < rhs.value
; } uint8_t value; using BaseType = uint8_t; };
1684LLVM_YAML_STRONG_TYPEDEF(uint16_t, Hex16)struct Hex16 { Hex16() = default; Hex16(const uint16_t v) : value
(v) {} Hex16(const Hex16 &v) = default; Hex16 &operator
=(const Hex16 &rhs) = default; Hex16 &operator=(const
uint16_t &rhs) { value = rhs; return *this; } operator const
uint16_t & () const { return value; } bool operator==(const
Hex16 &rhs) const { return value == rhs.value; } bool operator
==(const uint16_t &rhs) const { return value == rhs; } bool
operator<(const Hex16 &rhs) const { return value <
rhs.value; } uint16_t value; using BaseType = uint16_t; };
1685LLVM_YAML_STRONG_TYPEDEF(uint32_t, Hex32)struct Hex32 { Hex32() = default; Hex32(const uint32_t v) : value
(v) {} Hex32(const Hex32 &v) = default; Hex32 &operator
=(const Hex32 &rhs) = default; Hex32 &operator=(const
uint32_t &rhs) { value = rhs; return *this; } operator const
uint32_t & () const { return value; } bool operator==(const
Hex32 &rhs) const { return value == rhs.value; } bool operator
==(const uint32_t &rhs) const { return value == rhs; } bool
operator<(const Hex32 &rhs) const { return value <
rhs.value; } uint32_t value; using BaseType = uint32_t; };
1686LLVM_YAML_STRONG_TYPEDEF(uint64_t, Hex64)struct Hex64 { Hex64() = default; Hex64(const uint64_t v) : value
(v) {} Hex64(const Hex64 &v) = default; Hex64 &operator
=(const Hex64 &rhs) = default; Hex64 &operator=(const
uint64_t &rhs) { value = rhs; return *this; } operator const
uint64_t & () const { return value; } bool operator==(const
Hex64 &rhs) const { return value == rhs.value; } bool operator
==(const uint64_t &rhs) const { return value == rhs; } bool
operator<(const Hex64 &rhs) const { return value <
rhs.value; } uint64_t value; using BaseType = uint64_t; };
1687
1688template<>
1689struct ScalarTraits<Hex8> {
1690 static void output(const Hex8 &, void *, raw_ostream &);
1691 static StringRef input(StringRef, void *, Hex8 &);
1692 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1693};
1694
1695template<>
1696struct ScalarTraits<Hex16> {
1697 static void output(const Hex16 &, void *, raw_ostream &);
1698 static StringRef input(StringRef, void *, Hex16 &);
1699 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1700};
1701
1702template<>
1703struct ScalarTraits<Hex32> {
1704 static void output(const Hex32 &, void *, raw_ostream &);
1705 static StringRef input(StringRef, void *, Hex32 &);
1706 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1707};
1708
1709template<>
1710struct ScalarTraits<Hex64> {
1711 static void output(const Hex64 &, void *, raw_ostream &);
1712 static StringRef input(StringRef, void *, Hex64 &);
1713 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1714};
1715
1716template <> struct ScalarTraits<VersionTuple> {
1717 static void output(const VersionTuple &Value, void *, llvm::raw_ostream &Out);
1718 static StringRef input(StringRef, void *, VersionTuple &);
1719 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
1720};
1721
1722// Define non-member operator>> so that Input can stream in a document list.
1723template <typename T>
1724inline std::enable_if_t<has_DocumentListTraits<T>::value, Input &>
1725operator>>(Input &yin, T &docList) {
1726 int i = 0;
1727 EmptyContext Ctx;
1728 while ( yin.setCurrentDocument() ) {
1729 yamlize(yin, DocumentListTraits<T>::element(yin, docList, i), true, Ctx);
1730 if ( yin.error() )
1731 return yin;
1732 yin.nextDocument();
1733 ++i;
1734 }
1735 return yin;
1736}
1737
1738// Define non-member operator>> so that Input can stream in a map as a document.
1739template <typename T>
1740inline std::enable_if_t<has_MappingTraits<T, EmptyContext>::value, Input &>
1741operator>>(Input &yin, T &docMap) {
1742 EmptyContext Ctx;
1743 yin.setCurrentDocument();
1744 yamlize(yin, docMap, true, Ctx);
1745 return yin;
1746}
1747
1748// Define non-member operator>> so that Input can stream in a sequence as
1749// a document.
1750template <typename T>
1751inline std::enable_if_t<has_SequenceTraits<T>::value, Input &>
1752operator>>(Input &yin, T &docSeq) {
1753 EmptyContext Ctx;
1754 if (yin.setCurrentDocument())
1755 yamlize(yin, docSeq, true, Ctx);
1756 return yin;
1757}
1758
1759// Define non-member operator>> so that Input can stream in a block scalar.
1760template <typename T>
1761inline std::enable_if_t<has_BlockScalarTraits<T>::value, Input &>
1762operator>>(Input &In, T &Val) {
1763 EmptyContext Ctx;
1764 if (In.setCurrentDocument())
1765 yamlize(In, Val, true, Ctx);
1766 return In;
1767}
1768
1769// Define non-member operator>> so that Input can stream in a string map.
1770template <typename T>
1771inline std::enable_if_t<has_CustomMappingTraits<T>::value, Input &>
1772operator>>(Input &In, T &Val) {
1773 EmptyContext Ctx;
1774 if (In.setCurrentDocument())
1775 yamlize(In, Val, true, Ctx);
1776 return In;
1777}
1778
1779// Define non-member operator>> so that Input can stream in a polymorphic type.
1780template <typename T>
1781inline std::enable_if_t<has_PolymorphicTraits<T>::value, Input &>
1782operator>>(Input &In, T &Val) {
1783 EmptyContext Ctx;
1784 if (In.setCurrentDocument())
1785 yamlize(In, Val, true, Ctx);
1786 return In;
1787}
1788
1789// Provide better error message about types missing a trait specialization
1790template <typename T>
1791inline std::enable_if_t<missingTraits<T, EmptyContext>::value, Input &>
1792operator>>(Input &yin, T &docSeq) {
1793 char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
1794 return yin;
1795}
1796
1797// Define non-member operator<< so that Output can stream out document list.
1798template <typename T>
1799inline std::enable_if_t<has_DocumentListTraits<T>::value, Output &>
1800operator<<(Output &yout, T &docList) {
1801 EmptyContext Ctx;
1802 yout.beginDocuments();
1803 const size_t count = DocumentListTraits<T>::size(yout, docList);
1804 for(size_t i=0; i < count; ++i) {
1805 if ( yout.preflightDocument(i) ) {
1806 yamlize(yout, DocumentListTraits<T>::element(yout, docList, i), true,
1807 Ctx);
1808 yout.postflightDocument();
1809 }
1810 }
1811 yout.endDocuments();
1812 return yout;
1813}
1814
1815// Define non-member operator<< so that Output can stream out a map.
1816template <typename T>
1817inline std::enable_if_t<has_MappingTraits<T, EmptyContext>::value, Output &>
1818operator<<(Output &yout, T &map) {
1819 EmptyContext Ctx;
1820 yout.beginDocuments();
1821 if ( yout.preflightDocument(0) ) {
2
Assuming the condition is true
3
Taking true branch
1822 yamlize(yout, map, true, Ctx);
4
Calling 'yamlize<clang::clangd::Ref, llvm::yaml::EmptyContext>'
1823 yout.postflightDocument();
1824 }
1825 yout.endDocuments();
1826 return yout;
1827}
1828
1829// Define non-member operator<< so that Output can stream out a sequence.
1830template <typename T>
1831inline std::enable_if_t<has_SequenceTraits<T>::value, Output &>
1832operator<<(Output &yout, T &seq) {
1833 EmptyContext Ctx;
1834 yout.beginDocuments();
1835 if ( yout.preflightDocument(0) ) {
1836 yamlize(yout, seq, true, Ctx);
1837 yout.postflightDocument();
1838 }
1839 yout.endDocuments();
1840 return yout;
1841}
1842
1843// Define non-member operator<< so that Output can stream out a block scalar.
1844template <typename T>
1845inline std::enable_if_t<has_BlockScalarTraits<T>::value, Output &>
1846operator<<(Output &Out, T &Val) {
1847 EmptyContext Ctx;
1848 Out.beginDocuments();
1849 if (Out.preflightDocument(0)) {
1850 yamlize(Out, Val, true, Ctx);
1851 Out.postflightDocument();
1852 }
1853 Out.endDocuments();
1854 return Out;
1855}
1856
1857// Define non-member operator<< so that Output can stream out a string map.
1858template <typename T>
1859inline std::enable_if_t<has_CustomMappingTraits<T>::value, Output &>
1860operator<<(Output &Out, T &Val) {
1861 EmptyContext Ctx;
1862 Out.beginDocuments();
1863 if (Out.preflightDocument(0)) {
1864 yamlize(Out, Val, true, Ctx);
1865 Out.postflightDocument();
1866 }
1867 Out.endDocuments();
1868 return Out;
1869}
1870
1871// Define non-member operator<< so that Output can stream out a polymorphic
1872// type.
1873template <typename T>
1874inline std::enable_if_t<has_PolymorphicTraits<T>::value, Output &>
1875operator<<(Output &Out, T &Val) {
1876 EmptyContext Ctx;
1877 Out.beginDocuments();
1878 if (Out.preflightDocument(0)) {
1879 // FIXME: The parser does not support explicit documents terminated with a
1880 // plain scalar; the end-marker is included as part of the scalar token.
1881 assert(PolymorphicTraits<T>::getKind(Val) != NodeKind::Scalar && "plain scalar documents are not supported")(static_cast <bool> (PolymorphicTraits<T>::getKind
(Val) != NodeKind::Scalar && "plain scalar documents are not supported"
) ? void (0) : __assert_fail ("PolymorphicTraits<T>::getKind(Val) != NodeKind::Scalar && \"plain scalar documents are not supported\""
, "llvm/include/llvm/Support/YAMLTraits.h", 1881, __extension__
__PRETTY_FUNCTION__))
;
1882 yamlize(Out, Val, true, Ctx);
1883 Out.postflightDocument();
1884 }
1885 Out.endDocuments();
1886 return Out;
1887}
1888
1889// Provide better error message about types missing a trait specialization
1890template <typename T>
1891inline std::enable_if_t<missingTraits<T, EmptyContext>::value, Output &>
1892operator<<(Output &yout, T &seq) {
1893 char missing_yaml_trait_for_type[sizeof(MissingTrait<T>)];
1894 return yout;
1895}
1896
1897template <bool B> struct IsFlowSequenceBase {};
1898template <> struct IsFlowSequenceBase<true> { static const bool flow = true; };
1899
1900template <typename T, bool Flow>
1901struct SequenceTraitsImpl : IsFlowSequenceBase<Flow> {
1902private:
1903 using type = typename T::value_type;
1904
1905public:
1906 static size_t size(IO &io, T &seq) { return seq.size(); }
1907
1908 static type &element(IO &io, T &seq, size_t index) {
1909 if (index >= seq.size())
1910 seq.resize(index + 1);
1911 return seq[index];
1912 }
1913};
1914
1915// Simple helper to check an expression can be used as a bool-valued template
1916// argument.
1917template <bool> struct CheckIsBool { static const bool value = true; };
1918
1919// If T has SequenceElementTraits, then vector<T> and SmallVector<T, N> have
1920// SequenceTraits that do the obvious thing.
1921template <typename T>
1922struct SequenceTraits<
1923 std::vector<T>,
1924 std::enable_if_t<CheckIsBool<SequenceElementTraits<T>::flow>::value>>
1925 : SequenceTraitsImpl<std::vector<T>, SequenceElementTraits<T>::flow> {};
1926template <typename T, unsigned N>
1927struct SequenceTraits<
1928 SmallVector<T, N>,
1929 std::enable_if_t<CheckIsBool<SequenceElementTraits<T>::flow>::value>>
1930 : SequenceTraitsImpl<SmallVector<T, N>, SequenceElementTraits<T>::flow> {};
1931template <typename T>
1932struct SequenceTraits<
1933 SmallVectorImpl<T>,
1934 std::enable_if_t<CheckIsBool<SequenceElementTraits<T>::flow>::value>>
1935 : SequenceTraitsImpl<SmallVectorImpl<T>, SequenceElementTraits<T>::flow> {};
1936
1937// Sequences of fundamental types use flow formatting.
1938template <typename T>
1939struct SequenceElementTraits<T,
1940 std::enable_if_t<std::is_fundamental<T>::value>> {
1941 static const bool flow = true;
1942};
1943
1944// Sequences of strings use block formatting.
1945template<> struct SequenceElementTraits<std::string> {
1946 static const bool flow = false;
1947};
1948template<> struct SequenceElementTraits<StringRef> {
1949 static const bool flow = false;
1950};
1951template<> struct SequenceElementTraits<std::pair<std::string, std::string>> {
1952 static const bool flow = false;
1953};
1954
1955/// Implementation of CustomMappingTraits for std::map<std::string, T>.
1956template <typename T> struct StdMapStringCustomMappingTraitsImpl {
1957 using map_type = std::map<std::string, T>;
1958
1959 static void inputOne(IO &io, StringRef key, map_type &v) {
1960 io.mapRequired(key.str().c_str(), v[std::string(key)]);
1961 }
1962
1963 static void output(IO &io, map_type &v) {
1964 for (auto &p : v)
1965 io.mapRequired(p.first.c_str(), p.second);
1966 }
1967};
1968
1969} // end namespace yaml
1970} // end namespace llvm
1971
1972#define LLVM_YAML_IS_SEQUENCE_VECTOR_IMPL(TYPE, FLOW)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<TYPE>::value && !std::is_same<TYPE, std::string
>::value && !std::is_same<TYPE, llvm::StringRef
>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<TYPE>
{ static const bool flow = FLOW; }; } }
\
1973 namespace llvm { \
1974 namespace yaml { \
1975 static_assert( \
1976 !std::is_fundamental<TYPE>::value && \
1977 !std::is_same<TYPE, std::string>::value && \
1978 !std::is_same<TYPE, llvm::StringRef>::value, \
1979 "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"); \
1980 template <> struct SequenceElementTraits<TYPE> { \
1981 static const bool flow = FLOW; \
1982 }; \
1983 } \
1984 }
1985
1986/// Utility for declaring that a std::vector of a particular type
1987/// should be considered a YAML sequence.
1988#define LLVM_YAML_IS_SEQUENCE_VECTOR(type)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<type>::value && !std::is_same<type, std::string
>::value && !std::is_same<type, llvm::StringRef
>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<type>
{ static const bool flow = false; }; } }
\
1989 LLVM_YAML_IS_SEQUENCE_VECTOR_IMPL(type, false)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<type>::value && !std::is_same<type, std::string
>::value && !std::is_same<type, llvm::StringRef
>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<type>
{ static const bool flow = false; }; } }
1990
1991/// Utility for declaring that a std::vector of a particular type
1992/// should be considered a YAML flow sequence.
1993#define LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(type)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<type>::value && !std::is_same<type, std::string
>::value && !std::is_same<type, llvm::StringRef
>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<type>
{ static const bool flow = true; }; } }
\
1994 LLVM_YAML_IS_SEQUENCE_VECTOR_IMPL(type, true)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<type>::value && !std::is_same<type, std::string
>::value && !std::is_same<type, llvm::StringRef
>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<type>
{ static const bool flow = true; }; } }
1995
1996#define LLVM_YAML_DECLARE_MAPPING_TRAITS(Type)namespace llvm { namespace yaml { template <> struct MappingTraits
<Type> { static void mapping(IO &IO, Type &Obj)
; }; } }
\
1997 namespace llvm { \
1998 namespace yaml { \
1999 template <> struct MappingTraits<Type> { \
2000 static void mapping(IO &IO, Type &Obj); \
2001 }; \
2002 } \
2003 }
2004
2005#define LLVM_YAML_DECLARE_ENUM_TRAITS(Type)namespace llvm { namespace yaml { template <> struct ScalarEnumerationTraits
<Type> { static void enumeration(IO &io, Type &
Value); }; } }
\
2006 namespace llvm { \
2007 namespace yaml { \
2008 template <> struct ScalarEnumerationTraits<Type> { \
2009 static void enumeration(IO &io, Type &Value); \
2010 }; \
2011 } \
2012 }
2013
2014#define LLVM_YAML_DECLARE_BITSET_TRAITS(Type)namespace llvm { namespace yaml { template <> struct ScalarBitSetTraits
<Type> { static void bitset(IO &IO, Type &Options
); }; } }
\
2015 namespace llvm { \
2016 namespace yaml { \
2017 template <> struct ScalarBitSetTraits<Type> { \
2018 static void bitset(IO &IO, Type &Options); \
2019 }; \
2020 } \
2021 }
2022
2023#define LLVM_YAML_DECLARE_SCALAR_TRAITS(Type, MustQuote)namespace llvm { namespace yaml { template <> struct ScalarTraits
<Type> { static void output(const Type &Value, void
*ctx, raw_ostream &Out); static StringRef input(StringRef
Scalar, void *ctxt, Type &Value); static QuotingType mustQuote
(StringRef) { return MustQuote; } }; } }
\
2024 namespace llvm { \
2025 namespace yaml { \
2026 template <> struct ScalarTraits<Type> { \
2027 static void output(const Type &Value, void *ctx, raw_ostream &Out); \
2028 static StringRef input(StringRef Scalar, void *ctxt, Type &Value); \
2029 static QuotingType mustQuote(StringRef) { return MustQuote; } \
2030 }; \
2031 } \
2032 }
2033
2034/// Utility for declaring that a std::vector of a particular type
2035/// should be considered a YAML document list.
2036#define LLVM_YAML_IS_DOCUMENT_LIST_VECTOR(_type)namespace llvm { namespace yaml { template <unsigned N>
struct DocumentListTraits<SmallVector<_type, N>>
: public SequenceTraitsImpl<SmallVector<_type, N>, false
> {}; template <> struct DocumentListTraits<std::
vector<_type>> : public SequenceTraitsImpl<std::vector
<_type>, false> {}; } }
\
2037 namespace llvm { \
2038 namespace yaml { \
2039 template <unsigned N> \
2040 struct DocumentListTraits<SmallVector<_type, N>> \
2041 : public SequenceTraitsImpl<SmallVector<_type, N>, false> {}; \
2042 template <> \
2043 struct DocumentListTraits<std::vector<_type>> \
2044 : public SequenceTraitsImpl<std::vector<_type>, false> {}; \
2045 } \
2046 }
2047
2048/// Utility for declaring that std::map<std::string, _type> should be considered
2049/// a YAML map.
2050#define LLVM_YAML_IS_STRING_MAP(_type)namespace llvm { namespace yaml { template <> struct CustomMappingTraits
<std::map<std::string, _type>> : public StdMapStringCustomMappingTraitsImpl
<_type> {}; } }
\
2051 namespace llvm { \
2052 namespace yaml { \
2053 template <> \
2054 struct CustomMappingTraits<std::map<std::string, _type>> \
2055 : public StdMapStringCustomMappingTraitsImpl<_type> {}; \
2056 } \
2057 }
2058
2059LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::Hex64)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<llvm::yaml::Hex64>::value && !std::is_same<
llvm::yaml::Hex64, std::string>::value && !std::is_same
<llvm::yaml::Hex64, llvm::StringRef>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<llvm::yaml
::Hex64> { static const bool flow = true; }; } }
2060LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::Hex32)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<llvm::yaml::Hex32>::value && !std::is_same<
llvm::yaml::Hex32, std::string>::value && !std::is_same
<llvm::yaml::Hex32, llvm::StringRef>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<llvm::yaml
::Hex32> { static const bool flow = true; }; } }
2061LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::Hex16)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<llvm::yaml::Hex16>::value && !std::is_same<
llvm::yaml::Hex16, std::string>::value && !std::is_same
<llvm::yaml::Hex16, llvm::StringRef>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<llvm::yaml
::Hex16> { static const bool flow = true; }; } }
2062LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::Hex8)namespace llvm { namespace yaml { static_assert( !std::is_fundamental
<llvm::yaml::Hex8>::value && !std::is_same<llvm
::yaml::Hex8, std::string>::value && !std::is_same
<llvm::yaml::Hex8, llvm::StringRef>::value, "only use LLVM_YAML_IS_SEQUENCE_VECTOR for types you control"
); template <> struct SequenceElementTraits<llvm::yaml
::Hex8> { static const bool flow = true; }; } }
2063
2064#endif // LLVM_SUPPORT_YAMLTRAITS_H

/usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/new

1// The -*- C++ -*- dynamic memory management header.
2
3// Copyright (C) 1994-2020 Free Software Foundation, Inc.
4
5// This file is part of GCC.
6//
7// GCC is free software; you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation; either version 3, or (at your option)
10// any later version.
11//
12// GCC is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// Under Section 7 of GPL version 3, you are granted additional
18// permissions described in the GCC Runtime Library Exception, version
19// 3.1, as published by the Free Software Foundation.
20
21// You should have received a copy of the GNU General Public License and
22// a copy of the GCC Runtime Library Exception along with this program;
23// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24// <http://www.gnu.org/licenses/>.
25
26/** @file new
27 * This is a Standard C++ Library header.
28 *
29 * The header @c new defines several functions to manage dynamic memory and
30 * handling memory allocation errors; see
31 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/dynamic_memory.html
32 * for more.
33 */
34
35#ifndef _NEW
36#define _NEW
37
38#pragma GCC system_header
39
40#include <bits/c++config.h>
41#include <exception>
42
43#pragma GCC visibility push(default)
44
45extern "C++" {
46
47namespace std
48{
49 /**
50 * @brief Exception possibly thrown by @c new.
51 * @ingroup exceptions
52 *
53 * @c bad_alloc (or classes derived from it) is used to report allocation
54 * errors from the throwing forms of @c new. */
55 class bad_alloc : public exception
56 {
57 public:
58 bad_alloc() throw() { }
59
60#if __cplusplus201402L >= 201103L
61 bad_alloc(const bad_alloc&) = default;
62 bad_alloc& operator=(const bad_alloc&) = default;
63#endif
64
65 // This declaration is not useless:
66 // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
67 virtual ~bad_alloc() throw();
68
69 // See comment in eh_exception.cc.
70 virtual const char* what() const throw();
71 };
72
73#if __cplusplus201402L >= 201103L
74 class bad_array_new_length : public bad_alloc
75 {
76 public:
77 bad_array_new_length() throw() { }
78
79 // This declaration is not useless:
80 // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
81 virtual ~bad_array_new_length() throw();
82
83 // See comment in eh_exception.cc.
84 virtual const char* what() const throw();
85 };
86#endif
87
88#if __cpp_aligned_new
89 enum class align_val_t: size_t {};
90#endif
91
92 struct nothrow_t
93 {
94#if __cplusplus201402L >= 201103L
95 explicit nothrow_t() = default;
96#endif
97 };
98
99 extern const nothrow_t nothrow;
100
101 /** If you write your own error handler to be called by @c new, it must
102 * be of this type. */
103 typedef void (*new_handler)();
104
105 /// Takes a replacement handler as the argument, returns the
106 /// previous handler.
107 new_handler set_new_handler(new_handler) throw();
108
109#if __cplusplus201402L >= 201103L
110 /// Return the current new handler.
111 new_handler get_new_handler() noexcept;
112#endif
113} // namespace std
114
115//@{
116/** These are replaceable signatures:
117 * - normal single new and delete (no arguments, throw @c bad_alloc on error)
118 * - normal array new and delete (same)
119 * - @c nothrow single new and delete (take a @c nothrow argument, return
120 * @c NULL on error)
121 * - @c nothrow array new and delete (same)
122 *
123 * Placement new and delete signatures (take a memory address argument,
124 * does nothing) may not be replaced by a user's program.
125*/
126_GLIBCXX_NODISCARD void* operator new(std::size_t) _GLIBCXX_THROW (std::bad_alloc)
127 __attribute__((__externally_visible__));
128_GLIBCXX_NODISCARD void* operator new[](std::size_t) _GLIBCXX_THROW (std::bad_alloc)
129 __attribute__((__externally_visible__));
130void operator delete(void*) _GLIBCXX_USE_NOEXCEPTnoexcept
131 __attribute__((__externally_visible__));
132void operator delete[](void*) _GLIBCXX_USE_NOEXCEPTnoexcept
133 __attribute__((__externally_visible__));
134#if __cpp_sized_deallocation
135void operator delete(void*, std::size_t) _GLIBCXX_USE_NOEXCEPTnoexcept
136 __attribute__((__externally_visible__));
137void operator delete[](void*, std::size_t) _GLIBCXX_USE_NOEXCEPTnoexcept
138 __attribute__((__externally_visible__));
139#endif
140_GLIBCXX_NODISCARD void* operator new(std::size_t, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPTnoexcept
141 __attribute__((__externally_visible__, __malloc__));
142_GLIBCXX_NODISCARD void* operator new[](std::size_t, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPTnoexcept
143 __attribute__((__externally_visible__, __malloc__));
144void operator delete(void*, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPTnoexcept
145 __attribute__((__externally_visible__));
146void operator delete[](void*, const std::nothrow_t&) _GLIBCXX_USE_NOEXCEPTnoexcept
147 __attribute__((__externally_visible__));
148#if __cpp_aligned_new
149_GLIBCXX_NODISCARD void* operator new(std::size_t, std::align_val_t)
150 __attribute__((__externally_visible__));
151_GLIBCXX_NODISCARD void* operator new(std::size_t, std::align_val_t, const std::nothrow_t&)
152 _GLIBCXX_USE_NOEXCEPTnoexcept __attribute__((__externally_visible__, __malloc__));
153void operator delete(void*, std::align_val_t)
154 _GLIBCXX_USE_NOEXCEPTnoexcept __attribute__((__externally_visible__));
155void operator delete(void*, std::align_val_t, const std::nothrow_t&)
156 _GLIBCXX_USE_NOEXCEPTnoexcept __attribute__((__externally_visible__));
157_GLIBCXX_NODISCARD void* operator new[](std::size_t, std::align_val_t)
158 __attribute__((__externally_visible__));
159_GLIBCXX_NODISCARD void* operator new[](std::size_t, std::align_val_t, const std::nothrow_t&)
160 _GLIBCXX_USE_NOEXCEPTnoexcept __attribute__((__externally_visible__, __malloc__));
161void operator delete[](void*, std::align_val_t)
162 _GLIBCXX_USE_NOEXCEPTnoexcept __attribute__((__externally_visible__));
163void operator delete[](void*, std::align_val_t, const std::nothrow_t&)
164 _GLIBCXX_USE_NOEXCEPTnoexcept __attribute__((__externally_visible__));
165#if __cpp_sized_deallocation
166void operator delete(void*, std::size_t, std::align_val_t)
167 _GLIBCXX_USE_NOEXCEPTnoexcept __attribute__((__externally_visible__));
168void operator delete[](void*, std::size_t, std::align_val_t)
169 _GLIBCXX_USE_NOEXCEPTnoexcept __attribute__((__externally_visible__));
170#endif // __cpp_sized_deallocation
171#endif // __cpp_aligned_new
172
173// Default placement versions of operator new.
174_GLIBCXX_NODISCARD inline void* operator new(std::size_t, void* __p) _GLIBCXX_USE_NOEXCEPTnoexcept
175{ return __p; }
176_GLIBCXX_NODISCARD inline void* operator new[](std::size_t, void* __p) _GLIBCXX_USE_NOEXCEPTnoexcept
177{ return __p; }
178
179// Default placement versions of operator delete.
180inline void operator delete (void*, void*) _GLIBCXX_USE_NOEXCEPTnoexcept { }
181inline void operator delete[](void*, void*) _GLIBCXX_USE_NOEXCEPTnoexcept { }
182//@}
183} // extern "C++"
184
185#if __cplusplus201402L >= 201703L
186#ifdef _GLIBCXX_HAVE_BUILTIN_LAUNDER1
187namespace std
188{
189#define __cpp_lib_launder 201606
190 /// Pointer optimization barrier [ptr.launder]
191 template<typename _Tp>
192 [[nodiscard]] constexpr _Tp*
193 launder(_Tp* __p) noexcept
194 { return __builtin_launder(__p); }
195
196 // The program is ill-formed if T is a function type or
197 // (possibly cv-qualified) void.
198
199 template<typename _Ret, typename... _Args _GLIBCXX_NOEXCEPT_PARM>
200 void launder(_Ret (*)(_Args...) _GLIBCXX_NOEXCEPT_QUAL) = delete;
201 template<typename _Ret, typename... _Args _GLIBCXX_NOEXCEPT_PARM>
202 void launder(_Ret (*)(_Args......) _GLIBCXX_NOEXCEPT_QUAL) = delete;
203
204 void launder(void*) = delete;
205 void launder(const void*) = delete;
206 void launder(volatile void*) = delete;
207 void launder(const volatile void*) = delete;
208}
209#endif // _GLIBCXX_HAVE_BUILTIN_LAUNDER
210#endif // C++17
211
212#if __cplusplus201402L > 201703L
213namespace std
214{
215 /// Tag type used to declare a class-specific operator delete that can
216 /// invoke the destructor before deallocating the memory.
217 struct destroying_delete_t
218 {
219 explicit destroying_delete_t() = default;
220 };
221 /// Tag variable of type destroying_delete_t.
222 inline constexpr destroying_delete_t destroying_delete{};
223}
224// Only define the feature test macro if the compiler supports the feature:
225#if __cpp_impl_destroying_delete201806L
226# define __cpp_lib_destroying_delete 201806L
227#endif
228#endif // C++20
229
230#pragma GCC visibility pop
231
232#endif