PathDiagnostic.cpp revision 0cd59482abd8aec9ed1eaad11f5fe9c1e42639f6
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 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::PathDiagnosticLocation(const ProgramPoint& P, 201 const SourceManager &SMng) 202 : K(StmtK), S(0), D(0), SM(&SMng), LC(P.getLocationContext()) { 203 204 if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) { 205 const CFGBlock *BSrc = BE->getSrc(); 206 S = BSrc->getTerminatorCondition(); 207 } 208 else if (const PostStmt *PS = dyn_cast<PostStmt>(&P)) { 209 S = PS->getStmt(); 210 } 211 212 if (!S) 213 invalidate(); 214} 215 216PathDiagnosticLocation 217 PathDiagnosticLocation::createEndOfPath(const ExplodedNode* N, 218 const SourceManager &SM) { 219 assert(N && "Cannot create a location with a null node."); 220 221 const ExplodedNode *NI = N; 222 223 while (NI) { 224 ProgramPoint P = NI->getLocation(); 225 const LocationContext *LC = P.getLocationContext(); 226 if (const StmtPoint *PS = dyn_cast<StmtPoint>(&P)) { 227 return PathDiagnosticLocation(PS->getStmt(), SM, LC); 228 } 229 else if (const BlockEdge *BE = dyn_cast<BlockEdge>(&P)) { 230 const Stmt *Term = BE->getSrc()->getTerminator(); 231 assert(Term); 232 return PathDiagnosticLocation(Term, SM, LC); 233 } 234 NI = NI->succ_empty() ? 0 : *(NI->succ_begin()); 235 } 236 237 return createDeclEnd(N->getLocationContext(), SM); 238} 239 240FullSourceLoc PathDiagnosticLocation::asLocation() const { 241 assert(isValid()); 242 // Note that we want a 'switch' here so that the compiler can warn us in 243 // case we add more cases. 244 switch (K) { 245 case SingleLocK: 246 case RangeK: 247 break; 248 case StmtK: 249 return FullSourceLoc(getValidSourceLocation(S, LC), 250 const_cast<SourceManager&>(*SM)); 251 case DeclK: 252 return FullSourceLoc(D->getLocation(), const_cast<SourceManager&>(*SM)); 253 } 254 255 return FullSourceLoc(R.getBegin(), const_cast<SourceManager&>(*SM)); 256} 257 258PathDiagnosticRange PathDiagnosticLocation::asRange() const { 259 assert(isValid()); 260 // Note that we want a 'switch' here so that the compiler can warn us in 261 // case we add more cases. 262 switch (K) { 263 case SingleLocK: 264 return PathDiagnosticRange(R, true); 265 case RangeK: 266 break; 267 case StmtK: { 268 const Stmt *S = asStmt(); 269 switch (S->getStmtClass()) { 270 default: 271 break; 272 case Stmt::DeclStmtClass: { 273 const DeclStmt *DS = cast<DeclStmt>(S); 274 if (DS->isSingleDecl()) { 275 // Should always be the case, but we'll be defensive. 276 return SourceRange(DS->getLocStart(), 277 DS->getSingleDecl()->getLocation()); 278 } 279 break; 280 } 281 // FIXME: Provide better range information for different 282 // terminators. 283 case Stmt::IfStmtClass: 284 case Stmt::WhileStmtClass: 285 case Stmt::DoStmtClass: 286 case Stmt::ForStmtClass: 287 case Stmt::ChooseExprClass: 288 case Stmt::IndirectGotoStmtClass: 289 case Stmt::SwitchStmtClass: 290 case Stmt::BinaryConditionalOperatorClass: 291 case Stmt::ConditionalOperatorClass: 292 case Stmt::ObjCForCollectionStmtClass: { 293 SourceLocation L = getValidSourceLocation(S, LC); 294 return SourceRange(L, L); 295 } 296 } 297 298 return S->getSourceRange(); 299 } 300 case DeclK: 301 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) 302 return MD->getSourceRange(); 303 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 304 if (Stmt *Body = FD->getBody()) 305 return Body->getSourceRange(); 306 } 307 else { 308 SourceLocation L = D->getLocation(); 309 return PathDiagnosticRange(SourceRange(L, L), true); 310 } 311 } 312 313 return R; 314} 315 316void PathDiagnosticLocation::flatten() { 317 if (K == StmtK) { 318 R = asRange(); 319 K = RangeK; 320 S = 0; 321 D = 0; 322 } 323 else if (K == DeclK) { 324 SourceLocation L = D->getLocation(); 325 R = SourceRange(L, L); 326 K = SingleLocK; 327 S = 0; 328 D = 0; 329 } 330} 331 332//===----------------------------------------------------------------------===// 333// FoldingSet profiling methods. 334//===----------------------------------------------------------------------===// 335 336void PathDiagnosticLocation::Profile(llvm::FoldingSetNodeID &ID) const { 337 ID.AddInteger((unsigned) K); 338 switch (K) { 339 case RangeK: 340 ID.AddInteger(R.getBegin().getRawEncoding()); 341 ID.AddInteger(R.getEnd().getRawEncoding()); 342 break; 343 case SingleLocK: 344 ID.AddInteger(R.getBegin().getRawEncoding()); 345 break; 346 case StmtK: 347 ID.Add(S); 348 break; 349 case DeclK: 350 ID.Add(D); 351 break; 352 } 353 return; 354} 355 356void PathDiagnosticPiece::Profile(llvm::FoldingSetNodeID &ID) const { 357 ID.AddInteger((unsigned) getKind()); 358 ID.AddString(str); 359 // FIXME: Add profiling support for code hints. 360 ID.AddInteger((unsigned) getDisplayHint()); 361 for (range_iterator I = ranges_begin(), E = ranges_end(); I != E; ++I) { 362 ID.AddInteger(I->getBegin().getRawEncoding()); 363 ID.AddInteger(I->getEnd().getRawEncoding()); 364 } 365} 366 367void PathDiagnosticSpotPiece::Profile(llvm::FoldingSetNodeID &ID) const { 368 PathDiagnosticPiece::Profile(ID); 369 ID.Add(Pos); 370} 371 372void PathDiagnosticControlFlowPiece::Profile(llvm::FoldingSetNodeID &ID) const { 373 PathDiagnosticPiece::Profile(ID); 374 for (const_iterator I = begin(), E = end(); I != E; ++I) 375 ID.Add(*I); 376} 377 378void PathDiagnosticMacroPiece::Profile(llvm::FoldingSetNodeID &ID) const { 379 PathDiagnosticSpotPiece::Profile(ID); 380 for (const_iterator I = begin(), E = end(); I != E; ++I) 381 ID.Add(**I); 382} 383 384void PathDiagnostic::Profile(llvm::FoldingSetNodeID &ID) const { 385 ID.AddInteger(Size); 386 ID.AddString(BugType); 387 ID.AddString(Desc); 388 ID.AddString(Category); 389 for (const_iterator I = begin(), E = end(); I != E; ++I) 390 ID.Add(*I); 391 392 for (meta_iterator I = meta_begin(), E = meta_end(); I != E; ++I) 393 ID.AddString(*I); 394} 395