1 //===--- TextDiagnosticPrinter.cpp - Diagnostic Printer -------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This diagnostic client prints out their diagnostic messages.
11 //===----------------------------------------------------------------------===//
13 // Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
15 //===----------------------------------------------------------------------===//
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"
27 using namespace Fortran::frontend
;
29 TextDiagnosticPrinter::TextDiagnosticPrinter(raw_ostream
&diagOs
,
30 clang::DiagnosticOptions
*diags
)
31 : os(diagOs
), diagOpts(diags
) {}
33 TextDiagnosticPrinter::~TextDiagnosticPrinter() {}
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
38 static void printRemarkOption(llvm::raw_ostream
&os
,
39 clang::DiagnosticsEngine::Level level
,
40 const clang::Diagnostic
&info
) {
42 clang::DiagnosticIDs::getWarningOptionForDiag(info
.getID());
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")
48 llvm::StringRef optValue
= info
.getFlagValue();
49 if (!optValue
.empty())
50 os
<< "=" << optValue
;
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.
58 void 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
= ";;";
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());
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
);
82 // Used for changing only the bold attribute
83 if (diagOpts
->ShowColors
)
84 os
.changeColor(llvm::raw_ostream::SAVEDCOLOR
, true);
86 // Print path, file name, line and column
87 os
<< absPath
<< tokens
[0] << ": ";
91 void TextDiagnosticPrinter::HandleDiagnostic(
92 clang::DiagnosticsEngine::Level level
, const clang::Diagnostic
&info
) {
93 // Default implementation (Warnings/errors count).
94 DiagnosticConsumer::HandleDiagnostic(level
, info
);
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
);
101 llvm::raw_svector_ostream
diagMessageStream(outStr
);
102 printRemarkOption(diagMessageStream
, level
, info
);
105 os
<< prefix
<< ": ";
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");
111 llvm::StringRef diagMsg
;
112 printLocForRemarks(diagMessageStream
, diagMsg
);
114 Fortran::frontend::TextDiagnostic::printDiagnosticLevel(os
, level
,
115 diagOpts
->ShowColors
);
116 Fortran::frontend::TextDiagnostic::printDiagnosticMessage(
118 /*IsSupplemental=*/level
== clang::DiagnosticsEngine::Note
, diagMsg
,
119 diagOpts
->ShowColors
);