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