PathDiagnostic.cpp revision e97436732c0f20aa15ecbf92a5f905eee888528f
1//===--- PathDiagnostic.cpp - Path-Specific Diagnostic Handling -*- 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 PathDiagnostic-related interfaces. 11// 12//===----------------------------------------------------------------------===// 13 14#include "clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h" 15#include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h" 16#include "clang/AST/Expr.h" 17#include "clang/AST/Decl.h" 18#include "clang/AST/DeclObjC.h" 19#include "clang/AST/ParentMap.h" 20#include "clang/AST/StmtCXX.h" 21#include "llvm/ADT/SmallString.h" 22 23using namespace clang; 24using namespace ento; 25 26bool PathDiagnosticMacroPiece::containsEvent() const { 27 for (const_iterator I = begin(), E = end(); I!=E; ++I) { 28 if (isa<PathDiagnosticEventPiece>(*I)) 29 return true; 30 31 if (PathDiagnosticMacroPiece *MP = dyn_cast<PathDiagnosticMacroPiece>(*I)) 32 if (MP->containsEvent()) 33 return true; 34 } 35 36 return false; 37} 38 39static StringRef StripTrailingDots(StringRef s) { 40 for (StringRef::size_type i = s.size(); i != 0; --i) 41 if (s[i - 1] != '.') 42 return s.substr(0, i); 43 return ""; 44} 45 46PathDiagnosticPiece::PathDiagnosticPiece(StringRef s, 47 Kind k, DisplayHint hint) 48 : str(StripTrailingDots(s)), kind(k), Hint(hint) {} 49 50PathDiagnosticPiece::PathDiagnosticPiece(Kind k, DisplayHint hint) 51 : kind(k), Hint(hint) {} 52 53PathDiagnosticPiece::~PathDiagnosticPiece() {} 54PathDiagnosticEventPiece::~PathDiagnosticEventPiece() {} 55PathDiagnosticControlFlowPiece::~PathDiagnosticControlFlowPiece() {} 56 57PathDiagnosticMacroPiece::~PathDiagnosticMacroPiece() { 58 for (iterator I = begin(), E = end(); I != E; ++I) delete *I; 59} 60 61PathDiagnostic::PathDiagnostic() : Size(0) {} 62 63PathDiagnostic::~PathDiagnostic() { 64 for (iterator I = begin(), E = end(); I != E; ++I) delete &*I; 65} 66 67void PathDiagnostic::resetPath(bool deletePieces) { 68 Size = 0; 69 70 if (deletePieces) 71 for (iterator I=begin(), E=end(); I!=E; ++I) 72 delete &*I; 73 74 path.clear(); 75} 76 77 78PathDiagnostic::PathDiagnostic(StringRef bugtype, StringRef desc, 79 StringRef category) 80 : Size(0), 81 BugType(StripTrailingDots(bugtype)), 82 Desc(StripTrailingDots(desc)), 83 Category(StripTrailingDots(category)) {} 84 85void PathDiagnosticClient::HandleDiagnostic(Diagnostic::Level DiagLevel, 86 const DiagnosticInfo &Info) { 87 // Default implementation (Warnings/errors count). 88 DiagnosticClient::HandleDiagnostic(DiagLevel, Info); 89 90 // Create a PathDiagnostic with a single piece. 91 92 PathDiagnostic* D = new PathDiagnostic(); 93 94 const char *LevelStr; 95 switch (DiagLevel) { 96 default: 97 case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type"); 98 case Diagnostic::Note: LevelStr = "note: "; break; 99 case Diagnostic::Warning: LevelStr = "warning: "; break; 100 case Diagnostic::Error: LevelStr = "error: "; break; 101 case Diagnostic::Fatal: LevelStr = "fatal error: "; break; 102 } 103 104 llvm::SmallString<100> StrC; 105 StrC += LevelStr; 106 Info.FormatDiagnostic(StrC); 107 108 PathDiagnosticPiece *P = 109 new PathDiagnosticEventPiece(FullSourceLoc(Info.getLocation(), 110 Info.getSourceManager()), 111 StrC.str()); 112 113 for (unsigned i = 0, e = Info.getNumRanges(); i != e; ++i) 114 P->addRange(Info.getRange(i).getAsRange()); 115 for (unsigned i = 0, e = Info.getNumFixItHints(); i != e; ++i) 116 P->addFixItHint(Info.getFixItHint(i)); 117 D->push_front(P); 118 119 HandlePathDiagnostic(D); 120} 121 122void PathDiagnosticClient::HandlePathDiagnostic(const PathDiagnostic *D) { 123 // For now this simply forwards to HandlePathDiagnosticImpl. In the future 124 // we can use this indirection to control for multi-threaded access to 125 // the PathDiagnosticClient from multiple bug reporters. 126 HandlePathDiagnosticImpl(D); 127} 128 129//===----------------------------------------------------------------------===// 130// PathDiagnosticLocation methods. 131//===----------------------------------------------------------------------===// 132 133static SourceLocation getValidSourceLocation(const Stmt* S, 134 const LocationContext *LC) { 135 SourceLocation L = S->getLocStart(); 136 137 // S might be a temporary statement that does not have a location in the 138 // source code, so find an enclosing statement and use it's location. 139 if (!L.isValid()) { 140 const ParentMap &PM = LC->getParentMap(); 141 142 while (!L.isValid()) { 143 S = PM.getParent(S); 144 L = S->getLocStart(); 145 } 146 } 147 148 return L; 149} 150 151PathDiagnosticLocation 152 PathDiagnosticLocation::createBeginStmt(const Stmt *S, 153 const SourceManager &SM, 154 const LocationContext *LC) { 155 return PathDiagnosticLocation(getValidSourceLocation(S, LC), 156 SM, SingleLocK); 157} 158 159PathDiagnosticLocation 160 PathDiagnosticLocation::createOperatorLoc(const BinaryOperator *BO, 161 const SourceManager &SM) { 162 return PathDiagnosticLocation(BO->getOperatorLoc(), SM, SingleLocK); 163} 164 165PathDiagnosticLocation 166 PathDiagnosticLocation::createBeginBrace(const CompoundStmt *CS, 167 const SourceManager &SM) { 168 SourceLocation L = CS->getLBracLoc(); 169 return PathDiagnosticLocation(L, SM, SingleLocK); 170} 171 172PathDiagnosticLocation 173 PathDiagnosticLocation::createEndBrace(const CompoundStmt *CS, 174 const SourceManager &SM) { 175 SourceLocation L = CS->getRBracLoc(); 176 return PathDiagnosticLocation(L, SM, SingleLocK); 177} 178 179PathDiagnosticLocation 180 PathDiagnosticLocation::createDeclBegin(const LocationContext *LC, 181 const SourceManager &SM) { 182 // FIXME: Should handle CXXTryStmt if analyser starts supporting C++. 183 if (const CompoundStmt *CS = 184 dyn_cast_or_null<CompoundStmt>(LC->getDecl()->getBody())) 185 if (!CS->body_empty()) { 186 SourceLocation Loc = (*CS->body_begin())->getLocStart(); 187 return PathDiagnosticLocation(Loc, SM, SingleLocK); 188 } 189 190 return PathDiagnosticLocation(); 191} 192 193PathDiagnosticLocation 194 PathDiagnosticLocation::createDeclEnd(const LocationContext *LC, 195 const SourceManager &SM) { 196 SourceLocation L = LC->getDecl()->getBodyRBrace(); 197 return PathDiagnosticLocation(L, SM, SingleLocK); 198} 199 200PathDiagnosticLocation 201 PathDiagnosticLocation::create(const ProgramPoint& P, 202 const SourceManager &SMng) { 203 204 const Stmt* S = 0; 205 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) { 206 const CFGBlock *BSrc = BE->getSrc(); 207 S = BSrc->getTerminatorCondition(); 208 } 209 else if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) { 210 S = PS->getStmt(); 211 } 212 213 return PathDiagnosticLocation(S, SMng, P.getLocationContext()); 214 215 if (!S) 216 return PathDiagnosticLocation(); 217} 218 219PathDiagnosticLocation 220 PathDiagnosticLocation::createEndOfPath(const ExplodedNode* N, 221 const SourceManager &SM) { 222 assert(N && "Cannot create a location with a null node."); 223 224 const ExplodedNode *NI = N; 225 226 while (NI) { 227 ProgramPoint P = NI->getLocation(); 228 const LocationContext *LC = P.getLocationContext(); 229 if (const StmtPoint *PS = dyn_cast<StmtPoint>(&P)) 230 return PathDiagnosticLocation(PS->getStmt(), SM, LC); 231 else if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) { 232 const Stmt *Term = BE->getSrc()->getTerminator(); 233 assert(Term); 234 return PathDiagnosticLocation(Term, SM, LC); 235 } 236 NI = NI->succ_empty() ? 0 : *(NI->succ_begin()); 237 } 238 239 return createDeclEnd(N->getLocationContext(), SM); 240} 241 242PathDiagnosticLocation PathDiagnosticLocation::createSingleLocation( 243 const PathDiagnosticLocation &PDL) { 244 FullSourceLoc L = PDL.asLocation(); 245 return PathDiagnosticLocation(L, L.getManager(), SingleLocK); 246} 247 248FullSourceLoc 249 PathDiagnosticLocation::genLocation(const LocationContext *LC) const { 250 assert(isValid()); 251 // Note that we want a 'switch' here so that the compiler can warn us in 252 // case we add more cases. 253 switch (K) { 254 case SingleLocK: 255 case RangeK: 256 break; 257 case StmtK: 258 return FullSourceLoc(getValidSourceLocation(S, LC), 259 const_cast<SourceManager&>(*SM)); 260 case DeclK: 261 return FullSourceLoc(D->getLocation(), const_cast<SourceManager&>(*SM)); 262 } 263 264 return FullSourceLoc(R.getBegin(), const_cast<SourceManager&>(*SM)); 265} 266 267PathDiagnosticRange 268 PathDiagnosticLocation::genRange(const LocationContext *LC) const { 269 assert(isValid()); 270 // Note that we want a 'switch' here so that the compiler can warn us in 271 // case we add more cases. 272 switch (K) { 273 case SingleLocK: 274 return PathDiagnosticRange(R, true); 275 case RangeK: 276 break; 277 case StmtK: { 278 const Stmt *S = asStmt(); 279 switch (S->getStmtClass()) { 280 default: 281 break; 282 case Stmt::DeclStmtClass: { 283 const DeclStmt *DS = cast<DeclStmt>(S); 284 if (DS->isSingleDecl()) { 285 // Should always be the case, but we'll be defensive. 286 return SourceRange(DS->getLocStart(), 287 DS->getSingleDecl()->getLocation()); 288 } 289 break; 290 } 291 // FIXME: Provide better range information for different 292 // terminators. 293 case Stmt::IfStmtClass: 294 case Stmt::WhileStmtClass: 295 case Stmt::DoStmtClass: 296 case Stmt::ForStmtClass: 297 case Stmt::ChooseExprClass: 298 case Stmt::IndirectGotoStmtClass: 299 case Stmt::SwitchStmtClass: 300 case Stmt::BinaryConditionalOperatorClass: 301 case Stmt::ConditionalOperatorClass: 302 case Stmt::ObjCForCollectionStmtClass: { 303 SourceLocation L = getValidSourceLocation(S, LC); 304 return SourceRange(L, L); 305 } 306 } 307 308 return S->getSourceRange(); 309 } 310 case DeclK: 311 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) 312 return MD->getSourceRange(); 313 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 314 if (Stmt *Body = FD->getBody()) 315 return Body->getSourceRange(); 316 } 317 else { 318 SourceLocation L = D->getLocation(); 319 return PathDiagnosticRange(SourceRange(L, L), true); 320 } 321 } 322 323 return R; 324} 325 326void PathDiagnosticLocation::flatten() { 327 if (K == StmtK) { 328 R = asRange(); 329 K = RangeK; 330 S = 0; 331 D = 0; 332 } 333 else if (K == DeclK) { 334 SourceLocation L = D->getLocation(); 335 R = SourceRange(L, L); 336 K = SingleLocK; 337 S = 0; 338 D = 0; 339 } 340} 341 342//===----------------------------------------------------------------------===// 343// FoldingSet profiling methods. 344//===----------------------------------------------------------------------===// 345 346void PathDiagnosticLocation::Profile(llvm::FoldingSetNodeID &ID) const { 347 ID.AddInteger((unsigned) K); 348 switch (K) { 349 case RangeK: 350 ID.AddInteger(R.getBegin().getRawEncoding()); 351 ID.AddInteger(R.getEnd().getRawEncoding()); 352 break; 353 case SingleLocK: 354 ID.AddInteger(R.getBegin().getRawEncoding()); 355 break; 356 case StmtK: 357 ID.Add(S); 358 break; 359 case DeclK: 360 ID.Add(D); 361 break; 362 } 363 return; 364} 365 366void PathDiagnosticPiece::Profile(llvm::FoldingSetNodeID &ID) const { 367 ID.AddInteger((unsigned) getKind()); 368 ID.AddString(str); 369 // FIXME: Add profiling support for code hints. 370 ID.AddInteger((unsigned) getDisplayHint()); 371 for (range_iterator I = ranges_begin(), E = ranges_end(); I != E; ++I) { 372 ID.AddInteger(I->getBegin().getRawEncoding()); 373 ID.AddInteger(I->getEnd().getRawEncoding()); 374 } 375} 376 377void PathDiagnosticSpotPiece::Profile(llvm::FoldingSetNodeID &ID) const { 378 PathDiagnosticPiece::Profile(ID); 379 ID.Add(Pos); 380} 381 382void PathDiagnosticControlFlowPiece::Profile(llvm::FoldingSetNodeID &ID) const { 383 PathDiagnosticPiece::Profile(ID); 384 for (const_iterator I = begin(), E = end(); I != E; ++I) 385 ID.Add(*I); 386} 387 388void PathDiagnosticMacroPiece::Profile(llvm::FoldingSetNodeID &ID) const { 389 PathDiagnosticSpotPiece::Profile(ID); 390 for (const_iterator I = begin(), E = end(); I != E; ++I) 391 ID.Add(**I); 392} 393 394void PathDiagnostic::Profile(llvm::FoldingSetNodeID &ID) const { 395 ID.AddInteger(Size); 396 ID.AddString(BugType); 397 ID.AddString(Desc); 398 ID.AddString(Category); 399 for (const_iterator I = begin(), E = end(); I != E; ++I) 400 ID.Add(*I); 401 402 for (meta_iterator I = meta_begin(), E = meta_end(); I != E; ++I) 403 ID.AddString(*I); 404} 405