LLVM 24.0.0git
SyntheticTypeNameBuilder.cpp
Go to the documentation of this file.
1//===- SyntheticTypeNameBuilder.cpp ---------------------------------------===//
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
15#include "llvm/Support/xxhash.h"
16
17using namespace llvm;
18using namespace dwarf_linker;
19using namespace dwarf_linker::parallel;
20
22 UnitEntryPairTy InputUnitEntryPair,
23 std::optional<std::pair<size_t, size_t>> ChildIndex) {
24 [[maybe_unused]] const CompileUnit::DIEInfo &Info =
25 InputUnitEntryPair.CU->getDIEInfo(InputUnitEntryPair.DieEntry);
26 assert(Info.needToPlaceInTypeTable() &&
27 "Cann't assign name for non-type DIE");
28
29 if (InputUnitEntryPair.CU->getDieTypeEntry(InputUnitEntryPair.DieEntry) !=
30 nullptr)
31 return Error::success();
32
33 SyntheticName.truncate(0);
35 return addDIETypeName(InputUnitEntryPair, ChildIndex, true);
36}
37
39 UnitEntryPairTy InputUnitEntryPair) {
40 for (const DWARFDebugInfoEntry *CurChild =
41 InputUnitEntryPair.CU->getFirstChildEntry(
42 InputUnitEntryPair.DieEntry);
43 CurChild && CurChild->getAbbreviationDeclarationPtr();
44 CurChild = InputUnitEntryPair.CU->getSiblingEntry(CurChild)) {
45 if (CurChild->getTag() == dwarf::DW_TAG_subrange_type ||
46 CurChild->getTag() == dwarf::DW_TAG_generic_subrange) {
47 SyntheticName += "[";
48 if (std::optional<DWARFFormValue> Val =
49 InputUnitEntryPair.CU->find(CurChild, dwarf::DW_AT_count)) {
50 if (std::optional<uint64_t> ConstVal = Val->getAsUnsignedConstant()) {
51 SyntheticName += std::to_string(*ConstVal);
52 } else if (std::optional<int64_t> ConstVal =
53 Val->getAsSignedConstant()) {
54 SyntheticName += std::to_string(*ConstVal);
55 }
56 }
57
58 SyntheticName += "]";
59 }
60 }
61}
62
63static dwarf::Attribute TypeAttr[] = {dwarf::DW_AT_type};
64static dwarf::Attribute ContainingTypeAttr[] = {dwarf::DW_AT_containing_type};
66 bool addTemplateParameters) {
67 // Add entry type.
68 if (Error Err = addReferencedODRDies(InputUnitEntryPair, false, TypeAttr))
69 return Err;
70 SyntheticName += ':';
71
73 for (const DWARFDebugInfoEntry *CurChild =
74 InputUnitEntryPair.CU->getFirstChildEntry(
75 InputUnitEntryPair.DieEntry);
76 CurChild && CurChild->getAbbreviationDeclarationPtr();
77 CurChild = InputUnitEntryPair.CU->getSiblingEntry(CurChild)) {
78 dwarf::Tag ChildTag = CurChild->getTag();
79 if (ChildTag == dwarf::DW_TAG_formal_parameter ||
80 ChildTag == dwarf::DW_TAG_unspecified_parameters)
81 FunctionParameters.push_back(CurChild);
82 else if (ChildTag == dwarf::DW_TAG_GNU_formal_parameter_pack) {
83 for (const DWARFDebugInfoEntry *CurGNUChild =
84 InputUnitEntryPair.CU->getFirstChildEntry(CurChild);
85 CurGNUChild && CurGNUChild->getAbbreviationDeclarationPtr();
86 CurGNUChild = InputUnitEntryPair.CU->getSiblingEntry(CurGNUChild))
87 FunctionParameters.push_back(CurGNUChild);
88 }
89 }
90
91 // Add parameters.
92 if (Error Err = addParamNames(*InputUnitEntryPair.CU, FunctionParameters))
93 return Err;
94
95 if (addTemplateParameters)
96 return addTemplateArguments(InputUnitEntryPair);
97
98 return Error::success();
99}
100
102 UnitEntryPairTy InputUnitEntryPair) {
104 for (const DWARFDebugInfoEntry *CurChild =
105 InputUnitEntryPair.CU->getFirstChildEntry(
106 InputUnitEntryPair.DieEntry);
107 CurChild && CurChild->getAbbreviationDeclarationPtr();
108 CurChild = InputUnitEntryPair.CU->getSiblingEntry(CurChild)) {
109 dwarf::Tag ChildTag = CurChild->getTag();
110 if (ChildTag == dwarf::DW_TAG_template_type_parameter ||
111 ChildTag == dwarf::DW_TAG_template_value_parameter)
112 TemplateParameters.push_back(CurChild);
113 else if (ChildTag == dwarf::DW_TAG_GNU_template_parameter_pack) {
114 for (const DWARFDebugInfoEntry *CurGNUChild =
115 InputUnitEntryPair.CU->getFirstChildEntry(CurChild);
116 CurGNUChild && CurGNUChild->getAbbreviationDeclarationPtr();
117 CurGNUChild = InputUnitEntryPair.CU->getSiblingEntry(CurGNUChild))
118 TemplateParameters.push_back(CurGNUChild);
119 }
120 }
121
122 return addTemplateParamNames(*InputUnitEntryPair.CU, TemplateParameters);
123}
124
128 size_t ParamsStart = SyntheticName.size();
129 SyntheticName += '(';
130 for (const DWARFDebugInfoEntry *FunctionParameter : FunctionParameters) {
131 if (SyntheticName.size() != ParamsStart + 1)
132 SyntheticName += ", ";
133 if (dwarf::toUnsigned(CU.find(FunctionParameter, dwarf::DW_AT_artificial),
134 0))
135 SyntheticName += "^";
136 if (Error Err = addReferencedODRDies(
137 UnitEntryPairTy{&CU, FunctionParameter}, false, TypeAttr))
138 return Err;
139 }
140 SyntheticName += ')';
141 return Error::success();
142}
143
147 if (!TemplateParameters.empty()) {
148 size_t ParamsStart = SyntheticName.size();
149 SyntheticName += '<';
150 for (const DWARFDebugInfoEntry *Parameter : TemplateParameters) {
151 if (SyntheticName.size() != ParamsStart + 1)
152 SyntheticName += ", ";
153
154 if (Parameter->getTag() == dwarf::DW_TAG_template_value_parameter) {
155 if (std::optional<DWARFFormValue> Val =
156 CU.find(Parameter, dwarf::DW_AT_const_value)) {
157 if (std::optional<uint64_t> ConstVal = Val->getAsUnsignedConstant())
158 SyntheticName += std::to_string(*ConstVal);
159 else if (std::optional<int64_t> ConstVal = Val->getAsSignedConstant())
160 SyntheticName += std::to_string(*ConstVal);
161 }
162 }
163
164 if (Error Err = addReferencedODRDies(UnitEntryPairTy{&CU, Parameter},
165 false, TypeAttr))
166 return Err;
167 }
168 SyntheticName += '>';
169 }
170 return Error::success();
171}
172
174 std::pair<size_t, size_t> ChildIdx) {
175 std::string Name;
176 llvm::raw_string_ostream stream(Name);
177 stream << format_hex_no_prefix(ChildIdx.first, ChildIdx.second);
178 SyntheticName += Name;
179}
180
181// Examine DIE and return type deduplication candidate: some DIEs could not be
182// deduplicated, namespace may refer to another namespace.
183static std::optional<UnitEntryPairTy>
185 switch (UnitEntryPair.DieEntry->getTag()) {
186 case dwarf::DW_TAG_null:
187 case dwarf::DW_TAG_compile_unit:
188 case dwarf::DW_TAG_partial_unit:
189 case dwarf::DW_TAG_type_unit:
190 case dwarf::DW_TAG_skeleton_unit: {
191 return std::nullopt;
192 }
193 case dwarf::DW_TAG_namespace: {
194 // Check if current namespace refers another.
195 if (UnitEntryPair.CU->find(UnitEntryPair.DieEntry, dwarf::DW_AT_extension))
196 UnitEntryPair = UnitEntryPair.getNamespaceOrigin();
197
198 // Content of anonimous namespaces should not be deduplicated.
199 if (!UnitEntryPair.CU->find(UnitEntryPair.DieEntry, dwarf::DW_AT_name))
200 llvm_unreachable("Cann't deduplicate anonimous namespace");
201
202 return UnitEntryPair;
203 }
204 default:
205 return UnitEntryPair;
206 }
207}
208
210 UnitEntryPairTy &InputUnitEntryPair) {
211 std::optional<UnitEntryPairTy> UnitEntryPair = InputUnitEntryPair.getParent();
212 if (!UnitEntryPair)
213 return Error::success();
214
215 UnitEntryPair = getTypeDeduplicationCandidate(*UnitEntryPair);
216 if (!UnitEntryPair)
217 return Error::success();
218
219 if (TypeEntry *ImmediateParentName =
220 UnitEntryPair->CU->getDieTypeEntry(UnitEntryPair->DieEntry)) {
221 SyntheticName.appendRef(ImmediateParentName);
222 SyntheticName += ".";
223 return Error::success();
224 }
225
226 // Collect parent entries.
228 do {
229 Parents.push_back(*UnitEntryPair);
230
231 UnitEntryPair = UnitEntryPair->getParent();
232 if (!UnitEntryPair)
233 break;
234
235 UnitEntryPair = getTypeDeduplicationCandidate(*UnitEntryPair);
236 if (!UnitEntryPair)
237 break;
238
239 } while (!UnitEntryPair->CU->getDieTypeEntry(UnitEntryPair->DieEntry));
240
241 // Assign name for each parent entry.
242 size_t NameStart = SyntheticName.size();
243 for (UnitEntryPairTy Parent : reverse(Parents)) {
244 SyntheticName.truncate(NameStart);
245 if (Error Err = addDIETypeName(Parent, std::nullopt, true))
246 return Err;
247 }
248
249 // Add parents delimiter.
250 SyntheticName += ".";
251 return Error::success();
252}
253
255 UnitEntryPairTy &InputUnitEntryPair, bool &HasDeclFileName) {
256 if (std::optional<DWARFFormValue> DeclFileVal = InputUnitEntryPair.CU->find(
257 InputUnitEntryPair.DieEntry, dwarf::DW_AT_decl_file)) {
258 if (std::optional<DWARFFormValue> DeclLineVal = InputUnitEntryPair.CU->find(
259 InputUnitEntryPair.DieEntry, dwarf::DW_AT_decl_line)) {
260 if (std::optional<std::pair<StringRef, StringRef>> DirAndFilename =
261 InputUnitEntryPair.CU->getDirAndFilenameFromLineTable(
262 *DeclFileVal)) {
263 SyntheticName += DirAndFilename->first;
264 SyntheticName += DirAndFilename->second;
265
266 if (std::optional<uint64_t> DeclLineIntVal =
267 dwarf::toUnsigned(*DeclLineVal)) {
268 SyntheticName += " ";
269 SyntheticName += utohexstr(*DeclLineIntVal);
270 }
271
272 HasDeclFileName = true;
273 }
274 }
275 }
276}
277
279 dwarf::Attribute Attr) {
280 if (std::optional<DWARFFormValue> Val =
281 InputUnitEntryPair.CU->find(InputUnitEntryPair.DieEntry, Attr)) {
282 if (std::optional<uint64_t> ConstVal = Val->getAsUnsignedConstant()) {
283 SyntheticName += " ";
284 SyntheticName += std::to_string(*ConstVal);
285 } else if (std::optional<int64_t> ConstVal = Val->getAsSignedConstant()) {
286 SyntheticName += " ";
287 SyntheticName += std::to_string(*ConstVal);
288 }
289 }
290}
291
294 dwarf::Attribute Attr) {
295 std::optional<DWARFFormValue> AttrValue =
296 InputUnitEntryPair.CU->find(InputUnitEntryPair.DieEntry, Attr);
297 if (!AttrValue)
298 return std::nullopt;
299
300 std::optional<UnitEntryPairTy> RefDie =
301 InputUnitEntryPair.CU->resolveDIEReference(
303 if (RefDie && !RefDie->DieEntry)
304 return createStringError(std::errc::invalid_argument,
305 "cannot resolve DIE reference");
306 return RefDie;
307}
308
311 return createStringError(std::errc::invalid_argument,
312 "cannot parse input DWARF: recursive dependence");
313 return Error::success();
314}
315
317 UnitEntryPairTy InputUnitEntryPair, bool AssignNameToTypeDescriptor,
319 bool FirstIteration = true;
320 for (dwarf::Attribute Attr : ODRAttrs) {
322 resolveReference(InputUnitEntryPair, Attr);
323 if (!RefDie)
324 return RefDie.takeError();
325 if (!*RefDie)
326 continue;
327
328 if (!FirstIteration)
329 SyntheticName += ",";
330
331 if (Error Err = incrementRecursionDepth())
332 return Err;
333 if (Error Err =
334 addDIETypeName(**RefDie, std::nullopt, AssignNameToTypeDescriptor))
335 return Err;
337 FirstIteration = false;
338 }
339
340 return Error::success();
341}
342
344 bool AddParentNames) {
345 bool HasLinkageName = false;
346 bool HasShortName = false;
347 bool HasTemplatesInShortName = false;
348 bool HasDeclFileName = false;
349
350 // Try to get name from the DIE.
351 if (std::optional<DWARFFormValue> Val = InputUnitEntryPair.CU->find(
352 InputUnitEntryPair.DieEntry,
353 {dwarf::DW_AT_MIPS_linkage_name, dwarf::DW_AT_linkage_name})) {
354 // Firstly check for linkage name.
356 HasLinkageName = true;
357 } else if (std::optional<DWARFFormValue> Val = InputUnitEntryPair.CU->find(
358 InputUnitEntryPair.DieEntry, dwarf::DW_AT_name)) {
359 // Then check for short name.
360 StringRef Name = dwarf::toStringRef(Val);
361 SyntheticName += Name;
362
363 HasShortName = true;
364 HasTemplatesInShortName =
365 Name.ends_with(">") && Name.count("<") != 0 && !Name.ends_with("<=>");
366 } else {
367 // Finally check for declaration attributes.
368 addDieNameFromDeclFileAndDeclLine(InputUnitEntryPair, HasDeclFileName);
369
370 // Unnamed types declared on one line differ only in their contents.
371 if (HasDeclFileName)
372 if (Error Err = addUnnamedTypeContents(InputUnitEntryPair))
373 return Err;
374 }
375
376 // Add additional name parts for some DIEs.
377 switch (InputUnitEntryPair.DieEntry->getTag()) {
378 case dwarf::DW_TAG_union_type:
379 case dwarf::DW_TAG_interface_type:
380 case dwarf::DW_TAG_class_type:
381 case dwarf::DW_TAG_structure_type:
382 case dwarf::DW_TAG_subroutine_type:
383 case dwarf::DW_TAG_subprogram: {
384 if (InputUnitEntryPair.CU->find(InputUnitEntryPair.DieEntry,
385 dwarf::DW_AT_artificial))
386 SyntheticName += "^";
387
388 // No need to add signature information for linkage name,
389 // also no need to add template parameters name if short name already
390 // includes them.
391 if (!HasLinkageName) {
392 if (Error Err =
393 addSignature(InputUnitEntryPair, !HasTemplatesInShortName))
394 return Err;
395 } else if (InputUnitEntryPair.DieEntry->getTag() ==
396 dwarf::DW_TAG_subprogram &&
398 InputUnitEntryPair.CU->find(InputUnitEntryPair.DieEntry,
399 dwarf::DW_AT_external),
400 0)) {
401 // The linkage name of a function that is not external is unique within
402 // its unit only. Another unit can give the same name to a function
403 // declared elsewhere, or instantiated over other types.
404 SyntheticName += ' ';
405 addDieNameFromDeclFileAndDeclLine(InputUnitEntryPair, HasDeclFileName);
406 if (Error Err = addTemplateArguments(InputUnitEntryPair))
407 return Err;
408 }
409 } break;
410 case dwarf::DW_TAG_coarray_type:
411 case dwarf::DW_TAG_array_type: {
412 addArrayDimension(InputUnitEntryPair);
413 } break;
414 case dwarf::DW_TAG_subrange_type: {
415 addValueName(InputUnitEntryPair, dwarf::DW_AT_count);
416 } break;
417 case dwarf::DW_TAG_ptr_to_member_type: {
418 // A pointer to member is a different type for each containing class.
419 if (Error Err = addReferencedODRDies(InputUnitEntryPair, AddParentNames,
421 return Err;
422 SyntheticName += "::";
423 } break;
424 case dwarf::DW_TAG_template_value_parameter: {
425 if (!HasTemplatesInShortName) {
426 // TODO add support for DW_AT_location
427 addValueName(InputUnitEntryPair, dwarf::DW_AT_const_value);
428 }
429 } break;
430 default: {
431 // Nothing to do.
432 } break;
433 }
434
435 // If name for the DIE is not determined yet or if the DIE is a typedef, add
436 // referenced types to the name.
437 if ((!HasLinkageName && !HasShortName && !HasDeclFileName) ||
438 InputUnitEntryPair.DieEntry->getTag() == dwarf::DW_TAG_typedef) {
439 if (InputUnitEntryPair.CU->find(InputUnitEntryPair.DieEntry,
441 if (Error Err = addReferencedODRDies(InputUnitEntryPair, AddParentNames,
443 return Err;
444 }
445
446 return Error::success();
447}
448
450 UnitEntryPairTy InputUnitEntryPair,
451 std::optional<std::pair<size_t, size_t>> ChildIndex,
452 bool AssignNameToTypeDescriptor) {
453 std::optional<UnitEntryPairTy> UnitEntryPair =
454 getTypeDeduplicationCandidate(InputUnitEntryPair);
455 if (!UnitEntryPair)
456 return Error::success();
457
458 TypeEntry *TypeEntryPtr =
459 InputUnitEntryPair.CU->getDieTypeEntry(InputUnitEntryPair.DieEntry);
460 // Check if DIE already has a name.
461 if (!TypeEntryPtr) {
462 size_t NameStart = SyntheticName.size();
463 // Prepend the parent scope so this key matches the entry the type is
464 // assigned once it has one. Otherwise the same type gets different keys
465 // depending on whether it already has a pool entry, which races under
466 // parallel assignment and breaks deterministic deduplication.
467 if (Error Err = addParentName(*UnitEntryPair))
468 return Err;
469 addTypePrefix(*UnitEntryPair->CU, UnitEntryPair->DieEntry);
470
471 if (ChildIndex) {
472 addOrderedName(*ChildIndex);
473 } else {
474 if (Error Err = addTypeName(*UnitEntryPair, AssignNameToTypeDescriptor))
475 return Err;
476 }
477
478 // Intern unassigned keys too, since references point at interned entries.
479 TypeEntryPtr = TypePoolRef.insert(SyntheticName.str().substr(NameStart));
480 if (AssignNameToTypeDescriptor)
481 InputUnitEntryPair.CU->setDieTypeEntry(InputUnitEntryPair.DieEntry,
482 TypeEntryPtr);
483 SyntheticName.truncate(NameStart);
484 }
485 SyntheticName.appendRef(TypeEntryPtr);
486
487 return Error::success();
488}
489
491 UnitEntryPairTy InputUnitEntryPair) {
492 if (!isIdentifiedByChildren(InputUnitEntryPair.DieEntry->getTag()))
493 return Error::success();
494
495 auto It = ContentsDigests.find(InputUnitEntryPair.DieEntry);
496 if (It != ContentsDigests.end()) {
497 SyntheticName += It->second;
498 return Error::success();
499 }
500
501 CompileUnit &CU = *InputUnitEntryPair.CU;
502 size_t ContentsStart = SyntheticName.size();
503 SyntheticName += '(';
504 for (const DWARFDebugInfoEntry *CurChild =
505 CU.getFirstChildEntry(InputUnitEntryPair.DieEntry);
506 CurChild && CurChild->getAbbreviationDeclarationPtr();
507 CurChild = CU.getSiblingEntry(CurChild)) {
508 if (!isIdentifyingChild(DWARFDie(&CU.getOrigUnit(), CurChild)))
509 continue;
510
511 if (SyntheticName.size() != ContentsStart + 1)
512 SyntheticName += ", ";
513 addTypePrefix(CU, CurChild);
514 if (std::optional<DWARFFormValue> Val =
515 CU.find(CurChild, dwarf::DW_AT_name))
517 if (Error Err = addUnqualifiedTypeName(UnitEntryPairTy{&CU, CurChild}))
518 return Err;
519 }
520 SyntheticName += ')';
521
522 // Replace the contents with a fixed-size digest, so the names of the types
523 // referring to this one do not grow with its size or nesting depth.
524 StringRef Contents = SyntheticName.str().substr(ContentsStart);
526 std::string Digest = "<" + utohexstr(Hash.high64, true, 16) +
527 utohexstr(Hash.low64, true, 16) + ">";
528 SyntheticName.truncate(ContentsStart);
529 SyntheticName += Digest;
530 ContentsDigests.try_emplace(InputUnitEntryPair.DieEntry, std::move(Digest));
531 return Error::success();
532}
533
535 UnitEntryPairTy InputUnitEntryPair) {
537 resolveReference(InputUnitEntryPair, dwarf::DW_AT_type);
538 if (!RefDie)
539 return RefDie.takeError();
540 if (!*RefDie)
541 return Error::success();
542
543 if (Error Err = incrementRecursionDepth())
544 return Err;
545
546 UnitEntryPairTy Type = **RefDie;
547 SyntheticName += ':';
548 addTypePrefix(*Type.CU, Type.DieEntry);
549 if (std::optional<DWARFFormValue> Val =
550 Type.CU->find(Type.DieEntry, dwarf::DW_AT_name)) {
552 } else {
553 if (Type.DieEntry->getTag() == dwarf::DW_TAG_array_type)
556 return Err;
558 return Err;
559 }
560
562 return Error::success();
563}
564
566 CompileUnit &CU, const DWARFDebugInfoEntry *DieEntry) {
567 switch (DieEntry->getTag()) {
568 case dwarf::DW_TAG_base_type: {
569 SyntheticName += "{0}";
570 } break;
571 case dwarf::DW_TAG_namespace: {
572 SyntheticName += "{1}";
573 } break;
574 case dwarf::DW_TAG_formal_parameter: {
575 SyntheticName += "{2}";
576 } break;
577 // dwarf::DW_TAG_unspecified_parameters have the same prefix as before.
578 case dwarf::DW_TAG_unspecified_parameters: {
579 SyntheticName += "{2}";
580 } break;
581 case dwarf::DW_TAG_template_type_parameter: {
582 SyntheticName += "{3}";
583 } break;
584 // dwarf::DW_TAG_template_value_parameter have the same prefix as before.
585 case dwarf::DW_TAG_template_value_parameter: {
586 SyntheticName += "{3}";
587 } break;
588 case dwarf::DW_TAG_GNU_formal_parameter_pack: {
589 SyntheticName += "{4}";
590 } break;
591 case dwarf::DW_TAG_GNU_template_parameter_pack: {
592 SyntheticName += "{5}";
593 } break;
594 case dwarf::DW_TAG_inheritance: {
595 SyntheticName += "{6}";
596 } break;
597 case dwarf::DW_TAG_array_type: {
598 SyntheticName += "{7}";
599 } break;
600 case dwarf::DW_TAG_class_type: {
601 SyntheticName += "{8}";
602 } break;
603 case dwarf::DW_TAG_enumeration_type: {
604 SyntheticName += "{9}";
605 } break;
606 case dwarf::DW_TAG_imported_declaration: {
607 SyntheticName += "{A}";
608 } break;
609 case dwarf::DW_TAG_member: {
610 // Treat a static data member the same way regardless of whether it's
611 // described as a DW_TAG_member or a DW_TAG_variable.
612 if (isStaticDataMember(DWARFDie(&CU.getOrigUnit(), DieEntry)))
613 SyntheticName += "{d}";
614 else
615 SyntheticName += "{B}";
616 } break;
617 case dwarf::DW_TAG_pointer_type: {
618 SyntheticName += "{C}";
619 } break;
620 case dwarf::DW_TAG_reference_type: {
621 SyntheticName += "{D}";
622 } break;
623 case dwarf::DW_TAG_string_type: {
624 SyntheticName += "{E}";
625 } break;
626 case dwarf::DW_TAG_structure_type: {
627 SyntheticName += "{F}";
628 } break;
629 case dwarf::DW_TAG_subroutine_type: {
630 SyntheticName += "{G}";
631 } break;
632 case dwarf::DW_TAG_typedef: {
633 SyntheticName += "{H}";
634 } break;
635 case dwarf::DW_TAG_union_type: {
636 SyntheticName += "{I}";
637 } break;
638 case dwarf::DW_TAG_variant: {
639 SyntheticName += "{J}";
640 } break;
641 case dwarf::DW_TAG_inlined_subroutine: {
642 SyntheticName += "{K}";
643 } break;
644 case dwarf::DW_TAG_module: {
645 SyntheticName += "{L}";
646 } break;
647 case dwarf::DW_TAG_ptr_to_member_type: {
648 SyntheticName += "{M}";
649 } break;
650 case dwarf::DW_TAG_set_type: {
651 SyntheticName += "{N}";
652 } break;
653 case dwarf::DW_TAG_subrange_type: {
654 SyntheticName += "{O}";
655 } break;
656 case dwarf::DW_TAG_with_stmt: {
657 SyntheticName += "{P}";
658 } break;
659 case dwarf::DW_TAG_access_declaration: {
660 SyntheticName += "{Q}";
661 } break;
662 case dwarf::DW_TAG_catch_block: {
663 SyntheticName += "{R}";
664 } break;
665 case dwarf::DW_TAG_const_type: {
666 SyntheticName += "{S}";
667 } break;
668 case dwarf::DW_TAG_constant: {
669 SyntheticName += "{T}";
670 } break;
671 case dwarf::DW_TAG_enumerator: {
672 SyntheticName += "{U}";
673 } break;
674 case dwarf::DW_TAG_file_type: {
675 SyntheticName += "{V}";
676 } break;
677 case dwarf::DW_TAG_friend: {
678 SyntheticName += "{W}";
679 } break;
680 case dwarf::DW_TAG_namelist: {
681 SyntheticName += "{X}";
682 } break;
683 case dwarf::DW_TAG_namelist_item: {
684 SyntheticName += "{Y}";
685 } break;
686 case dwarf::DW_TAG_packed_type: {
687 SyntheticName += "{Z}";
688 } break;
689 case dwarf::DW_TAG_subprogram: {
690 SyntheticName += "{a}";
691 } break;
692 case dwarf::DW_TAG_thrown_type: {
693 SyntheticName += "{b}";
694 } break;
695 case dwarf::DW_TAG_variant_part: {
696 SyntheticName += "{c}";
697 } break;
698 case dwarf::DW_TAG_variable: {
699 SyntheticName += "{d}";
700 } break;
701 case dwarf::DW_TAG_volatile_type: {
702 SyntheticName += "{e}";
703 } break;
704 case dwarf::DW_TAG_dwarf_procedure: {
705 SyntheticName += "{f}";
706 } break;
707 case dwarf::DW_TAG_restrict_type: {
708 SyntheticName += "{g}";
709 } break;
710 case dwarf::DW_TAG_interface_type: {
711 SyntheticName += "{h}";
712 } break;
713 case dwarf::DW_TAG_imported_module: {
714 SyntheticName += "{i}";
715 } break;
716 case dwarf::DW_TAG_unspecified_type: {
717 SyntheticName += "{j}";
718 } break;
719 case dwarf::DW_TAG_imported_unit: {
720 SyntheticName += "{k}";
721 } break;
722 case dwarf::DW_TAG_condition: {
723 SyntheticName += "{l}";
724 } break;
725 case dwarf::DW_TAG_shared_type: {
726 SyntheticName += "{m}";
727 } break;
728 case dwarf::DW_TAG_rvalue_reference_type: {
729 SyntheticName += "{n}";
730 } break;
731 case dwarf::DW_TAG_template_alias: {
732 SyntheticName += "{o}";
733 } break;
734 case dwarf::DW_TAG_coarray_type: {
735 SyntheticName += "{p}";
736 } break;
737 case dwarf::DW_TAG_generic_subrange: {
738 SyntheticName += "{q}";
739 } break;
740 case dwarf::DW_TAG_dynamic_type: {
741 SyntheticName += "{r}";
742 } break;
743 case dwarf::DW_TAG_atomic_type: {
744 SyntheticName += "{s}";
745 } break;
746 case dwarf::DW_TAG_call_site: {
747 SyntheticName += "{t}";
748 } break;
749 case dwarf::DW_TAG_call_site_parameter: {
750 SyntheticName += "{u}";
751 } break;
752 case dwarf::DW_TAG_immutable_type: {
753 SyntheticName += "{v}";
754 } break;
755 case dwarf::DW_TAG_entry_point: {
756 SyntheticName += "{w}";
757 } break;
758 case dwarf::DW_TAG_label: {
759 SyntheticName += "{x}";
760 } break;
761 case dwarf::DW_TAG_lexical_block: {
762 SyntheticName += "{y}";
763 } break;
764 case dwarf::DW_TAG_common_block: {
765 SyntheticName += "{z}";
766 } break;
767 case dwarf::DW_TAG_common_inclusion: {
768 SyntheticName += "{|}";
769 } break;
770 case dwarf::DW_TAG_try_block: {
771 SyntheticName += "{~}";
772 } break;
773
774 case dwarf::DW_TAG_null: {
775 llvm_unreachable("No type prefix for DW_TAG_null");
776 } break;
777 case dwarf::DW_TAG_compile_unit: {
778 llvm_unreachable("No type prefix for DW_TAG_compile_unit");
779 } break;
780 case dwarf::DW_TAG_partial_unit: {
781 llvm_unreachable("No type prefix for DW_TAG_partial_unit");
782 } break;
783 case dwarf::DW_TAG_type_unit: {
784 llvm_unreachable("No type prefix for DW_TAG_type_unit");
785 } break;
786 case dwarf::DW_TAG_skeleton_unit: {
787 llvm_unreachable("No type prefix for DW_TAG_skeleton_unit");
788 } break;
789
790 default: {
791 SyntheticName += "{~~";
792 SyntheticName += utohexstr(DieEntry->getTag());
793 SyntheticName += "}";
794 } break;
795 }
796}
797
799 CompileUnit &CU, const DWARFDebugInfoEntry *DieEntry) {
800 switch (DieEntry->getTag()) {
801 case dwarf::DW_TAG_array_type:
802 case dwarf::DW_TAG_coarray_type:
803 case dwarf::DW_TAG_class_type:
804 case dwarf::DW_TAG_common_block:
805 case dwarf::DW_TAG_lexical_block:
806 case dwarf::DW_TAG_structure_type:
807 case dwarf::DW_TAG_subprogram:
808 case dwarf::DW_TAG_subroutine_type:
809 case dwarf::DW_TAG_union_type:
810 case dwarf::DW_TAG_GNU_template_template_param:
811 case dwarf::DW_TAG_GNU_formal_parameter_pack:
812 case dwarf::DW_TAG_GNU_template_parameter_pack: {
813 NeedCountChildren = true;
814 } break;
815 case dwarf::DW_TAG_enumeration_type: {
816 // TODO : do we need to add condition
817 NeedCountChildren = true;
818 } break;
819 default: {
820 // Nothing to do.
821 }
822 }
823
824 // Calculate maximal index value
825 if (NeedCountChildren) {
826 for (const DWARFDebugInfoEntry *CurChild = CU.getFirstChildEntry(DieEntry);
827 CurChild && CurChild->getAbbreviationDeclarationPtr();
828 CurChild = CU.getSiblingEntry(CurChild)) {
829 std::optional<size_t> ArrayIndex = tagToArrayIndex(CU, CurChild);
830 if (!ArrayIndex)
831 continue;
832
833 assert((*ArrayIndex < ChildIndexesWidth.size()) &&
834 "Wrong index for ChildIndexesWidth");
835 ChildIndexesWidth[*ArrayIndex]++;
836 }
837
838 // Calculate index field width(number of digits in hexadecimal
839 // representation).
840 for (size_t &Width : ChildIndexesWidth) {
841 size_t digitsCounter = 1;
842 size_t NumToCompare = 15;
843
844 while (NumToCompare < Width) {
845 NumToCompare <<= 4;
846 digitsCounter++;
847 }
848
849 Width = digitsCounter;
850 }
851 }
852}
853
855 CompileUnit &CU, const DWARFDebugInfoEntry *DieEntry) {
857 return std::nullopt;
858
859 switch (DieEntry->getTag()) {
860 case dwarf::DW_TAG_unspecified_parameters:
861 case dwarf::DW_TAG_formal_parameter:
862 return 0;
863 case dwarf::DW_TAG_template_value_parameter:
864 case dwarf::DW_TAG_template_type_parameter:
865 case dwarf::DW_TAG_GNU_template_template_param:
866 return 1;
867 case dwarf::DW_TAG_enumeration_type:
868 if (std::optional<uint32_t> ParentIdx = DieEntry->getParentIdx()) {
869 if (*ParentIdx && CU.getDebugInfoEntry(*ParentIdx)->getTag() ==
870 dwarf::DW_TAG_array_type)
871 return 2;
872 }
873 return std::nullopt;
874 case dwarf::DW_TAG_subrange_type:
875 return 3;
876 case dwarf::DW_TAG_generic_subrange:
877 return 4;
878 case dwarf::DW_TAG_enumerator:
879 return 5;
880 case dwarf::DW_TAG_namelist_item:
881 return 6;
882 case dwarf::DW_TAG_member:
883 // Treat a static data member the same way regardless of whether it's
884 // described as a DW_TAG_member or a DW_TAG_variable.
885 if (isStaticDataMember(DWARFDie(&CU.getOrigUnit(), DieEntry)))
886 return std::nullopt;
887 return 7;
888 default:
889 return std::nullopt;
890 };
891}
892
893std::optional<std::pair<size_t, size_t>>
895 CompileUnit &CU, const DWARFDebugInfoEntry *ChildDieEntry) {
896 std::optional<size_t> ArrayIndex = tagToArrayIndex(CU, ChildDieEntry);
897 if (!ArrayIndex)
898 return std::nullopt;
899
900 assert((*ArrayIndex < OrderedChildIdxs.size()) &&
901 "Wrong index for ChildIndexesWidth");
902 assert(ChildIndexesWidth[*ArrayIndex] < 16 &&
903 "Index width exceeds 16 digits.");
904
905 std::pair<size_t, size_t> Result = std::make_pair(
906 OrderedChildIdxs[*ArrayIndex], ChildIndexesWidth[*ArrayIndex]);
907 OrderedChildIdxs[*ArrayIndex]++;
908 return Result;
909}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static std::optional< UnitEntryPairTy > getTypeDeduplicationCandidate(UnitEntryPairTy UnitEntryPair)
static dwarf::Attribute TypeAttr[]
static dwarf::Attribute ContainingTypeAttr[]
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
DWARFDebugInfoEntry - A DIE with only the minimum required data.
std::optional< uint32_t > getParentIdx() const
Returns index of the parent die.
const DWARFAbbreviationDeclaration * getAbbreviationDeclarationPtr() const
Utility class that carries the DWARF compile/type unit and the debug info entry in an object.
Definition DWARFDie.h:43
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
Stores all information related to a compile unit, be it in its original instance of the object file o...
std::optional< std::pair< StringRef, StringRef > > getDirAndFilenameFromLineTable(const DWARFFormValue &FileIdxValue)
Returns directory and file from the line table by index.
std::optional< UnitEntryPairTy > resolveDIEReference(const DWARFFormValue &RefValue, ResolveInterCUReferencesMode CanResolveInterCUReferences)
Resolve the DIE attribute reference that has been extracted in RefValue.
OrderedChildrenIndexAssigner(CompileUnit &CU, const DWARFDebugInfoEntry *DieEntry)
std::optional< size_t > tagToArrayIndex(CompileUnit &CU, const DWARFDebugInfoEntry *DieEntry)
std::optional< std::pair< size_t, size_t > > getChildIndex(CompileUnit &CU, const DWARFDebugInfoEntry *ChildDieEntry)
Returns index of the specified child and width of hexadecimal representation.
Error addReferencedODRDies(UnitEntryPairTy InputUnitEntryPair, bool AssignNameToTypeDescriptor, ArrayRef< dwarf::Attribute > ODRAttrs)
Analyze InputUnitEntryPair's ODR attributes and put names of the referenced type dies to the built na...
Error addTemplateParamNames(CompileUnit &CU, SmallVector< const DWARFDebugInfoEntry *, 10 > &TemplateParameters)
Add specified TemplateParameters to the built name.
Error incrementRecursionDepth()
Count a referenced DIE toward the recursion limit.
Error addParentName(UnitEntryPairTy &InputUnitEntryPair)
Add names of parent dies to the built name.
Error addUnqualifiedTypeName(UnitEntryPairTy InputUnitEntryPair)
Add the type InputUnitEntryPair refers to, without parent scopes.
Error addSignature(UnitEntryPairTy InputUnitEntryPair, bool addTemplateParameters)
Add signature( entry type plus type of parameters plus type of template parameters(if addTemplatePara...
Error addParamNames(CompileUnit &CU, SmallVector< const DWARFDebugInfoEntry *, 20 > &FunctionParameters)
Add specified FunctionParameters to the built name.
void addDieNameFromDeclFileAndDeclLine(UnitEntryPairTy &InputUnitEntryPair, bool &HasDeclFileName)
Error addUnnamedTypeContents(UnitEntryPairTy InputUnitEntryPair)
Add a fixed-size digest of the contents of the unnamed type InputUnitEntryPair, so the names of the t...
Expected< std::optional< UnitEntryPairTy > > resolveReference(UnitEntryPairTy InputUnitEntryPair, dwarf::Attribute Attr)
Resolve the DIE that Attr of InputUnitEntryPair refers to.
void addOrderedName(std::pair< size_t, size_t > ChildIdx)
Add ordered name to the built name.
Error assignName(UnitEntryPairTy InputUnitEntryPair, std::optional< std::pair< size_t, size_t > > ChildIndex)
Create synthetic name for the specified DIE InputUnitEntryPair and assign created name to the DIE typ...
Error addDIETypeName(UnitEntryPairTy InputUnitEntryPair, std::optional< std::pair< size_t, size_t > > ChildIndex, bool AssignNameToTypeDescriptor)
Analyze InputUnitEntryPair for the type name and possibly assign built type name to the DIE's type in...
static constexpr size_t MaxRecursionDepth
Bounds RecursionDepth, which malformed input can make unbounded.
Error addTypeName(UnitEntryPairTy InputUnitEntryPair, bool AddParentNames)
Add type name to the built name.
DenseMap< const DWARFDebugInfoEntry *, std::string > ContentsDigests
Digests of the contents of unnamed types, by DIE.
void addArrayDimension(UnitEntryPairTy InputUnitEntryPair)
Add array type dimension.
void addValueName(UnitEntryPairTy InputUnitEntryPair, dwarf::Attribute Attr)
Add value name to the built name.
Error addTemplateArguments(UnitEntryPairTy InputUnitEntryPair)
Add the template arguments of InputUnitEntryPair to the built name.
void addTypePrefix(CompileUnit &CU, const DWARFDebugInfoEntry *DieEntry)
Add type prefix to the built name.
A raw_ostream that writes to an std::string.
TypeEntry * getDieTypeEntry(uint32_t Idx)
Idx index of the DIE.
DIEInfo & getDIEInfo(unsigned Idx)
Idx index of the DIE.
void setDieTypeEntry(uint32_t Idx, TypeEntry *Entry)
Idx index of the DIE.
const DWARFDebugInfoEntry * getSiblingEntry(const DWARFDebugInfoEntry *Die) const
const DWARFDebugInfoEntry * getFirstChildEntry(const DWARFDebugInfoEntry *Die) const
std::optional< DWARFFormValue > find(uint32_t DieIdx, ArrayRef< dwarf::Attribute > Attrs) const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
StringMapEntry< std::atomic< TypeEntryBody * > > TypeEntry
Definition TypePool.h:30
ArrayRef< dwarf::Attribute > getODRAttributes()
LLVM_ABI bool isIdentifiedByChildren(dwarf::Tag Tag)
Test if an unnamed type with tag Tag is identified by its children, which tell it apart from other un...
Definition Utils.cpp:57
LLVM_ABI bool isStaticDataMember(const DWARFDie &Die)
Test if the DW_TAG_member Die is a DWARF 2-4 style static data member: a declaration with no storage ...
Definition Utils.cpp:43
LLVM_ABI bool isIdentifyingChild(const DWARFDie &Die)
Test if Die is a child that identifies its unnamed parent type.
Definition Utils.cpp:70
Attribute
Attributes.
Definition Dwarf.h:125
StringRef toStringRef(const std::optional< DWARFFormValue > &V, StringRef Default={})
Take an optional DWARFFormValue and try to extract a string value from it.
std::optional< uint64_t > toUnsigned(const std::optional< DWARFFormValue > &V)
Take an optional DWARFFormValue and try to extract an unsigned constant.
This is an optimization pass for GlobalISel generic memory operations.
ArrayRef< CharT > arrayRefFromStringRef(StringRef Input)
Construct an array ref of bytes from a string ref.
std::string utohexstr(uint64_t X, bool LowerCase=false, unsigned Width=0)
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1321
XXH128_hash_t xxh3_128bits(ArrayRef< uint8_t > data)
Definition ArrayRef.h:561
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
FormattedNumber format_hex_no_prefix(uint64_t N, unsigned Width, bool Upper=false)
format_hex_no_prefix - Output N as a fixed width hexadecimal.
Definition Format.h:177
The return value from 128-bit hashes.
Definition xxhash.h:58
uint64_t low64
value & 0xFFFFFFFFFFFFFFFF
Definition xxhash.h:59
uint64_t high64
value >> 64
Definition xxhash.h:60
This is a helper structure which keeps a debug info entry with it's containing compilation unit.