Coverage Report

Created: 2020-02-25 14:32

/Users/buildslave/jenkins/workspace/coverage/llvm-project/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.45k
                   FileID FID) {
29
8.45k
  FIDMap::iterator I = FIDs.find(FID);
30
8.45k
  if (I != FIDs.end())
31
8.38k
    return I->second;
32
63
  unsigned NewValue = V.size();
33
63
  FIDs[FID] = NewValue;
34
63
  V.push_back(FID);
35
63
  return NewValue;
36
63
}
37
38
inline unsigned AddFID(FIDMap &FIDs, SmallVectorImpl<FileID> &V,
39
7.83k
                   const SourceManager &SM, SourceLocation L) {
40
7.83k
  FileID FID = SM.getFileID(SM.getExpansionLoc(L));
41
7.83k
  return AddFID(FIDs, V, FID);
42
7.83k
}
43
44
14.8k
inline unsigned GetFID(const FIDMap &FIDs, FileID FID) {
45
14.8k
  FIDMap::const_iterator I = FIDs.find(FID);
46
14.8k
  assert(I != FIDs.end());
47
14.8k
  return I->second;
48
14.8k
}
49
50
inline unsigned GetFID(const FIDMap &FIDs, const SourceManager &SM,
51
14.8k
                       SourceLocation L) {
52
14.8k
  FileID FID = SM.getFileID(SM.getExpansionLoc(L));
53
14.8k
  return GetFID(FIDs, FID);
54
14.8k
}
55
56
138k
inline raw_ostream &Indent(raw_ostream &o, const unsigned indent) {
57
1.13M
  for (unsigned i = 0; i < indent; 
++i998k
)
58
998k
    o << ' ';
59
138k
  return o;
60
138k
}
61
62
62
inline raw_ostream &EmitPlistHeader(raw_ostream &o) {
63
62
  static const char *PlistHeader =
64
62
      "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
65
62
      "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" "
66
62
      "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
67
62
      "<plist version=\"1.0\">\n";
68
62
  return o << PlistHeader;
69
62
}
70
71
49.9k
inline raw_ostream &EmitInteger(raw_ostream &o, int64_t value) {
72
49.9k
  o << "<integer>";
73
49.9k
  o << value;
74
49.9k
  o << "</integer>";
75
49.9k
  return o;
76
49.9k
}
77
78
8.41k
inline raw_ostream &EmitString(raw_ostream &o, StringRef s) {
79
8.41k
  o << "<string>";
80
357k
  for (StringRef::const_iterator I = s.begin(), E = s.end(); I != E; 
++I349k
) {
81
349k
    char c = *I;
82
349k
    switch (c) {
83
340k
    default:
84
340k
      o << c;
85
340k
      break;
86
12
    case '&':
87
12
      o << "&amp;";
88
12
      break;
89
347
    case '<':
90
347
      o << "&lt;";
91
347
      break;
92
349
    case '>':
93
349
      o << "&gt;";
94
349
      break;
95
7.84k
    case '\'':
96
7.84k
      o << "&apos;";
97
7.84k
      break;
98
60
    case '\"':
99
60
      o << "&quot;";
100
60
      break;
101
349k
    }
102
349k
  }
103
8.41k
  o << "</string>";
104
8.41k
  return o;
105
8.41k
}
106
107
inline void EmitLocation(raw_ostream &o, const SourceManager &SM,
108
14.8k
                         SourceLocation L, const FIDMap &FM, unsigned indent) {
109
14.8k
  if (L.isInvalid()) 
return0
;
110
14.8k
111
14.8k
  FullSourceLoc Loc(SM.getExpansionLoc(L), const_cast<SourceManager &>(SM));
112
14.8k
113
14.8k
  Indent(o, indent) << "<dict>\n";
114
14.8k
  Indent(o, indent) << " <key>line</key>";
115
14.8k
  EmitInteger(o, Loc.getExpansionLineNumber()) << '\n';
116
14.8k
  Indent(o, indent) << " <key>col</key>";
117
14.8k
  EmitInteger(o, Loc.getExpansionColumnNumber()) << '\n';
118
14.8k
  Indent(o, indent) << " <key>file</key>";
119
14.8k
  EmitInteger(o, GetFID(FM, SM, Loc)) << '\n';
120
14.8k
  Indent(o, indent) << "</dict>\n";
121
14.8k
}
122
123
inline void EmitRange(raw_ostream &o, const SourceManager &SM,
124
6.13k
                      CharSourceRange R, const FIDMap &FM, unsigned indent) {
125
6.13k
  if (R.isInvalid()) 
return0
;
126
6.13k
127
6.13k
  assert(R.isCharRange() && "cannot handle a token range");
128
6.13k
  Indent(o, indent) << "<array>\n";
129
6.13k
  EmitLocation(o, SM, R.getBegin(), FM, indent + 1);
130
6.13k
131
6.13k
  // The ".getLocWithOffset(-1)" emulates the behavior of an off-by-one bug
132
6.13k
  // in Lexer that is already fixed. It is here for backwards compatibility
133
6.13k
  // even though it is incorrect.
134
6.13k
  EmitLocation(o, SM, R.getEnd().getLocWithOffset(-1), FM, indent + 1);
135
6.13k
  Indent(o, indent) << "</array>\n";
136
6.13k
}
137
138
} // namespace markup
139
} // namespace clang
140
141
#endif // LLVM_CLANG_BASIC_PLISTSUPPORT_H