From 632182d0e2011a6e21cf9abe34eef5a1f037e7ef Mon Sep 17 00:00:00 2001 From: Jordan Rose Date: Thu, 6 Jun 2013 01:57:19 +0000 Subject: [PATCH] [analyzer] Improve debug output for PathDiagnosticPieces. You can now dump a single PathDiagnosticPiece or PathDiagnosticLocation. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@183367 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../Core/BugReporter/PathDiagnostic.h | 18 ++- lib/StaticAnalyzer/Core/BugReporter.cpp | 129 ++++++++++-------- 2 files changed, 90 insertions(+), 57 deletions(-) diff --git a/include/clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h b/include/clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h index cae47f8916..abcd13daf9 100644 --- a/include/clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h +++ b/include/clang/StaticAnalyzer/Core/BugReporter/PathDiagnostic.h @@ -285,6 +285,8 @@ public: void Profile(llvm::FoldingSetNodeID &ID) const; + void dump() const; + /// \brief Given an exploded node, retrieve the statement that should be used /// for the diagnostic location. static const Stmt *getStmt(const ExplodedNode *N); @@ -401,6 +403,8 @@ public: bool isLastInMainSourceFile() const { return LastInMainSourceFile; } + + virtual void dump() const = 0; }; @@ -435,7 +439,7 @@ public: PathDiagnosticLocation getLocation() const { return Pos; } virtual void flattenLocations() { Pos.flatten(); } - + virtual void Profile(llvm::FoldingSetNodeID &ID) const; static bool classof(const PathDiagnosticPiece *P) { @@ -529,6 +533,8 @@ public: return ""; } + virtual void dump() const; + static inline bool classof(const PathDiagnosticPiece *P) { return P->getKind() == Event; } @@ -596,6 +602,8 @@ public: static PathDiagnosticCallPiece *construct(PathPieces &pieces, const Decl *caller); + virtual void dump() const; + virtual void Profile(llvm::FoldingSetNodeID &ID) const; static inline bool classof(const PathDiagnosticPiece *P) { @@ -663,7 +671,9 @@ public: static inline bool classof(const PathDiagnosticPiece *P) { return P->getKind() == ControlFlow; } - + + virtual void dump() const; + virtual void Profile(llvm::FoldingSetNodeID &ID) const; }; @@ -687,7 +697,9 @@ public: static inline bool classof(const PathDiagnosticPiece *P) { return P->getKind() == Macro; } - + + virtual void dump() const; + virtual void Profile(llvm::FoldingSetNodeID &ID) const; }; diff --git a/lib/StaticAnalyzer/Core/BugReporter.cpp b/lib/StaticAnalyzer/Core/BugReporter.cpp index 15073bb676..829e7482eb 100644 --- a/lib/StaticAnalyzer/Core/BugReporter.cpp +++ b/lib/StaticAnalyzer/Core/BugReporter.cpp @@ -1886,60 +1886,6 @@ static bool isIncrementOrInitInForLoop(const Stmt *S, const Stmt *FL) { typedef llvm::DenseSet OptimizedCallsSet; -void PathPieces::dump() const { - unsigned index = 0; - for (PathPieces::const_iterator I = begin(), E = end(); I != E; ++I ) { - llvm::errs() << "[" << index++ << "]"; - - switch ((*I)->getKind()) { - case PathDiagnosticPiece::Call: - llvm::errs() << " CALL\n--------------\n"; - - if (const Stmt *SLoc = getLocStmt((*I)->getLocation())) { - SLoc->dump(); - } else { - const PathDiagnosticCallPiece *Call = cast(*I); - if (const NamedDecl *ND = dyn_cast(Call->getCallee())) - llvm::errs() << *ND << "\n"; - } - break; - case PathDiagnosticPiece::Event: - llvm::errs() << " EVENT\n--------------\n"; - llvm::errs() << (*I)->getString() << "\n"; - if (const Stmt *SLoc = getLocStmt((*I)->getLocation())) { - llvm::errs() << " ---- at ----\n"; - SLoc->dump(); - } - break; - case PathDiagnosticPiece::Macro: - llvm::errs() << " MACRO\n--------------\n"; - // FIXME: print which macro is being invoked. - break; - case PathDiagnosticPiece::ControlFlow: { - const PathDiagnosticControlFlowPiece *CP = - cast(*I); - llvm::errs() << " CONTROL\n--------------\n"; - - if (const Stmt *s1Start = getLocStmt(CP->getStartLocation())) - s1Start->dump(); - else - llvm::errs() << "NULL\n"; - - llvm::errs() << " ---- to ----\n"; - - if (const Stmt *s1End = getLocStmt(CP->getEndLocation())) - s1End->dump(); - else - llvm::errs() << "NULL\n"; - - break; - } - } - - llvm::errs() << "\n"; - } -} - /// Adds synthetic edges from top-level statements to their subexpressions. /// /// This avoids a "swoosh" effect, where an edge from a top-level statement A @@ -3448,3 +3394,78 @@ BugType *BugReporter::getBugTypeForName(StringRef name, } return BT; } + + +void PathPieces::dump() const { + unsigned index = 0; + for (PathPieces::const_iterator I = begin(), E = end(); I != E; ++I) { + llvm::errs() << "[" << index++ << "] "; + (*I)->dump(); + llvm::errs() << "\n"; + } +} + +void PathDiagnosticCallPiece::dump() const { + llvm::errs() << "CALL\n--------------\n"; + + if (const Stmt *SLoc = getLocStmt(getLocation())) + SLoc->dump(); + else if (const NamedDecl *ND = dyn_cast(getCallee())) + llvm::errs() << *ND << "\n"; + else + getLocation().dump(); +} + +void PathDiagnosticEventPiece::dump() const { + llvm::errs() << "EVENT\n--------------\n"; + llvm::errs() << getString() << "\n"; + llvm::errs() << " ---- at ----\n"; + getLocation().dump(); +} + +void PathDiagnosticControlFlowPiece::dump() const { + llvm::errs() << "CONTROL\n--------------\n"; + getStartLocation().dump(); + llvm::errs() << " ---- to ----\n"; + getEndLocation().dump(); +} + +void PathDiagnosticMacroPiece::dump() const { + llvm::errs() << "MACRO\n--------------\n"; + // FIXME: Print which macro is being invoked. +} + +void PathDiagnosticLocation::dump() const { + if (!isValid()) { + llvm::errs() << "\n"; + return; + } + + switch (K) { + case RangeK: + // FIXME: actually print the range. + llvm::errs() << "\n"; + break; + case SingleLocK: + asLocation().dump(); + llvm::errs() << "\n"; + break; + case StmtK: + if (S) + S->dump(); + else + llvm::errs() << "\n"; + break; + case DeclK: + if (const NamedDecl *ND = dyn_cast_or_null(D)) + llvm::errs() << *ND << "\n"; + else if (isa(D)) + // FIXME: Make this nicer. + llvm::errs() << "\n"; + else if (D) + llvm::errs() << "\n"; + else + llvm::errs() << "\n"; + break; + } +} -- 2.40.0