PathDiagnostic.h revision be2fa7ebf01259b63dc52fe46c8d101c18e72269
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/Basic/SourceLocation.h" 18#include "clang/Analysis/ProgramPoint.h" 19#include "llvm/ADT/FoldingSet.h" 20#include "llvm/ADT/IntrusiveRefCntPtr.h" 21#include "llvm/ADT/PointerUnion.h" 22#include "llvm/ADT/Optional.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 assert(Loc.isValid()); 141 assert(Range.isValid()); 142 } 143 144 FullSourceLoc 145 genLocation(SourceLocation L = SourceLocation(), 146 LocationOrAnalysisDeclContext LAC = (AnalysisDeclContext*)0) const; 147 148 PathDiagnosticRange 149 genRange(LocationOrAnalysisDeclContext LAC = (AnalysisDeclContext*)0) const; 150 151public: 152 /// Create an invalid location. 153 PathDiagnosticLocation() 154 : K(SingleLocK), S(0), D(0), SM(0) {} 155 156 /// Create a location corresponding to the given statement. 157 PathDiagnosticLocation(const Stmt *s, 158 const SourceManager &sm, 159 LocationOrAnalysisDeclContext lac) 160 : K(StmtK), S(s), D(0), SM(&sm), 161 Loc(genLocation(SourceLocation(), lac)), 162 Range(genRange(lac)) { 163 assert(S); 164 assert(Loc.isValid()); 165 assert(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 288class PathDiagnosticLocationPair { 289private: 290 PathDiagnosticLocation Start, End; 291public: 292 PathDiagnosticLocationPair(const PathDiagnosticLocation &start, 293 const PathDiagnosticLocation &end) 294 : Start(start), End(end) {} 295 296 const PathDiagnosticLocation &getStart() const { return Start; } 297 const PathDiagnosticLocation &getEnd() const { return End; } 298 299 void flatten() { 300 Start.flatten(); 301 End.flatten(); 302 } 303 304 void Profile(llvm::FoldingSetNodeID &ID) const { 305 Start.Profile(ID); 306 End.Profile(ID); 307 } 308}; 309 310//===----------------------------------------------------------------------===// 311// Path "pieces" for path-sensitive diagnostics. 312//===----------------------------------------------------------------------===// 313 314class PathDiagnosticPiece : public RefCountedBaseVPTR { 315public: 316 enum Kind { ControlFlow, Event, Macro, Call }; 317 enum DisplayHint { Above, Below }; 318 319private: 320 const std::string str; 321 const Kind kind; 322 const DisplayHint Hint; 323 std::vector<SourceRange> ranges; 324 325 PathDiagnosticPiece() LLVM_DELETED_FUNCTION; 326 PathDiagnosticPiece(const PathDiagnosticPiece &P) LLVM_DELETED_FUNCTION; 327 void operator=(const PathDiagnosticPiece &P) LLVM_DELETED_FUNCTION; 328 329protected: 330 PathDiagnosticPiece(StringRef s, Kind k, DisplayHint hint = Below); 331 332 PathDiagnosticPiece(Kind k, DisplayHint hint = Below); 333 334public: 335 virtual ~PathDiagnosticPiece(); 336 337 llvm::StringRef getString() const { return str; } 338 339 /// getDisplayHint - Return a hint indicating where the diagnostic should 340 /// be displayed by the PathDiagnosticConsumer. 341 DisplayHint getDisplayHint() const { return Hint; } 342 343 virtual PathDiagnosticLocation getLocation() const = 0; 344 virtual void flattenLocations() = 0; 345 346 Kind getKind() const { return kind; } 347 348 void addRange(SourceRange R) { 349 if (!R.isValid()) 350 return; 351 ranges.push_back(R); 352 } 353 354 void addRange(SourceLocation B, SourceLocation E) { 355 if (!B.isValid() || !E.isValid()) 356 return; 357 ranges.push_back(SourceRange(B,E)); 358 } 359 360 /// Return the SourceRanges associated with this PathDiagnosticPiece. 361 ArrayRef<SourceRange> getRanges() const { return ranges; } 362 363 static inline bool classof(const PathDiagnosticPiece *P) { 364 return true; 365 } 366 367 virtual void Profile(llvm::FoldingSetNodeID &ID) const; 368}; 369 370 371class PathPieces : public std::deque<IntrusiveRefCntPtr<PathDiagnosticPiece> > { 372 void flattenTo(PathPieces &Primary, PathPieces &Current, 373 bool ShouldFlattenMacros) const; 374public: 375 ~PathPieces(); 376 377 PathPieces flatten(bool ShouldFlattenMacros) const { 378 PathPieces Result; 379 flattenTo(Result, Result, ShouldFlattenMacros); 380 return Result; 381 } 382}; 383 384class PathDiagnosticSpotPiece : public PathDiagnosticPiece { 385private: 386 PathDiagnosticLocation Pos; 387public: 388 PathDiagnosticSpotPiece(const PathDiagnosticLocation &pos, 389 StringRef s, 390 PathDiagnosticPiece::Kind k, 391 bool addPosRange = true) 392 : PathDiagnosticPiece(s, k), Pos(pos) { 393 assert(Pos.isValid() && Pos.asLocation().isValid() && 394 "PathDiagnosticSpotPiece's must have a valid location."); 395 if (addPosRange && Pos.hasRange()) addRange(Pos.asRange()); 396 } 397 398 PathDiagnosticLocation getLocation() const { return Pos; } 399 virtual void flattenLocations() { Pos.flatten(); } 400 401 virtual void Profile(llvm::FoldingSetNodeID &ID) const; 402}; 403 404/// \brief Interface for classes constructing Stack hints. 405/// 406/// If a PathDiagnosticEvent occurs in a different frame than the final 407/// diagnostic the hints can be used to summarize the effect of the call. 408class StackHintGenerator { 409public: 410 virtual ~StackHintGenerator() = 0; 411 412 /// \brief Construct the Diagnostic message for the given ExplodedNode. 413 virtual std::string getMessage(const ExplodedNode *N) = 0; 414}; 415 416/// \brief Constructs a Stack hint for the given symbol. 417/// 418/// The class knows how to construct the stack hint message based on 419/// traversing the CallExpr associated with the call and checking if the given 420/// symbol is returned or is one of the arguments. 421/// The hint can be customized by redefining 'getMessageForX()' methods. 422class StackHintGeneratorForSymbol : public StackHintGenerator { 423private: 424 SymbolRef Sym; 425 std::string Msg; 426 427public: 428 StackHintGeneratorForSymbol(SymbolRef S, StringRef M) : Sym(S), Msg(M) {} 429 virtual ~StackHintGeneratorForSymbol() {} 430 431 /// \brief Search the call expression for the symbol Sym and dispatch the 432 /// 'getMessageForX()' methods to construct a specific message. 433 virtual std::string getMessage(const ExplodedNode *N); 434 435 /// Prints the ordinal form of the given integer, 436 /// only valid for ValNo : ValNo > 0. 437 void printOrdinal(unsigned ValNo, llvm::raw_svector_ostream &Out); 438 439 /// Produces the message of the following form: 440 /// 'Msg via Nth parameter' 441 virtual std::string getMessageForArg(const Expr *ArgE, unsigned ArgIndex); 442 virtual std::string getMessageForReturn(const CallExpr *CallExpr) { 443 return Msg; 444 } 445 virtual std::string getMessageForSymbolNotFound() { 446 return Msg; 447 } 448}; 449 450class PathDiagnosticEventPiece : public PathDiagnosticSpotPiece { 451 llvm::Optional<bool> IsPrunable; 452 453 /// If the event occurs in a different frame than the final diagnostic, 454 /// supply a message that will be used to construct an extra hint on the 455 /// returns from all the calls on the stack from this event to the final 456 /// diagnostic. 457 llvm::OwningPtr<StackHintGenerator> CallStackHint; 458 459public: 460 PathDiagnosticEventPiece(const PathDiagnosticLocation &pos, 461 StringRef s, bool addPosRange = true, 462 StackHintGenerator *stackHint = 0) 463 : PathDiagnosticSpotPiece(pos, s, Event, addPosRange), 464 CallStackHint(stackHint) {} 465 466 ~PathDiagnosticEventPiece(); 467 468 /// Mark the diagnostic piece as being potentially prunable. This 469 /// flag may have been previously set, at which point it will not 470 /// be reset unless one specifies to do so. 471 void setPrunable(bool isPrunable, bool override = false) { 472 if (IsPrunable.hasValue() && !override) 473 return; 474 IsPrunable = isPrunable; 475 } 476 477 /// Return true if the diagnostic piece is prunable. 478 bool isPrunable() const { 479 return IsPrunable.hasValue() ? IsPrunable.getValue() : false; 480 } 481 482 bool hasCallStackHint() { 483 return (CallStackHint != 0); 484 } 485 486 /// Produce the hint for the given node. The node contains 487 /// information about the call for which the diagnostic can be generated. 488 std::string getCallStackMessage(const ExplodedNode *N) { 489 if (CallStackHint) 490 return CallStackHint->getMessage(N); 491 return ""; 492 } 493 494 static inline bool classof(const PathDiagnosticPiece *P) { 495 return P->getKind() == Event; 496 } 497}; 498 499class PathDiagnosticCallPiece : public PathDiagnosticPiece { 500 PathDiagnosticCallPiece(const Decl *callerD, 501 const PathDiagnosticLocation &callReturnPos) 502 : PathDiagnosticPiece(Call), Caller(callerD), Callee(0), 503 NoExit(false), callReturn(callReturnPos) {} 504 505 PathDiagnosticCallPiece(PathPieces &oldPath, const Decl *caller) 506 : PathDiagnosticPiece(Call), Caller(caller), Callee(0), 507 NoExit(true), path(oldPath) {} 508 509 const Decl *Caller; 510 const Decl *Callee; 511 512 // Flag signifying that this diagnostic has only call enter and no matching 513 // call exit. 514 bool NoExit; 515 516 // The custom string, which should appear after the call Return Diagnostic. 517 // TODO: Should we allow multiple diagnostics? 518 std::string CallStackMessage; 519 520public: 521 PathDiagnosticLocation callEnter; 522 PathDiagnosticLocation callEnterWithin; 523 PathDiagnosticLocation callReturn; 524 PathPieces path; 525 526 virtual ~PathDiagnosticCallPiece(); 527 528 const Decl *getCaller() const { return Caller; } 529 530 const Decl *getCallee() const { return Callee; } 531 void setCallee(const CallEnter &CE, const SourceManager &SM); 532 533 bool hasCallStackMessage() { return !CallStackMessage.empty(); } 534 void setCallStackMessage(StringRef st) { 535 CallStackMessage = st; 536 } 537 538 virtual PathDiagnosticLocation getLocation() const { 539 return callEnter; 540 } 541 542 IntrusiveRefCntPtr<PathDiagnosticEventPiece> getCallEnterEvent() const; 543 IntrusiveRefCntPtr<PathDiagnosticEventPiece> 544 getCallEnterWithinCallerEvent() const; 545 IntrusiveRefCntPtr<PathDiagnosticEventPiece> getCallExitEvent() const; 546 547 virtual void flattenLocations() { 548 callEnter.flatten(); 549 callReturn.flatten(); 550 for (PathPieces::iterator I = path.begin(), 551 E = path.end(); I != E; ++I) (*I)->flattenLocations(); 552 } 553 554 static PathDiagnosticCallPiece *construct(const ExplodedNode *N, 555 const CallExitEnd &CE, 556 const SourceManager &SM); 557 558 static PathDiagnosticCallPiece *construct(PathPieces &pieces, 559 const Decl *caller); 560 561 virtual void Profile(llvm::FoldingSetNodeID &ID) const; 562 563 static inline bool classof(const PathDiagnosticPiece *P) { 564 return P->getKind() == Call; 565 } 566}; 567 568class PathDiagnosticControlFlowPiece : public PathDiagnosticPiece { 569 std::vector<PathDiagnosticLocationPair> LPairs; 570public: 571 PathDiagnosticControlFlowPiece(const PathDiagnosticLocation &startPos, 572 const PathDiagnosticLocation &endPos, 573 StringRef s) 574 : PathDiagnosticPiece(s, ControlFlow) { 575 LPairs.push_back(PathDiagnosticLocationPair(startPos, endPos)); 576 } 577 578 PathDiagnosticControlFlowPiece(const PathDiagnosticLocation &startPos, 579 const PathDiagnosticLocation &endPos) 580 : PathDiagnosticPiece(ControlFlow) { 581 LPairs.push_back(PathDiagnosticLocationPair(startPos, endPos)); 582 } 583 584 ~PathDiagnosticControlFlowPiece(); 585 586 PathDiagnosticLocation getStartLocation() const { 587 assert(!LPairs.empty() && 588 "PathDiagnosticControlFlowPiece needs at least one location."); 589 return LPairs[0].getStart(); 590 } 591 592 PathDiagnosticLocation getEndLocation() const { 593 assert(!LPairs.empty() && 594 "PathDiagnosticControlFlowPiece needs at least one location."); 595 return LPairs[0].getEnd(); 596 } 597 598 void push_back(const PathDiagnosticLocationPair &X) { LPairs.push_back(X); } 599 600 virtual PathDiagnosticLocation getLocation() const { 601 return getStartLocation(); 602 } 603 604 typedef std::vector<PathDiagnosticLocationPair>::iterator iterator; 605 iterator begin() { return LPairs.begin(); } 606 iterator end() { return LPairs.end(); } 607 608 virtual void flattenLocations() { 609 for (iterator I=begin(), E=end(); I!=E; ++I) I->flatten(); 610 } 611 612 typedef std::vector<PathDiagnosticLocationPair>::const_iterator 613 const_iterator; 614 const_iterator begin() const { return LPairs.begin(); } 615 const_iterator end() const { return LPairs.end(); } 616 617 static inline bool classof(const PathDiagnosticPiece *P) { 618 return P->getKind() == ControlFlow; 619 } 620 621 virtual void Profile(llvm::FoldingSetNodeID &ID) const; 622}; 623 624class PathDiagnosticMacroPiece : public PathDiagnosticSpotPiece { 625public: 626 PathDiagnosticMacroPiece(const PathDiagnosticLocation &pos) 627 : PathDiagnosticSpotPiece(pos, "", Macro) {} 628 629 ~PathDiagnosticMacroPiece(); 630 631 PathPieces subPieces; 632 633 bool containsEvent() const; 634 635 virtual void flattenLocations() { 636 PathDiagnosticSpotPiece::flattenLocations(); 637 for (PathPieces::iterator I = subPieces.begin(), 638 E = subPieces.end(); I != E; ++I) (*I)->flattenLocations(); 639 } 640 641 static inline bool classof(const PathDiagnosticPiece *P) { 642 return P->getKind() == Macro; 643 } 644 645 virtual void Profile(llvm::FoldingSetNodeID &ID) const; 646}; 647 648/// PathDiagnostic - PathDiagnostic objects represent a single path-sensitive 649/// diagnostic. It represents an ordered-collection of PathDiagnosticPieces, 650/// each which represent the pieces of the path. 651class PathDiagnostic : public llvm::FoldingSetNode { 652 const Decl *DeclWithIssue; 653 std::string BugType; 654 std::string VerboseDesc; 655 std::string ShortDesc; 656 std::string Category; 657 std::deque<std::string> OtherDesc; 658 PathDiagnosticLocation Loc; 659 PathPieces pathImpl; 660 llvm::SmallVector<PathPieces *, 3> pathStack; 661 662 PathDiagnostic(); // Do not implement. 663public: 664 PathDiagnostic(const Decl *DeclWithIssue, StringRef bugtype, 665 StringRef verboseDesc, StringRef shortDesc, 666 StringRef category); 667 668 ~PathDiagnostic(); 669 670 const PathPieces &path; 671 672 /// Return the path currently used by builders for constructing the 673 /// PathDiagnostic. 674 PathPieces &getActivePath() { 675 if (pathStack.empty()) 676 return pathImpl; 677 return *pathStack.back(); 678 } 679 680 /// Return a mutable version of 'path'. 681 PathPieces &getMutablePieces() { 682 return pathImpl; 683 } 684 685 /// Return the unrolled size of the path. 686 unsigned full_size(); 687 688 void pushActivePath(PathPieces *p) { pathStack.push_back(p); } 689 void popActivePath() { if (!pathStack.empty()) pathStack.pop_back(); } 690 691 bool isWithinCall() const { return !pathStack.empty(); } 692 693 void setEndOfPath(PathDiagnosticPiece *EndPiece) { 694 assert(!Loc.isValid() && "End location already set!"); 695 Loc = EndPiece->getLocation(); 696 assert(Loc.isValid() && "Invalid location for end-of-path piece"); 697 getActivePath().push_back(EndPiece); 698 } 699 700 void resetPath() { 701 pathStack.clear(); 702 pathImpl.clear(); 703 Loc = PathDiagnosticLocation(); 704 } 705 706 StringRef getVerboseDescription() const { return VerboseDesc; } 707 StringRef getShortDescription() const { 708 return ShortDesc.empty() ? VerboseDesc : ShortDesc; 709 } 710 StringRef getBugType() const { return BugType; } 711 StringRef getCategory() const { return Category; } 712 713 /// Return the semantic context where an issue occurred. If the 714 /// issue occurs along a path, this represents the "central" area 715 /// where the bug manifests. 716 const Decl *getDeclWithIssue() const { return DeclWithIssue; } 717 718 typedef std::deque<std::string>::const_iterator meta_iterator; 719 meta_iterator meta_begin() const { return OtherDesc.begin(); } 720 meta_iterator meta_end() const { return OtherDesc.end(); } 721 void addMeta(StringRef s) { OtherDesc.push_back(s); } 722 723 PathDiagnosticLocation getLocation() const { 724 assert(Loc.isValid() && "No end-of-path location set yet!"); 725 return Loc; 726 } 727 728 void flattenLocations() { 729 Loc.flatten(); 730 for (PathPieces::iterator I = pathImpl.begin(), E = pathImpl.end(); 731 I != E; ++I) (*I)->flattenLocations(); 732 } 733 734 /// Profiles the diagnostic, independent of the path it references. 735 /// 736 /// This can be used to merge diagnostics that refer to the same issue 737 /// along different paths. 738 void Profile(llvm::FoldingSetNodeID &ID) const; 739 740 /// Profiles the diagnostic, including its path. 741 /// 742 /// Two diagnostics with the same issue along different paths will generate 743 /// different profiles. 744 void FullProfile(llvm::FoldingSetNodeID &ID) const; 745}; 746 747} // end GR namespace 748 749} //end clang namespace 750 751#endif 752