1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
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 | OwnsOutputStream(_OwnsOutputStream) { |
29 | } |
30 | |
31 | TextDiagnosticPrinter::~TextDiagnosticPrinter() { |
32 | if (OwnsOutputStream) |
33 | delete &OS; |
34 | } |
35 | |
36 | void TextDiagnosticPrinter::BeginSourceFile(const LangOptions &LO, |
37 | const Preprocessor *PP) { |
38 | |
39 | TextDiag.reset(new TextDiagnostic(OS, LO, &*DiagOpts)); |
40 | } |
41 | |
42 | void TextDiagnosticPrinter::EndSourceFile() { |
43 | TextDiag.reset(); |
44 | } |
45 | |
46 | |
47 | |
48 | |
49 | |
50 | |
51 | static void printDiagnosticOptions(raw_ostream &OS, |
52 | DiagnosticsEngine::Level Level, |
53 | const Diagnostic &Info, |
54 | const DiagnosticOptions &DiagOpts) { |
55 | bool Started = false; |
56 | if (DiagOpts.ShowOptionNames) { |
57 | |
58 | if (Info.getID() == diag::fatal_too_many_errors) { |
59 | OS << " [-ferror-limit=]"; |
60 | return; |
61 | } |
62 | |
63 | |
64 | |
65 | |
66 | |
67 | |
68 | |
69 | |
70 | |
71 | |
72 | if (Level == DiagnosticsEngine::Error && |
73 | DiagnosticIDs::isBuiltinWarningOrExtension(Info.getID()) && |
74 | !DiagnosticIDs::isDefaultMappingAsError(Info.getID())) { |
75 | OS << " [-Werror"; |
76 | Started = true; |
77 | } |
78 | |
79 | StringRef Opt = DiagnosticIDs::getWarningOptionForDiag(Info.getID()); |
80 | if (!Opt.empty()) { |
81 | OS << (Started ? "," : " [") |
82 | << (Level == DiagnosticsEngine::Remark ? "-R" : "-W") << Opt; |
83 | StringRef OptValue = Info.getDiags()->getFlagValue(); |
84 | if (!OptValue.empty()) |
85 | OS << "=" << OptValue; |
86 | Started = true; |
87 | } |
88 | } |
89 | |
90 | |
91 | if (DiagOpts.ShowCategories) { |
92 | unsigned DiagCategory = |
93 | DiagnosticIDs::getCategoryNumberForDiag(Info.getID()); |
94 | if (DiagCategory) { |
95 | OS << (Started ? "," : " ["); |
96 | Started = true; |
97 | if (DiagOpts.ShowCategories == 1) |
98 | OS << DiagCategory; |
99 | else { |
100 | (0) . __assert_fail ("DiagOpts.ShowCategories == 2 && \"Invalid ShowCategories value\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnosticPrinter.cpp", 100, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(DiagOpts.ShowCategories == 2 && "Invalid ShowCategories value"); |
101 | OS << DiagnosticIDs::getCategoryNameFromID(DiagCategory); |
102 | } |
103 | } |
104 | } |
105 | if (Started) |
106 | OS << ']'; |
107 | } |
108 | |
109 | void TextDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level, |
110 | const Diagnostic &Info) { |
111 | |
112 | DiagnosticConsumer::HandleDiagnostic(Level, Info); |
113 | |
114 | |
115 | |
116 | SmallString<100> OutStr; |
117 | Info.FormatDiagnostic(OutStr); |
118 | |
119 | llvm::raw_svector_ostream DiagMessageStream(OutStr); |
120 | printDiagnosticOptions(DiagMessageStream, Level, Info, *DiagOpts); |
121 | |
122 | |
123 | |
124 | |
125 | |
126 | uint64_t StartOfLocationInfo = OS.tell(); |
127 | |
128 | if (!Prefix.empty()) |
129 | OS << Prefix << ": "; |
130 | |
131 | |
132 | |
133 | |
134 | |
135 | if (!Info.getLocation().isValid()) { |
136 | TextDiagnostic::printDiagnosticLevel(OS, Level, DiagOpts->ShowColors, |
137 | DiagOpts->CLFallbackMode); |
138 | TextDiagnostic::printDiagnosticMessage(OS, Level, DiagMessageStream.str(), |
139 | OS.tell() - StartOfLocationInfo, |
140 | DiagOpts->MessageLength, |
141 | DiagOpts->ShowColors); |
142 | OS.flush(); |
143 | return; |
144 | } |
145 | |
146 | |
147 | (0) . __assert_fail ("DiagOpts && \"Unexpected diagnostic without options set\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnosticPrinter.cpp", 147, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(DiagOpts && "Unexpected diagnostic without options set"); |
148 | (0) . __assert_fail ("Info.hasSourceManager() && \"Unexpected diagnostic with no source manager\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnosticPrinter.cpp", 149, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Info.hasSourceManager() && |
149 | (0) . __assert_fail ("Info.hasSourceManager() && \"Unexpected diagnostic with no source manager\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnosticPrinter.cpp", 149, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Unexpected diagnostic with no source manager"); |
150 | (0) . __assert_fail ("TextDiag && \"Unexpected diagnostic outside source file processing\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnosticPrinter.cpp", 150, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(TextDiag && "Unexpected diagnostic outside source file processing"); |
151 | |
152 | TextDiag->emitDiagnostic( |
153 | FullSourceLoc(Info.getLocation(), Info.getSourceManager()), Level, |
154 | DiagMessageStream.str(), Info.getRanges(), Info.getFixItHints()); |
155 | |
156 | OS.flush(); |
157 | } |
158 | |