1//===--- TextDiagnosticPrinter.cpp - Diagnostic Printer -------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This diagnostic client prints out their diagnostic messages.
10//
11//===----------------------------------------------------------------------===//
12//
13// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
14//
15//===----------------------------------------------------------------------===//
16
17#include "flang/Frontend/TextDiagnosticPrinter.h"
18#include "flang/Frontend/TextDiagnostic.h"
19#include "clang/Basic/DiagnosticOptions.h"
20#include "llvm/ADT/SmallString.h"
21#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/Support/ErrorHandling.h"
24#include "llvm/Support/Path.h"
25#include "llvm/Support/raw_ostream.h"
26
27using namespace Fortran::frontend;
28
29TextDiagnosticPrinter::TextDiagnosticPrinter(raw_ostream &diagOs,
30 clang::DiagnosticOptions *diags)
31 : os(diagOs), diagOpts(diags) {}
32
33TextDiagnosticPrinter::~TextDiagnosticPrinter() {}
34
35// For remarks only, print the remark option and pass name that was used to a
36// raw_ostream. This also supports warnings from invalid remark arguments
37// provided.
38static void printRemarkOption(llvm::raw_ostream &os,
39 clang::DiagnosticsEngine::Level level,
40 const clang::Diagnostic &info) {
41 llvm::StringRef opt =
42 clang::DiagnosticIDs::getWarningOptionForDiag(info.getID());
43 if (!opt.empty()) {
44 // We still need to check if the level is a Remark since, an unknown option
45 // warning could be printed i.e. [-Wunknown-warning-option]
46 os << " [" << (level == clang::DiagnosticsEngine::Remark ? "-R" : "-W")
47 << opt;
48 llvm::StringRef optValue = info.getDiags()->getFlagValue();
49 if (!optValue.empty())
50 os << "=" << optValue;
51 os << ']';
52 }
53}
54
55// For remarks only, if we are receiving a message of this format
56// [file location with line and column];;[path to file];;[the remark message]
57// then print the absolute file path, line and column number.
58void TextDiagnosticPrinter::printLocForRemarks(
59 llvm::raw_svector_ostream &diagMessageStream, llvm::StringRef &diagMsg) {
60 // split incoming string to get the absolute path and filename in the
61 // case we are receiving optimization remarks from BackendRemarkConsumer
62 diagMsg = diagMessageStream.str();
63 llvm::StringRef delimiter = ";;";
64
65 size_t pos = 0;
66 llvm::SmallVector<llvm::StringRef> tokens;
67 while ((pos = diagMsg.find(delimiter)) != std::string::npos) {
68 tokens.push_back(diagMsg.substr(0, pos));
69 diagMsg = diagMsg.drop_front(pos + delimiter.size());
70 }
71
72 // tokens will always be of size 2 in the case of optimization
73 // remark message received
74 if (tokens.size() == 2) {
75 // Extract absolute path
76 llvm::SmallString<128> absPath = llvm::sys::path::relative_path(tokens[1]);
77 llvm::sys::path::remove_filename(absPath);
78 // Add the last separator before the file name
79 llvm::sys::path::append(absPath, llvm::sys::path::get_separator());
80 llvm::sys::path::make_preferred(absPath);
81
82 // Used for changing only the bold attribute
83 if (diagOpts->ShowColors)
84 os.changeColor(llvm::raw_ostream::SAVEDCOLOR, true);
85
86 // Print path, file name, line and column
87 os << absPath << tokens[0] << ": ";
88 }
89}
90
91void TextDiagnosticPrinter::HandleDiagnostic(
92 clang::DiagnosticsEngine::Level level, const clang::Diagnostic &info) {
93 // Default implementation (Warnings/errors count).
94 DiagnosticConsumer::HandleDiagnostic(level, info);
95
96 // Render the diagnostic message into a temporary buffer eagerly. We'll use
97 // this later as we print out the diagnostic to the terminal.
98 llvm::SmallString<100> outStr;
99 info.FormatDiagnostic(outStr);
100
101 llvm::raw_svector_ostream diagMessageStream(outStr);
102 printRemarkOption(diagMessageStream, level, info);
103
104 if (!prefix.empty())
105 os << prefix << ": ";
106
107 // We only emit diagnostics in contexts that lack valid source locations.
108 assert(!info.getLocation().isValid() &&
109 "Diagnostics with valid source location are not supported");
110
111 llvm::StringRef diagMsg;
112 printLocForRemarks(diagMessageStream, diagMsg);
113
114 Fortran::frontend::TextDiagnostic::printDiagnosticLevel(os, level,
115 diagOpts->ShowColors);
116 Fortran::frontend::TextDiagnostic::printDiagnosticMessage(
117 os,
118 /*IsSupplemental=*/level == clang::DiagnosticsEngine::Note, diagMsg,
119 diagOpts->ShowColors);
120
121 os.flush();
122}
123

source code of flang/lib/Frontend/TextDiagnosticPrinter.cpp