Coverage Report

Created: 2020-02-15 09:57

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