PathDiagnostic.h revision 615a092a511cd2dfe1a5364ebf5f80e55e33034d
125b3c049e70834cf33790a28643ab058b507b35cBen Cheng//===--- PathDiagnostic.h - Path-Specific Diagnostic Handling ---*- C++ -*-===// 225b3c049e70834cf33790a28643ab058b507b35cBen Cheng// 325b3c049e70834cf33790a28643ab058b507b35cBen Cheng// The LLVM Compiler Infrastructure 425b3c049e70834cf33790a28643ab058b507b35cBen Cheng// 525b3c049e70834cf33790a28643ab058b507b35cBen Cheng// This file is distributed under the University of Illinois Open Source 625b3c049e70834cf33790a28643ab058b507b35cBen Cheng// License. See LICENSE.TXT for details. 725b3c049e70834cf33790a28643ab058b507b35cBen Cheng// 825b3c049e70834cf33790a28643ab058b507b35cBen Cheng//===----------------------------------------------------------------------===// 925b3c049e70834cf33790a28643ab058b507b35cBen Cheng// 1025b3c049e70834cf33790a28643ab058b507b35cBen Cheng// This file defines the PathDiagnostic-related interfaces. 1125b3c049e70834cf33790a28643ab058b507b35cBen Cheng// 1225b3c049e70834cf33790a28643ab058b507b35cBen Cheng//===----------------------------------------------------------------------===// 1325b3c049e70834cf33790a28643ab058b507b35cBen Cheng 1425b3c049e70834cf33790a28643ab058b507b35cBen Cheng#ifndef LLVM_CLANG_PATH_DIAGNOSTIC_H 1525b3c049e70834cf33790a28643ab058b507b35cBen Cheng#define LLVM_CLANG_PATH_DIAGNOSTIC_H 1625b3c049e70834cf33790a28643ab058b507b35cBen Cheng 1725b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include "clang/Basic/SourceLocation.h" 1825b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include "clang/Analysis/ProgramPoint.h" 1925b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include "llvm/ADT/FoldingSet.h" 2025b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include "llvm/ADT/IntrusiveRefCntPtr.h" 2125b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include "llvm/ADT/PointerUnion.h" 2225b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include "llvm/ADT/Optional.h" 2325b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include <deque> 2425b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include <iterator> 2525b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include <string> 2625b3c049e70834cf33790a28643ab058b507b35cBen Cheng#include <vector> 2725b3c049e70834cf33790a28643ab058b507b35cBen Cheng 2825b3c049e70834cf33790a28643ab058b507b35cBen Chengnamespace clang { 2925b3c049e70834cf33790a28643ab058b507b35cBen Cheng 3025b3c049e70834cf33790a28643ab058b507b35cBen Chengclass AnalysisDeclContext; 3125b3c049e70834cf33790a28643ab058b507b35cBen Chengclass BinaryOperator; 3225b3c049e70834cf33790a28643ab058b507b35cBen Chengclass CompoundStmt; 3325b3c049e70834cf33790a28643ab058b507b35cBen Chengclass Decl; 3425b3c049e70834cf33790a28643ab058b507b35cBen Chengclass LocationContext; 3525b3c049e70834cf33790a28643ab058b507b35cBen Chengclass MemberExpr; 3625b3c049e70834cf33790a28643ab058b507b35cBen Chengclass ParentMap; 3725b3c049e70834cf33790a28643ab058b507b35cBen Chengclass ProgramPoint; 3825b3c049e70834cf33790a28643ab058b507b35cBen Chengclass SourceManager; 3925b3c049e70834cf33790a28643ab058b507b35cBen Chengclass Stmt; 4025b3c049e70834cf33790a28643ab058b507b35cBen Cheng 4125b3c049e70834cf33790a28643ab058b507b35cBen Chengnamespace ento { 4225b3c049e70834cf33790a28643ab058b507b35cBen Cheng 4325b3c049e70834cf33790a28643ab058b507b35cBen Chengclass ExplodedNode; 4425b3c049e70834cf33790a28643ab058b507b35cBen Chengclass SymExpr; 4525b3c049e70834cf33790a28643ab058b507b35cBen Chengtypedef const SymExpr* SymbolRef; 4625b3c049e70834cf33790a28643ab058b507b35cBen Cheng 4725b3c049e70834cf33790a28643ab058b507b35cBen Cheng//===----------------------------------------------------------------------===// 4825b3c049e70834cf33790a28643ab058b507b35cBen Cheng// High-level interface for handlers of path-sensitive diagnostics. 4925b3c049e70834cf33790a28643ab058b507b35cBen Cheng//===----------------------------------------------------------------------===// 5025b3c049e70834cf33790a28643ab058b507b35cBen Cheng 5125b3c049e70834cf33790a28643ab058b507b35cBen Chengclass PathDiagnostic; 5225b3c049e70834cf33790a28643ab058b507b35cBen Cheng 5325b3c049e70834cf33790a28643ab058b507b35cBen Chengclass PathDiagnosticConsumer { 5425b3c049e70834cf33790a28643ab058b507b35cBen Chengpublic: 5525b3c049e70834cf33790a28643ab058b507b35cBen Cheng class PDFileEntry : public llvm::FoldingSetNode { 5625b3c049e70834cf33790a28643ab058b507b35cBen Cheng public: 5725b3c049e70834cf33790a28643ab058b507b35cBen Cheng PDFileEntry(llvm::FoldingSetNodeID &NodeID) : NodeID(NodeID) {} 5825b3c049e70834cf33790a28643ab058b507b35cBen Cheng 5925b3c049e70834cf33790a28643ab058b507b35cBen Cheng typedef std::vector<std::pair<StringRef, StringRef> > ConsumerFiles; 6025b3c049e70834cf33790a28643ab058b507b35cBen Cheng 6125b3c049e70834cf33790a28643ab058b507b35cBen Cheng /// \brief A vector of <consumer,file> pairs. 6225b3c049e70834cf33790a28643ab058b507b35cBen Cheng ConsumerFiles files; 6325b3c049e70834cf33790a28643ab058b507b35cBen Cheng 6425b3c049e70834cf33790a28643ab058b507b35cBen Cheng /// \brief A precomputed hash tag used for uniquing PDFileEntry objects. 6525b3c049e70834cf33790a28643ab058b507b35cBen Cheng const llvm::FoldingSetNodeID NodeID; 6625b3c049e70834cf33790a28643ab058b507b35cBen Cheng 6725b3c049e70834cf33790a28643ab058b507b35cBen Cheng /// \brief Used for profiling in the FoldingSet. 6825b3c049e70834cf33790a28643ab058b507b35cBen Cheng void Profile(llvm::FoldingSetNodeID &ID) { ID = NodeID; } 6925b3c049e70834cf33790a28643ab058b507b35cBen Cheng }; 7025b3c049e70834cf33790a28643ab058b507b35cBen Cheng 7125b3c049e70834cf33790a28643ab058b507b35cBen Cheng struct FilesMade : public llvm::FoldingSet<PDFileEntry> { 7225b3c049e70834cf33790a28643ab058b507b35cBen Cheng llvm::BumpPtrAllocator Alloc; 7325b3c049e70834cf33790a28643ab058b507b35cBen Cheng 7425b3c049e70834cf33790a28643ab058b507b35cBen Cheng void addDiagnostic(const PathDiagnostic &PD, 7525b3c049e70834cf33790a28643ab058b507b35cBen Cheng StringRef ConsumerName, 7625b3c049e70834cf33790a28643ab058b507b35cBen Cheng StringRef fileName); 7725b3c049e70834cf33790a28643ab058b507b35cBen Cheng 7825b3c049e70834cf33790a28643ab058b507b35cBen Cheng PDFileEntry::ConsumerFiles *getFiles(const PathDiagnostic &PD); 7925b3c049e70834cf33790a28643ab058b507b35cBen Cheng }; 8025b3c049e70834cf33790a28643ab058b507b35cBen Cheng 8125b3c049e70834cf33790a28643ab058b507b35cBen Chengprivate: 8225b3c049e70834cf33790a28643ab058b507b35cBen Cheng virtual void anchor(); 8325b3c049e70834cf33790a28643ab058b507b35cBen Chengpublic: 8425b3c049e70834cf33790a28643ab058b507b35cBen Cheng PathDiagnosticConsumer() : flushed(false) {} 8525b3c049e70834cf33790a28643ab058b507b35cBen Cheng virtual ~PathDiagnosticConsumer(); 8625b3c049e70834cf33790a28643ab058b507b35cBen Cheng 8725b3c049e70834cf33790a28643ab058b507b35cBen Cheng void FlushDiagnostics(FilesMade *FilesMade); 8825b3c049e70834cf33790a28643ab058b507b35cBen Cheng 8925b3c049e70834cf33790a28643ab058b507b35cBen Cheng virtual void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags, 9025b3c049e70834cf33790a28643ab058b507b35cBen Cheng FilesMade *filesMade) = 0; 9125b3c049e70834cf33790a28643ab058b507b35cBen Cheng 9225b3c049e70834cf33790a28643ab058b507b35cBen Cheng virtual StringRef getName() const = 0; 9325b3c049e70834cf33790a28643ab058b507b35cBen Cheng 9425b3c049e70834cf33790a28643ab058b507b35cBen Cheng void HandlePathDiagnostic(PathDiagnostic *D); 9525b3c049e70834cf33790a28643ab058b507b35cBen Cheng 9625b3c049e70834cf33790a28643ab058b507b35cBen Cheng enum PathGenerationScheme { None, Minimal, Extensive }; 9725b3c049e70834cf33790a28643ab058b507b35cBen Cheng virtual PathGenerationScheme getGenerationScheme() const { return Minimal; } 9825b3c049e70834cf33790a28643ab058b507b35cBen Cheng virtual bool supportsLogicalOpControlFlow() const { return false; } 9925b3c049e70834cf33790a28643ab058b507b35cBen Cheng virtual bool supportsAllBlockEdges() const { return false; } 10025b3c049e70834cf33790a28643ab058b507b35cBen Cheng 10125b3c049e70834cf33790a28643ab058b507b35cBen Cheng /// Return true if the PathDiagnosticConsumer supports individual 10225b3c049e70834cf33790a28643ab058b507b35cBen Cheng /// PathDiagnostics that span multiple files. 10325b3c049e70834cf33790a28643ab058b507b35cBen Cheng virtual bool supportsCrossFileDiagnostics() const { return false; } 10425b3c049e70834cf33790a28643ab058b507b35cBen Cheng 10525b3c049e70834cf33790a28643ab058b507b35cBen Chengprotected: 10625b3c049e70834cf33790a28643ab058b507b35cBen Cheng bool flushed; 10725b3c049e70834cf33790a28643ab058b507b35cBen Cheng llvm::FoldingSet<PathDiagnostic> Diags; 10825b3c049e70834cf33790a28643ab058b507b35cBen Cheng}; 10925b3c049e70834cf33790a28643ab058b507b35cBen Cheng 11025b3c049e70834cf33790a28643ab058b507b35cBen Cheng//===----------------------------------------------------------------------===// 11125b3c049e70834cf33790a28643ab058b507b35cBen Cheng// Path-sensitive diagnostics. 11225b3c049e70834cf33790a28643ab058b507b35cBen Cheng//===----------------------------------------------------------------------===// 11325b3c049e70834cf33790a28643ab058b507b35cBen Cheng 11425b3c049e70834cf33790a28643ab058b507b35cBen Chengclass PathDiagnosticRange : public SourceRange { 11525b3c049e70834cf33790a28643ab058b507b35cBen Chengpublic: 11625b3c049e70834cf33790a28643ab058b507b35cBen Cheng bool isPoint; 11725b3c049e70834cf33790a28643ab058b507b35cBen Cheng 11825b3c049e70834cf33790a28643ab058b507b35cBen Cheng PathDiagnosticRange(const SourceRange &R, bool isP = false) 11925b3c049e70834cf33790a28643ab058b507b35cBen Cheng : SourceRange(R), isPoint(isP) {} 12025b3c049e70834cf33790a28643ab058b507b35cBen Cheng 12125b3c049e70834cf33790a28643ab058b507b35cBen Cheng PathDiagnosticRange() : isPoint(false) {} 12225b3c049e70834cf33790a28643ab058b507b35cBen Cheng}; 12325b3c049e70834cf33790a28643ab058b507b35cBen Cheng 12425b3c049e70834cf33790a28643ab058b507b35cBen Chengtypedef llvm::PointerUnion<const LocationContext*, AnalysisDeclContext*> 12525b3c049e70834cf33790a28643ab058b507b35cBen Cheng LocationOrAnalysisDeclContext; 12625b3c049e70834cf33790a28643ab058b507b35cBen Cheng 12725b3c049e70834cf33790a28643ab058b507b35cBen Chengclass PathDiagnosticLocation { 12825b3c049e70834cf33790a28643ab058b507b35cBen Chengprivate: 12925b3c049e70834cf33790a28643ab058b507b35cBen Cheng enum Kind { RangeK, SingleLocK, StmtK, DeclK } K; 13025b3c049e70834cf33790a28643ab058b507b35cBen Cheng const Stmt *S; 13125b3c049e70834cf33790a28643ab058b507b35cBen Cheng const Decl *D; 13225b3c049e70834cf33790a28643ab058b507b35cBen Cheng const SourceManager *SM; 13325b3c049e70834cf33790a28643ab058b507b35cBen Cheng FullSourceLoc Loc; 13425b3c049e70834cf33790a28643ab058b507b35cBen Cheng PathDiagnosticRange Range; 13525b3c049e70834cf33790a28643ab058b507b35cBen Cheng 13625b3c049e70834cf33790a28643ab058b507b35cBen Cheng PathDiagnosticLocation(SourceLocation L, const SourceManager &sm, 13725b3c049e70834cf33790a28643ab058b507b35cBen Cheng Kind kind) 13825b3c049e70834cf33790a28643ab058b507b35cBen Cheng : K(kind), S(0), D(0), SM(&sm), 13925b3c049e70834cf33790a28643ab058b507b35cBen Cheng Loc(genLocation(L)), Range(genRange()) { 14025b3c049e70834cf33790a28643ab058b507b35cBen Cheng } 14125b3c049e70834cf33790a28643ab058b507b35cBen Cheng 14225b3c049e70834cf33790a28643ab058b507b35cBen Cheng FullSourceLoc 14325b3c049e70834cf33790a28643ab058b507b35cBen Cheng genLocation(SourceLocation L = SourceLocation(), 14425b3c049e70834cf33790a28643ab058b507b35cBen Cheng LocationOrAnalysisDeclContext LAC = (AnalysisDeclContext*)0) const; 14525b3c049e70834cf33790a28643ab058b507b35cBen Cheng 14625b3c049e70834cf33790a28643ab058b507b35cBen Cheng PathDiagnosticRange 14725b3c049e70834cf33790a28643ab058b507b35cBen Cheng genRange(LocationOrAnalysisDeclContext LAC = (AnalysisDeclContext*)0) const; 14825b3c049e70834cf33790a28643ab058b507b35cBen Cheng 14925b3c049e70834cf33790a28643ab058b507b35cBen Chengpublic: 15025b3c049e70834cf33790a28643ab058b507b35cBen Cheng /// Create an invalid location. 15125b3c049e70834cf33790a28643ab058b507b35cBen Cheng PathDiagnosticLocation() 15225b3c049e70834cf33790a28643ab058b507b35cBen Cheng : K(SingleLocK), S(0), D(0), SM(0) {} 15325b3c049e70834cf33790a28643ab058b507b35cBen Cheng 15425b3c049e70834cf33790a28643ab058b507b35cBen Cheng /// Create a location corresponding to the given statement. 15525b3c049e70834cf33790a28643ab058b507b35cBen Cheng PathDiagnosticLocation(const Stmt *s, 15625b3c049e70834cf33790a28643ab058b507b35cBen Cheng const SourceManager &sm, 15725b3c049e70834cf33790a28643ab058b507b35cBen Cheng LocationOrAnalysisDeclContext lac) 15825b3c049e70834cf33790a28643ab058b507b35cBen Cheng : K(s->getLocStart().isValid() ? StmtK : SingleLocK), 15925b3c049e70834cf33790a28643ab058b507b35cBen Cheng S(K == StmtK ? s : 0), 16025b3c049e70834cf33790a28643ab058b507b35cBen Cheng D(0), SM(&sm), 16125b3c049e70834cf33790a28643ab058b507b35cBen Cheng Loc(genLocation(SourceLocation(), lac)), 16225b3c049e70834cf33790a28643ab058b507b35cBen Cheng Range(genRange(lac)) { 16325b3c049e70834cf33790a28643ab058b507b35cBen Cheng assert(K == SingleLocK || S); 16425b3c049e70834cf33790a28643ab058b507b35cBen Cheng assert(K == SingleLocK || Loc.isValid()); 16525b3c049e70834cf33790a28643ab058b507b35cBen Cheng assert(K == SingleLocK || Range.isValid()); 16625b3c049e70834cf33790a28643ab058b507b35cBen Cheng } 16725b3c049e70834cf33790a28643ab058b507b35cBen Cheng 16825b3c049e70834cf33790a28643ab058b507b35cBen Cheng /// Create a location corresponding to the given declaration. 16925b3c049e70834cf33790a28643ab058b507b35cBen Cheng PathDiagnosticLocation(const Decl *d, const SourceManager &sm) 17025b3c049e70834cf33790a28643ab058b507b35cBen Cheng : K(DeclK), S(0), D(d), SM(&sm), 17125b3c049e70834cf33790a28643ab058b507b35cBen Cheng Loc(genLocation()), Range(genRange()) { 17225b3c049e70834cf33790a28643ab058b507b35cBen Cheng assert(D); 17325b3c049e70834cf33790a28643ab058b507b35cBen Cheng assert(Loc.isValid()); 17425b3c049e70834cf33790a28643ab058b507b35cBen Cheng assert(Range.isValid()); 17525b3c049e70834cf33790a28643ab058b507b35cBen Cheng } 17625b3c049e70834cf33790a28643ab058b507b35cBen Cheng 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 /// Produces the message of the following form: 436 /// 'Msg via Nth parameter' 437 virtual std::string getMessageForArg(const Expr *ArgE, unsigned ArgIndex); 438 virtual std::string getMessageForReturn(const CallExpr *CallExpr) { 439 return Msg; 440 } 441 virtual std::string getMessageForSymbolNotFound() { 442 return Msg; 443 } 444}; 445 446class PathDiagnosticEventPiece : public PathDiagnosticSpotPiece { 447 llvm::Optional<bool> IsPrunable; 448 449 /// If the event occurs in a different frame than the final diagnostic, 450 /// supply a message that will be used to construct an extra hint on the 451 /// returns from all the calls on the stack from this event to the final 452 /// diagnostic. 453 llvm::OwningPtr<StackHintGenerator> CallStackHint; 454 455public: 456 PathDiagnosticEventPiece(const PathDiagnosticLocation &pos, 457 StringRef s, bool addPosRange = true, 458 StackHintGenerator *stackHint = 0) 459 : PathDiagnosticSpotPiece(pos, s, Event, addPosRange), 460 CallStackHint(stackHint) {} 461 462 ~PathDiagnosticEventPiece(); 463 464 /// Mark the diagnostic piece as being potentially prunable. This 465 /// flag may have been previously set, at which point it will not 466 /// be reset unless one specifies to do so. 467 void setPrunable(bool isPrunable, bool override = false) { 468 if (IsPrunable.hasValue() && !override) 469 return; 470 IsPrunable = isPrunable; 471 } 472 473 /// Return true if the diagnostic piece is prunable. 474 bool isPrunable() const { 475 return IsPrunable.hasValue() ? IsPrunable.getValue() : false; 476 } 477 478 bool hasCallStackHint() { 479 return (CallStackHint != 0); 480 } 481 482 /// Produce the hint for the given node. The node contains 483 /// information about the call for which the diagnostic can be generated. 484 std::string getCallStackMessage(const ExplodedNode *N) { 485 if (CallStackHint) 486 return CallStackHint->getMessage(N); 487 return ""; 488 } 489 490 static inline bool classof(const PathDiagnosticPiece *P) { 491 return P->getKind() == Event; 492 } 493}; 494 495class PathDiagnosticCallPiece : public PathDiagnosticPiece { 496 PathDiagnosticCallPiece(const Decl *callerD, 497 const PathDiagnosticLocation &callReturnPos) 498 : PathDiagnosticPiece(Call), Caller(callerD), Callee(0), 499 NoExit(false), callReturn(callReturnPos) {} 500 501 PathDiagnosticCallPiece(PathPieces &oldPath, const Decl *caller) 502 : PathDiagnosticPiece(Call), Caller(caller), Callee(0), 503 NoExit(true), path(oldPath) {} 504 505 const Decl *Caller; 506 const Decl *Callee; 507 508 // Flag signifying that this diagnostic has only call enter and no matching 509 // call exit. 510 bool NoExit; 511 512 // The custom string, which should appear after the call Return Diagnostic. 513 // TODO: Should we allow multiple diagnostics? 514 std::string CallStackMessage; 515 516public: 517 PathDiagnosticLocation callEnter; 518 PathDiagnosticLocation callEnterWithin; 519 PathDiagnosticLocation callReturn; 520 PathPieces path; 521 522 virtual ~PathDiagnosticCallPiece(); 523 524 const Decl *getCaller() const { return Caller; } 525 526 const Decl *getCallee() const { return Callee; } 527 void setCallee(const CallEnter &CE, const SourceManager &SM); 528 529 bool hasCallStackMessage() { return !CallStackMessage.empty(); } 530 void setCallStackMessage(StringRef st) { 531 CallStackMessage = st; 532 } 533 534 virtual PathDiagnosticLocation getLocation() const { 535 return callEnter; 536 } 537 538 IntrusiveRefCntPtr<PathDiagnosticEventPiece> getCallEnterEvent() const; 539 IntrusiveRefCntPtr<PathDiagnosticEventPiece> 540 getCallEnterWithinCallerEvent() const; 541 IntrusiveRefCntPtr<PathDiagnosticEventPiece> getCallExitEvent() const; 542 543 virtual void flattenLocations() { 544 callEnter.flatten(); 545 callReturn.flatten(); 546 for (PathPieces::iterator I = path.begin(), 547 E = path.end(); I != E; ++I) (*I)->flattenLocations(); 548 } 549 550 static PathDiagnosticCallPiece *construct(const ExplodedNode *N, 551 const CallExitEnd &CE, 552 const SourceManager &SM); 553 554 static PathDiagnosticCallPiece *construct(PathPieces &pieces, 555 const Decl *caller); 556 557 virtual void Profile(llvm::FoldingSetNodeID &ID) const; 558 559 static inline bool classof(const PathDiagnosticPiece *P) { 560 return P->getKind() == Call; 561 } 562}; 563 564class PathDiagnosticControlFlowPiece : public PathDiagnosticPiece { 565 std::vector<PathDiagnosticLocationPair> LPairs; 566public: 567 PathDiagnosticControlFlowPiece(const PathDiagnosticLocation &startPos, 568 const PathDiagnosticLocation &endPos, 569 StringRef s) 570 : PathDiagnosticPiece(s, ControlFlow) { 571 LPairs.push_back(PathDiagnosticLocationPair(startPos, endPos)); 572 } 573 574 PathDiagnosticControlFlowPiece(const PathDiagnosticLocation &startPos, 575 const PathDiagnosticLocation &endPos) 576 : PathDiagnosticPiece(ControlFlow) { 577 LPairs.push_back(PathDiagnosticLocationPair(startPos, endPos)); 578 } 579 580 ~PathDiagnosticControlFlowPiece(); 581 582 PathDiagnosticLocation getStartLocation() const { 583 assert(!LPairs.empty() && 584 "PathDiagnosticControlFlowPiece needs at least one location."); 585 return LPairs[0].getStart(); 586 } 587 588 PathDiagnosticLocation getEndLocation() const { 589 assert(!LPairs.empty() && 590 "PathDiagnosticControlFlowPiece needs at least one location."); 591 return LPairs[0].getEnd(); 592 } 593 594 void push_back(const PathDiagnosticLocationPair &X) { LPairs.push_back(X); } 595 596 virtual PathDiagnosticLocation getLocation() const { 597 return getStartLocation(); 598 } 599 600 typedef std::vector<PathDiagnosticLocationPair>::iterator iterator; 601 iterator begin() { return LPairs.begin(); } 602 iterator end() { return LPairs.end(); } 603 604 virtual void flattenLocations() { 605 for (iterator I=begin(), E=end(); I!=E; ++I) I->flatten(); 606 } 607 608 typedef std::vector<PathDiagnosticLocationPair>::const_iterator 609 const_iterator; 610 const_iterator begin() const { return LPairs.begin(); } 611 const_iterator end() const { return LPairs.end(); } 612 613 static inline bool classof(const PathDiagnosticPiece *P) { 614 return P->getKind() == ControlFlow; 615 } 616 617 virtual void Profile(llvm::FoldingSetNodeID &ID) const; 618}; 619 620class PathDiagnosticMacroPiece : public PathDiagnosticSpotPiece { 621public: 622 PathDiagnosticMacroPiece(const PathDiagnosticLocation &pos) 623 : PathDiagnosticSpotPiece(pos, "", Macro) {} 624 625 ~PathDiagnosticMacroPiece(); 626 627 PathPieces subPieces; 628 629 bool containsEvent() const; 630 631 virtual void flattenLocations() { 632 PathDiagnosticSpotPiece::flattenLocations(); 633 for (PathPieces::iterator I = subPieces.begin(), 634 E = subPieces.end(); I != E; ++I) (*I)->flattenLocations(); 635 } 636 637 static inline bool classof(const PathDiagnosticPiece *P) { 638 return P->getKind() == Macro; 639 } 640 641 virtual void Profile(llvm::FoldingSetNodeID &ID) const; 642}; 643 644/// PathDiagnostic - PathDiagnostic objects represent a single path-sensitive 645/// diagnostic. It represents an ordered-collection of PathDiagnosticPieces, 646/// each which represent the pieces of the path. 647class PathDiagnostic : public llvm::FoldingSetNode { 648 const Decl *DeclWithIssue; 649 std::string BugType; 650 std::string VerboseDesc; 651 std::string ShortDesc; 652 std::string Category; 653 std::deque<std::string> OtherDesc; 654 PathDiagnosticLocation Loc; 655 PathPieces pathImpl; 656 llvm::SmallVector<PathPieces *, 3> pathStack; 657 658 PathDiagnostic(); // Do not implement. 659public: 660 PathDiagnostic(const Decl *DeclWithIssue, StringRef bugtype, 661 StringRef verboseDesc, StringRef shortDesc, 662 StringRef category); 663 664 ~PathDiagnostic(); 665 666 const PathPieces &path; 667 668 /// Return the path currently used by builders for constructing the 669 /// PathDiagnostic. 670 PathPieces &getActivePath() { 671 if (pathStack.empty()) 672 return pathImpl; 673 return *pathStack.back(); 674 } 675 676 /// Return a mutable version of 'path'. 677 PathPieces &getMutablePieces() { 678 return pathImpl; 679 } 680 681 /// Return the unrolled size of the path. 682 unsigned full_size(); 683 684 void pushActivePath(PathPieces *p) { pathStack.push_back(p); } 685 void popActivePath() { if (!pathStack.empty()) pathStack.pop_back(); } 686 687 bool isWithinCall() const { return !pathStack.empty(); } 688 689 void setEndOfPath(PathDiagnosticPiece *EndPiece) { 690 assert(!Loc.isValid() && "End location already set!"); 691 Loc = EndPiece->getLocation(); 692 assert(Loc.isValid() && "Invalid location for end-of-path piece"); 693 getActivePath().push_back(EndPiece); 694 } 695 696 void resetPath() { 697 pathStack.clear(); 698 pathImpl.clear(); 699 Loc = PathDiagnosticLocation(); 700 } 701 702 StringRef getVerboseDescription() const { return VerboseDesc; } 703 StringRef getShortDescription() const { 704 return ShortDesc.empty() ? VerboseDesc : ShortDesc; 705 } 706 StringRef getBugType() const { return BugType; } 707 StringRef getCategory() const { return Category; } 708 709 /// Return the semantic context where an issue occurred. If the 710 /// issue occurs along a path, this represents the "central" area 711 /// where the bug manifests. 712 const Decl *getDeclWithIssue() const { return DeclWithIssue; } 713 714 typedef std::deque<std::string>::const_iterator meta_iterator; 715 meta_iterator meta_begin() const { return OtherDesc.begin(); } 716 meta_iterator meta_end() const { return OtherDesc.end(); } 717 void addMeta(StringRef s) { OtherDesc.push_back(s); } 718 719 PathDiagnosticLocation getLocation() const { 720 assert(Loc.isValid() && "No end-of-path location set yet!"); 721 return Loc; 722 } 723 724 void flattenLocations() { 725 Loc.flatten(); 726 for (PathPieces::iterator I = pathImpl.begin(), E = pathImpl.end(); 727 I != E; ++I) (*I)->flattenLocations(); 728 } 729 730 /// Profiles the diagnostic, independent of the path it references. 731 /// 732 /// This can be used to merge diagnostics that refer to the same issue 733 /// along different paths. 734 void Profile(llvm::FoldingSetNodeID &ID) const; 735 736 /// Profiles the diagnostic, including its path. 737 /// 738 /// Two diagnostics with the same issue along different paths will generate 739 /// different profiles. 740 void FullProfile(llvm::FoldingSetNodeID &ID) const; 741}; 742 743} // end GR namespace 744 745} //end clang namespace 746 747#endif 748