PathDiagnostic.cpp revision 77ce46d769b581b8a6ddb0d58231b8be9a8a6026
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 LocationOrAnalysisContext LAC) { 135 SourceLocation L = S->getLocStart(); 136 assert(!LAC.isNull() && "A valid LocationContext or AnalysisContext should " 137 "be passed to PathDiagnosticLocation upon creation."); 138 139 // S might be a temporary statement that does not have a location in the 140 // source code, so find an enclosing statement and use it's location. 141 if (!L.isValid()) { 142 143 ParentMap *PM = 0; 144 if (LAC.is<const LocationContext*>()) 145 PM = &LAC.get<const LocationContext*>()->getParentMap(); 146 else 147 PM = &LAC.get<AnalysisContext*>()->getParentMap(); 148 149 while (!L.isValid()) { 150 S = PM->getParent(S); 151 L = S->getLocStart(); 152 } 153 } 154 155 return L; 156} 157 158PathDiagnosticLocation 159 PathDiagnosticLocation::createBeginStmt(const Stmt *S, 160 const SourceManager &SM, 161 LocationOrAnalysisContext LAC) { 162 return PathDiagnosticLocation(getValidSourceLocation(S, LAC), 163 SM, SingleLocK); 164} 165 166PathDiagnosticLocation 167 PathDiagnosticLocation::createOperatorLoc(const BinaryOperator *BO, 168 const SourceManager &SM) { 169 return PathDiagnosticLocation(BO->getOperatorLoc(), SM, SingleLocK); 170} 171 172PathDiagnosticLocation 173 PathDiagnosticLocation::createBeginBrace(const CompoundStmt *CS, 174 const SourceManager &SM) { 175 SourceLocation L = CS->getLBracLoc(); 176 return PathDiagnosticLocation(L, SM, SingleLocK); 177} 178 179PathDiagnosticLocation 180 PathDiagnosticLocation::createEndBrace(const CompoundStmt *CS, 181 const SourceManager &SM) { 182 SourceLocation L = CS->getRBracLoc(); 183 return PathDiagnosticLocation(L, SM, SingleLocK); 184} 185 186PathDiagnosticLocation 187 PathDiagnosticLocation::createDeclBegin(const LocationContext *LC, 188 const SourceManager &SM) { 189 // FIXME: Should handle CXXTryStmt if analyser starts supporting C++. 190 if (const CompoundStmt *CS = 191 dyn_cast_or_null<CompoundStmt>(LC->getDecl()->getBody())) 192 if (!CS->body_empty()) { 193 SourceLocation Loc = (*CS->body_begin())->getLocStart(); 194 return PathDiagnosticLocation(Loc, SM, SingleLocK); 195 } 196 197 return PathDiagnosticLocation(); 198} 199 200PathDiagnosticLocation 201 PathDiagnosticLocation::createDeclEnd(const LocationContext *LC, 202 const SourceManager &SM) { 203 SourceLocation L = LC->getDecl()->getBodyRBrace(); 204 return PathDiagnosticLocation(L, SM, SingleLocK); 205} 206 207PathDiagnosticLocation 208 PathDiagnosticLocation::create(const ProgramPoint& P, 209 const SourceManager &SMng) { 210 211 const Stmt* S = 0; 212 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) { 213 const CFGBlock *BSrc = BE->getSrc(); 214 S = BSrc->getTerminatorCondition(); 215 } 216 else if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) { 217 S = PS->getStmt(); 218 } 219 220 return PathDiagnosticLocation(S, SMng, P.getLocationContext()); 221 222 if (!S) 223 return PathDiagnosticLocation(); 224} 225 226PathDiagnosticLocation 227 PathDiagnosticLocation::createEndOfPath(const ExplodedNode* N, 228 const SourceManager &SM) { 229 assert(N && "Cannot create a location with a null node."); 230 231 const ExplodedNode *NI = N; 232 233 while (NI) { 234 ProgramPoint P = NI->getLocation(); 235 const LocationContext *LC = P.getLocationContext(); 236 if (const StmtPoint *PS = dyn_cast<StmtPoint>(&P)) 237 return PathDiagnosticLocation(PS->getStmt(), SM, LC); 238 else if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) { 239 const Stmt *Term = BE->getSrc()->getTerminator(); 240 assert(Term); 241 return PathDiagnosticLocation(Term, SM, LC); 242 } 243 NI = NI->succ_empty() ? 0 : *(NI->succ_begin()); 244 } 245 246 return createDeclEnd(N->getLocationContext(), SM); 247} 248 249PathDiagnosticLocation PathDiagnosticLocation::createSingleLocation( 250 const PathDiagnosticLocation &PDL) { 251 FullSourceLoc L = PDL.asLocation(); 252 return PathDiagnosticLocation(L, L.getManager(), SingleLocK); 253} 254 255FullSourceLoc 256 PathDiagnosticLocation::genLocation(LocationOrAnalysisContext LAC) const { 257 assert(isValid()); 258 // Note that we want a 'switch' here so that the compiler can warn us in 259 // case we add more cases. 260 switch (K) { 261 case SingleLocK: 262 case RangeK: 263 break; 264 case StmtK: 265 return FullSourceLoc(getValidSourceLocation(S, LAC), 266 const_cast<SourceManager&>(*SM)); 267 case DeclK: 268 return FullSourceLoc(D->getLocation(), const_cast<SourceManager&>(*SM)); 269 } 270 271 return FullSourceLoc(R.getBegin(), const_cast<SourceManager&>(*SM)); 272} 273 274PathDiagnosticRange 275 PathDiagnosticLocation::genRange(LocationOrAnalysisContext LAC) const { 276 assert(isValid()); 277 // Note that we want a 'switch' here so that the compiler can warn us in 278 // case we add more cases. 279 switch (K) { 280 case SingleLocK: 281 return PathDiagnosticRange(R, true); 282 case RangeK: 283 break; 284 case StmtK: { 285 const Stmt *S = asStmt(); 286 switch (S->getStmtClass()) { 287 default: 288 break; 289 case Stmt::DeclStmtClass: { 290 const DeclStmt *DS = cast<DeclStmt>(S); 291 if (DS->isSingleDecl()) { 292 // Should always be the case, but we'll be defensive. 293 return SourceRange(DS->getLocStart(), 294 DS->getSingleDecl()->getLocation()); 295 } 296 break; 297 } 298 // FIXME: Provide better range information for different 299 // terminators. 300 case Stmt::IfStmtClass: 301 case Stmt::WhileStmtClass: 302 case Stmt::DoStmtClass: 303 case Stmt::ForStmtClass: 304 case Stmt::ChooseExprClass: 305 case Stmt::IndirectGotoStmtClass: 306 case Stmt::SwitchStmtClass: 307 case Stmt::BinaryConditionalOperatorClass: 308 case Stmt::ConditionalOperatorClass: 309 case Stmt::ObjCForCollectionStmtClass: { 310 SourceLocation L = getValidSourceLocation(S, LAC); 311 return SourceRange(L, L); 312 } 313 } 314 315 return S->getSourceRange(); 316 } 317 case DeclK: 318 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) 319 return MD->getSourceRange(); 320 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 321 if (Stmt *Body = FD->getBody()) 322 return Body->getSourceRange(); 323 } 324 else { 325 SourceLocation L = D->getLocation(); 326 return PathDiagnosticRange(SourceRange(L, L), true); 327 } 328 } 329 330 return R; 331} 332 333void PathDiagnosticLocation::flatten() { 334 if (K == StmtK) { 335 R = asRange(); 336 K = RangeK; 337 S = 0; 338 D = 0; 339 } 340 else if (K == DeclK) { 341 SourceLocation L = D->getLocation(); 342 R = SourceRange(L, L); 343 K = SingleLocK; 344 S = 0; 345 D = 0; 346 } 347} 348 349//===----------------------------------------------------------------------===// 350// FoldingSet profiling methods. 351//===----------------------------------------------------------------------===// 352 353void PathDiagnosticLocation::Profile(llvm::FoldingSetNodeID &ID) const { 354 ID.AddInteger(Range.getBegin().getRawEncoding()); 355 ID.AddInteger(Range.getEnd().getRawEncoding()); 356 ID.AddInteger(Loc.getRawEncoding()); 357 return; 358} 359 360void PathDiagnosticPiece::Profile(llvm::FoldingSetNodeID &ID) const { 361 ID.AddInteger((unsigned) getKind()); 362 ID.AddString(str); 363 // FIXME: Add profiling support for code hints. 364 ID.AddInteger((unsigned) getDisplayHint()); 365 for (range_iterator I = ranges_begin(), E = ranges_end(); I != E; ++I) { 366 ID.AddInteger(I->getBegin().getRawEncoding()); 367 ID.AddInteger(I->getEnd().getRawEncoding()); 368 } 369} 370 371void PathDiagnosticSpotPiece::Profile(llvm::FoldingSetNodeID &ID) const { 372 PathDiagnosticPiece::Profile(ID); 373 ID.Add(Pos); 374} 375 376void PathDiagnosticControlFlowPiece::Profile(llvm::FoldingSetNodeID &ID) const { 377 PathDiagnosticPiece::Profile(ID); 378 for (const_iterator I = begin(), E = end(); I != E; ++I) 379 ID.Add(*I); 380} 381 382void PathDiagnosticMacroPiece::Profile(llvm::FoldingSetNodeID &ID) const { 383 PathDiagnosticSpotPiece::Profile(ID); 384 for (const_iterator I = begin(), E = end(); I != E; ++I) 385 ID.Add(**I); 386} 387 388void PathDiagnostic::Profile(llvm::FoldingSetNodeID &ID) const { 389 ID.AddInteger(Size); 390 ID.AddString(BugType); 391 ID.AddString(Desc); 392 ID.AddString(Category); 393 for (const_iterator I = begin(), E = end(); I != E; ++I) 394 ID.Add(*I); 395 396 for (meta_iterator I = meta_begin(), E = meta_end(); I != E; ++I) 397 ID.AddString(*I); 398} 399