| 1 | //===--- BackendDiagnosticHandler.cpp - Shared Backend Diagnostics ------===// |
| 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 | #include "clang/CodeGenUtils/BackendDiagnosticHandler.h" |
| 10 | #include "clang/Basic/CodeGenOptions.h" |
| 11 | #include "clang/Basic/Diagnostic.h" |
| 12 | #include "clang/Basic/DiagnosticFrontend.h" |
| 13 | #include "clang/Basic/FileManager.h" |
| 14 | #include "clang/Basic/SourceManager.h" |
| 15 | #include "llvm/ADT/Hashing.h" |
| 16 | #include "llvm/CodeGen/MachineOptimizationRemarkEmitter.h" |
| 17 | #include "llvm/Demangle/Demangle.h" |
| 18 | #include "llvm/IR/DiagnosticInfo.h" |
| 19 | #include "llvm/IR/DiagnosticPrinter.h" |
| 20 | #include "llvm/IR/Function.h" |
| 21 | #include "llvm/IR/Module.h" |
| 22 | #include "llvm/Support/raw_ostream.h" |
| 23 | |
| 24 | using namespace clang; |
| 25 | using namespace llvm; |
| 26 | |
| 27 | namespace { |
| 28 | /// Forwards llvm::DiagnosticHandler callbacks into a BackendDiagnosticConsumer. |
| 29 | class BackendDiagnosticHandlerImpl final : public DiagnosticHandler { |
| 30 | public: |
| 31 | BackendDiagnosticHandlerImpl(const CodeGenOptions &CGOpts, |
| 32 | BackendDiagnosticConsumer *Consumer) |
| 33 | : CodeGenOpts(CGOpts), Consumer(Consumer) {} |
| 34 | |
| 35 | bool handleDiagnostics(const DiagnosticInfo &DI) override { |
| 36 | Consumer->handleDiagnostics(DI); |
| 37 | return true; |
| 38 | } |
| 39 | |
| 40 | bool isAnalysisRemarkEnabled(StringRef PassName) const override { |
| 41 | return CodeGenOpts.OptimizationRemarkAnalysis.patternMatches(String: PassName); |
| 42 | } |
| 43 | bool isMissedOptRemarkEnabled(StringRef PassName) const override { |
| 44 | return CodeGenOpts.OptimizationRemarkMissed.patternMatches(String: PassName); |
| 45 | } |
| 46 | bool isPassedOptRemarkEnabled(StringRef PassName) const override { |
| 47 | return CodeGenOpts.OptimizationRemark.patternMatches(String: PassName); |
| 48 | } |
| 49 | |
| 50 | bool isAnyRemarkEnabled() const override { |
| 51 | return CodeGenOpts.OptimizationRemarkAnalysis.hasValidPattern() || |
| 52 | CodeGenOpts.OptimizationRemarkMissed.hasValidPattern() || |
| 53 | CodeGenOpts.OptimizationRemark.hasValidPattern(); |
| 54 | } |
| 55 | |
| 56 | private: |
| 57 | const CodeGenOptions &CodeGenOpts; |
| 58 | BackendDiagnosticConsumer *Consumer; |
| 59 | }; |
| 60 | |
| 61 | /// Convert a location in a temporary llvm::SourceMgr buffer to be a valid |
| 62 | /// FullSourceLoc. |
| 63 | FullSourceLoc ConvertBackendLocation(const llvm::SMDiagnostic &D, |
| 64 | SourceManager &CSM) { |
| 65 | // Get both the clang and llvm source managers. The location is relative to |
| 66 | // a memory buffer that the LLVM Source Manager is handling, we need to add |
| 67 | // a copy to the Clang source manager. |
| 68 | const llvm::SourceMgr &LSM = *D.getSourceMgr(); |
| 69 | |
| 70 | // We need to copy the underlying LLVM memory buffer because llvm::SourceMgr |
| 71 | // already owns its one and clang::SourceManager wants to own its one. |
| 72 | const MemoryBuffer *LBuf = |
| 73 | LSM.getMemoryBuffer(i: LSM.FindBufferContainingLoc(Loc: D.getLoc())); |
| 74 | |
| 75 | // Create the copy and transfer ownership to clang::SourceManager. |
| 76 | // TODO: Avoid copying files into memory. |
| 77 | std::unique_ptr<llvm::MemoryBuffer> CBuf = |
| 78 | llvm::MemoryBuffer::getMemBufferCopy(InputData: LBuf->getBuffer(), |
| 79 | BufferName: LBuf->getBufferIdentifier()); |
| 80 | // FIXME: Keep a file ID map instead of creating new IDs for each location. |
| 81 | FileID FID = CSM.createFileID(Buffer: std::move(CBuf)); |
| 82 | |
| 83 | // Translate the offset into the file. |
| 84 | unsigned Offset = D.getLoc().getPointer() - LBuf->getBufferStart(); |
| 85 | SourceLocation NewLoc = |
| 86 | CSM.getLocForStartOfFile(FID).getLocWithOffset(Offset); |
| 87 | return FullSourceLoc(NewLoc, CSM); |
| 88 | } |
| 89 | |
| 90 | } // namespace |
| 91 | |
| 92 | #define ComputeDiagID(Severity, GroupName, DiagID) \ |
| 93 | do { \ |
| 94 | switch (Severity) { \ |
| 95 | case llvm::DS_Error: \ |
| 96 | DiagID = diag::err_fe_##GroupName; \ |
| 97 | break; \ |
| 98 | case llvm::DS_Warning: \ |
| 99 | DiagID = diag::warn_fe_##GroupName; \ |
| 100 | break; \ |
| 101 | case llvm::DS_Remark: \ |
| 102 | llvm_unreachable("'remark' severity not expected"); \ |
| 103 | break; \ |
| 104 | case llvm::DS_Note: \ |
| 105 | DiagID = diag::note_fe_##GroupName; \ |
| 106 | break; \ |
| 107 | } \ |
| 108 | } while (false) |
| 109 | |
| 110 | #define (Severity, GroupName, DiagID) \ |
| 111 | do { \ |
| 112 | switch (Severity) { \ |
| 113 | case llvm::DS_Error: \ |
| 114 | DiagID = diag::err_fe_##GroupName; \ |
| 115 | break; \ |
| 116 | case llvm::DS_Warning: \ |
| 117 | DiagID = diag::warn_fe_##GroupName; \ |
| 118 | break; \ |
| 119 | case llvm::DS_Remark: \ |
| 120 | DiagID = diag::remark_fe_##GroupName; \ |
| 121 | break; \ |
| 122 | case llvm::DS_Note: \ |
| 123 | DiagID = diag::note_fe_##GroupName; \ |
| 124 | break; \ |
| 125 | } \ |
| 126 | } while (false) |
| 127 | |
| 128 | std::unique_ptr<llvm::DiagnosticHandler> |
| 129 | BackendDiagnosticConsumer::createDiagnosticHandler() { |
| 130 | return std::make_unique<BackendDiagnosticHandlerImpl>(args: CodeGenOpts, args: this); |
| 131 | } |
| 132 | |
| 133 | void BackendDiagnosticConsumer::addFunctionSourceLocation(StringRef MangledName, |
| 134 | FullSourceLoc Loc) { |
| 135 | // TODO: use a fast content hash when available. |
| 136 | auto NameHash = llvm::hash_value(S: MangledName); |
| 137 | ManglingFullSourceLocs.push_back(x: std::make_pair(x&: NameHash, y&: Loc)); |
| 138 | } |
| 139 | |
| 140 | std::optional<FullSourceLoc> |
| 141 | BackendDiagnosticConsumer::getFunctionSourceLocation(const Function &F) const { |
| 142 | auto Hash = llvm::hash_value(S: F.getName()); |
| 143 | for (const auto &Pair : ManglingFullSourceLocs) { |
| 144 | if (Pair.first == Hash) |
| 145 | return Pair.second; |
| 146 | } |
| 147 | return std::nullopt; |
| 148 | } |
| 149 | |
| 150 | OptionalFileEntryRef |
| 151 | BackendDiagnosticConsumer::getSourceFile(const llvm::DIFile *File, |
| 152 | ArrayRef<StringRef> Names) const { |
| 153 | auto It = DIFileSources.find(Val: File); |
| 154 | // A DIFile shared by several files identifies none of them, and looking up |
| 155 | // its name could find the wrong one. |
| 156 | if (It != DIFileSources.end()) |
| 157 | return It->second; |
| 158 | FileManager &FileMgr = SM->getFileManager(); |
| 159 | for (StringRef Name : Names) |
| 160 | if (auto FE = FileMgr.getOptionalFileRef(Filename: Name)) |
| 161 | return FE; |
| 162 | return std::nullopt; |
| 163 | } |
| 164 | |
| 165 | const FullSourceLoc BackendDiagnosticConsumer::getBestLocationFromDebugLoc( |
| 166 | const llvm::DiagnosticInfoWithLocationBase &D, bool &BadDebugInfo, |
| 167 | StringRef &Filename, unsigned &Line, unsigned &Column) const { |
| 168 | SourceManager &SourceMgr = *SM; |
| 169 | SourceLocation DILoc; |
| 170 | |
| 171 | if (D.isLocationAvailable()) { |
| 172 | D.getLocation(RelativePath&: Filename, Line, Column); |
| 173 | if (Line > 0) { |
| 174 | auto FE = getSourceFile(File: D.getLocation().getFile(), |
| 175 | Names: {Filename, D.getAbsolutePath()}); |
| 176 | if (FE) { |
| 177 | // If -gcolumn-info was not used, Column will be 0. This upsets the |
| 178 | // source manager, so pass 1 if Column is not set. |
| 179 | DILoc = SourceMgr.translateFileLineCol(SourceFile: *FE, Line, Col: Column ? Column : 1); |
| 180 | } |
| 181 | } |
| 182 | BadDebugInfo = DILoc.isInvalid(); |
| 183 | } |
| 184 | |
| 185 | // If a location isn't available, try to approximate it using the associated |
| 186 | // function definition. We use the definition's right brace to differentiate |
| 187 | // from diagnostics that genuinely relate to the function itself. |
| 188 | FullSourceLoc Loc(DILoc, SourceMgr); |
| 189 | if (Loc.isInvalid()) { |
| 190 | if (auto MaybeLoc = getFunctionSourceLocation(F: D.getFunction())) |
| 191 | Loc = *MaybeLoc; |
| 192 | } |
| 193 | |
| 194 | if (DILoc.isInvalid() && D.isLocationAvailable()) |
| 195 | // If we were not able to translate the file:line:col information |
| 196 | // back to a SourceLocation, at least emit a note stating that |
| 197 | // we could not translate this location. This can happen in the |
| 198 | // case of #line directives. |
| 199 | Diags.Report(Loc, DiagID: diag::note_fe_backend_invalid_loc) |
| 200 | << Filename << Line << Column; |
| 201 | |
| 202 | return Loc; |
| 203 | } |
| 204 | |
| 205 | void BackendDiagnosticConsumer::SrcMgrDiagHandler( |
| 206 | const llvm::DiagnosticInfoSrcMgr &DI) { |
| 207 | const llvm::SMDiagnostic &D = DI.getSMDiag(); |
| 208 | |
| 209 | unsigned DiagID; |
| 210 | if (DI.isInlineAsmDiag()) |
| 211 | ComputeDiagID(DI.getSeverity(), inline_asm, DiagID); |
| 212 | else |
| 213 | ComputeDiagID(DI.getSeverity(), source_mgr, DiagID); |
| 214 | |
| 215 | // This is for the empty consumer that uses the clang diagnostic |
| 216 | // handler for IR input files. |
| 217 | if (!SM) { |
| 218 | D.print(ProgName: nullptr, S&: llvm::errs()); |
| 219 | Diags.Report(DiagID).AddString(V: "cannot compile inline asm" ); |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | // There are a couple of different kinds of errors we could get here. |
| 224 | // First, we re-format the SMDiagnostic in terms of a clang diagnostic. |
| 225 | |
| 226 | // Strip "error: " off the start of the message string. |
| 227 | StringRef Message = D.getMessage(); |
| 228 | (void)Message.consume_front(Prefix: "error: " ); |
| 229 | |
| 230 | // If the SMDiagnostic has an inline asm source location, translate it. |
| 231 | FullSourceLoc Loc; |
| 232 | if (D.getLoc() != SMLoc()) |
| 233 | Loc = ConvertBackendLocation(D, CSM&: *SM); |
| 234 | |
| 235 | // If this problem has clang-level source location information, report the |
| 236 | // issue in the source with a note showing the instantiated |
| 237 | // code. |
| 238 | if (DI.isInlineAsmDiag()) { |
| 239 | SourceLocation LocCookie = |
| 240 | SourceLocation::getFromRawEncoding(Encoding: DI.getLocCookie()); |
| 241 | if (LocCookie.isValid()) { |
| 242 | Diags.Report(Loc: LocCookie, DiagID).AddString(V: Message); |
| 243 | |
| 244 | if (D.getLoc().isValid()) { |
| 245 | DiagnosticBuilder B = Diags.Report(Loc, DiagID: diag::note_fe_inline_asm_here); |
| 246 | // Convert the SMDiagnostic ranges into SourceRange and attach them |
| 247 | // to the diagnostic. |
| 248 | for (const std::pair<unsigned, unsigned> &Range : D.getRanges()) { |
| 249 | unsigned Column = D.getColumnNo(); |
| 250 | B << SourceRange(Loc.getLocWithOffset(Offset: Range.first - Column), |
| 251 | Loc.getLocWithOffset(Offset: Range.second - Column)); |
| 252 | } |
| 253 | } |
| 254 | return; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | // Otherwise, report the backend issue as occurring in the generated .s file. |
| 259 | // If Loc is invalid, we still need to report the issue, it just gets no |
| 260 | // location info. |
| 261 | Diags.Report(Loc, DiagID).AddString(V: Message); |
| 262 | } |
| 263 | |
| 264 | bool BackendDiagnosticConsumer::InlineAsmDiagHandler( |
| 265 | const llvm::DiagnosticInfoInlineAsm &D) { |
| 266 | unsigned DiagID; |
| 267 | ComputeDiagID(D.getSeverity(), inline_asm, DiagID); |
| 268 | std::string Message = D.getMsgStr().str(); |
| 269 | |
| 270 | // If this problem has clang-level source location information, report the |
| 271 | // issue as being a problem in the source with a note showing the instantiated |
| 272 | // code. |
| 273 | SourceLocation LocCookie = |
| 274 | SourceLocation::getFromRawEncoding(Encoding: D.getLocCookie()); |
| 275 | if (LocCookie.isValid()) |
| 276 | Diags.Report(Loc: LocCookie, DiagID).AddString(V: Message); |
| 277 | else { |
| 278 | // Otherwise, report the backend diagnostic as occurring in the generated |
| 279 | // .s file. |
| 280 | // If Loc is invalid, we still need to report the diagnostic, it just gets |
| 281 | // no location info. |
| 282 | FullSourceLoc Loc; |
| 283 | Diags.Report(Loc, DiagID).AddString(V: Message); |
| 284 | } |
| 285 | // We handled all the possible severities. |
| 286 | return true; |
| 287 | } |
| 288 | |
| 289 | bool BackendDiagnosticConsumer::StackSizeDiagHandler( |
| 290 | const llvm::DiagnosticInfoStackSize &D) { |
| 291 | if (D.getSeverity() != llvm::DS_Warning) |
| 292 | // For now, the only support we have for StackSize diagnostic is warning. |
| 293 | // We do not know how to format other severities. |
| 294 | return false; |
| 295 | |
| 296 | auto Loc = getFunctionSourceLocation(F: D.getFunction()); |
| 297 | if (!Loc) |
| 298 | return false; |
| 299 | |
| 300 | Diags.Report(Loc: *Loc, DiagID: diag::warn_fe_frame_larger_than) |
| 301 | << D.getStackSize() << D.getStackLimit() |
| 302 | << llvm::demangle(MangledName: D.getFunction().getName()); |
| 303 | return true; |
| 304 | } |
| 305 | |
| 306 | bool BackendDiagnosticConsumer::ResourceLimitDiagHandler( |
| 307 | const llvm::DiagnosticInfoResourceLimit &D) { |
| 308 | auto Loc = getFunctionSourceLocation(F: D.getFunction()); |
| 309 | if (!Loc) |
| 310 | return false; |
| 311 | unsigned DiagID = diag::err_fe_backend_resource_limit; |
| 312 | ComputeDiagID(D.getSeverity(), backend_resource_limit, DiagID); |
| 313 | |
| 314 | Diags.Report(Loc: *Loc, DiagID) |
| 315 | << D.getResourceName() << D.getResourceSize() << D.getResourceLimit() |
| 316 | << llvm::demangle(MangledName: D.getFunction().getName()); |
| 317 | return true; |
| 318 | } |
| 319 | |
| 320 | void BackendDiagnosticConsumer::UnsupportedDiagHandler( |
| 321 | const llvm::DiagnosticInfoUnsupported &D) { |
| 322 | // We only support warnings or errors. |
| 323 | assert(D.getSeverity() == llvm::DS_Error || |
| 324 | D.getSeverity() == llvm::DS_Warning); |
| 325 | |
| 326 | StringRef Filename; |
| 327 | unsigned Line, Column; |
| 328 | bool BadDebugInfo = false; |
| 329 | FullSourceLoc Loc; |
| 330 | std::string Msg; |
| 331 | raw_string_ostream MsgStream(Msg); |
| 332 | |
| 333 | // SM will be null for IR input files, we will construct the diag |
| 334 | // message from llvm::DiagnosticInfoUnsupported. |
| 335 | if (SM != nullptr) { |
| 336 | Loc = getBestLocationFromDebugLoc(D, BadDebugInfo, Filename, Line, Column); |
| 337 | MsgStream << D.getMessage(); |
| 338 | } else { |
| 339 | DiagnosticPrinterRawOStream DP(MsgStream); |
| 340 | D.print(DP); |
| 341 | } |
| 342 | |
| 343 | auto DiagType = D.getSeverity() == llvm::DS_Error |
| 344 | ? diag::err_fe_backend_unsupported |
| 345 | : diag::warn_fe_backend_unsupported; |
| 346 | Diags.Report(Loc, DiagID: DiagType) << Msg; |
| 347 | |
| 348 | if (BadDebugInfo) |
| 349 | // If we were not able to translate the file:line:col information |
| 350 | // back to a SourceLocation, at least emit a note stating that |
| 351 | // we could not translate this location. This can happen in the |
| 352 | // case of #line directives. |
| 353 | Diags.Report(Loc, DiagID: diag::note_fe_backend_invalid_loc) |
| 354 | << Filename << Line << Column; |
| 355 | } |
| 356 | |
| 357 | void BackendDiagnosticConsumer::UnsupportedTargetIntrinsicDiagHandler( |
| 358 | const llvm::DiagnosticInfoUnsupportedTargetIntrinsic &D) { |
| 359 | assert(D.getSeverity() == llvm::DS_Error && |
| 360 | "unsupported target intrinsic diagnostic should be an error" ); |
| 361 | |
| 362 | StringRef Filename; |
| 363 | unsigned Line, Column; |
| 364 | bool BadDebugInfo = false; |
| 365 | FullSourceLoc Loc; |
| 366 | std::string Msg; |
| 367 | raw_string_ostream MsgStream(Msg); |
| 368 | |
| 369 | // SM will be null for IR input files, so construct the diagnostic |
| 370 | // message from llvm::DiagnosticInfoUnsupportedTargetIntrinsic. |
| 371 | if (SM != nullptr) { |
| 372 | Loc = getBestLocationFromDebugLoc(D, BadDebugInfo, Filename, Line, Column); |
| 373 | MsgStream << D.getMessage(); |
| 374 | } else { |
| 375 | DiagnosticPrinterRawOStream DP(MsgStream); |
| 376 | D.print(DP); |
| 377 | } |
| 378 | |
| 379 | Diags.Report(Loc, DiagID: diag::err_fe_backend_unsupported) << Msg; |
| 380 | |
| 381 | if (BadDebugInfo) { |
| 382 | // If we were not able to translate the file:line:col information |
| 383 | // back to a SourceLocation, at least emit a note stating that |
| 384 | // we could not translate this location. This can happen in the |
| 385 | // case of #line directives. |
| 386 | Diags.Report(Loc, DiagID: diag::note_fe_backend_invalid_loc) |
| 387 | << Filename << Line << Column; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | void BackendDiagnosticConsumer::EmitOptimizationMessage( |
| 392 | const llvm::DiagnosticInfoOptimizationBase &D, unsigned DiagID) { |
| 393 | // We only support warnings and remarks. |
| 394 | assert(D.getSeverity() == llvm::DS_Remark || |
| 395 | D.getSeverity() == llvm::DS_Warning); |
| 396 | |
| 397 | StringRef Filename; |
| 398 | unsigned Line, Column; |
| 399 | bool BadDebugInfo = false; |
| 400 | FullSourceLoc Loc; |
| 401 | std::string Msg; |
| 402 | raw_string_ostream MsgStream(Msg); |
| 403 | |
| 404 | // SM will be null for IR input files, we will construct the remark |
| 405 | // message from llvm::DiagnosticInfoOptimizationBase. |
| 406 | if (SM != nullptr) { |
| 407 | Loc = getBestLocationFromDebugLoc(D, BadDebugInfo, Filename, Line, Column); |
| 408 | MsgStream << D.getMsg(); |
| 409 | } else { |
| 410 | DiagnosticPrinterRawOStream DP(MsgStream); |
| 411 | D.print(DP); |
| 412 | } |
| 413 | |
| 414 | if (D.getHotness()) |
| 415 | MsgStream << " (hotness: " << *D.getHotness() << ")" ; |
| 416 | |
| 417 | Diags.Report(Loc, DiagID) << AddFlagValue(D.getPassName()) << Msg; |
| 418 | |
| 419 | if (BadDebugInfo) |
| 420 | // If we were not able to translate the file:line:col information |
| 421 | // back to a SourceLocation, at least emit a note stating that |
| 422 | // we could not translate this location. This can happen in the |
| 423 | // case of #line directives. |
| 424 | Diags.Report(Loc, DiagID: diag::note_fe_backend_invalid_loc) |
| 425 | << Filename << Line << Column; |
| 426 | } |
| 427 | |
| 428 | void BackendDiagnosticConsumer::OptimizationRemarkHandler( |
| 429 | const llvm::DiagnosticInfoOptimizationBase &D) { |
| 430 | // Without hotness information, don't show noisy remarks. |
| 431 | if (D.isVerbose() && !D.getHotness()) |
| 432 | return; |
| 433 | |
| 434 | if (D.isPassed()) { |
| 435 | // Optimization remarks are active only if the -Rpass flag has a regular |
| 436 | // expression that matches the name of the pass name in \p D. |
| 437 | if (CodeGenOpts.OptimizationRemark.patternMatches(String: D.getPassName())) |
| 438 | EmitOptimizationMessage(D, DiagID: diag::remark_fe_backend_optimization_remark); |
| 439 | } else if (D.isMissed()) { |
| 440 | // Missed optimization remarks are active only if the -Rpass-missed |
| 441 | // flag has a regular expression that matches the name of the pass |
| 442 | // name in \p D. |
| 443 | if (CodeGenOpts.OptimizationRemarkMissed.patternMatches(String: D.getPassName())) |
| 444 | EmitOptimizationMessage( |
| 445 | D, DiagID: diag::remark_fe_backend_optimization_remark_missed); |
| 446 | } else { |
| 447 | assert(D.isAnalysis() && "Unknown remark type" ); |
| 448 | |
| 449 | bool ShouldAlwaysPrint = false; |
| 450 | if (auto *ORA = dyn_cast<llvm::OptimizationRemarkAnalysis>(Val: &D)) |
| 451 | ShouldAlwaysPrint = ORA->shouldAlwaysPrint(); |
| 452 | |
| 453 | if (ShouldAlwaysPrint || |
| 454 | CodeGenOpts.OptimizationRemarkAnalysis.patternMatches(String: D.getPassName())) |
| 455 | EmitOptimizationMessage( |
| 456 | D, DiagID: diag::remark_fe_backend_optimization_remark_analysis); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | void BackendDiagnosticConsumer::OptimizationRemarkHandler( |
| 461 | const llvm::OptimizationRemarkAnalysisFPCommute &D) { |
| 462 | // Optimization analysis remarks are active if the pass name is set to |
| 463 | // llvm::DiagnosticInfo::AlwaysPrint or if the -Rpass-analysis flag has a |
| 464 | // regular expression that matches the name of the pass name in \p D. |
| 465 | |
| 466 | if (D.shouldAlwaysPrint() || |
| 467 | CodeGenOpts.OptimizationRemarkAnalysis.patternMatches(String: D.getPassName())) |
| 468 | EmitOptimizationMessage( |
| 469 | D, DiagID: diag::remark_fe_backend_optimization_remark_analysis_fpcommute); |
| 470 | } |
| 471 | |
| 472 | void BackendDiagnosticConsumer::OptimizationRemarkHandler( |
| 473 | const llvm::OptimizationRemarkAnalysisAliasing &D) { |
| 474 | // Optimization analysis remarks are active if the pass name is set to |
| 475 | // llvm::DiagnosticInfo::AlwaysPrint or if the -Rpass-analysis flag has a |
| 476 | // regular expression that matches the name of the pass name in \p D. |
| 477 | |
| 478 | if (D.shouldAlwaysPrint() || |
| 479 | CodeGenOpts.OptimizationRemarkAnalysis.patternMatches(String: D.getPassName())) |
| 480 | EmitOptimizationMessage( |
| 481 | D, DiagID: diag::remark_fe_backend_optimization_remark_analysis_aliasing); |
| 482 | } |
| 483 | |
| 484 | void BackendDiagnosticConsumer::OptimizationFailureHandler( |
| 485 | const llvm::DiagnosticInfoOptimizationFailure &D) { |
| 486 | EmitOptimizationMessage(D, DiagID: diag::warn_fe_backend_optimization_failure); |
| 487 | } |
| 488 | |
| 489 | void BackendDiagnosticConsumer::DontCallDiagHandler( |
| 490 | const DiagnosticInfoDontCall &D) { |
| 491 | SourceLocation LocCookie = |
| 492 | SourceLocation::getFromRawEncoding(Encoding: D.getLocCookie()); |
| 493 | |
| 494 | // FIXME: we can't yet diagnose indirect calls. When/if we can, we |
| 495 | // should instead assert that LocCookie.isValid(). |
| 496 | if (!LocCookie.isValid()) |
| 497 | return; |
| 498 | |
| 499 | Diags.Report(Loc: LocCookie, DiagID: D.getSeverity() == DiagnosticSeverity::DS_Error |
| 500 | ? diag::err_fe_backend_error_attr |
| 501 | : diag::warn_fe_backend_warning_attr) |
| 502 | << llvm::demangle(MangledName: D.getFunctionName()) << D.getNote(); |
| 503 | |
| 504 | if (!CodeGenOpts.ShowInliningChain) |
| 505 | return; |
| 506 | |
| 507 | auto EmitNote = [&](SourceLocation Loc, StringRef FuncName, bool IsFirst) { |
| 508 | if (!Loc.isValid()) |
| 509 | Loc = LocCookie; |
| 510 | unsigned DiagID = |
| 511 | IsFirst ? diag::note_fe_backend_in : diag::note_fe_backend_inlined; |
| 512 | Diags.Report(Loc, DiagID) << llvm::demangle(MangledName: FuncName.str()); |
| 513 | }; |
| 514 | |
| 515 | // Try debug info first for accurate source locations. |
| 516 | if (!D.getDebugInlineChain().empty()) { |
| 517 | SourceManager &SrcMgr = *SM; |
| 518 | for (const auto &[I, Info] : llvm::enumerate(First: D.getDebugInlineChain())) { |
| 519 | SourceLocation Loc; |
| 520 | if (Info.Line > 0) |
| 521 | if (auto FE = getSourceFile(File: Info.File, Names: Info.Filename)) |
| 522 | Loc = SrcMgr.translateFileLineCol(SourceFile: *FE, Line: Info.Line, |
| 523 | Col: Info.Column ? Info.Column : 1); |
| 524 | EmitNote(Loc, Info.FuncName, I == 0); |
| 525 | } |
| 526 | return; |
| 527 | } |
| 528 | |
| 529 | // Fall back to heuristic (srcloc metadata) when debug info is unavailable. |
| 530 | auto InliningDecisions = D.getInliningDecisions(); |
| 531 | if (InliningDecisions.empty()) |
| 532 | return; |
| 533 | |
| 534 | for (const auto &[I, Entry] : llvm::enumerate(First&: InliningDecisions)) { |
| 535 | SourceLocation Loc = |
| 536 | I == 0 ? LocCookie : SourceLocation::getFromRawEncoding(Encoding: Entry.second); |
| 537 | EmitNote(Loc, Entry.first, I == 0); |
| 538 | } |
| 539 | |
| 540 | // Suggest enabling debug info (at least -gline-directives-only) for more |
| 541 | // accurate locations. |
| 542 | Diags.Report(Loc: LocCookie, DiagID: diag::note_fe_backend_inlining_debug_info); |
| 543 | } |
| 544 | |
| 545 | void BackendDiagnosticConsumer::MisExpectDiagHandler( |
| 546 | const llvm::DiagnosticInfoMisExpect &D) { |
| 547 | StringRef Filename; |
| 548 | unsigned Line, Column; |
| 549 | bool BadDebugInfo = false; |
| 550 | FullSourceLoc Loc = |
| 551 | getBestLocationFromDebugLoc(D, BadDebugInfo, Filename, Line, Column); |
| 552 | |
| 553 | Diags.Report(Loc, DiagID: diag::warn_profile_data_misexpect) << D.getMsg().str(); |
| 554 | |
| 555 | if (BadDebugInfo) |
| 556 | // If we were not able to translate the file:line:col information |
| 557 | // back to a SourceLocation, at least emit a note stating that |
| 558 | // we could not translate this location. This can happen in the |
| 559 | // case of #line directives. |
| 560 | Diags.Report(Loc, DiagID: diag::note_fe_backend_invalid_loc) |
| 561 | << Filename << Line << Column; |
| 562 | } |
| 563 | |
| 564 | void BackendDiagnosticConsumer::handleDiagnostics(const DiagnosticInfo &DI) { |
| 565 | unsigned DiagID = diag::err_fe_inline_asm; |
| 566 | llvm::DiagnosticSeverity Severity = DI.getSeverity(); |
| 567 | // Get the diagnostic ID based. |
| 568 | switch (DI.getKind()) { |
| 569 | case llvm::DK_InlineAsm: |
| 570 | if (InlineAsmDiagHandler(D: cast<DiagnosticInfoInlineAsm>(Val: DI))) |
| 571 | return; |
| 572 | ComputeDiagID(Severity, inline_asm, DiagID); |
| 573 | break; |
| 574 | case llvm::DK_SrcMgr: |
| 575 | SrcMgrDiagHandler(DI: cast<DiagnosticInfoSrcMgr>(Val: DI)); |
| 576 | return; |
| 577 | case llvm::DK_StackSize: |
| 578 | if (StackSizeDiagHandler(D: cast<DiagnosticInfoStackSize>(Val: DI))) |
| 579 | return; |
| 580 | ComputeDiagID(Severity, backend_frame_larger_than, DiagID); |
| 581 | break; |
| 582 | case llvm::DK_ResourceLimit: |
| 583 | if (ResourceLimitDiagHandler(D: cast<DiagnosticInfoResourceLimit>(Val: DI))) |
| 584 | return; |
| 585 | ComputeDiagID(Severity, backend_resource_limit, DiagID); |
| 586 | break; |
| 587 | case DK_Linker: |
| 588 | ComputeDiagID(Severity, linking_module, DiagID); |
| 589 | break; |
| 590 | case llvm::DK_OptimizationRemark: |
| 591 | // Optimization remarks are always handled completely by this |
| 592 | // handler. There is no generic way of emitting them. |
| 593 | OptimizationRemarkHandler(D: cast<OptimizationRemark>(Val: DI)); |
| 594 | return; |
| 595 | case llvm::DK_OptimizationRemarkMissed: |
| 596 | // Optimization remarks are always handled completely by this |
| 597 | // handler. There is no generic way of emitting them. |
| 598 | OptimizationRemarkHandler(D: cast<OptimizationRemarkMissed>(Val: DI)); |
| 599 | return; |
| 600 | case llvm::DK_OptimizationRemarkAnalysis: |
| 601 | // Optimization remarks are always handled completely by this |
| 602 | // handler. There is no generic way of emitting them. |
| 603 | OptimizationRemarkHandler(D: cast<OptimizationRemarkAnalysis>(Val: DI)); |
| 604 | return; |
| 605 | case llvm::DK_OptimizationRemarkAnalysisFPCommute: |
| 606 | // Optimization remarks are always handled completely by this |
| 607 | // handler. There is no generic way of emitting them. |
| 608 | OptimizationRemarkHandler(D: cast<OptimizationRemarkAnalysisFPCommute>(Val: DI)); |
| 609 | return; |
| 610 | case llvm::DK_OptimizationRemarkAnalysisAliasing: |
| 611 | // Optimization remarks are always handled completely by this |
| 612 | // handler. There is no generic way of emitting them. |
| 613 | OptimizationRemarkHandler(D: cast<OptimizationRemarkAnalysisAliasing>(Val: DI)); |
| 614 | return; |
| 615 | case llvm::DK_MachineOptimizationRemark: |
| 616 | // Optimization remarks are always handled completely by this |
| 617 | // handler. There is no generic way of emitting them. |
| 618 | OptimizationRemarkHandler(D: cast<MachineOptimizationRemark>(Val: DI)); |
| 619 | return; |
| 620 | case llvm::DK_MachineOptimizationRemarkMissed: |
| 621 | // Optimization remarks are always handled completely by this |
| 622 | // handler. There is no generic way of emitting them. |
| 623 | OptimizationRemarkHandler(D: cast<MachineOptimizationRemarkMissed>(Val: DI)); |
| 624 | return; |
| 625 | case llvm::DK_MachineOptimizationRemarkAnalysis: |
| 626 | // Optimization remarks are always handled completely by this |
| 627 | // handler. There is no generic way of emitting them. |
| 628 | OptimizationRemarkHandler(D: cast<MachineOptimizationRemarkAnalysis>(Val: DI)); |
| 629 | return; |
| 630 | case llvm::DK_OptimizationFailure: |
| 631 | // Optimization failures are always handled completely by this |
| 632 | // handler. |
| 633 | OptimizationFailureHandler(D: cast<DiagnosticInfoOptimizationFailure>(Val: DI)); |
| 634 | return; |
| 635 | case llvm::DK_Unsupported: |
| 636 | UnsupportedDiagHandler(D: cast<DiagnosticInfoUnsupported>(Val: DI)); |
| 637 | return; |
| 638 | case llvm::DK_UnsupportedTargetIntrinsic: |
| 639 | UnsupportedTargetIntrinsicDiagHandler( |
| 640 | D: cast<DiagnosticInfoUnsupportedTargetIntrinsic>(Val: DI)); |
| 641 | return; |
| 642 | case llvm::DK_DontCall: |
| 643 | DontCallDiagHandler(D: cast<DiagnosticInfoDontCall>(Val: DI)); |
| 644 | return; |
| 645 | case llvm::DK_MisExpect: |
| 646 | MisExpectDiagHandler(D: cast<DiagnosticInfoMisExpect>(Val: DI)); |
| 647 | return; |
| 648 | default: |
| 649 | // Plugin IDs are not bound to any value as they are set dynamically. |
| 650 | ComputeDiagRemarkID(Severity, backend_plugin, DiagID); |
| 651 | break; |
| 652 | } |
| 653 | std::string MsgStorage; |
| 654 | { |
| 655 | raw_string_ostream Stream(MsgStorage); |
| 656 | DiagnosticPrinterRawOStream DP(Stream); |
| 657 | DI.print(DP); |
| 658 | } |
| 659 | |
| 660 | if (DI.getKind() == DK_Linker) { |
| 661 | assert(CurLinkModule && "CurLinkModule must be set for linker diagnostics" ); |
| 662 | Diags.Report(DiagID) << CurLinkModule->getModuleIdentifier() << MsgStorage; |
| 663 | return; |
| 664 | } |
| 665 | |
| 666 | // Report the backend message using the usual diagnostic mechanism. |
| 667 | FullSourceLoc Loc; |
| 668 | Diags.Report(Loc, DiagID).AddString(V: MsgStorage); |
| 669 | } |
| 670 | |
| 671 | #undef ComputeDiagID |
| 672 | #undef ComputeDiagRemarkID |
| 673 | |