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/SourceManager.h"
14 #include "llvm/Support/raw_ostream.h"
18 typedef llvm::DenseMap<FileID, unsigned> FIDMap;
20 inline void AddFID(FIDMap &FIDs, SmallVectorImpl<FileID> &V,
21 const SourceManager &SM, SourceLocation L) {
22 FileID FID = SM.getFileID(SM.getExpansionLoc(L));
23 FIDMap::iterator I = FIDs.find(FID);
30 inline unsigned GetFID(const FIDMap &FIDs, const SourceManager &SM,
32 FileID FID = SM.getFileID(SM.getExpansionLoc(L));
33 FIDMap::const_iterator I = FIDs.find(FID);
34 assert(I != FIDs.end());
38 inline raw_ostream &Indent(raw_ostream &o, const unsigned indent) {
39 for (unsigned i = 0; i < indent; ++i)
44 inline raw_ostream &EmitPlistHeader(raw_ostream &o) {
45 static const char *PlistHeader =
46 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
47 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
48 "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
49 "<plist version=\"1.0\">\n";
50 return o << PlistHeader;
53 inline raw_ostream &EmitInteger(raw_ostream &o, int64_t value) {
60 inline raw_ostream &EmitString(raw_ostream &o, StringRef s) {
62 for (StringRef::const_iterator I = s.begin(), E = s.end(); I != E; ++I) {
89 inline void EmitLocation(raw_ostream &o, const SourceManager &SM,
90 SourceLocation L, const FIDMap &FM, unsigned indent) {
91 if (L.isInvalid()) return;
93 FullSourceLoc Loc(SM.getExpansionLoc(L), const_cast<SourceManager &>(SM));
95 Indent(o, indent) << "<dict>\n";
96 Indent(o, indent) << " <key>line</key>";
97 EmitInteger(o, Loc.getExpansionLineNumber()) << '\n';
98 Indent(o, indent) << " <key>col</key>";
99 EmitInteger(o, Loc.getExpansionColumnNumber()) << '\n';
100 Indent(o, indent) << " <key>file</key>";
101 EmitInteger(o, GetFID(FM, SM, Loc)) << '\n';
102 Indent(o, indent) << "</dict>\n";
105 inline void EmitRange(raw_ostream &o, const SourceManager &SM,
106 CharSourceRange R, const FIDMap &FM, unsigned indent) {
107 if (R.isInvalid()) return;
109 assert(R.isCharRange() && "cannot handle a token range");
110 Indent(o, indent) << "<array>\n";
111 EmitLocation(o, SM, R.getBegin(), FM, indent + 1);
112 EmitLocation(o, SM, R.getEnd(), FM, indent + 1);
113 Indent(o, indent) << "</array>\n";