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