1db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth//===--- TextDiagnostic.h - Text Diagnostic Pretty-Printing -----*- C++ -*-===// 2db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth// 3db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth// The LLVM Compiler Infrastructure 4db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth// 5db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth// This file is distributed under the University of Illinois Open Source 6db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth// License. See LICENSE.TXT for details. 7db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth// 8db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth//===----------------------------------------------------------------------===// 9db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth// 10db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth// This is a utility class that provides support for textual pretty-printing of 11db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth// diagnostics. It is used to implement the different code paths which require 12db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth// such functionality in a consistent way. 13db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth// 14db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth//===----------------------------------------------------------------------===// 15db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth 16db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth#ifndef LLVM_CLANG_FRONTEND_TEXT_DIAGNOSTIC_H_ 17db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth#define LLVM_CLANG_FRONTEND_TEXT_DIAGNOSTIC_H_ 18db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth 192898d4f7648e6ed5e9047068f1e8ee2f3c2bcd75Ted Kremenek#include "clang/Frontend/DiagnosticRenderer.h" 20db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth 21db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruthnamespace clang { 22db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth 23db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth/// \brief Class to encapsulate the logic for formatting and printing a textual 24db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth/// diagnostic message. 25db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth/// 26db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth/// This class provides an interface for building and emitting a textual 27db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth/// diagnostic, including all of the macro backtraces, caret diagnostics, FixIt 28db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth/// Hints, and code snippets. In the presence of macros this involves 29db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth/// a recursive process, synthesizing notes for each macro expansion. 30db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth/// 31db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth/// The purpose of this class is to isolate the implementation of printing 32db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth/// beautiful text diagnostics from any particular interfaces. The Clang 33db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth/// DiagnosticClient is implemented through this class as is diagnostic 34db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth/// printing coming out of libclang. 352898d4f7648e6ed5e9047068f1e8ee2f3c2bcd75Ted Kremenekclass TextDiagnostic : public DiagnosticRenderer { 36db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth raw_ostream &OS; 37db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth 38db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruthpublic: 39db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth TextDiagnostic(raw_ostream &OS, 40db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth const LangOptions &LangOpts, 4102c23ebf41ae2f70da0ba7337e05c51fbfe35f7fDouglas Gregor DiagnosticOptions *DiagOpts); 42db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth 432898d4f7648e6ed5e9047068f1e8ee2f3c2bcd75Ted Kremenek virtual ~TextDiagnostic(); 442898d4f7648e6ed5e9047068f1e8ee2f3c2bcd75Ted Kremenek 45db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth /// \brief Print the diagonstic level to a raw_ostream. 46db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth /// 47db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth /// This is a static helper that handles colorizing the level and formatting 48db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth /// it into an arbitrary output stream. This is used internally by the 49db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth /// TextDiagnostic emission code, but it can also be used directly by 50db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth /// consumers that don't have a source manager or other state that the full 51db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth /// TextDiagnostic logic requires. 52db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth static void printDiagnosticLevel(raw_ostream &OS, 53db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth DiagnosticsEngine::Level Level, 5489e32745123c02159fa1d1f19f96bdd4173029f0Hans Wennborg bool ShowColors, 5589e32745123c02159fa1d1f19f96bdd4173029f0Hans Wennborg bool CLFallbackMode = false); 56db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth 57db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth /// \brief Pretty-print a diagnostic message to a raw_ostream. 58db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth /// 59db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth /// This is a static helper to handle the line wrapping, colorizing, and 60db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth /// rendering of a diagnostic message to a particular ostream. It is 6148d798ce32447607144db70a484cdb99c1180663Benjamin Kramer /// publicly visible so that clients which do not have sufficient state to 62db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth /// build a complete TextDiagnostic object can still get consistent 63db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth /// formatting of their diagnostic messages. 64db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth /// 65db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth /// \param OS Where the message is printed 66ef8225444452a1486bd721f3285301fe84643b00Stephen Hines /// \param IsSupplemental true if this is a continuation note diagnostic 67db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth /// \param Message The text actually printed 68db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth /// \param CurrentColumn The starting column of the first line, accounting 69db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth /// for any prefix. 70db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth /// \param Columns The number of columns to use in line-wrapping, 0 disables 71db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth /// all line-wrapping. 72db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth /// \param ShowColors Enable colorizing of the message. 73ef8225444452a1486bd721f3285301fe84643b00Stephen Hines static void printDiagnosticMessage(raw_ostream &OS, bool IsSupplemental, 74ef8225444452a1486bd721f3285301fe84643b00Stephen Hines StringRef Message, unsigned CurrentColumn, 75ef8225444452a1486bd721f3285301fe84643b00Stephen Hines unsigned Columns, bool ShowColors); 76db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth 772898d4f7648e6ed5e9047068f1e8ee2f3c2bcd75Ted Kremenekprotected: 78651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines void emitDiagnosticMessage(SourceLocation Loc,PresumedLoc PLoc, 79651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines DiagnosticsEngine::Level Level, 80651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines StringRef Message, 81651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines ArrayRef<CharSourceRange> Ranges, 82651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines const SourceManager *SM, 83651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines DiagOrStoredDiag D) override; 84651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 85651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines void emitDiagnosticLoc(SourceLocation Loc, PresumedLoc PLoc, 86651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines DiagnosticsEngine::Level Level, 87651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines ArrayRef<CharSourceRange> Ranges, 88651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines const SourceManager &SM) override; 89651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 90651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines void emitCodeContext(SourceLocation Loc, 91651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines DiagnosticsEngine::Level Level, 92651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines SmallVectorImpl<CharSourceRange>& Ranges, 93651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines ArrayRef<FixItHint> Hints, 94651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines const SourceManager &SM) override { 9516afdf76b6f12e41ff6f6e6828bfb1d4732523baArgyrios Kyrtzidis emitSnippetAndCaret(Loc, Level, Ranges, Hints, SM); 962898d4f7648e6ed5e9047068f1e8ee2f3c2bcd75Ted Kremenek } 972898d4f7648e6ed5e9047068f1e8ee2f3c2bcd75Ted Kremenek 98651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines void emitIncludeLocation(SourceLocation Loc, PresumedLoc PLoc, 99651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines const SourceManager &SM) override; 1006c3254316de2d0d554b19702f4b10117ae46b77bDouglas Gregor 101651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines void emitImportLocation(SourceLocation Loc, PresumedLoc PLoc, 102651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines StringRef ModuleName, 103651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines const SourceManager &SM) override; 104651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines 105651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines void emitBuildingModuleLocation(SourceLocation Loc, PresumedLoc PLoc, 106651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines StringRef ModuleName, 107651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines const SourceManager &SM) override; 108830ea5b7c75413526c19531f0180fa6e45b98919Douglas Gregor 109db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruthprivate: 1104ba55659982bf45eedc9d32851815e5c4b6d8460Chandler Carruth void emitSnippetAndCaret(SourceLocation Loc, DiagnosticsEngine::Level Level, 1116ddd8871c07ab161742e8ed37d3ef121ac79fa59Chandler Carruth SmallVectorImpl<CharSourceRange>& Ranges, 11216afdf76b6f12e41ff6f6e6828bfb1d4732523baArgyrios Kyrtzidis ArrayRef<FixItHint> Hints, 11316afdf76b6f12e41ff6f6e6828bfb1d4732523baArgyrios Kyrtzidis const SourceManager &SM); 1146ddd8871c07ab161742e8ed37d3ef121ac79fa59Chandler Carruth 1156749dd50869281f9b49ad5960e25288c15a71cacSeth Cantrell void emitSnippet(StringRef SourceLine); 1166749dd50869281f9b49ad5960e25288c15a71cacSeth Cantrell 11716afdf76b6f12e41ff6f6e6828bfb1d4732523baArgyrios Kyrtzidis void emitParseableFixits(ArrayRef<FixItHint> Hints, const SourceManager &SM); 118db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth}; 119db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth 120db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth} // end namespace clang 121db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth 122db463bb2e4a9751f4cbe53996db751e1985ee966Chandler Carruth#endif 123