Coverage Report

Created: 2019-05-19 14:56

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/include/clang/Basic/PlistSupport.h
Line
Count
Source (jump to first uncovered line)
1
//===- PlistSupport.h - Plist Output Utilities ------------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#ifndef LLVM_CLANG_BASIC_PLISTSUPPORT_H
10
#define LLVM_CLANG_BASIC_PLISTSUPPORT_H
11
12
#include "clang/Basic/LLVM.h"
13
#include "clang/Basic/SourceLocation.h"
14
#include "clang/Basic/SourceManager.h"
15
#include "llvm/ADT/DenseMap.h"
16
#include "llvm/ADT/SmallVector.h"
17
#include "llvm/ADT/StringRef.h"
18
#include "llvm/Support/raw_ostream.h"
19
#include <cassert>
20
#include <cstdint>
21
22
namespace clang {
23
namespace markup {
24
25
using FIDMap = llvm::DenseMap<FileID, unsigned>;
26
27
inline unsigned AddFID(FIDMap &FIDs, SmallVectorImpl<FileID> &V,
28
8.16k
                   FileID FID) {
29
8.16k
  FIDMap::iterator I = FIDs.find(FID);
30
8.16k
  if (I != FIDs.end())
31
8.11k
    return I->second;
32
54
  unsigned NewValue = V.size();
33
54
  FIDs[FID] = NewValue;
34
54
  V.push_back(FID);
35
54
  return NewValue;
36
54
}
37
38
inline unsigned AddFID(FIDMap &FIDs, SmallVectorImpl<FileID> &V,
39
7.56k
                   const SourceManager &SM, SourceLocation L) {
40
7.56k
  FileID FID = SM.getFileID(SM.getExpansionLoc(L));
41
7.56k
  return AddFID(FIDs, V, FID);
42
7.56k
}
43
44
14.4k
inline unsigned GetFID(const FIDMap &FIDs, FileID FID) {
45
14.4k
  FIDMap::const_iterator I = FIDs.find(FID);
46
14.4k
  assert(I != FIDs.end());
47
14.4k
  return I->second;
48
14.4k
}
49
50
inline unsigned GetFID(const FIDMap &FIDs, const SourceManager &SM,
51
14.4k
                       SourceLocation L) {
52
14.4k
  FileID FID = SM.getFileID(SM.getExpansionLoc(L));
53
14.4k
  return GetFID(FIDs, FID);
54
14.4k
}
55
56
133k
inline raw_ostream &Indent(raw_ostream &o, const unsigned indent) {
57
1.10M
  for (unsigned i = 0; i < indent; 
++i968k
)
58
968k
    o << ' ';
59
133k
  return o;
60
133k
}
61
62
54
inline raw_ostream &EmitPlistHeader(raw_ostream &o) {
63
54
  static const char *PlistHeader =
64
54
      "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
65
54
      "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
66
54
      "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
67
54
      "<plist version=\"1.0\">\n";
68
54
  return o << PlistHeader;
69
54
}
70
71
48.4k
inline raw_ostream &EmitInteger(raw_ostream &o, int64_t value) {
72
48.4k
  o << "<integer>";
73
48.4k
  o << value;
74
48.4k
  o << "</integer>";
75
48.4k
  return o;
76
48.4k
}
77
78
8.14k
inline raw_ostream &EmitString(raw_ostream &o, StringRef s) {
79
8.14k
  o << "<string>";
80
345k
  for (StringRef::const_iterator I = s.begin(), E = s.end(); I != E; 
++I336k
) {
81
336k
    char c = *I;
82
336k
    switch (c) {
83
336k
    default:
84
328k
      o << c;
85
328k
      break;
86
336k
    case '&':
87
10
      o << "&amp;";
88
10
      break;
89
336k
    case '<':
90
347
      o << "&lt;";
91
347
      break;
92
336k
    case '>':
93
345
      o << "&gt;";
94
345
      break;
95
336k
    case '\'':
96
7.51k
      o << "&apos;";
97
7.51k
      break;
98
336k
    case '\"':
99
26
      o << "&quot;";
100
26
      break;
101
336k
    }
102
336k
  }
103
8.14k
  o << "</string>";
104
8.14k
  return o;
105
8.14k
}
106
107
inline void EmitLocation(raw_ostream &o, const SourceManager &SM,
108
14.4k
                         SourceLocation L, const FIDMap &FM, unsigned indent) {
109
14.4k
  if (L.isInvalid()) 
return0
;
110
14.4k
111
14.4k
  FullSourceLoc Loc(SM.getExpansionLoc(L), const_cast<SourceManager &>(SM));
112
14.4k
113
14.4k
  Indent(o, indent) << "<dict>\n";
114
14.4k
  Indent(o, indent) << " <key>line</key>";
115
14.4k
  EmitInteger(o, Loc.getExpansionLineNumber()) << '\n';
116
14.4k
  Indent(o, indent) << " <key>col</key>";
117
14.4k
  EmitInteger(o, Loc.getExpansionColumnNumber()) << '\n';
118
14.4k
  Indent(o, indent) << " <key>file</key>";
119
14.4k
  EmitInteger(o, GetFID(FM, SM, Loc)) << '\n';
120
14.4k
  Indent(o, indent) << "</dict>\n";
121
14.4k
}
122
123
inline void EmitRange(raw_ostream &o, const SourceManager &SM,
124
5.95k
                      CharSourceRange R, const FIDMap &FM, unsigned indent) {
125
5.95k
  if (R.isInvalid()) 
return0
;
126
5.95k
127
5.95k
  assert(R.isCharRange() && "cannot handle a token range");
128
5.95k
  Indent(o, indent) << "<array>\n";
129
5.95k
  EmitLocation(o, SM, R.getBegin(), FM, indent + 1);
130
5.95k
131
5.95k
  // The ".getLocWithOffset(-1)" emulates the behavior of an off-by-one bug
132
5.95k
  // in Lexer that is already fixed. It is here for backwards compatibility
133
5.95k
  // even though it is incorrect.
134
5.95k
  EmitLocation(o, SM, R.getEnd().getLocWithOffset(-1), FM, indent + 1);
135
5.95k
  Indent(o, indent) << "</array>\n";
136
5.95k
}
137
138
} // namespace markup
139
} // namespace clang
140
141
#endif // LLVM_CLANG_BASIC_PLISTSUPPORT_H