clang 20.0.0git
PlistSupport.h
Go to the documentation of this file.
1//===- PlistSupport.h - Plist Output Utilities ------------------*- 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_CLANG_BASIC_PLISTSUPPORT_H
10#define LLVM_CLANG_BASIC_PLISTSUPPORT_H
11
12#include "clang/Basic/LLVM.h"
15#include "llvm/ADT/DenseMap.h"
16#include "llvm/ADT/SmallVector.h"
17#include "llvm/ADT/StringRef.h"
18#include "llvm/Support/raw_ostream.h"
19#include <cassert>
20#include <cstdint>
21
22namespace clang {
23namespace markup {
24
25using FIDMap = llvm::DenseMap<FileID, unsigned>;
26
27inline unsigned AddFID(FIDMap &FIDs, SmallVectorImpl<FileID> &V,
28 FileID FID) {
29 auto [I, Inserted] = FIDs.try_emplace(FID, V.size());
30 if (Inserted)
31 V.push_back(FID);
32 return I->second;
33}
34
35inline unsigned AddFID(FIDMap &FIDs, SmallVectorImpl<FileID> &V,
37 FileID FID = SM.getFileID(SM.getExpansionLoc(L));
38 return AddFID(FIDs, V, FID);
39}
40
41inline unsigned GetFID(const FIDMap &FIDs, FileID FID) {
42 FIDMap::const_iterator I = FIDs.find(FID);
43 assert(I != FIDs.end());
44 return I->second;
45}
46
47inline unsigned GetFID(const FIDMap &FIDs, const SourceManager &SM,
49 FileID FID = SM.getFileID(SM.getExpansionLoc(L));
50 return GetFID(FIDs, FID);
51}
52
53inline raw_ostream &Indent(raw_ostream &o, const unsigned indent) {
54 for (unsigned i = 0; i < indent; ++i)
55 o << ' ';
56 return o;
57}
58
59inline raw_ostream &EmitPlistHeader(raw_ostream &o) {
60 static const char *PlistHeader =
61 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
62 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
63 "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
64 "<plist version=\"1.0\">\n";
65 return o << PlistHeader;
66}
67
68inline raw_ostream &EmitInteger(raw_ostream &o, int64_t value) {
69 o << "<integer>";
70 o << value;
71 o << "</integer>";
72 return o;
73}
74
75inline raw_ostream &EmitString(raw_ostream &o, StringRef s) {
76 o << "<string>";
77 for (char c : s) {
78 switch (c) {
79 default:
80 o << c;
81 break;
82 case '&':
83 o << "&amp;";
84 break;
85 case '<':
86 o << "&lt;";
87 break;
88 case '>':
89 o << "&gt;";
90 break;
91 case '\'':
92 o << "&apos;";
93 break;
94 case '\"':
95 o << "&quot;";
96 break;
97 }
98 }
99 o << "</string>";
100 return o;
101}
102
103inline void EmitLocation(raw_ostream &o, const SourceManager &SM,
104 SourceLocation L, const FIDMap &FM, unsigned indent) {
105 if (L.isInvalid()) return;
106
107 FullSourceLoc Loc(SM.getExpansionLoc(L), const_cast<SourceManager &>(SM));
108
109 Indent(o, indent) << "<dict>\n";
110 Indent(o, indent) << " <key>line</key>";
111 EmitInteger(o, Loc.getExpansionLineNumber()) << '\n';
112 Indent(o, indent) << " <key>col</key>";
113 EmitInteger(o, Loc.getExpansionColumnNumber()) << '\n';
114 Indent(o, indent) << " <key>file</key>";
115 EmitInteger(o, GetFID(FM, SM, Loc)) << '\n';
116 Indent(o, indent) << "</dict>\n";
117}
118
119inline void EmitRange(raw_ostream &o, const SourceManager &SM,
120 CharSourceRange R, const FIDMap &FM, unsigned indent) {
121 if (R.isInvalid()) return;
122
123 assert(R.isCharRange() && "cannot handle a token range");
124 Indent(o, indent) << "<array>\n";
125 EmitLocation(o, SM, R.getBegin(), FM, indent + 1);
126
127 // The ".getLocWithOffset(-1)" emulates the behavior of an off-by-one bug
128 // in Lexer that is already fixed. It is here for backwards compatibility
129 // even though it is incorrect.
130 EmitLocation(o, SM, R.getEnd().getLocWithOffset(-1), FM, indent + 1);
131 Indent(o, indent) << "</array>\n";
132}
133
134} // namespace markup
135} // namespace clang
136
137#endif // LLVM_CLANG_BASIC_PLISTSUPPORT_H
#define V(N, I)
Definition: ASTContext.h:3443
#define SM(sm)
Definition: Cuda.cpp:84
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
SourceLocation Loc
Definition: SemaObjC.cpp:759
Defines the clang::SourceLocation class and associated facilities.
Defines the SourceManager interface.
__device__ __2f16 float __ockl_bool s
__device__ __2f16 float c
Represents a character-granular source range.
SourceLocation getEnd() const
SourceLocation getBegin() const
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
A SourceLocation and its associated SourceManager.
Encodes a location in the source.
SourceLocation getLocWithOffset(IntTy Offset) const
Return a source location with the specified offset from this SourceLocation.
This class handles loading and caching of source files into memory.
void EmitRange(raw_ostream &o, const SourceManager &SM, CharSourceRange R, const FIDMap &FM, unsigned indent)
Definition: PlistSupport.h:119
raw_ostream & EmitString(raw_ostream &o, StringRef s)
Definition: PlistSupport.h:75
unsigned GetFID(const FIDMap &FIDs, FileID FID)
Definition: PlistSupport.h:41
unsigned AddFID(FIDMap &FIDs, SmallVectorImpl< FileID > &V, FileID FID)
Definition: PlistSupport.h:27
raw_ostream & EmitInteger(raw_ostream &o, int64_t value)
Definition: PlistSupport.h:68
llvm::DenseMap< FileID, unsigned > FIDMap
Definition: PlistSupport.h:25
raw_ostream & EmitPlistHeader(raw_ostream &o)
Definition: PlistSupport.h:59
void EmitLocation(raw_ostream &o, const SourceManager &SM, SourceLocation L, const FIDMap &FM, unsigned indent)
Definition: PlistSupport.h:103
The JSON file list parser is used to communicate input to InstallAPI.