1 //===--- PlistReporter.cpp - ARC Migrate Tool Plist Reporter ----*- C++ -*-===//
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 //===----------------------------------------------------------------------===//
10 #include "clang/Basic/FileManager.h"
11 #include "clang/Basic/PlistSupport.h"
12 #include "clang/Basic/SourceManager.h"
13 #include "clang/Lex/Lexer.h"
14 using namespace clang
;
15 using namespace arcmt
;
16 using namespace markup
;
18 static StringRef
getLevelName(DiagnosticsEngine::Level Level
) {
20 case DiagnosticsEngine::Ignored
:
21 llvm_unreachable("ignored");
22 case DiagnosticsEngine::Note
:
24 case DiagnosticsEngine::Remark
:
25 case DiagnosticsEngine::Warning
:
27 case DiagnosticsEngine::Fatal
:
28 case DiagnosticsEngine::Error
:
31 llvm_unreachable("Invalid DiagnosticsEngine level!");
34 void arcmt::writeARCDiagsToPlist(const std::string
&outPath
,
35 ArrayRef
<StoredDiagnostic
> diags
,
37 const LangOptions
&LangOpts
) {
38 DiagnosticIDs DiagIDs
;
40 // Build up a set of FIDs that we use by scanning the locations and
41 // ranges of the diagnostics.
43 SmallVector
<FileID
, 10> Fids
;
45 for (ArrayRef
<StoredDiagnostic
>::iterator
46 I
= diags
.begin(), E
= diags
.end(); I
!= E
; ++I
) {
47 const StoredDiagnostic
&D
= *I
;
49 AddFID(FM
, Fids
, SM
, D
.getLocation());
51 for (StoredDiagnostic::range_iterator
52 RI
= D
.range_begin(), RE
= D
.range_end(); RI
!= RE
; ++RI
) {
53 AddFID(FM
, Fids
, SM
, RI
->getBegin());
54 AddFID(FM
, Fids
, SM
, RI
->getEnd());
59 llvm::raw_fd_ostream
o(outPath
, EC
, llvm::sys::fs::OF_TextWithCRLF
);
61 llvm::errs() << "error: could not create file: " << outPath
<< '\n';
67 // Write the root object: a <dict> containing...
68 // - "files", an <array> mapping from FIDs to file names
69 // - "diagnostics", an <array> containing the diagnostics
74 for (FileID FID
: Fids
)
75 EmitString(o
<< " ", SM
.getFileEntryRefForID(FID
)->getName()) << '\n';
78 " <key>diagnostics</key>\n"
81 for (ArrayRef
<StoredDiagnostic
>::iterator
82 DI
= diags
.begin(), DE
= diags
.end(); DI
!= DE
; ++DI
) {
84 const StoredDiagnostic
&D
= *DI
;
86 if (D
.getLevel() == DiagnosticsEngine::Ignored
)
91 // Output the diagnostic.
92 o
<< " <key>description</key>";
93 EmitString(o
, D
.getMessage()) << '\n';
94 o
<< " <key>category</key>";
95 EmitString(o
, DiagIDs
.getCategoryNameFromID(
96 DiagIDs
.getCategoryNumberForDiag(D
.getID()))) << '\n';
97 o
<< " <key>type</key>";
98 EmitString(o
, getLevelName(D
.getLevel())) << '\n';
100 // Output the location of the bug.
101 o
<< " <key>location</key>\n";
102 EmitLocation(o
, SM
, D
.getLocation(), FM
, 2);
104 // Output the ranges (if any).
105 if (!D
.getRanges().empty()) {
106 o
<< " <key>ranges</key>\n";
108 for (auto &R
: D
.getRanges()) {
109 CharSourceRange ExpansionRange
= SM
.getExpansionRange(R
);
110 EmitRange(o
, SM
, Lexer::getAsCharRange(ExpansionRange
, SM
, LangOpts
),
116 // Close up the entry.
123 o
<< "</dict>\n</plist>\n";