1 //===- PlistSupport.h - Plist Output Utilities ------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_CLANG_BASIC_PLISTSUPPORT_H
11 #define LLVM_CLANG_BASIC_PLISTSUPPORT_H
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"
26 using FIDMap = llvm::DenseMap<FileID, unsigned>;
28 inline unsigned AddFID(FIDMap &FIDs, SmallVectorImpl<FileID> &V,
30 FIDMap::iterator I = FIDs.find(FID);
33 unsigned NewValue = V.size();
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);
45 inline unsigned GetFID(const FIDMap &FIDs, FileID FID) {
46 FIDMap::const_iterator I = FIDs.find(FID);
47 assert(I != FIDs.end());
51 inline unsigned GetFID(const FIDMap &FIDs, const SourceManager &SM,
53 FileID FID = SM.getFileID(SM.getExpansionLoc(L));
54 return GetFID(FIDs, FID);
57 inline raw_ostream &Indent(raw_ostream &o, const unsigned indent) {
58 for (unsigned i = 0; i < indent; ++i)
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;
72 inline raw_ostream &EmitInteger(raw_ostream &o, int64_t value) {
79 inline raw_ostream &EmitString(raw_ostream &o, StringRef s) {
81 for (StringRef::const_iterator I = s.begin(), E = s.end(); I != E; ++I) {
108 inline void EmitLocation(raw_ostream &o, const SourceManager &SM,
109 SourceLocation L, const FIDMap &FM, unsigned indent) {
110 if (L.isInvalid()) return;
112 FullSourceLoc Loc(SM.getExpansionLoc(L), const_cast<SourceManager &>(SM));
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";
124 inline void EmitRange(raw_ostream &o, const SourceManager &SM,
125 CharSourceRange R, const FIDMap &FM, unsigned indent) {
126 if (R.isInvalid()) return;
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";
135 } // namespace markup
138 #endif // LLVM_CLANG_BASIC_PLISTSUPPORT_H