clang  8.0.0svn
PlistSupport.h
Go to the documentation of this file.
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"
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
void EmitLocation(raw_ostream &o, const SourceManager &SM, SourceLocation L, const FIDMap &FM, unsigned indent)
Definition: PlistSupport.h:108
unsigned GetFID(const FIDMap &FIDs, FileID FID)
Definition: PlistSupport.h:45
Defines the SourceManager interface.
raw_ostream & EmitInteger(raw_ostream &o, int64_t value)
Definition: PlistSupport.h:72
SourceLocation getBegin() const
unsigned AddFID(FIDMap &FIDs, SmallVectorImpl< FileID > &V, FileID FID)
Definition: PlistSupport.h:28
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
SourceLocation getExpansionLoc(SourceLocation Loc) const
Given a SourceLocation object Loc, return the expansion location referenced by the ID...
unsigned getExpansionColumnNumber(bool *Invalid=nullptr) const
Represents a character-granular source range.
const SourceManager & SM
Definition: Format.cpp:1472
llvm::DenseMap< FileID, unsigned > FIDMap
Definition: PlistSupport.h:26
Encodes a location in the source.
raw_ostream & EmitPlistHeader(raw_ostream &o)
Definition: PlistSupport.h:63
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Dataflow Directional Tag Classes.
raw_ostream & Indent(raw_ostream &o, const unsigned indent)
Definition: PlistSupport.h:57
void EmitRange(raw_ostream &o, const SourceManager &SM, CharSourceRange R, const FIDMap &FM, unsigned indent)
Definition: PlistSupport.h:124
unsigned getExpansionLineNumber(bool *Invalid=nullptr) const
FileID getFileID(SourceLocation SpellingLoc) const
Return the FileID for a SourceLocation.
SourceLocation getEnd() const
raw_ostream & EmitString(raw_ostream &o, StringRef s)
Definition: PlistSupport.h:79
Defines the clang::SourceLocation class and associated facilities.
A SourceLocation and its associated SourceManager.
This class handles loading and caching of source files into memory.