]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/Basic/PlistSupport.h
MFV r348568: 9466 add JSON output support to channel programs
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / include / clang / Basic / PlistSupport.h
1 //===- PlistSupport.h - Plist Output Utilities ------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_CLANG_BASIC_PLISTSUPPORT_H
11 #define LLVM_CLANG_BASIC_PLISTSUPPORT_H
12
13 #include "clang/Basic/LLVM.h"
14 #include "clang/Basic/SourceLocation.h"
15 #include "clang/Basic/SourceManager.h"
16 #include "llvm/ADT/DenseMap.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include <cassert>
21 #include <cstdint>
22
23 namespace clang {
24 namespace markup {
25
26 using FIDMap = llvm::DenseMap<FileID, unsigned>;
27
28 inline unsigned AddFID(FIDMap &FIDs, SmallVectorImpl<FileID> &V,
29                    FileID FID) {
30   FIDMap::iterator I = FIDs.find(FID);
31   if (I != FIDs.end())
32     return I->second;
33   unsigned NewValue = V.size();
34   FIDs[FID] = NewValue;
35   V.push_back(FID);
36   return NewValue;
37 }
38
39 inline unsigned AddFID(FIDMap &FIDs, SmallVectorImpl<FileID> &V,
40                    const SourceManager &SM, SourceLocation L) {
41   FileID FID = SM.getFileID(SM.getExpansionLoc(L));
42   return AddFID(FIDs, V, FID);
43 }
44
45 inline unsigned GetFID(const FIDMap &FIDs, FileID FID) {
46   FIDMap::const_iterator I = FIDs.find(FID);
47   assert(I != FIDs.end());
48   return I->second;
49 }
50
51 inline unsigned GetFID(const FIDMap &FIDs, const SourceManager &SM,
52                        SourceLocation L) {
53   FileID FID = SM.getFileID(SM.getExpansionLoc(L));
54   return GetFID(FIDs, FID);
55 }
56
57 inline raw_ostream &Indent(raw_ostream &o, const unsigned indent) {
58   for (unsigned i = 0; i < indent; ++i)
59     o << ' ';
60   return o;
61 }
62
63 inline raw_ostream &EmitPlistHeader(raw_ostream &o) {
64   static const char *PlistHeader =
65       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
66       "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
67       "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
68       "<plist version=\"1.0\">\n";
69   return o << PlistHeader;
70 }
71
72 inline raw_ostream &EmitInteger(raw_ostream &o, int64_t value) {
73   o << "<integer>";
74   o << value;
75   o << "</integer>";
76   return o;
77 }
78
79 inline raw_ostream &EmitString(raw_ostream &o, StringRef s) {
80   o << "<string>";
81   for (StringRef::const_iterator I = s.begin(), E = s.end(); I != E; ++I) {
82     char c = *I;
83     switch (c) {
84     default:
85       o << c;
86       break;
87     case '&':
88       o << "&amp;";
89       break;
90     case '<':
91       o << "&lt;";
92       break;
93     case '>':
94       o << "&gt;";
95       break;
96     case '\'':
97       o << "&apos;";
98       break;
99     case '\"':
100       o << "&quot;";
101       break;
102     }
103   }
104   o << "</string>";
105   return o;
106 }
107
108 inline void EmitLocation(raw_ostream &o, const SourceManager &SM,
109                          SourceLocation L, const FIDMap &FM, unsigned indent) {
110   if (L.isInvalid()) return;
111
112   FullSourceLoc Loc(SM.getExpansionLoc(L), const_cast<SourceManager &>(SM));
113
114   Indent(o, indent) << "<dict>\n";
115   Indent(o, indent) << " <key>line</key>";
116   EmitInteger(o, Loc.getExpansionLineNumber()) << '\n';
117   Indent(o, indent) << " <key>col</key>";
118   EmitInteger(o, Loc.getExpansionColumnNumber()) << '\n';
119   Indent(o, indent) << " <key>file</key>";
120   EmitInteger(o, GetFID(FM, SM, Loc)) << '\n';
121   Indent(o, indent) << "</dict>\n";
122 }
123
124 inline void EmitRange(raw_ostream &o, const SourceManager &SM,
125                       CharSourceRange R, const FIDMap &FM, unsigned indent) {
126   if (R.isInvalid()) return;
127
128   assert(R.isCharRange() && "cannot handle a token range");
129   Indent(o, indent) << "<array>\n";
130   EmitLocation(o, SM, R.getBegin(), FM, indent + 1);
131   EmitLocation(o, SM, R.getEnd(), FM, indent + 1);
132   Indent(o, indent) << "</array>\n";
133 }
134
135 } // namespace markup
136 } // namespace clang
137
138 #endif // LLVM_CLANG_BASIC_PLISTSUPPORT_H