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/FileManager.h"
14 #include "clang/Basic/SourceManager.h"
15 #include "clang/Lex/Lexer.h"
16 #include "llvm/Support/raw_ostream.h"
20 typedef llvm::DenseMap<FileID, unsigned> FIDMap;
22 inline void AddFID(FIDMap &FIDs, SmallVectorImpl<FileID> &V,
23 const SourceManager &SM, SourceLocation L) {
24 FileID FID = SM.getFileID(SM.getExpansionLoc(L));
25 FIDMap::iterator I = FIDs.find(FID);
32 inline unsigned GetFID(const FIDMap &FIDs, const SourceManager &SM,
34 FileID FID = SM.getFileID(SM.getExpansionLoc(L));
35 FIDMap::const_iterator I = FIDs.find(FID);
36 assert(I != FIDs.end());
40 inline raw_ostream &Indent(raw_ostream &o, const unsigned indent) {
41 for (unsigned i = 0; i < indent; ++i)
46 inline raw_ostream &EmitPlistHeader(raw_ostream &o) {
47 static const char *PlistHeader =
48 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
49 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
50 "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
51 "<plist version=\"1.0\">\n";
52 return o << PlistHeader;
55 inline raw_ostream &EmitInteger(raw_ostream &o, int64_t value) {
62 inline raw_ostream &EmitString(raw_ostream &o, StringRef s) {
64 for (StringRef::const_iterator I = s.begin(), E = s.end(); I != E; ++I) {
91 inline void EmitLocation(raw_ostream &o, const SourceManager &SM,
92 const LangOptions &LangOpts, SourceLocation L,
93 const FIDMap &FM, unsigned indent,
94 bool extend = false) {
95 FullSourceLoc Loc(SM.getExpansionLoc(L), const_cast<SourceManager &>(SM));
97 // Add in the length of the token, so that we cover multi-char tokens.
99 extend ? Lexer::MeasureTokenLength(Loc, SM, LangOpts) - 1 : 0;
101 Indent(o, indent) << "<dict>\n";
102 Indent(o, indent) << " <key>line</key>";
103 EmitInteger(o, Loc.getExpansionLineNumber()) << '\n';
104 Indent(o, indent) << " <key>col</key>";
105 EmitInteger(o, Loc.getExpansionColumnNumber() + offset) << '\n';
106 Indent(o, indent) << " <key>file</key>";
107 EmitInteger(o, GetFID(FM, SM, Loc)) << '\n';
108 Indent(o, indent) << "</dict>\n";
111 inline void EmitRange(raw_ostream &o, const SourceManager &SM,
112 const LangOptions &LangOpts, CharSourceRange R,
113 const FIDMap &FM, unsigned indent) {
114 Indent(o, indent) << "<array>\n";
115 EmitLocation(o, SM, LangOpts, R.getBegin(), FM, indent + 1);
116 EmitLocation(o, SM, LangOpts, R.getEnd(), FM, indent + 1, R.isTokenRange());
117 Indent(o, indent) << "</array>\n";