/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/ARCMigrate/PlistReporter.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- PlistReporter.cpp - ARC Migrate Tool Plist Reporter ----*- C++ -*-===// |
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 "Internals.h" |
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; |
17 | | |
18 | 1 | static StringRef getLevelName(DiagnosticsEngine::Level Level) { |
19 | 1 | switch (Level) { |
20 | 0 | case DiagnosticsEngine::Ignored: |
21 | 0 | llvm_unreachable("ignored"); |
22 | 0 | case DiagnosticsEngine::Note: |
23 | 0 | return "note"; |
24 | 0 | case DiagnosticsEngine::Remark: |
25 | 0 | case DiagnosticsEngine::Warning: |
26 | 0 | return "warning"; |
27 | 0 | case DiagnosticsEngine::Fatal: |
28 | 1 | case DiagnosticsEngine::Error: |
29 | 1 | return "error"; |
30 | 0 | } |
31 | 0 | llvm_unreachable("Invalid DiagnosticsEngine level!"); |
32 | 0 | } |
33 | | |
34 | | void arcmt::writeARCDiagsToPlist(const std::string &outPath, |
35 | | ArrayRef<StoredDiagnostic> diags, |
36 | | SourceManager &SM, |
37 | 1 | const LangOptions &LangOpts) { |
38 | 1 | DiagnosticIDs DiagIDs; |
39 | | |
40 | | // Build up a set of FIDs that we use by scanning the locations and |
41 | | // ranges of the diagnostics. |
42 | 1 | FIDMap FM; |
43 | 1 | SmallVector<FileID, 10> Fids; |
44 | | |
45 | 1 | for (ArrayRef<StoredDiagnostic>::iterator |
46 | 2 | I = diags.begin(), E = diags.end(); I != E; ++I1 ) { |
47 | 1 | const StoredDiagnostic &D = *I; |
48 | | |
49 | 1 | AddFID(FM, Fids, SM, D.getLocation()); |
50 | | |
51 | 1 | for (StoredDiagnostic::range_iterator |
52 | 2 | RI = D.range_begin(), RE = D.range_end(); RI != RE; ++RI1 ) { |
53 | 1 | AddFID(FM, Fids, SM, RI->getBegin()); |
54 | 1 | AddFID(FM, Fids, SM, RI->getEnd()); |
55 | 1 | } |
56 | 1 | } |
57 | | |
58 | 1 | std::error_code EC; |
59 | 1 | llvm::raw_fd_ostream o(outPath, EC, llvm::sys::fs::OF_Text); |
60 | 1 | if (EC) { |
61 | 0 | llvm::errs() << "error: could not create file: " << outPath << '\n'; |
62 | 0 | return; |
63 | 0 | } |
64 | | |
65 | 1 | EmitPlistHeader(o); |
66 | | |
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 |
70 | 1 | o << "<dict>\n" |
71 | 1 | " <key>files</key>\n" |
72 | 1 | " <array>\n"; |
73 | | |
74 | 1 | for (FileID FID : Fids) |
75 | 1 | EmitString(o << " ", SM.getFileEntryForID(FID)->getName()) << '\n'; |
76 | | |
77 | 1 | o << " </array>\n" |
78 | 1 | " <key>diagnostics</key>\n" |
79 | 1 | " <array>\n"; |
80 | | |
81 | 1 | for (ArrayRef<StoredDiagnostic>::iterator |
82 | 2 | DI = diags.begin(), DE = diags.end(); DI != DE; ++DI1 ) { |
83 | | |
84 | 1 | const StoredDiagnostic &D = *DI; |
85 | | |
86 | 1 | if (D.getLevel() == DiagnosticsEngine::Ignored) |
87 | 0 | continue; |
88 | | |
89 | 1 | o << " <dict>\n"; |
90 | | |
91 | | // Output the diagnostic. |
92 | 1 | o << " <key>description</key>"; |
93 | 1 | EmitString(o, D.getMessage()) << '\n'; |
94 | 1 | o << " <key>category</key>"; |
95 | 1 | EmitString(o, DiagIDs.getCategoryNameFromID( |
96 | 1 | DiagIDs.getCategoryNumberForDiag(D.getID()))) << '\n'; |
97 | 1 | o << " <key>type</key>"; |
98 | 1 | EmitString(o, getLevelName(D.getLevel())) << '\n'; |
99 | | |
100 | | // Output the location of the bug. |
101 | 1 | o << " <key>location</key>\n"; |
102 | 1 | EmitLocation(o, SM, D.getLocation(), FM, 2); |
103 | | |
104 | | // Output the ranges (if any). |
105 | 1 | if (!D.getRanges().empty()) { |
106 | 1 | o << " <key>ranges</key>\n"; |
107 | 1 | o << " <array>\n"; |
108 | 1 | for (auto &R : D.getRanges()) { |
109 | 1 | CharSourceRange ExpansionRange = SM.getExpansionRange(R); |
110 | 1 | EmitRange(o, SM, Lexer::getAsCharRange(ExpansionRange, SM, LangOpts), |
111 | 1 | FM, 4); |
112 | 1 | } |
113 | 1 | o << " </array>\n"; |
114 | 1 | } |
115 | | |
116 | | // Close up the entry. |
117 | 1 | o << " </dict>\n"; |
118 | 1 | } |
119 | | |
120 | 1 | o << " </array>\n"; |
121 | | |
122 | | // Finish. |
123 | 1 | o << "</dict>\n</plist>\n"; |
124 | 1 | } |