Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/Frontend/LogDiagnosticPrinter.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- LogDiagnosticPrinter.cpp - Log Diagnostic Printer ----------------===//
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
#include "clang/Frontend/LogDiagnosticPrinter.h"
10
#include "clang/Basic/DiagnosticOptions.h"
11
#include "clang/Basic/FileManager.h"
12
#include "clang/Basic/PlistSupport.h"
13
#include "clang/Basic/SourceManager.h"
14
#include "llvm/ADT/SmallString.h"
15
#include "llvm/Support/ErrorHandling.h"
16
#include "llvm/Support/raw_ostream.h"
17
using namespace clang;
18
using namespace markup;
19
20
LogDiagnosticPrinter::LogDiagnosticPrinter(
21
    raw_ostream &os, DiagnosticOptions *diags,
22
    std::unique_ptr<raw_ostream> StreamOwner)
23
    : OS(os), StreamOwner(std::move(StreamOwner)), LangOpts(nullptr),
24
3
      DiagOpts(diags) {}
25
26
18
static StringRef getLevelName(DiagnosticsEngine::Level Level) {
27
18
  switch (Level) {
28
18
  
case DiagnosticsEngine::Ignored: return "ignored"0
;
29
18
  
case DiagnosticsEngine::Remark: return "remark"0
;
30
18
  
case DiagnosticsEngine::Note: return "note"0
;
31
18
  case DiagnosticsEngine::Warning: return "warning";
32
18
  
case DiagnosticsEngine::Error: return "error"0
;
33
18
  
case DiagnosticsEngine::Fatal: return "fatal error"0
;
34
0
  }
35
0
  llvm_unreachable("Invalid DiagnosticsEngine level!");
36
0
}
37
38
void
39
LogDiagnosticPrinter::EmitDiagEntry(llvm::raw_ostream &OS,
40
18
                                    const LogDiagnosticPrinter::DiagEntry &DE) {
41
18
  OS << "    <dict>\n";
42
18
  OS << "      <key>level</key>\n"
43
18
     << "      ";
44
18
  EmitString(OS, getLevelName(DE.DiagnosticLevel)) << '\n';
45
18
  if (!DE.Filename.empty()) {
46
17
    OS << "      <key>filename</key>\n"
47
17
       << "      ";
48
17
    EmitString(OS, DE.Filename) << '\n';
49
17
  }
50
18
  if (DE.Line != 0) {
51
17
    OS << "      <key>line</key>\n"
52
17
       << "      ";
53
17
    EmitInteger(OS, DE.Line) << '\n';
54
17
  }
55
18
  if (DE.Column != 0) {
56
17
    OS << "      <key>column</key>\n"
57
17
       << "      ";
58
17
    EmitInteger(OS, DE.Column) << '\n';
59
17
  }
60
18
  if (!DE.Message.empty()) {
61
18
    OS << "      <key>message</key>\n"
62
18
       << "      ";
63
18
    EmitString(OS, DE.Message) << '\n';
64
18
  }
65
18
  OS << "      <key>ID</key>\n"
66
18
     << "      ";
67
18
  EmitInteger(OS, DE.DiagnosticID) << '\n';
68
18
  if (!DE.WarningOption.empty()) {
69
18
    OS << "      <key>WarningOption</key>\n"
70
18
       << "      ";
71
18
    EmitString(OS, DE.WarningOption) << '\n';
72
18
  }
73
18
  OS << "    </dict>\n";
74
18
}
75
76
2
void LogDiagnosticPrinter::EndSourceFile() {
77
2
  // We emit all the diagnostics in EndSourceFile. However, we don't emit any
78
2
  // entry if no diagnostics were present.
79
2
  //
80
2
  // Note that DiagnosticConsumer has no "end-of-compilation" callback, so we
81
2
  // will miss any diagnostics which are emitted after and outside the
82
2
  // translation unit processing.
83
2
  if (Entries.empty())
84
0
    return;
85
2
86
2
  // Write to a temporary string to ensure atomic write of diagnostic object.
87
2
  SmallString<512> Msg;
88
2
  llvm::raw_svector_ostream OS(Msg);
89
2
90
2
  OS << "<dict>\n";
91
2
  if (!MainFilename.empty()) {
92
2
    OS << "  <key>main-file</key>\n"
93
2
       << "  ";
94
2
    EmitString(OS, MainFilename) << '\n';
95
2
  }
96
2
  if (!DwarfDebugFlags.empty()) {
97
2
    OS << "  <key>dwarf-debug-flags</key>\n"
98
2
       << "  ";
99
2
    EmitString(OS, DwarfDebugFlags) << '\n';
100
2
  }
101
2
  OS << "  <key>diagnostics</key>\n";
102
2
  OS << "  <array>\n";
103
2
  for (auto &DE : Entries)
104
18
    EmitDiagEntry(OS, DE);
105
2
  OS << "  </array>\n";
106
2
  OS << "</dict>\n";
107
2
108
2
  this->OS << OS.str();
109
2
}
110
111
void LogDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level,
112
19
                                            const Diagnostic &Info) {
113
19
  // Default implementation (Warnings/errors count).
114
19
  DiagnosticConsumer::HandleDiagnostic(Level, Info);
115
19
116
19
  // Initialize the main file name, if we haven't already fetched it.
117
19
  if (MainFilename.empty() && 
Info.hasSourceManager()4
) {
118
2
    const SourceManager &SM = Info.getSourceManager();
119
2
    FileID FID = SM.getMainFileID();
120
2
    if (FID.isValid()) {
121
2
      const FileEntry *FE = SM.getFileEntryForID(FID);
122
2
      if (FE && FE->isValid())
123
2
        MainFilename = FE->getName();
124
2
    }
125
2
  }
126
19
127
19
  // Create the diag entry.
128
19
  DiagEntry DE;
129
19
  DE.DiagnosticID = Info.getID();
130
19
  DE.DiagnosticLevel = Level;
131
19
132
19
  DE.WarningOption = DiagnosticIDs::getWarningOptionForDiag(DE.DiagnosticID);
133
19
134
19
  // Format the message.
135
19
  SmallString<100> MessageStr;
136
19
  Info.FormatDiagnostic(MessageStr);
137
19
  DE.Message = MessageStr.str();
138
19
139
19
  // Set the location information.
140
19
  DE.Filename = "";
141
19
  DE.Line = DE.Column = 0;
142
19
  if (Info.getLocation().isValid() && 
Info.hasSourceManager()17
) {
143
17
    const SourceManager &SM = Info.getSourceManager();
144
17
    PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation());
145
17
146
17
    if (PLoc.isInvalid()) {
147
0
      // At least print the file name if available:
148
0
      FileID FID = SM.getFileID(Info.getLocation());
149
0
      if (FID.isValid()) {
150
0
        const FileEntry *FE = SM.getFileEntryForID(FID);
151
0
        if (FE && FE->isValid())
152
0
          DE.Filename = FE->getName();
153
0
      }
154
17
    } else {
155
17
      DE.Filename = PLoc.getFilename();
156
17
      DE.Line = PLoc.getLine();
157
17
      DE.Column = PLoc.getColumn();
158
17
    }
159
17
  }
160
19
161
19
  // Record the diagnostic entry.
162
19
  Entries.push_back(DE);
163
19
}
164