llvm.org GIT mirror llvm / 89f33fd
SourceMgr: Colorize diagnostics. Same color scheme as clang uses. The colors are only enabled if the output is a tty. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@155035 91177308-0d34-0410-b5e6-96231b3b80d8 Benjamin Kramer 8 years ago
2 changed file(s) with 47 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
127127 /// PrintMessage - Emit a message about the specified location with the
128128 /// specified string.
129129 ///
130 /// @param ShowColors - Display colored messages if output is a terminal and
131 /// the default error handler is used.
130132 void PrintMessage(SMLoc Loc, DiagKind Kind, const Twine &Msg,
131 ArrayRef Ranges = ArrayRef()) const;
133 ArrayRef Ranges = ArrayRef(),
134 bool ShowColors = true) const;
132135
133136
134137 /// GetMessage - Return an SMDiagnostic at the specified location with the
187190 const std::vector > &getRanges() const {
188191 return Ranges;
189192 }
190 void print(const char *ProgName, raw_ostream &S) const;
193 void print(const char *ProgName, raw_ostream &S, bool ShowColors = true) const;
191194 };
192195
193196 } // end llvm namespace
192192 }
193193
194194 void SourceMgr::PrintMessage(SMLoc Loc, SourceMgr::DiagKind Kind,
195 const Twine &Msg, ArrayRef Ranges) const {
195 const Twine &Msg, ArrayRef Ranges,
196 bool ShowColors) const {
196197 SMDiagnostic Diagnostic = GetMessage(Loc, Kind, Msg, Ranges);
197198
198199 // Report the message with the diagnostic handler if present.
207208 assert(CurBuf != -1 && "Invalid or unspecified location!");
208209 PrintIncludeStack(getBufferInfo(CurBuf).IncludeLoc, OS);
209210
210 Diagnostic.print(0, OS);
211 Diagnostic.print(0, OS, ShowColors);
211212 }
212213
213214 //===----------------------------------------------------------------------===//
224225 }
225226
226227
227 void SMDiagnostic::print(const char *ProgName, raw_ostream &S) const {
228 void SMDiagnostic::print(const char *ProgName, raw_ostream &S,
229 bool ShowColors) const {
230 // Display colors only if OS goes to a tty.
231 ShowColors &= S.is_displayed();
232
233 if (ShowColors)
234 S.changeColor(raw_ostream::SAVEDCOLOR, true);
235
228236 if (ProgName && ProgName[0])
229237 S << ProgName << ": ";
230238
243251 }
244252
245253 switch (Kind) {
246 case SourceMgr::DK_Error: S << "error: "; break;
247 case SourceMgr::DK_Warning: S << "warning: "; break;
248 case SourceMgr::DK_Note: S << "note: "; break;
249 }
250
254 case SourceMgr::DK_Error:
255 if (ShowColors)
256 S.changeColor(raw_ostream::RED, true);
257 S << "error: ";
258 break;
259 case SourceMgr::DK_Warning:
260 if (ShowColors)
261 S.changeColor(raw_ostream::MAGENTA, true);
262 S << "warning: ";
263 break;
264 case SourceMgr::DK_Note:
265 if (ShowColors)
266 S.changeColor(raw_ostream::BLACK, true);
267 S << "note: ";
268 break;
269 }
270
271 if (ShowColors) {
272 S.resetColor();
273 S.changeColor(raw_ostream::SAVEDCOLOR, true);
274 }
275
251276 S << Message << '\n';
277
278 if (ShowColors)
279 S.resetColor();
252280
253281 if (LineNo == -1 || ColumnNo == -1)
254282 return;
291319 }
292320 S << '\n';
293321
322 if (ShowColors)
323 S.changeColor(raw_ostream::GREEN, true);
324
294325 // Print out the caret line, matching tabs in the source line.
295326 for (unsigned i = 0, e = CaretLine.size(), OutCol = 0; i != e; ++i) {
296327 if (i >= LineContents.size() || LineContents[i] != '\t') {
305336 ++OutCol;
306337 } while (OutCol & 7);
307338 }
339
340 if (ShowColors)
341 S.resetColor();
308342
309343 S << '\n';
310344 }