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