clang  7.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 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
void AddFID(FIDMap &FIDs, SmallVectorImpl< FileID > &V, const SourceManager &SM, SourceLocation L)
Definition: PlistSupport.h:28
void EmitLocation(raw_ostream &o, const SourceManager &SM, SourceLocation L, const FIDMap &FM, unsigned indent)
Definition: PlistSupport.h:97
Defines the SourceManager interface.
raw_ostream & EmitInteger(raw_ostream &o, int64_t value)
Definition: PlistSupport.h:61
SourceLocation getBegin() const
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
unsigned GetFID(const FIDMap &FIDs, const SourceManager &SM, SourceLocation L)
Definition: PlistSupport.h:38
Represents a character-granular source range.
const SourceManager & SM
Definition: Format.cpp:1474
llvm::DenseMap< FileID, unsigned > FIDMap
Definition: PlistSupport.h:26
Encodes a location in the source.
raw_ostream & EmitPlistHeader(raw_ostream &o)
Definition: PlistSupport.h:52
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:46
void EmitRange(raw_ostream &o, const SourceManager &SM, CharSourceRange R, const FIDMap &FM, unsigned indent)
Definition: PlistSupport.h:113
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:68
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.