Path: blob/main/contrib/llvm-project/llvm/lib/DebugInfo/CodeView/Formatters.cpp
35271 views
//===- Formatters.cpp -----------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "llvm/DebugInfo/CodeView/Formatters.h"9#include "llvm/ADT/ArrayRef.h"10#include "llvm/DebugInfo/CodeView/GUID.h"11#include "llvm/Support/Endian.h"12#include "llvm/Support/ErrorHandling.h"13#include "llvm/Support/Format.h"14#include "llvm/Support/raw_ostream.h"15#include <cassert>1617using namespace llvm;18using namespace llvm::codeview;19using namespace llvm::codeview::detail;2021GuidAdapter::GuidAdapter(StringRef Guid)22: FormatAdapter(ArrayRef(Guid.bytes_begin(), Guid.bytes_end())) {}2324GuidAdapter::GuidAdapter(ArrayRef<uint8_t> Guid)25: FormatAdapter(std::move(Guid)) {}2627// From https://docs.microsoft.com/en-us/windows/win32/msi/guid documentation:28// The GUID data type is a text string representing a Class identifier (ID).29// All GUIDs must be authored in uppercase.30// The valid format for a GUID is {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX} where31// X is a hex digit (0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F).32//33// The individual string components must be padded to comply with the specific34// lengths of {8-4-4-4-12} characters.35// The llvm-yaml2obj tool checks that a GUID follow that format:36// - the total length to be 38 (including the curly braces.37// - there is a dash at the positions: 8, 13, 18 and 23.38void GuidAdapter::format(raw_ostream &Stream, StringRef Style) {39assert(Item.size() == 16 && "Expected 16-byte GUID");40struct MSGuid {41support::ulittle32_t Data1;42support::ulittle16_t Data2;43support::ulittle16_t Data3;44support::ubig64_t Data4;45};46const MSGuid *G = reinterpret_cast<const MSGuid *>(Item.data());47Stream48<< '{' << format_hex_no_prefix(G->Data1, 8, /*Upper=*/true)49<< '-' << format_hex_no_prefix(G->Data2, 4, /*Upper=*/true)50<< '-' << format_hex_no_prefix(G->Data3, 4, /*Upper=*/true)51<< '-' << format_hex_no_prefix(G->Data4 >> 48, 4, /*Upper=*/true) << '-'52<< format_hex_no_prefix(G->Data4 & ((1ULL << 48) - 1), 12, /*Upper=*/true)53<< '}';54}5556raw_ostream &llvm::codeview::operator<<(raw_ostream &OS, const GUID &Guid) {57codeview::detail::GuidAdapter A(Guid.Guid);58A.format(OS, "");59return OS;60}616263