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 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);
38 inline unsigned GetFID(const FIDMap &FIDs, const SourceManager &SM,
40 FileID FID = SM.getFileID(SM.getExpansionLoc(L));
41 FIDMap::const_iterator I = FIDs.find(FID);
42 assert(I != FIDs.end());
46 inline raw_ostream &Indent(raw_ostream &o, const unsigned indent) {
47 for (unsigned i = 0; i < indent; ++i)
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;
61 inline raw_ostream &EmitInteger(raw_ostream &o, int64_t value) {
68 inline raw_ostream &EmitString(raw_ostream &o, StringRef s) {
70 for (StringRef::const_iterator I = s.begin(), E = s.end(); I != E; ++I) {
97 inline void EmitLocation(raw_ostream &o, const SourceManager &SM,
98 SourceLocation L, const FIDMap &FM, unsigned indent) {
99 if (L.isInvalid()) return;
101 FullSourceLoc Loc(SM.getExpansionLoc(L), const_cast<SourceManager &>(SM));
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";
113 inline void EmitRange(raw_ostream &o, const SourceManager &SM,
114 CharSourceRange R, const FIDMap &FM, unsigned indent) {
115 if (R.isInvalid()) return;
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";
124 } // namespace markup
127 #endif // LLVM_CLANG_BASIC_PLISTSUPPORT_H