Coverage Report

Created: 2023-11-11 10:31

/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/Path.h"
16
#include "llvm/Support/raw_ostream.h"
17
#include <iterator>
18
19
namespace clang {
20
21
inline raw_ostream &Indent(raw_ostream &Out, const unsigned int Space,
22
3.97k
                           bool IsDot) {
23
20.2k
  for (unsigned int I = 0; I < Space * 2; 
++I16.3k
)
24
16.3k
    Out << (IsDot ? 
"&nbsp;"11.5k
:
" "4.72k
);
25
3.97k
  return Out;
26
3.97k
}
27
28
677
inline std::string JsonFormat(StringRef RawSR, bool AddQuotes) {
29
677
  if (RawSR.empty())
30
28
    return "null";
31
32
  // Trim special characters.
33
649
  std::string Str = RawSR.trim().str();
34
649
  size_t Pos = 0;
35
36
  // Escape backslashes.
37
689
  while (true) {
38
689
    Pos = Str.find('\\', Pos);
39
689
    if (Pos == std::string::npos)
40
649
      break;
41
42
    // Prevent bad conversions.
43
40
    size_t TempPos = (Pos != 0) ? Pos - 1 : 
00
;
44
45
    // See whether the current backslash is not escaped.
46
40
    if (TempPos != Str.find("\\\\", Pos)) {
47
40
      Str.insert(Pos, "\\");
48
40
      ++Pos; // As we insert the backslash move plus one.
49
40
    }
50
51
40
    ++Pos;
52
40
  }
53
54
  // Escape double quotes.
55
649
  Pos = 0;
56
725
  while (true) {
57
725
    Pos = Str.find('\"', Pos);
58
725
    if (Pos == std::string::npos)
59
649
      break;
60
61
    // Prevent bad conversions.
62
76
    size_t TempPos = (Pos != 0) ? 
Pos - 162
:
014
;
63
64
    // See whether the current double quote is not escaped.
65
76
    if (TempPos != Str.find("\\\"", Pos)) {
66
76
      Str.insert(Pos, "\\");
67
76
      ++Pos; // As we insert the escape-character move plus one.
68
76
    }
69
70
76
    ++Pos;
71
76
  }
72
73
  // Remove new-lines.
74
649
  llvm::erase(Str, '\n');
75
76
649
  if (!AddQuotes)
77
0
    return Str;
78
79
649
  return '\"' + Str + '\"';
80
649
}
81
82
inline void printSourceLocationAsJson(raw_ostream &Out, SourceLocation Loc,
83
                                      const SourceManager &SM,
84
170
                                      bool AddBraces = true) {
85
  // Mostly copy-pasted from SourceLocation::print.
86
170
  if (!Loc.isValid()) {
87
0
    Out << "null";
88
0
    return;
89
0
  }
90
91
170
  if (Loc.isFileID()) {
92
165
    PresumedLoc PLoc = SM.getPresumedLoc(Loc);
93
94
165
    if (PLoc.isInvalid()) {
95
0
      Out << "null";
96
0
      return;
97
0
    }
98
    // The macro expansion and spelling pos is identical for file locs.
99
165
    if (AddBraces)
100
160
      Out << "{ ";
101
165
    std::string filename(PLoc.getFilename());
102
165
    if (is_style_windows(llvm::sys::path::Style::native)) {
103
      // Remove forbidden Windows path characters
104
0
      llvm::erase_if(filename, [](auto Char) {
105
0
        static const char ForbiddenChars[] = "<>*?\"|";
106
0
        return llvm::is_contained(ForbiddenChars, Char);
107
0
      });
108
      // Handle windows-specific path delimiters.
109
0
      std::replace(filename.begin(), filename.end(), '\\', '/');
110
0
    }
111
165
    Out << "\"line\": " << PLoc.getLine()
112
165
        << ", \"column\": " << PLoc.getColumn()
113
165
        << ", \"file\": \"" << filename << "\"";
114
165
    if (AddBraces)
115
160
      Out << " }";
116
165
    return;
117
165
  }
118
119
  // We want 'location: { ..., spelling: { ... }}' but not
120
  // 'location: { ... }, spelling: { ... }', hence the dance
121
  // with braces.
122
5
  Out << "{ ";
123
5
  printSourceLocationAsJson(Out, SM.getExpansionLoc(Loc), SM, false);
124
5
  Out << ", \"spelling\": ";
125
5
  printSourceLocationAsJson(Out, SM.getSpellingLoc(Loc), SM, true);
126
5
  Out << " }";
127
5
}
128
} // namespace clang
129
130
#endif // LLVM_CLANG_BASIC_JSONSUPPORT_H