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"
13 #include "clang/Basic/SourceLocation.h"
14 #include "clang/Basic/SourceManager.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
22 namespace clang {
23 namespace markup {
24
25 using FIDMap = llvm::DenseMap<FileID, unsigned>;
26
AddFID(FIDMap & FIDs,SmallVectorImpl<FileID> & V,FileID FID)27 inline 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
AddFID(FIDMap & FIDs,SmallVectorImpl<FileID> & V,const SourceManager & SM,SourceLocation L)35 inline unsigned AddFID(FIDMap &FIDs, SmallVectorImpl<FileID> &V,
36 const SourceManager &SM, SourceLocation L) {
37 FileID FID = SM.getFileID(SM.getExpansionLoc(L));
38 return AddFID(FIDs, V, FID);
39 }
40
GetFID(const FIDMap & FIDs,FileID FID)41 inline 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
GetFID(const FIDMap & FIDs,const SourceManager & SM,SourceLocation L)47 inline unsigned GetFID(const FIDMap &FIDs, const SourceManager &SM,
48 SourceLocation L) {
49 FileID FID = SM.getFileID(SM.getExpansionLoc(L));
50 return GetFID(FIDs, FID);
51 }
52
Indent(raw_ostream & o,const unsigned indent)53 inline raw_ostream &Indent(raw_ostream &o, const unsigned indent) {
54 for (unsigned i = 0; i < indent; ++i)
55 o << ' ';
56 return o;
57 }
58
EmitPlistHeader(raw_ostream & o)59 inline 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
EmitInteger(raw_ostream & o,int64_t value)68 inline raw_ostream &EmitInteger(raw_ostream &o, int64_t value) {
69 o << "<integer>";
70 o << value;
71 o << "</integer>";
72 return o;
73 }
74
EmitString(raw_ostream & o,StringRef s)75 inline 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 << "&";
84 break;
85 case '<':
86 o << "<";
87 break;
88 case '>':
89 o << ">";
90 break;
91 case '\'':
92 o << "'";
93 break;
94 case '\"':
95 o << """;
96 break;
97 }
98 }
99 o << "</string>";
100 return o;
101 }
102
EmitLocation(raw_ostream & o,const SourceManager & SM,SourceLocation L,const FIDMap & FM,unsigned indent)103 inline 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
EmitRange(raw_ostream & o,const SourceManager & SM,CharSourceRange R,const FIDMap & FM,unsigned indent)119 inline 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
138