Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 1 | //===--- PlistDiagnostics.cpp - Plist Diagnostics for Paths -----*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the PlistDiagnostics object. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Daniel Dunbar | e1bd4e6 | 2009-03-02 06:16:29 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/PathDiagnosticClients.h" |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/PathDiagnostic.h" |
| 16 | #include "clang/Basic/SourceManager.h" |
| 17 | #include "clang/Basic/FileManager.h" |
| 18 | #include "llvm/Support/Compiler.h" |
| 19 | #include "llvm/Support/raw_ostream.h" |
| 20 | #include "llvm/System/Path.h" |
| 21 | #include "llvm/ADT/DenseMap.h" |
| 22 | #include "llvm/ADT/SmallVector.h" |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 23 | using namespace clang; |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 24 | |
| 25 | typedef llvm::DenseMap<FileID, unsigned> FIDMap; |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 26 | |
Ted Kremenek | 4fc82c8 | 2008-11-03 23:18:07 +0000 | [diff] [blame] | 27 | namespace clang { |
| 28 | class Preprocessor; |
| 29 | class PreprocessorFactory; |
| 30 | } |
| 31 | |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 32 | namespace { |
| 33 | class VISIBILITY_HIDDEN PlistDiagnostics : public PathDiagnosticClient { |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 34 | std::vector<const PathDiagnostic*> BatchedDiags; |
| 35 | const std::string OutputFile; |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 36 | public: |
| 37 | PlistDiagnostics(const std::string& prefix); |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 38 | ~PlistDiagnostics(); |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 39 | void HandlePathDiagnostic(const PathDiagnostic* D); |
| 40 | }; |
| 41 | } // end anonymous namespace |
| 42 | |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 43 | PlistDiagnostics::PlistDiagnostics(const std::string& output) |
| 44 | : OutputFile(output) {} |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 45 | |
Ted Kremenek | 4fc82c8 | 2008-11-03 23:18:07 +0000 | [diff] [blame] | 46 | PathDiagnosticClient* |
| 47 | clang::CreatePlistDiagnosticClient(const std::string& s, |
| 48 | Preprocessor*, PreprocessorFactory*) { |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 49 | return new PlistDiagnostics(s); |
| 50 | } |
| 51 | |
Chris Lattner | a11d617 | 2009-01-19 07:46:45 +0000 | [diff] [blame] | 52 | static void AddFID(FIDMap &FIDs, llvm::SmallVectorImpl<FileID> &V, |
Ted Kremenek | c472d79 | 2009-01-23 20:06:20 +0000 | [diff] [blame] | 53 | SourceManager* SM, SourceLocation L) { |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 54 | |
Ted Kremenek | c472d79 | 2009-01-23 20:06:20 +0000 | [diff] [blame] | 55 | FileID FID = SM->getFileID(SM->getInstantiationLoc(L)); |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 56 | FIDMap::iterator I = FIDs.find(FID); |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 57 | if (I != FIDs.end()) return; |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 58 | FIDs[FID] = V.size(); |
| 59 | V.push_back(FID); |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Ted Kremenek | c472d79 | 2009-01-23 20:06:20 +0000 | [diff] [blame] | 62 | static unsigned GetFID(const FIDMap& FIDs, SourceManager* SM, SourceLocation L){ |
| 63 | FileID FID = SM->getFileID(SM->getInstantiationLoc(L)); |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 64 | FIDMap::const_iterator I = FIDs.find(FID); |
| 65 | assert(I != FIDs.end()); |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 66 | return I->second; |
| 67 | } |
| 68 | |
| 69 | static llvm::raw_ostream& Indent(llvm::raw_ostream& o, const unsigned indent) { |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 70 | for (unsigned i = 0; i < indent; ++i) |
| 71 | o << ' '; |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 72 | return o; |
| 73 | } |
| 74 | |
Ted Kremenek | c472d79 | 2009-01-23 20:06:20 +0000 | [diff] [blame] | 75 | static void EmitLocation(llvm::raw_ostream& o, SourceManager* SM, |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 76 | SourceLocation L, const FIDMap& FM, |
| 77 | const unsigned indent) { |
| 78 | |
| 79 | Indent(o, indent) << "<dict>\n"; |
| 80 | Indent(o, indent) << " <key>line</key><integer>" |
Ted Kremenek | c472d79 | 2009-01-23 20:06:20 +0000 | [diff] [blame] | 81 | << SM->getInstantiationLineNumber(L) << "</integer>\n"; |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 82 | Indent(o, indent) << " <key>col</key><integer>" |
Ted Kremenek | c472d79 | 2009-01-23 20:06:20 +0000 | [diff] [blame] | 83 | << SM->getInstantiationColumnNumber(L) << "</integer>\n"; |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 84 | Indent(o, indent) << " <key>file</key><integer>" |
| 85 | << GetFID(FM, SM, L) << "</integer>\n"; |
| 86 | Indent(o, indent) << "</dict>\n"; |
| 87 | } |
| 88 | |
Ted Kremenek | c472d79 | 2009-01-23 20:06:20 +0000 | [diff] [blame] | 89 | static void EmitRange(llvm::raw_ostream& o, SourceManager* SM, SourceRange R, |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 90 | const FIDMap& FM, const unsigned indent) { |
| 91 | |
| 92 | Indent(o, indent) << "<array>\n"; |
| 93 | EmitLocation(o, SM, R.getBegin(), FM, indent+1); |
| 94 | EmitLocation(o, SM, R.getEnd(), FM, indent+1); |
| 95 | Indent(o, indent) << "</array>\n"; |
| 96 | } |
| 97 | |
| 98 | static void ReportDiag(llvm::raw_ostream& o, const PathDiagnosticPiece& P, |
Ted Kremenek | c472d79 | 2009-01-23 20:06:20 +0000 | [diff] [blame] | 99 | const FIDMap& FM, SourceManager* SM) { |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 100 | |
Ted Kremenek | d671c5a | 2009-02-02 21:45:32 +0000 | [diff] [blame] | 101 | unsigned indent = 4; |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 102 | Indent(o, indent) << "<dict>\n"; |
| 103 | ++indent; |
| 104 | |
| 105 | // Output the location. |
| 106 | FullSourceLoc L = P.getLocation(); |
| 107 | |
| 108 | Indent(o, indent) << "<key>location</key>\n"; |
Chris Lattner | 59ddeab | 2009-01-16 23:06:35 +0000 | [diff] [blame] | 109 | EmitLocation(o, SM, L, FM, indent); |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 110 | |
| 111 | // Output the ranges (if any). |
| 112 | PathDiagnosticPiece::range_iterator RI = P.ranges_begin(), |
| 113 | RE = P.ranges_end(); |
| 114 | |
| 115 | if (RI != RE) { |
| 116 | Indent(o, indent) << "<key>ranges</key>\n"; |
| 117 | Indent(o, indent) << "<array>\n"; |
Ted Kremenek | d671c5a | 2009-02-02 21:45:32 +0000 | [diff] [blame] | 118 | ++indent; |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 119 | for ( ; RI != RE; ++RI ) EmitRange(o, SM, *RI, FM, indent+1); |
Ted Kremenek | d671c5a | 2009-02-02 21:45:32 +0000 | [diff] [blame] | 120 | --indent; |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 121 | Indent(o, indent) << "</array>\n"; |
| 122 | } |
| 123 | |
| 124 | // Output the text. |
| 125 | Indent(o, indent) << "<key>message</key>\n"; |
Ted Kremenek | 4fc82c8 | 2008-11-03 23:18:07 +0000 | [diff] [blame] | 126 | Indent(o, indent) << "<string>" << P.getString() << "</string>\n"; |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 127 | |
| 128 | // Output the hint. |
Ted Kremenek | 3d7f2bc | 2009-03-02 19:40:15 +0000 | [diff] [blame] | 129 | #if 0 |
| 130 | // Disable the display hint until we clear up its meaning. |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 131 | Indent(o, indent) << "<key>displayhint</key>\n"; |
| 132 | Indent(o, indent) << "<string>" |
| 133 | << (P.getDisplayHint() == PathDiagnosticPiece::Above |
| 134 | ? "above" : "below") |
| 135 | << "</string>\n"; |
Ted Kremenek | 3d7f2bc | 2009-03-02 19:40:15 +0000 | [diff] [blame] | 136 | #endif |
Ted Kremenek | ae3487f | 2009-03-02 21:44:02 +0000 | [diff] [blame] | 137 | // Output the PathDiagnosticPiece::Kind. |
| 138 | Indent(o, indent) << "<key>kind</key>\n"; |
Ted Kremenek | 0008683 | 2009-03-10 02:49:29 +0000 | [diff] [blame] | 139 | Indent(o, indent) << "<string>"; |
| 140 | |
| 141 | switch (P.getKind()) { |
| 142 | case PathDiagnosticPiece::Event: o << "Event"; break; |
| 143 | case PathDiagnosticPiece::ControlFlow: o << "ControlFlow"; break; |
| 144 | case PathDiagnosticPiece::Macro: o << "Macro"; break; |
| 145 | } |
| 146 | o << "</string>\n"; |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 147 | |
| 148 | |
| 149 | // Finish up. |
| 150 | --indent; |
| 151 | Indent(o, indent); o << "</dict>\n"; |
| 152 | } |
| 153 | |
| 154 | void PlistDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) { |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 155 | if (!D) |
| 156 | return; |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 157 | |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 158 | if (D->empty()) { |
| 159 | delete D; |
| 160 | return; |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 163 | BatchedDiags.push_back(D); |
| 164 | } |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 165 | |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 166 | PlistDiagnostics::~PlistDiagnostics() { |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 167 | |
| 168 | // Build up a set of FIDs that we use by scanning the locations and |
| 169 | // ranges of the diagnostics. |
| 170 | FIDMap FM; |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 171 | llvm::SmallVector<FileID, 10> Fids; |
Ted Kremenek | c472d79 | 2009-01-23 20:06:20 +0000 | [diff] [blame] | 172 | SourceManager* SM = 0; |
| 173 | |
| 174 | if (!BatchedDiags.empty()) |
| 175 | SM = &(*BatchedDiags.begin())->begin()->getLocation().getManager(); |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 176 | |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 177 | for (std::vector<const PathDiagnostic*>::iterator DI = BatchedDiags.begin(), |
| 178 | DE = BatchedDiags.end(); DI != DE; ++DI) { |
| 179 | |
| 180 | const PathDiagnostic *D = *DI; |
| 181 | |
| 182 | for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I!=E; ++I) { |
| 183 | AddFID(FM, Fids, SM, I->getLocation()); |
| 184 | |
| 185 | for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(), |
| 186 | RE=I->ranges_end(); RI!=RE; ++RI) { |
| 187 | AddFID(FM, Fids, SM, RI->getBegin()); |
| 188 | AddFID(FM, Fids, SM, RI->getEnd()); |
| 189 | } |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 193 | // Open the file. |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 194 | std::string ErrMsg; |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 195 | llvm::raw_fd_ostream o(OutputFile.c_str(), false, ErrMsg); |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 196 | if (!ErrMsg.empty()) { |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 197 | llvm::errs() << "warning: could not creat file: " << OutputFile << '\n'; |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 198 | return; |
| 199 | } |
| 200 | |
| 201 | // Write the plist header. |
| 202 | o << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 203 | "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" " |
| 204 | "http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n" |
| 205 | "<plist version=\"1.0\">\n"; |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 206 | |
| 207 | // Write the root object: a <dict> containing... |
| 208 | // - "files", an <array> mapping from FIDs to file names |
| 209 | // - "diagnostics", an <array> containing the path diagnostics |
| 210 | o << "<dict>\n" |
| 211 | " <key>files</key>\n" |
| 212 | " <array>\n"; |
| 213 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 214 | for (llvm::SmallVectorImpl<FileID>::iterator I=Fids.begin(), E=Fids.end(); |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 215 | I!=E; ++I) |
Ted Kremenek | c472d79 | 2009-01-23 20:06:20 +0000 | [diff] [blame] | 216 | o << " <string>" << SM->getFileEntryForID(*I)->getName() << "</string>\n"; |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 217 | |
| 218 | o << " </array>\n" |
| 219 | " <key>diagnostics</key>\n" |
| 220 | " <array>\n"; |
| 221 | |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 222 | for (std::vector<const PathDiagnostic*>::iterator DI=BatchedDiags.begin(), |
| 223 | DE = BatchedDiags.end(); DI!=DE; ++DI) { |
| 224 | |
| 225 | o << " <dict>\n" |
| 226 | " <key>path</key>\n"; |
| 227 | |
| 228 | const PathDiagnostic *D = *DI; |
| 229 | // Create an owning smart pointer for 'D' just so that we auto-free it |
| 230 | // when we exit this method. |
| 231 | llvm::OwningPtr<PathDiagnostic> OwnedD(const_cast<PathDiagnostic*>(D)); |
| 232 | |
| 233 | o << " <array>\n"; |
| 234 | |
| 235 | for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I != E; ++I) |
| 236 | ReportDiag(o, *I, FM, SM); |
| 237 | |
| 238 | o << " </array>\n"; |
| 239 | |
| 240 | // Output the bug type and bug category. |
Ted Kremenek | d671c5a | 2009-02-02 21:45:32 +0000 | [diff] [blame] | 241 | o << " <key>description</key>\n <string>" << D->getDescription() |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 242 | << "</string>\n" |
| 243 | << " <key>category</key>\n <string>" << D->getCategory() |
| 244 | << "</string>\n" |
| 245 | << " </dict>\n"; |
| 246 | } |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 247 | |
| 248 | o << " </array>\n"; |
Ted Kremenek | ddf32da | 2009-01-21 00:42:24 +0000 | [diff] [blame] | 249 | |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 250 | // Finish. |
Ted Kremenek | 4fc82c8 | 2008-11-03 23:18:07 +0000 | [diff] [blame] | 251 | o << "</dict>\n</plist>"; |
Ted Kremenek | 5d86625 | 2008-11-03 22:33:57 +0000 | [diff] [blame] | 252 | } |