10b57cec5SDimitry Andric //===--- TextDiagnosticPrinter.cpp - Diagnostic Printer -------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This diagnostic client prints out their diagnostic messages.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric
130b57cec5SDimitry Andric #include "clang/Frontend/TextDiagnosticPrinter.h"
140b57cec5SDimitry Andric #include "clang/Basic/DiagnosticOptions.h"
150b57cec5SDimitry Andric #include "clang/Basic/SourceManager.h"
160b57cec5SDimitry Andric #include "clang/Frontend/TextDiagnostic.h"
170b57cec5SDimitry Andric #include "clang/Lex/Lexer.h"
180b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h"
190b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
200b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
210b57cec5SDimitry Andric #include <algorithm>
220b57cec5SDimitry Andric using namespace clang;
230b57cec5SDimitry Andric
TextDiagnosticPrinter(raw_ostream & os,DiagnosticOptions * diags,bool _OwnsOutputStream)240b57cec5SDimitry Andric TextDiagnosticPrinter::TextDiagnosticPrinter(raw_ostream &os,
250b57cec5SDimitry Andric DiagnosticOptions *diags,
260b57cec5SDimitry Andric bool _OwnsOutputStream)
270b57cec5SDimitry Andric : OS(os), DiagOpts(diags),
280b57cec5SDimitry Andric OwnsOutputStream(_OwnsOutputStream) {
290b57cec5SDimitry Andric }
300b57cec5SDimitry Andric
~TextDiagnosticPrinter()310b57cec5SDimitry Andric TextDiagnosticPrinter::~TextDiagnosticPrinter() {
320b57cec5SDimitry Andric if (OwnsOutputStream)
330b57cec5SDimitry Andric delete &OS;
340b57cec5SDimitry Andric }
350b57cec5SDimitry Andric
BeginSourceFile(const LangOptions & LO,const Preprocessor * PP)360b57cec5SDimitry Andric void TextDiagnosticPrinter::BeginSourceFile(const LangOptions &LO,
370b57cec5SDimitry Andric const Preprocessor *PP) {
380b57cec5SDimitry Andric // Build the TextDiagnostic utility.
39*0fca6ea1SDimitry Andric TextDiag.reset(new TextDiagnostic(OS, LO, &*DiagOpts, PP));
400b57cec5SDimitry Andric }
410b57cec5SDimitry Andric
EndSourceFile()420b57cec5SDimitry Andric void TextDiagnosticPrinter::EndSourceFile() {
430b57cec5SDimitry Andric TextDiag.reset();
440b57cec5SDimitry Andric }
450b57cec5SDimitry Andric
460b57cec5SDimitry Andric /// Print any diagnostic option information to a raw_ostream.
470b57cec5SDimitry Andric ///
480b57cec5SDimitry Andric /// This implements all of the logic for adding diagnostic options to a message
490b57cec5SDimitry Andric /// (via OS). Each relevant option is comma separated and all are enclosed in
500b57cec5SDimitry Andric /// the standard bracketing: " [...]".
printDiagnosticOptions(raw_ostream & OS,DiagnosticsEngine::Level Level,const Diagnostic & Info,const DiagnosticOptions & DiagOpts)510b57cec5SDimitry Andric static void printDiagnosticOptions(raw_ostream &OS,
520b57cec5SDimitry Andric DiagnosticsEngine::Level Level,
530b57cec5SDimitry Andric const Diagnostic &Info,
540b57cec5SDimitry Andric const DiagnosticOptions &DiagOpts) {
550b57cec5SDimitry Andric bool Started = false;
560b57cec5SDimitry Andric if (DiagOpts.ShowOptionNames) {
570b57cec5SDimitry Andric // Handle special cases for non-warnings early.
580b57cec5SDimitry Andric if (Info.getID() == diag::fatal_too_many_errors) {
590b57cec5SDimitry Andric OS << " [-ferror-limit=]";
600b57cec5SDimitry Andric return;
610b57cec5SDimitry Andric }
620b57cec5SDimitry Andric
630b57cec5SDimitry Andric // The code below is somewhat fragile because we are essentially trying to
640b57cec5SDimitry Andric // report to the user what happened by inferring what the diagnostic engine
650b57cec5SDimitry Andric // did. Eventually it might make more sense to have the diagnostic engine
660b57cec5SDimitry Andric // include some "why" information in the diagnostic.
670b57cec5SDimitry Andric
680b57cec5SDimitry Andric // If this is a warning which has been mapped to an error by the user (as
690b57cec5SDimitry Andric // inferred by checking whether the default mapping is to an error) then
700b57cec5SDimitry Andric // flag it as such. Note that diagnostics could also have been mapped by a
710b57cec5SDimitry Andric // pragma, but we don't currently have a way to distinguish this.
720b57cec5SDimitry Andric if (Level == DiagnosticsEngine::Error &&
730b57cec5SDimitry Andric DiagnosticIDs::isBuiltinWarningOrExtension(Info.getID()) &&
740b57cec5SDimitry Andric !DiagnosticIDs::isDefaultMappingAsError(Info.getID())) {
750b57cec5SDimitry Andric OS << " [-Werror";
760b57cec5SDimitry Andric Started = true;
770b57cec5SDimitry Andric }
780b57cec5SDimitry Andric
790b57cec5SDimitry Andric StringRef Opt = DiagnosticIDs::getWarningOptionForDiag(Info.getID());
800b57cec5SDimitry Andric if (!Opt.empty()) {
810b57cec5SDimitry Andric OS << (Started ? "," : " [")
820b57cec5SDimitry Andric << (Level == DiagnosticsEngine::Remark ? "-R" : "-W") << Opt;
830b57cec5SDimitry Andric StringRef OptValue = Info.getDiags()->getFlagValue();
840b57cec5SDimitry Andric if (!OptValue.empty())
850b57cec5SDimitry Andric OS << "=" << OptValue;
860b57cec5SDimitry Andric Started = true;
870b57cec5SDimitry Andric }
880b57cec5SDimitry Andric }
890b57cec5SDimitry Andric
900b57cec5SDimitry Andric // If the user wants to see category information, include it too.
910b57cec5SDimitry Andric if (DiagOpts.ShowCategories) {
920b57cec5SDimitry Andric unsigned DiagCategory =
930b57cec5SDimitry Andric DiagnosticIDs::getCategoryNumberForDiag(Info.getID());
940b57cec5SDimitry Andric if (DiagCategory) {
950b57cec5SDimitry Andric OS << (Started ? "," : " [");
960b57cec5SDimitry Andric Started = true;
970b57cec5SDimitry Andric if (DiagOpts.ShowCategories == 1)
980b57cec5SDimitry Andric OS << DiagCategory;
990b57cec5SDimitry Andric else {
1000b57cec5SDimitry Andric assert(DiagOpts.ShowCategories == 2 && "Invalid ShowCategories value");
1010b57cec5SDimitry Andric OS << DiagnosticIDs::getCategoryNameFromID(DiagCategory);
1020b57cec5SDimitry Andric }
1030b57cec5SDimitry Andric }
1040b57cec5SDimitry Andric }
1050b57cec5SDimitry Andric if (Started)
1060b57cec5SDimitry Andric OS << ']';
1070b57cec5SDimitry Andric }
1080b57cec5SDimitry Andric
HandleDiagnostic(DiagnosticsEngine::Level Level,const Diagnostic & Info)1090b57cec5SDimitry Andric void TextDiagnosticPrinter::HandleDiagnostic(DiagnosticsEngine::Level Level,
1100b57cec5SDimitry Andric const Diagnostic &Info) {
1110b57cec5SDimitry Andric // Default implementation (Warnings/errors count).
1120b57cec5SDimitry Andric DiagnosticConsumer::HandleDiagnostic(Level, Info);
1130b57cec5SDimitry Andric
1140b57cec5SDimitry Andric // Render the diagnostic message into a temporary buffer eagerly. We'll use
1150b57cec5SDimitry Andric // this later as we print out the diagnostic to the terminal.
1160b57cec5SDimitry Andric SmallString<100> OutStr;
1170b57cec5SDimitry Andric Info.FormatDiagnostic(OutStr);
1180b57cec5SDimitry Andric
1190b57cec5SDimitry Andric llvm::raw_svector_ostream DiagMessageStream(OutStr);
1200b57cec5SDimitry Andric printDiagnosticOptions(DiagMessageStream, Level, Info, *DiagOpts);
1210b57cec5SDimitry Andric
1220b57cec5SDimitry Andric // Keeps track of the starting position of the location
1230b57cec5SDimitry Andric // information (e.g., "foo.c:10:4:") that precedes the error
1240b57cec5SDimitry Andric // message. We use this information to determine how long the
1250b57cec5SDimitry Andric // file+line+column number prefix is.
1260b57cec5SDimitry Andric uint64_t StartOfLocationInfo = OS.tell();
1270b57cec5SDimitry Andric
1280b57cec5SDimitry Andric if (!Prefix.empty())
1290b57cec5SDimitry Andric OS << Prefix << ": ";
1300b57cec5SDimitry Andric
1310b57cec5SDimitry Andric // Use a dedicated, simpler path for diagnostics without a valid location.
1320b57cec5SDimitry Andric // This is important as if the location is missing, we may be emitting
1330b57cec5SDimitry Andric // diagnostics in a context that lacks language options, a source manager, or
1340b57cec5SDimitry Andric // other infrastructure necessary when emitting more rich diagnostics.
1350b57cec5SDimitry Andric if (!Info.getLocation().isValid()) {
136fe6060f1SDimitry Andric TextDiagnostic::printDiagnosticLevel(OS, Level, DiagOpts->ShowColors);
137e8d8bef9SDimitry Andric TextDiagnostic::printDiagnosticMessage(
138e8d8bef9SDimitry Andric OS, /*IsSupplemental=*/Level == DiagnosticsEngine::Note,
139e8d8bef9SDimitry Andric DiagMessageStream.str(), OS.tell() - StartOfLocationInfo,
140e8d8bef9SDimitry Andric DiagOpts->MessageLength, DiagOpts->ShowColors);
1410b57cec5SDimitry Andric OS.flush();
1420b57cec5SDimitry Andric return;
1430b57cec5SDimitry Andric }
1440b57cec5SDimitry Andric
1450b57cec5SDimitry Andric // Assert that the rest of our infrastructure is setup properly.
1460b57cec5SDimitry Andric assert(DiagOpts && "Unexpected diagnostic without options set");
1470b57cec5SDimitry Andric assert(Info.hasSourceManager() &&
1480b57cec5SDimitry Andric "Unexpected diagnostic with no source manager");
1490b57cec5SDimitry Andric assert(TextDiag && "Unexpected diagnostic outside source file processing");
1500b57cec5SDimitry Andric
1510b57cec5SDimitry Andric TextDiag->emitDiagnostic(
1520b57cec5SDimitry Andric FullSourceLoc(Info.getLocation(), Info.getSourceManager()), Level,
1530b57cec5SDimitry Andric DiagMessageStream.str(), Info.getRanges(), Info.getFixItHints());
1540b57cec5SDimitry Andric
1550b57cec5SDimitry Andric OS.flush();
1560b57cec5SDimitry Andric }
157