Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/include/clang/Basic/JsonSupport.h
Line
Count
Source (jump to first uncovered line)
1
//===- JsonSupport.h - JSON 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_JSONSUPPORT_H
10
#define LLVM_CLANG_BASIC_JSONSUPPORT_H
11
12
#include "clang/Basic/LLVM.h"
13
#include "clang/Basic/SourceManager.h"
14
#include "llvm/ADT/StringRef.h"
15
#include "llvm/Support/raw_ostream.h"
16
17
18
namespace clang {
19
20
inline raw_ostream &Indent(raw_ostream &Out, const unsigned int Space,
21
92
                           bool IsDot) {
22
398
  for (unsigned int I = 0; I < Space * 2; 
++I306
)
23
306
    Out << (IsDot ? 
"&nbsp;"0
: " ");
24
92
  return Out;
25
92
}
26
27
18
inline std::string JsonFormat(StringRef RawSR, bool AddQuotes) {
28
18
  if (RawSR.empty())
29
0
    return "null";
30
18
31
18
  // Trim special characters.
32
18
  std::string Str = RawSR.trim().str();
33
18
  size_t Pos = 0;
34
18
35
18
  // Escape backslashes.
36
18
  while (true) {
37
18
    Pos = Str.find('\\', Pos);
38
18
    if (Pos == std::string::npos)
39
18
      break;
40
0
41
0
    // Prevent bad conversions.
42
0
    size_t TempPos = (Pos != 0) ? Pos - 1 : 0;
43
0
44
0
    // See whether the current backslash is not escaped.
45
0
    if (TempPos != Str.find("\\\\", Pos)) {
46
0
      Str.insert(Pos, "\\");
47
0
      ++Pos; // As we insert the backslash move plus one.
48
0
    }
49
0
50
0
    ++Pos;
51
0
  }
52
18
53
18
  // Escape double quotes.
54
18
  Pos = 0;
55
18
  while (true) {
56
18
    Pos = Str.find('\"', Pos);
57
18
    if (Pos == std::string::npos)
58
18
      break;
59
0
60
0
    // Prevent bad conversions.
61
0
    size_t TempPos = (Pos != 0) ? Pos - 1 : 0;
62
0
63
0
    // See whether the current double quote is not escaped.
64
0
    if (TempPos != Str.find("\\\"", Pos)) {
65
0
      Str.insert(Pos, "\\");
66
0
      ++Pos; // As we insert the escape-character move plus one.
67
0
    }
68
0
69
0
    ++Pos;
70
0
  }
71
18
72
18
  // Remove new-lines.
73
18
  Str.erase(std::remove(Str.begin(), Str.end(), '\n'), Str.end());
74
18
75
18
  if (!AddQuotes)
76
0
    return Str;
77
18
78
18
  return '\"' + Str + '\"';
79
18
}
80
81
inline void printSourceLocationAsJson(raw_ostream &Out, SourceLocation Loc,
82
                                      const SourceManager &SM,
83
0
                                      bool AddBraces = true) {
84
0
  // Mostly copy-pasted from SourceLocation::print.
85
0
  if (!Loc.isValid()) {
86
0
    Out << "null";
87
0
    return;
88
0
  }
89
0
90
0
  if (Loc.isFileID()) {
91
0
    PresumedLoc PLoc = SM.getPresumedLoc(Loc);
92
0
93
0
    if (PLoc.isInvalid()) {
94
0
      Out << "null";
95
0
      return;
96
0
    }
97
0
    // The macro expansion and spelling pos is identical for file locs.
98
0
    if (AddBraces)
99
0
      Out << "{ ";
100
0
    Out << "\"line\": " << PLoc.getLine()
101
0
        << ", \"column\": " << PLoc.getColumn()
102
0
        << ", \"file\": \"" << PLoc.getFilename() << "\"";
103
0
    if (AddBraces)
104
0
      Out << " }";
105
0
    return;
106
0
  }
107
0
108
0
  // We want 'location: { ..., spelling: { ... }}' but not
109
0
  // 'location: { ... }, spelling: { ... }', hence the dance
110
0
  // with braces.
111
0
  Out << "{ ";
112
0
  printSourceLocationAsJson(Out, SM.getExpansionLoc(Loc), SM, false);
113
0
  Out << ", \"spelling\": ";
114
0
  printSourceLocationAsJson(Out, SM.getSpellingLoc(Loc), SM, true);
115
0
  Out << " }";
116
0
}
117
} // namespace clang
118
119
#endif // LLVM_CLANG_BASIC_JSONSUPPORT_H