clang  13.0.0git
LogDiagnosticPrinter.cpp
Go to the documentation of this file.
1 //===--- LogDiagnosticPrinter.cpp - Log 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 
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/Support/ErrorHandling.h"
16 #include "llvm/Support/raw_ostream.h"
17 using namespace clang;
18 using namespace markup;
19 
21  raw_ostream &os, DiagnosticOptions *diags,
22  std::unique_ptr<raw_ostream> StreamOwner)
23  : OS(os), StreamOwner(std::move(StreamOwner)), LangOpts(nullptr),
24  DiagOpts(diags) {}
25 
27  switch (Level) {
28  case DiagnosticsEngine::Ignored: return "ignored";
29  case DiagnosticsEngine::Remark: return "remark";
30  case DiagnosticsEngine::Note: return "note";
31  case DiagnosticsEngine::Warning: return "warning";
32  case DiagnosticsEngine::Error: return "error";
33  case DiagnosticsEngine::Fatal: return "fatal error";
34  }
35  llvm_unreachable("Invalid DiagnosticsEngine level!");
36 }
37 
38 void
39 LogDiagnosticPrinter::EmitDiagEntry(llvm::raw_ostream &OS,
40  const LogDiagnosticPrinter::DiagEntry &DE) {
41  OS << " <dict>\n";
42  OS << " <key>level</key>\n"
43  << " ";
44  EmitString(OS, getLevelName(DE.DiagnosticLevel)) << '\n';
45  if (!DE.Filename.empty()) {
46  OS << " <key>filename</key>\n"
47  << " ";
48  EmitString(OS, DE.Filename) << '\n';
49  }
50  if (DE.Line != 0) {
51  OS << " <key>line</key>\n"
52  << " ";
53  EmitInteger(OS, DE.Line) << '\n';
54  }
55  if (DE.Column != 0) {
56  OS << " <key>column</key>\n"
57  << " ";
58  EmitInteger(OS, DE.Column) << '\n';
59  }
60  if (!DE.Message.empty()) {
61  OS << " <key>message</key>\n"
62  << " ";
63  EmitString(OS, DE.Message) << '\n';
64  }
65  OS << " <key>ID</key>\n"
66  << " ";
67  EmitInteger(OS, DE.DiagnosticID) << '\n';
68  if (!DE.WarningOption.empty()) {
69  OS << " <key>WarningOption</key>\n"
70  << " ";
71  EmitString(OS, DE.WarningOption) << '\n';
72  }
73  OS << " </dict>\n";
74 }
75 
77  // We emit all the diagnostics in EndSourceFile. However, we don't emit any
78  // entry if no diagnostics were present.
79  //
80  // Note that DiagnosticConsumer has no "end-of-compilation" callback, so we
81  // will miss any diagnostics which are emitted after and outside the
82  // translation unit processing.
83  if (Entries.empty())
84  return;
85 
86  // Write to a temporary string to ensure atomic write of diagnostic object.
87  SmallString<512> Msg;
88  llvm::raw_svector_ostream OS(Msg);
89 
90  OS << "<dict>\n";
91  if (!MainFilename.empty()) {
92  OS << " <key>main-file</key>\n"
93  << " ";
94  EmitString(OS, MainFilename) << '\n';
95  }
96  if (!DwarfDebugFlags.empty()) {
97  OS << " <key>dwarf-debug-flags</key>\n"
98  << " ";
99  EmitString(OS, DwarfDebugFlags) << '\n';
100  }
101  OS << " <key>diagnostics</key>\n";
102  OS << " <array>\n";
103  for (auto &DE : Entries)
104  EmitDiagEntry(OS, DE);
105  OS << " </array>\n";
106  OS << "</dict>\n";
107 
108  this->OS << OS.str();
109 }
110 
112  const Diagnostic &Info) {
113  // Default implementation (Warnings/errors count).
115 
116  // Initialize the main file name, if we haven't already fetched it.
117  if (MainFilename.empty() && Info.hasSourceManager()) {
118  const SourceManager &SM = Info.getSourceManager();
119  FileID FID = SM.getMainFileID();
120  if (FID.isValid()) {
121  const FileEntry *FE = SM.getFileEntryForID(FID);
122  if (FE && FE->isValid())
123  MainFilename = std::string(FE->getName());
124  }
125  }
126 
127  // Create the diag entry.
128  DiagEntry DE;
129  DE.DiagnosticID = Info.getID();
130  DE.DiagnosticLevel = Level;
131 
132  DE.WarningOption =
133  std::string(DiagnosticIDs::getWarningOptionForDiag(DE.DiagnosticID));
134 
135  // Format the message.
136  SmallString<100> MessageStr;
137  Info.FormatDiagnostic(MessageStr);
138  DE.Message = std::string(MessageStr.str());
139 
140  // Set the location information.
141  DE.Filename = "";
142  DE.Line = DE.Column = 0;
143  if (Info.getLocation().isValid() && Info.hasSourceManager()) {
144  const SourceManager &SM = Info.getSourceManager();
145  PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation());
146 
147  if (PLoc.isInvalid()) {
148  // At least print the file name if available:
149  FileID FID = SM.getFileID(Info.getLocation());
150  if (FID.isValid()) {
151  const FileEntry *FE = SM.getFileEntryForID(FID);
152  if (FE && FE->isValid())
153  DE.Filename = std::string(FE->getName());
154  }
155  } else {
156  DE.Filename = PLoc.getFilename();
157  DE.Line = PLoc.getLine();
158  DE.Column = PLoc.getColumn();
159  }
160  }
161 
162  // Record the diagnostic entry.
163  Entries.push_back(DE);
164 }
165 
clang::DiagnosticsEngine::Level
Level
The level of the diagnostic, after it has been through mapping.
Definition: Diagnostic.h:194
clang::PresumedLoc::getLine
unsigned getLine() const
Return the presumed line number of this location.
Definition: SourceLocation.h:339
PlistSupport.h
clang::LogDiagnosticPrinter::EndSourceFile
void EndSourceFile() override
Callback to inform the diagnostic client that processing of a source file has ended.
Definition: LogDiagnosticPrinter.cpp:76
SourceManager.h
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:624
clang::Diagnostic
A little helper class (which is basically a smart pointer that forwards info from DiagnosticsEngine) ...
Definition: Diagnostic.h:1532
clang::DiagnosticConsumer::HandleDiagnostic
virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info)
Handle this diagnostic, reporting it to the user or capturing it to a log as needed.
Definition: Diagnostic.cpp:547
clang::markup::EmitInteger
raw_ostream & EmitInteger(raw_ostream &o, int64_t value)
Definition: PlistSupport.h:71
clang::PresumedLoc::getFilename
const char * getFilename() const
Return the presumed filename of this location.
Definition: SourceLocation.h:326
clang::DiagnosticsEngine::Remark
@ Remark
Definition: Diagnostic.h:197
clang::FileID::isValid
bool isValid() const
Definition: SourceLocation.h:47
clang::DiagnosticsEngine::Error
@ Error
Definition: Diagnostic.h:199
clang::LogDiagnosticPrinter::HandleDiagnostic
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const Diagnostic &Info) override
Handle this diagnostic, reporting it to the user or capturing it to a log as needed.
Definition: LogDiagnosticPrinter.cpp:111
llvm::SmallString< 512 >
clang::PresumedLoc::getColumn
unsigned getColumn() const
Return the presumed column number of this location.
Definition: SourceLocation.h:347
clang::FileEntry
Cached information about one file (either on disk or in the virtual file system).
Definition: FileEntry.h:331
LogDiagnosticPrinter.h
clang::PresumedLoc::isInvalid
bool isInvalid() const
Return true if this object is invalid or uninitialized.
Definition: SourceLocation.h:320
clang::FileEntry::isValid
bool isValid() const
Definition: FileEntry.h:368
clang::LogDiagnosticPrinter::LogDiagnosticPrinter
LogDiagnosticPrinter(raw_ostream &OS, DiagnosticOptions *Diags, std::unique_ptr< raw_ostream > StreamOwner)
Definition: LogDiagnosticPrinter.cpp:20
clang::DiagnosticsEngine::Ignored
@ Ignored
Definition: Diagnostic.h:195
clang::Diagnostic::getLocation
const SourceLocation & getLocation() const
Definition: Diagnostic.h:1543
clang::markup::EmitString
raw_ostream & EmitString(raw_ostream &o, StringRef s)
Definition: PlistSupport.h:78
clang::Diagnostic::getSourceManager
SourceManager & getSourceManager() const
Definition: Diagnostic.h:1545
getLevelName
static StringRef getLevelName(DiagnosticsEngine::Level Level)
Definition: LogDiagnosticPrinter.cpp:26
std
Definition: Format.h:3549
clang
Dataflow Directional Tag Classes.
Definition: CalledOnceCheck.h:17
clang::prec::Level
Level
Definition: OperatorPrecedence.h:26
clang::FileEntry::getName
StringRef getName() const
Definition: FileEntry.h:364
clang::FileID
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Definition: SourceLocation.h:41
clang::SourceLocation::isValid
bool isValid() const
Return true if this is a valid SourceLocation object.
Definition: SourceLocation.h:110
DiagnosticOptions.h
clang::Diagnostic::hasSourceManager
bool hasSourceManager() const
Definition: Diagnostic.h:1544
clang::DiagnosticsEngine::Fatal
@ Fatal
Definition: Diagnostic.h:200
clang::DiagnosticsEngine::Note
@ Note
Definition: Diagnostic.h:196
clang::PresumedLoc
Represents an unpacked "presumed" location which can be presented to the user.
Definition: SourceLocation.h:304
clang::Diagnostic::getID
unsigned getID() const
Definition: Diagnostic.h:1542
FileManager.h
SM
#define SM(sm)
Definition: Cuda.cpp:68
clang::Diagnostic::FormatDiagnostic
void FormatDiagnostic(SmallVectorImpl< char > &OutStr) const
Format this diagnostic into a string, substituting the formal arguments into the %0 slots.
Definition: Diagnostic.cpp:778
clang::DiagnosticsEngine::Warning
@ Warning
Definition: Diagnostic.h:198
clang::DiagnosticIDs::getWarningOptionForDiag
static StringRef getWarningOptionForDiag(unsigned DiagID)
Return the lowest-level warning option that enables the specified diagnostic.
Definition: DiagnosticIDs.cpp:620
clang::DiagnosticOptions
Options for controlling the compiler diagnostics engine.
Definition: DiagnosticOptions.h:70
clang::ento::ObjKind::OS
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...