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