Coverage Report

Created: 2020-02-18 08:44

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Frontend/TextDiagnosticPrinter.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- TextDiagnosticPrinter.cpp - 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
// This diagnostic client prints out their diagnostic messages.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/Frontend/TextDiagnosticPrinter.h"
14
#include "clang/Basic/DiagnosticOptions.h"
15
#include "clang/Basic/SourceManager.h"
16
#include "clang/Frontend/TextDiagnostic.h"
17
#include "clang/Lex/Lexer.h"
18
#include "llvm/ADT/SmallString.h"
19
#include "llvm/Support/ErrorHandling.h"
20
#include "llvm/Support/raw_ostream.h"
21
#include <algorithm>
22
using namespace clang;
23
24
TextDiagnosticPrinter::TextDiagnosticPrinter(raw_ostream &os,
25
                                             DiagnosticOptions *diags,
26
                                             bool _OwnsOutputStream)
27
  : OS(os), DiagOpts(diags),
28
93.9k
    OwnsOutputStream(_OwnsOutputStream) {
29
93.9k
}
30
31
86.2k
TextDiagnosticPrinter::~TextDiagnosticPrinter() {
32
86.2k
  if (OwnsOutputStream)
33
0
    delete &OS;
34
86.2k
}
35
36
void TextDiagnosticPrinter::BeginSourceFile(const LangOptions &LO,
37
57.6k
                                            const Preprocessor *PP) {
38
57.6k
  // Build the TextDiagnostic utility.
39
57.6k
  TextDiag.reset(new TextDiagnostic(OS, LO, &*DiagOpts));
40
57.6k
}
41
42
53.6k
void TextDiagnosticPrinter::EndSourceFile() {
43
53.6k
  TextDiag.reset();
44
53.6k
}
45
46
/// Print any diagnostic option information to a raw_ostream.
47
///
48
/// This implements all of the logic for adding diagnostic options to a message
49
/// (via OS). Each relevant option is comma separated and all are enclosed in
50
/// the standard bracketing: " [...]".
51
static void printDiagnosticOptions(raw_ostream &OS,
52
                                   DiagnosticsEngine::Level Level,
53
                                   const Diagnostic &Info,
54
27.4k
                                   const DiagnosticOptions &DiagOpts) {
55
27.4k
  bool Started = false;
56
27.4k
  if (DiagOpts.ShowOptionNames) {
57
5.94k
    // Handle special cases for non-warnings early.
58
5.94k
    if (Info.getID() == diag::fatal_too_many_errors) {
59
2
      OS << " [-ferror-limit=]";
60
2
      return;
61
2
    }
62
5.94k
63
5.94k
    // The code below is somewhat fragile because we are essentially trying to
64
5.94k
    // report to the user what happened by inferring what the diagnostic engine
65
5.94k
    // did. Eventually it might make more sense to have the diagnostic engine
66
5.94k
    // include some "why" information in the diagnostic.
67
5.94k
68
5.94k
    // If this is a warning which has been mapped to an error by the user (as
69
5.94k
    // inferred by checking whether the default mapping is to an error) then
70
5.94k
    // flag it as such. Note that diagnostics could also have been mapped by a
71
5.94k
    // pragma, but we don't currently have a way to distinguish this.
72
5.94k
    if (Level == DiagnosticsEngine::Error &&
73
5.94k
        
DiagnosticIDs::isBuiltinWarningOrExtension(Info.getID())2.02k
&&
74
5.94k
        
!DiagnosticIDs::isDefaultMappingAsError(Info.getID())115
) {
75
110
      OS << " [-Werror";
76
110
      Started = true;
77
110
    }
78
5.94k
79
5.94k
    StringRef Opt = DiagnosticIDs::getWarningOptionForDiag(Info.getID());
80
5.94k
    if (!Opt.empty()) {
81
2.09k
      OS << (Started ? 
","110
:
" ["1.98k
)
82
2.09k
         << (Level == DiagnosticsEngine::Remark ? 
"-R"4
:
"-W"2.09k
) << Opt;
83
2.09k
      StringRef OptValue = Info.getDiags()->getFlagValue();
84
2.09k
      if (!OptValue.empty())
85
5
        OS << "=" << OptValue;
86
2.09k
      Started = true;
87
2.09k
    }
88
5.94k
  }
89
27.4k
90
27.4k
  // If the user wants to see category information, include it too.
91
27.4k
  
if (27.4k
DiagOpts.ShowCategories27.4k
) {
92
9
    unsigned DiagCategory =
93
9
      DiagnosticIDs::getCategoryNumberForDiag(Info.getID());
94
9
    if (DiagCategory) {
95
9
      OS << (Started ? 
","1
:
" ["8
);
96
9
      Started = true;
97
9
      if (DiagOpts.ShowCategories == 1)
98
3
        OS << DiagCategory;
99
6
      else {
100
6
        assert(DiagOpts.ShowCategories == 2 && "Invalid ShowCategories value");
101
6
        OS << DiagnosticIDs::getCategoryNameFromID(DiagCategory);
102
6
      }
103
9
    }
104
9
  }
105
27.4k
  if (Started)
106
2.10k
    OS << ']';
107
27.4k
}
108
109
void TextDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level,
110
27.4k
                                             const Diagnostic &Info) {
111
27.4k
  // Default implementation (Warnings/errors count).
112
27.4k
  DiagnosticConsumer::HandleDiagnostic(Level, Info);
113
27.4k
114
27.4k
  // Render the diagnostic message into a temporary buffer eagerly. We'll use
115
27.4k
  // this later as we print out the diagnostic to the terminal.
116
27.4k
  SmallString<100> OutStr;
117
27.4k
  Info.FormatDiagnostic(OutStr);
118
27.4k
119
27.4k
  llvm::raw_svector_ostream DiagMessageStream(OutStr);
120
27.4k
  printDiagnosticOptions(DiagMessageStream, Level, Info, *DiagOpts);
121
27.4k
122
27.4k
  // Keeps track of the starting position of the location
123
27.4k
  // information (e.g., "foo.c:10:4:") that precedes the error
124
27.4k
  // message. We use this information to determine how long the
125
27.4k
  // file+line+column number prefix is.
126
27.4k
  uint64_t StartOfLocationInfo = OS.tell();
127
27.4k
128
27.4k
  if (!Prefix.empty())
129
3.97k
    OS << Prefix << ": ";
130
27.4k
131
27.4k
  // Use a dedicated, simpler path for diagnostics without a valid location.
132
27.4k
  // This is important as if the location is missing, we may be emitting
133
27.4k
  // diagnostics in a context that lacks language options, a source manager, or
134
27.4k
  // other infrastructure necessary when emitting more rich diagnostics.
135
27.4k
  if (!Info.getLocation().isValid()) {
136
4.76k
    TextDiagnostic::printDiagnosticLevel(OS, Level, DiagOpts->ShowColors,
137
4.76k
                                         DiagOpts->CLFallbackMode);
138
4.76k
    TextDiagnostic::printDiagnosticMessage(OS, Level, DiagMessageStream.str(),
139
4.76k
                                           OS.tell() - StartOfLocationInfo,
140
4.76k
                                           DiagOpts->MessageLength,
141
4.76k
                                           DiagOpts->ShowColors);
142
4.76k
    OS.flush();
143
4.76k
    return;
144
4.76k
  }
145
22.6k
146
22.6k
  // Assert that the rest of our infrastructure is setup properly.
147
22.6k
  assert(DiagOpts && "Unexpected diagnostic without options set");
148
22.6k
  assert(Info.hasSourceManager() &&
149
22.6k
         "Unexpected diagnostic with no source manager");
150
22.6k
  assert(TextDiag && "Unexpected diagnostic outside source file processing");
151
22.6k
152
22.6k
  TextDiag->emitDiagnostic(
153
22.6k
      FullSourceLoc(Info.getLocation(), Info.getSourceManager()), Level,
154
22.6k
      DiagMessageStream.str(), Info.getRanges(), Info.getFixItHints());
155
22.6k
156
22.6k
  OS.flush();
157
22.6k
}