PathDiagnostic.h revision ed866e73bab7733f5226f84c52edefe23d694b2f
1//===--- PathDiagnostic.h - 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#ifndef LLVM_CLANG_PATH_DIAGNOSTIC_H 15#define LLVM_CLANG_PATH_DIAGNOSTIC_H 16 17#include "clang/Analysis/ProgramPoint.h" 18#include "clang/Basic/SourceLocation.h" 19#include "llvm/ADT/FoldingSet.h" 20#include "llvm/ADT/IntrusiveRefCntPtr.h" 21#include "llvm/ADT/Optional.h" 22#include "llvm/ADT/PointerUnion.h" 23#include <deque> 24#include <iterator> 25#include <string> 26#include <vector> 27 28namespace clang { 29 30class AnalysisDeclContext; 31class BinaryOperator; 32class CompoundStmt; 33class Decl; 34class LocationContext; 35class MemberExpr; 36class ParentMap; 37class ProgramPoint; 38class SourceManager; 39class Stmt; 40 41namespace ento { 42 43class ExplodedNode; 44class SymExpr; 45typedef const SymExpr* SymbolRef; 46 47//===----------------------------------------------------------------------===// 48// High-level interface for handlers of path-sensitive diagnostics. 49//===----------------------------------------------------------------------===// 50 51class PathDiagnostic; 52 53class PathDiagnosticConsumer { 54public: 55 class PDFileEntry : public llvm::FoldingSetNode { 56 public: 57 PDFileEntry(llvm::FoldingSetNodeID &NodeID) : NodeID(NodeID) {} 58 59 typedef std::vector<std::pair<StringRef, StringRef> > ConsumerFiles; 60 61 /// \brief A vector of <consumer,file> pairs. 62 ConsumerFiles files; 63 64 /// \brief A precomputed hash tag used for uniquing PDFileEntry objects. 65 const llvm::FoldingSetNodeID NodeID; 66 67 /// \brief Used for profiling in the FoldingSet. 68 void Profile(llvm::FoldingSetNodeID &ID) { ID = NodeID; } 69 }; 70 71 struct FilesMade : public llvm::FoldingSet<PDFileEntry> { 72 llvm::BumpPtrAllocator Alloc; 73 74 void addDiagnostic(const PathDiagnostic &PD, 75 StringRef ConsumerName, 76 StringRef fileName); 77 78 PDFileEntry::ConsumerFiles *getFiles(const PathDiagnostic &PD); 79 }; 80 81private: 82 virtual void anchor(); 83public: 84 PathDiagnosticConsumer() : flushed(false) {} 85 virtual ~PathDiagnosticConsumer(); 86 87 void FlushDiagnostics(FilesMade *FilesMade); 88 89 virtual void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags, 90 FilesMade *filesMade) = 0; 91 92 virtual StringRef getName() const = 0; 93 94 void HandlePathDiagnostic(PathDiagnostic *D); 95 96 enum PathGenerationScheme { None, Minimal, Extensive }; 97 virtual PathGenerationScheme getGenerationScheme() const { return Minimal; } 98 virtual bool supportsLogicalOpControlFlow() const { return false; } 99 virtual bool supportsAllBlockEdges() const { return false; } 100 101 /// Return true if the PathDiagnosticConsumer supports individual 102 /// PathDiagnostics that span multiple files. 103 virtual bool supportsCrossFileDiagnostics() const { return false; } 104 105protected: 106 bool flushed; 107 llvm::FoldingSet<PathDiagnostic> Diags; 108}; 109 110//===----------------------------------------------------------------------===// 111// Path-sensitive diagnostics. 112//===----------------------------------------------------------------------===// 113 114class PathDiagnosticRange : public SourceRange { 115public: 116 bool isPoint; 117 118 PathDiagnosticRange(const SourceRange &R, bool isP = false) 119 : SourceRange(R), isPoint(isP) {} 120 121 PathDiagnosticRange() : isPoint(false) {} 122}; 123 124typedef llvm::PointerUnion<const LocationContext*, AnalysisDeclContext*> 125 LocationOrAnalysisDeclContext; 126 127class PathDiagnosticLocation { 128private: 129 enum Kind { RangeK, SingleLocK, StmtK, DeclK } K; 130 const Stmt *S; 131 const Decl *D; 132 const SourceManager *SM; 133 FullSourceLoc Loc; 134 PathDiagnosticRange Range; 135 136 PathDiagnosticLocation(SourceLocation L, const SourceManager &sm, 137 Kind kind) 138 : K(kind), S(0), D(0), SM(&sm), 139 Loc(genLocation(L)), Range(genRange()) { 140 } 141 142 FullSourceLoc 143 genLocation(SourceLocation L = SourceLocation(), 144 LocationOrAnalysisDeclContext LAC = (AnalysisDeclContext*)0) const; 145 146 PathDiagnosticRange 147 genRange(LocationOrAnalysisDeclContext LAC = (AnalysisDeclContext*)0) const; 148 149public: 150 /// Create an invalid location. 151 PathDiagnosticLocation() 152 : K(SingleLocK), S(0), D(0), SM(0) {} 153 154 /// Create a location corresponding to the given statement. 155 PathDiagnosticLocation(const Stmt *s, 156 const SourceManager &sm, 157 LocationOrAnalysisDeclContext lac) 158 : K(s->getLocStart().isValid() ? StmtK : SingleLocK), 159 S(K == StmtK ? s : 0), 160 D(0), SM(&sm), 161 Loc(genLocation(SourceLocation(), lac)), 162 Range(genRange(lac)) { 163 assert(K == SingleLocK || S); 164 assert(K == SingleLocK || Loc.isValid()); 165 assert(K == SingleLocK || Range.isValid()); 166 } 167 168 /// Create a location corresponding to the given declaration. 169 PathDiagnosticLocation(const Decl *d, const SourceManager &sm) 170 : K(DeclK), S(0), D(d), SM(&sm), 171 Loc(genLocation()), Range(genRange()) { 172 assert(D); 173 assert(Loc.isValid()); 174 assert(Range.isValid()); 175 } 176 177 /// Create a location at an explicit offset in the source. 178 /// 179 /// This should only be used if there are no more appropriate constructors. 180 PathDiagnosticLocation(SourceLocation loc, const SourceManager &sm) 181 : K(SingleLocK), S(0), D(0), SM(&sm), Loc(loc, sm), Range(genRange()) { 182 assert(Loc.isValid()); 183 assert(Range.isValid()); 184 } 185 186 /// Create a location corresponding to the given declaration. 187 static PathDiagnosticLocation create(const Decl *D, 188 const SourceManager &SM) { 189 return PathDiagnosticLocation(D, SM); 190 } 191 192 /// Create a location for the beginning of the declaration. 193 static PathDiagnosticLocation createBegin(const Decl *D, 194 const SourceManager &SM); 195 196 /// Create a location for the beginning of the statement. 197 static PathDiagnosticLocation createBegin(const Stmt *S, 198 const SourceManager &SM, 199 const LocationOrAnalysisDeclContext LAC); 200 201 /// Create a location for the end of the statement. 202 /// 203 /// If the statement is a CompoundStatement, the location will point to the 204 /// closing brace instead of following it. 205 static PathDiagnosticLocation createEnd(const Stmt *S, 206 const SourceManager &SM, 207 const LocationOrAnalysisDeclContext LAC); 208 209 /// Create the location for the operator of the binary expression. 210 /// Assumes the statement has a valid location. 211 static PathDiagnosticLocation createOperatorLoc(const BinaryOperator *BO, 212 const SourceManager &SM); 213 214 /// For member expressions, return the location of the '.' or '->'. 215 /// Assumes the statement has a valid location. 216 static PathDiagnosticLocation createMemberLoc(const MemberExpr *ME, 217 const SourceManager &SM); 218 219 /// Create a location for the beginning of the compound statement. 220 /// Assumes the statement has a valid location. 221 static PathDiagnosticLocation createBeginBrace(const CompoundStmt *CS, 222 const SourceManager &SM); 223 224 /// Create a location for the end of the compound statement. 225 /// Assumes the statement has a valid location. 226 static PathDiagnosticLocation createEndBrace(const CompoundStmt *CS, 227 const SourceManager &SM); 228 229 /// Create a location for the beginning of the enclosing declaration body. 230 /// Defaults to the beginning of the first statement in the declaration body. 231 static PathDiagnosticLocation createDeclBegin(const LocationContext *LC, 232 const SourceManager &SM); 233 234 /// Constructs a location for the end of the enclosing declaration body. 235 /// Defaults to the end of brace. 236 static PathDiagnosticLocation createDeclEnd(const LocationContext *LC, 237 const SourceManager &SM); 238 239 /// Create a location corresponding to the given valid ExplodedNode. 240 static PathDiagnosticLocation create(const ProgramPoint& P, 241 const SourceManager &SMng); 242 243 /// Create a location corresponding to the next valid ExplodedNode as end 244 /// of path location. 245 static PathDiagnosticLocation createEndOfPath(const ExplodedNode* N, 246 const SourceManager &SM); 247 248 /// Convert the given location into a single kind location. 249 static PathDiagnosticLocation createSingleLocation( 250 const PathDiagnosticLocation &PDL); 251 252 bool operator==(const PathDiagnosticLocation &X) const { 253 return K == X.K && Loc == X.Loc && Range == X.Range; 254 } 255 256 bool operator!=(const PathDiagnosticLocation &X) const { 257 return !(*this == X); 258 } 259 260 bool isValid() const { 261 return SM != 0; 262 } 263 264 FullSourceLoc asLocation() const { 265 return Loc; 266 } 267 268 PathDiagnosticRange asRange() const { 269 return Range; 270 } 271 272 const Stmt *asStmt() const { assert(isValid()); return S; } 273 const Decl *asDecl() const { assert(isValid()); return D; } 274 275 bool hasRange() const { return K == StmtK || K == RangeK || K == DeclK; } 276 277 void invalidate() { 278 *this = PathDiagnosticLocation(); 279 } 280 281 void flatten(); 282 283 const SourceManager& getManager() const { assert(isValid()); return *SM; } 284 285 void Profile(llvm::FoldingSetNodeID &ID) const; 286 287 /// \brief Given an exploded node, retrieve the statement that should be used 288 /// for the diagnostic location. 289 static const Stmt *getStmt(const ExplodedNode *N); 290 291 /// \brief Retrieve the statement corresponding to the sucessor node. 292 static const Stmt *getNextStmt(const ExplodedNode *N); 293}; 294 295class PathDiagnosticLocationPair { 296private: 297 PathDiagnosticLocation Start, End; 298public: 299 PathDiagnosticLocationPair(const PathDiagnosticLocation &start, 300 const PathDiagnosticLocation &end) 301 : Start(start), End(end) {} 302 303 const PathDiagnosticLocation &getStart() const { return Start; } 304 const PathDiagnosticLocation &getEnd() const { return End; } 305 306 void flatten() { 307 Start.flatten(); 308 End.flatten(); 309 } 310 311 void Profile(llvm::FoldingSetNodeID &ID) const { 312 Start.Profile(ID); 313 End.Profile(ID); 314 } 315}; 316 317//===----------------------------------------------------------------------===// 318// Path "pieces" for path-sensitive diagnostics. 319//===----------------------------------------------------------------------===// 320 321class PathDiagnosticPiece : public RefCountedBaseVPTR { 322public: 323 enum Kind { ControlFlow, Event, Macro, Call }; 324 enum DisplayHint { Above, Below }; 325 326private: 327 const std::string str; 328 const Kind kind; 329 const DisplayHint Hint; 330 331 /// A constant string that can be used to tag the PathDiagnosticPiece, 332 /// typically with the identification of the creator. The actual pointer 333 /// value is meant to be an identifier; the string itself is useful for 334 /// debugging. 335 StringRef Tag; 336 337 std::vector<SourceRange> ranges; 338 339 PathDiagnosticPiece() LLVM_DELETED_FUNCTION; 340 PathDiagnosticPiece(const PathDiagnosticPiece &P) LLVM_DELETED_FUNCTION; 341 void operator=(const PathDiagnosticPiece &P) LLVM_DELETED_FUNCTION; 342 343protected: 344 PathDiagnosticPiece(StringRef s, Kind k, DisplayHint hint = Below); 345 346 PathDiagnosticPiece(Kind k, DisplayHint hint = Below); 347 348public: 349 virtual ~PathDiagnosticPiece(); 350 351 StringRef getString() const { return str; } 352 353 /// Tag this PathDiagnosticPiece with the given C-string. 354 void setTag(const char *tag) { Tag = tag; } 355 356 /// Return the opaque tag (if any) on the PathDiagnosticPiece. 357 const void *getTag() const { return Tag.data(); } 358 359 /// Return the string representation of the tag. This is useful 360 /// for debugging. 361 StringRef getTagStr() const { return Tag; } 362 363 /// getDisplayHint - Return a hint indicating where the diagnostic should 364 /// be displayed by the PathDiagnosticConsumer. 365 DisplayHint getDisplayHint() const { return Hint; } 366 367 virtual PathDiagnosticLocation getLocation() const = 0; 368 virtual void flattenLocations() = 0; 369 370 Kind getKind() const { return kind; } 371 372 void addRange(SourceRange R) { 373 if (!R.isValid()) 374 return; 375 ranges.push_back(R); 376 } 377 378 void addRange(SourceLocation B, SourceLocation E) { 379 if (!B.isValid() || !E.isValid()) 380 return; 381 ranges.push_back(SourceRange(B,E)); 382 } 383 384 /// Return the SourceRanges associated with this PathDiagnosticPiece. 385 ArrayRef<SourceRange> getRanges() const { return ranges; } 386 387 virtual void Profile(llvm::FoldingSetNodeID &ID) const; 388}; 389 390 391class PathPieces : public std::deque<IntrusiveRefCntPtr<PathDiagnosticPiece> > { 392 void flattenTo(PathPieces &Primary, PathPieces &Current, 393 bool ShouldFlattenMacros) const; 394public: 395 ~PathPieces(); 396 397 PathPieces flatten(bool ShouldFlattenMacros) const { 398 PathPieces Result; 399 flattenTo(Result, Result, ShouldFlattenMacros); 400 return Result; 401 } 402}; 403 404class PathDiagnosticSpotPiece : public PathDiagnosticPiece { 405private: 406 PathDiagnosticLocation Pos; 407public: 408 PathDiagnosticSpotPiece(const PathDiagnosticLocation &pos, 409 StringRef s, 410 PathDiagnosticPiece::Kind k, 411 bool addPosRange = true) 412 : PathDiagnosticPiece(s, k), Pos(pos) { 413 assert(Pos.isValid() && Pos.asLocation().isValid() && 414 "PathDiagnosticSpotPiece's must have a valid location."); 415 if (addPosRange && Pos.hasRange()) addRange(Pos.asRange()); 416 } 417 418 PathDiagnosticLocation getLocation() const { return Pos; } 419 virtual void flattenLocations() { Pos.flatten(); } 420 421 virtual void Profile(llvm::FoldingSetNodeID &ID) const; 422 423 static bool classof(const PathDiagnosticPiece *P) { 424 return P->getKind() == Event || P->getKind() == Macro; 425 } 426}; 427 428/// \brief Interface for classes constructing Stack hints. 429/// 430/// If a PathDiagnosticEvent occurs in a different frame than the final 431/// diagnostic the hints can be used to summarize the effect of the call. 432class StackHintGenerator { 433public: 434 virtual ~StackHintGenerator() = 0; 435 436 /// \brief Construct the Diagnostic message for the given ExplodedNode. 437 virtual std::string getMessage(const ExplodedNode *N) = 0; 438}; 439 440/// \brief Constructs a Stack hint for the given symbol. 441/// 442/// The class knows how to construct the stack hint message based on 443/// traversing the CallExpr associated with the call and checking if the given 444/// symbol is returned or is one of the arguments. 445/// The hint can be customized by redefining 'getMessageForX()' methods. 446class StackHintGeneratorForSymbol : public StackHintGenerator { 447private: 448 SymbolRef Sym; 449 std::string Msg; 450 451public: 452 StackHintGeneratorForSymbol(SymbolRef S, StringRef M) : Sym(S), Msg(M) {} 453 virtual ~StackHintGeneratorForSymbol() {} 454 455 /// \brief Search the call expression for the symbol Sym and dispatch the 456 /// 'getMessageForX()' methods to construct a specific message. 457 virtual std::string getMessage(const ExplodedNode *N); 458 459 /// Produces the message of the following form: 460 /// 'Msg via Nth parameter' 461 virtual std::string getMessageForArg(const Expr *ArgE, unsigned ArgIndex); 462 virtual std::string getMessageForReturn(const CallExpr *CallExpr) { 463 return Msg; 464 } 465 virtual std::string getMessageForSymbolNotFound() { 466 return Msg; 467 } 468}; 469 470class PathDiagnosticEventPiece : public PathDiagnosticSpotPiece { 471 Optional<bool> IsPrunable; 472 473 /// If the event occurs in a different frame than the final diagnostic, 474 /// supply a message that will be used to construct an extra hint on the 475 /// returns from all the calls on the stack from this event to the final 476 /// diagnostic. 477 OwningPtr<StackHintGenerator> CallStackHint; 478 479public: 480 PathDiagnosticEventPiece(const PathDiagnosticLocation &pos, 481 StringRef s, bool addPosRange = true, 482 StackHintGenerator *stackHint = 0) 483 : PathDiagnosticSpotPiece(pos, s, Event, addPosRange), 484 CallStackHint(stackHint) {} 485 486 ~PathDiagnosticEventPiece(); 487 488 /// Mark the diagnostic piece as being potentially prunable. This 489 /// flag may have been previously set, at which point it will not 490 /// be reset unless one specifies to do so. 491 void setPrunable(bool isPrunable, bool override = false) { 492 if (IsPrunable.hasValue() && !override) 493 return; 494 IsPrunable = isPrunable; 495 } 496 497 /// Return true if the diagnostic piece is prunable. 498 bool isPrunable() const { 499 return IsPrunable.hasValue() ? IsPrunable.getValue() : false; 500 } 501 502 bool hasCallStackHint() { 503 return (CallStackHint != 0); 504 } 505 506 /// Produce the hint for the given node. The node contains 507 /// information about the call for which the diagnostic can be generated. 508 std::string getCallStackMessage(const ExplodedNode *N) { 509 if (CallStackHint) 510 return CallStackHint->getMessage(N); 511 return ""; 512 } 513 514 static inline bool classof(const PathDiagnosticPiece *P) { 515 return P->getKind() == Event; 516 } 517}; 518 519class PathDiagnosticCallPiece : public PathDiagnosticPiece { 520 PathDiagnosticCallPiece(const Decl *callerD, 521 const PathDiagnosticLocation &callReturnPos) 522 : PathDiagnosticPiece(Call), Caller(callerD), Callee(0), 523 NoExit(false), callReturn(callReturnPos) {} 524 525 PathDiagnosticCallPiece(PathPieces &oldPath, const Decl *caller) 526 : PathDiagnosticPiece(Call), Caller(caller), Callee(0), 527 NoExit(true), path(oldPath) {} 528 529 const Decl *Caller; 530 const Decl *Callee; 531 532 // Flag signifying that this diagnostic has only call enter and no matching 533 // call exit. 534 bool NoExit; 535 536 // The custom string, which should appear after the call Return Diagnostic. 537 // TODO: Should we allow multiple diagnostics? 538 std::string CallStackMessage; 539 540public: 541 PathDiagnosticLocation callEnter; 542 PathDiagnosticLocation callEnterWithin; 543 PathDiagnosticLocation callReturn; 544 PathPieces path; 545 546 virtual ~PathDiagnosticCallPiece(); 547 548 const Decl *getCaller() const { return Caller; } 549 550 const Decl *getCallee() const { return Callee; } 551 void setCallee(const CallEnter &CE, const SourceManager &SM); 552 553 bool hasCallStackMessage() { return !CallStackMessage.empty(); } 554 void setCallStackMessage(StringRef st) { 555 CallStackMessage = st; 556 } 557 558 virtual PathDiagnosticLocation getLocation() const { 559 return callEnter; 560 } 561 562 IntrusiveRefCntPtr<PathDiagnosticEventPiece> getCallEnterEvent() const; 563 IntrusiveRefCntPtr<PathDiagnosticEventPiece> 564 getCallEnterWithinCallerEvent() const; 565 IntrusiveRefCntPtr<PathDiagnosticEventPiece> getCallExitEvent() const; 566 567 virtual void flattenLocations() { 568 callEnter.flatten(); 569 callReturn.flatten(); 570 for (PathPieces::iterator I = path.begin(), 571 E = path.end(); I != E; ++I) (*I)->flattenLocations(); 572 } 573 574 static PathDiagnosticCallPiece *construct(const ExplodedNode *N, 575 const CallExitEnd &CE, 576 const SourceManager &SM); 577 578 static PathDiagnosticCallPiece *construct(PathPieces &pieces, 579 const Decl *caller); 580 581 virtual void Profile(llvm::FoldingSetNodeID &ID) const; 582 583 static inline bool classof(const PathDiagnosticPiece *P) { 584 return P->getKind() == Call; 585 } 586}; 587 588class PathDiagnosticControlFlowPiece : public PathDiagnosticPiece { 589 std::vector<PathDiagnosticLocationPair> LPairs; 590public: 591 PathDiagnosticControlFlowPiece(const PathDiagnosticLocation &startPos, 592 const PathDiagnosticLocation &endPos, 593 StringRef s) 594 : PathDiagnosticPiece(s, ControlFlow) { 595 LPairs.push_back(PathDiagnosticLocationPair(startPos, endPos)); 596 } 597 598 PathDiagnosticControlFlowPiece(const PathDiagnosticLocation &startPos, 599 const PathDiagnosticLocation &endPos) 600 : PathDiagnosticPiece(ControlFlow) { 601 LPairs.push_back(PathDiagnosticLocationPair(startPos, endPos)); 602 } 603 604 ~PathDiagnosticControlFlowPiece(); 605 606 PathDiagnosticLocation getStartLocation() const { 607 assert(!LPairs.empty() && 608 "PathDiagnosticControlFlowPiece needs at least one location."); 609 return LPairs[0].getStart(); 610 } 611 612 PathDiagnosticLocation getEndLocation() const { 613 assert(!LPairs.empty() && 614 "PathDiagnosticControlFlowPiece needs at least one location."); 615 return LPairs[0].getEnd(); 616 } 617 618 void push_back(const PathDiagnosticLocationPair &X) { LPairs.push_back(X); } 619 620 virtual PathDiagnosticLocation getLocation() const { 621 return getStartLocation(); 622 } 623 624 typedef std::vector<PathDiagnosticLocationPair>::iterator iterator; 625 iterator begin() { return LPairs.begin(); } 626 iterator end() { return LPairs.end(); } 627 628 virtual void flattenLocations() { 629 for (iterator I=begin(), E=end(); I!=E; ++I) I->flatten(); 630 } 631 632 typedef std::vector<PathDiagnosticLocationPair>::const_iterator 633 const_iterator; 634 const_iterator begin() const { return LPairs.begin(); } 635 const_iterator end() const { return LPairs.end(); } 636 637 static inline bool classof(const PathDiagnosticPiece *P) { 638 return P->getKind() == ControlFlow; 639 } 640 641 virtual void Profile(llvm::FoldingSetNodeID &ID) const; 642}; 643 644class PathDiagnosticMacroPiece : public PathDiagnosticSpotPiece { 645public: 646 PathDiagnosticMacroPiece(const PathDiagnosticLocation &pos) 647 : PathDiagnosticSpotPiece(pos, "", Macro) {} 648 649 ~PathDiagnosticMacroPiece(); 650 651 PathPieces subPieces; 652 653 bool containsEvent() const; 654 655 virtual void flattenLocations() { 656 PathDiagnosticSpotPiece::flattenLocations(); 657 for (PathPieces::iterator I = subPieces.begin(), 658 E = subPieces.end(); I != E; ++I) (*I)->flattenLocations(); 659 } 660 661 static inline bool classof(const PathDiagnosticPiece *P) { 662 return P->getKind() == Macro; 663 } 664 665 virtual void Profile(llvm::FoldingSetNodeID &ID) const; 666}; 667 668/// PathDiagnostic - PathDiagnostic objects represent a single path-sensitive 669/// diagnostic. It represents an ordered-collection of PathDiagnosticPieces, 670/// each which represent the pieces of the path. 671class PathDiagnostic : public llvm::FoldingSetNode { 672 const Decl *DeclWithIssue; 673 std::string BugType; 674 std::string VerboseDesc; 675 std::string ShortDesc; 676 std::string Category; 677 std::deque<std::string> OtherDesc; 678 PathDiagnosticLocation Loc; 679 PathPieces pathImpl; 680 SmallVector<PathPieces *, 3> pathStack; 681 682 /// \brief Important bug uniqueing location. 683 /// The location info is useful to differentiate between bugs. 684 PathDiagnosticLocation UniqueingLoc; 685 const Decl *UniqueingDecl; 686 687 PathDiagnostic() LLVM_DELETED_FUNCTION; 688public: 689 PathDiagnostic(const Decl *DeclWithIssue, StringRef bugtype, 690 StringRef verboseDesc, StringRef shortDesc, 691 StringRef category, PathDiagnosticLocation LocationToUnique, 692 const Decl *DeclToUnique); 693 694 ~PathDiagnostic(); 695 696 const PathPieces &path; 697 698 /// Return the path currently used by builders for constructing the 699 /// PathDiagnostic. 700 PathPieces &getActivePath() { 701 if (pathStack.empty()) 702 return pathImpl; 703 return *pathStack.back(); 704 } 705 706 /// Return a mutable version of 'path'. 707 PathPieces &getMutablePieces() { 708 return pathImpl; 709 } 710 711 /// Return the unrolled size of the path. 712 unsigned full_size(); 713 714 void pushActivePath(PathPieces *p) { pathStack.push_back(p); } 715 void popActivePath() { if (!pathStack.empty()) pathStack.pop_back(); } 716 717 bool isWithinCall() const { return !pathStack.empty(); } 718 719 void setEndOfPath(PathDiagnosticPiece *EndPiece) { 720 assert(!Loc.isValid() && "End location already set!"); 721 Loc = EndPiece->getLocation(); 722 assert(Loc.isValid() && "Invalid location for end-of-path piece"); 723 getActivePath().push_back(EndPiece); 724 } 725 726 void resetPath() { 727 pathStack.clear(); 728 pathImpl.clear(); 729 Loc = PathDiagnosticLocation(); 730 } 731 732 StringRef getVerboseDescription() const { return VerboseDesc; } 733 StringRef getShortDescription() const { 734 return ShortDesc.empty() ? VerboseDesc : ShortDesc; 735 } 736 StringRef getBugType() const { return BugType; } 737 StringRef getCategory() const { return Category; } 738 739 /// Return the semantic context where an issue occurred. If the 740 /// issue occurs along a path, this represents the "central" area 741 /// where the bug manifests. 742 const Decl *getDeclWithIssue() const { return DeclWithIssue; } 743 744 typedef std::deque<std::string>::const_iterator meta_iterator; 745 meta_iterator meta_begin() const { return OtherDesc.begin(); } 746 meta_iterator meta_end() const { return OtherDesc.end(); } 747 void addMeta(StringRef s) { OtherDesc.push_back(s); } 748 749 PathDiagnosticLocation getLocation() const { 750 assert(Loc.isValid() && "No end-of-path location set yet!"); 751 return Loc; 752 } 753 754 /// \brief Get the location on which the report should be uniqued. 755 PathDiagnosticLocation getUniqueingLoc() const { 756 return UniqueingLoc; 757 } 758 759 /// \brief Get the declaration containing the uniqueing location. 760 const Decl *getUniqueingDecl() const { 761 return UniqueingDecl; 762 } 763 764 void flattenLocations() { 765 Loc.flatten(); 766 for (PathPieces::iterator I = pathImpl.begin(), E = pathImpl.end(); 767 I != E; ++I) (*I)->flattenLocations(); 768 } 769 770 /// Profiles the diagnostic, independent of the path it references. 771 /// 772 /// This can be used to merge diagnostics that refer to the same issue 773 /// along different paths. 774 void Profile(llvm::FoldingSetNodeID &ID) const; 775 776 /// Profiles the diagnostic, including its path. 777 /// 778 /// Two diagnostics with the same issue along different paths will generate 779 /// different profiles. 780 void FullProfile(llvm::FoldingSetNodeID &ID) const; 781}; 782 783} // end GR namespace 784 785} //end clang namespace 786 787#endif 788