]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/include/clang/Basic/PlistSupport.h
Merge clang 7.0.1 and several follow-up changes
[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 void AddFID(FIDMap &FIDs, SmallVectorImpl<FileID> &V,
29                    const SourceManager &SM, SourceLocation L) {
30   FileID FID = SM.getFileID(SM.getExpansionLoc(L));
31   FIDMap::iterator I = FIDs.find(FID);
32   if (I != FIDs.end())
33     return;
34   FIDs[FID] = V.size();
35   V.push_back(FID);
36 }
37
38 inline unsigned GetFID(const FIDMap &FIDs, const SourceManager &SM,
39                        SourceLocation L) {
40   FileID FID = SM.getFileID(SM.getExpansionLoc(L));
41   FIDMap::const_iterator I = FIDs.find(FID);
42   assert(I != FIDs.end());
43   return I->second;
44 }
45
46 inline raw_ostream &Indent(raw_ostream &o, const unsigned indent) {
47   for (unsigned i = 0; i < indent; ++i)
48     o << ' ';
49   return o;
50 }
51
52 inline raw_ostream &EmitPlistHeader(raw_ostream &o) {
53   static const char *PlistHeader =
54       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
55       "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
56       "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
57       "<plist version=\"1.0\">\n";
58   return o << PlistHeader;
59 }
60
61 inline raw_ostream &EmitInteger(raw_ostream &o, int64_t value) {
62   o << "<integer>";
63   o << value;
64   o << "</integer>";
65   return o;
66 }
67
68 inline raw_ostream &EmitString(raw_ostream &o, StringRef s) {
69   o << "<string>";
70   for (StringRef::const_iterator I = s.begin(), E = s.end(); I != E; ++I) {
71     char c = *I;
72     switch (c) {
73     default:
74       o << c;
75       break;
76     case '&':
77       o << "&amp;";
78       break;
79     case '<':
80       o << "&lt;";
81       break;
82     case '>':
83       o << "&gt;";
84       break;
85     case '\'':
86       o << "&apos;";
87       break;
88     case '\"':
89       o << "&quot;";
90       break;
91     }
92   }
93   o << "</string>";
94   return o;
95 }
96
97 inline void EmitLocation(raw_ostream &o, const SourceManager &SM,
98                          SourceLocation L, const FIDMap &FM, unsigned indent) {
99   if (L.isInvalid()) return;
100
101   FullSourceLoc Loc(SM.getExpansionLoc(L), const_cast<SourceManager &>(SM));
102
103   Indent(o, indent) << "<dict>\n";
104   Indent(o, indent) << " <key>line</key>";
105   EmitInteger(o, Loc.getExpansionLineNumber()) << '\n';
106   Indent(o, indent) << " <key>col</key>";
107   EmitInteger(o, Loc.getExpansionColumnNumber()) << '\n';
108   Indent(o, indent) << " <key>file</key>";
109   EmitInteger(o, GetFID(FM, SM, Loc)) << '\n';
110   Indent(o, indent) << "</dict>\n";
111 }
112
113 inline void EmitRange(raw_ostream &o, const SourceManager &SM,
114                       CharSourceRange R, const FIDMap &FM, unsigned indent) {
115   if (R.isInvalid()) return;
116
117   assert(R.isCharRange() && "cannot handle a token range");
118   Indent(o, indent) << "<array>\n";
119   EmitLocation(o, SM, R.getBegin(), FM, indent + 1);
120   EmitLocation(o, SM, R.getEnd(), FM, indent + 1);
121   Indent(o, indent) << "</array>\n";
122 }
123
124 } // namespace markup
125 } // namespace clang
126
127 #endif // LLVM_CLANG_BASIC_PLISTSUPPORT_H