From: Ted Kremenek Date: Mon, 14 Jul 2008 20:56:04 +0000 (+0000) Subject: Refactor Dead Stores error reporting to use the simplified BugReporter::EmitBasicRepo... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8f2698621f5090db1dea691059bd0ebd79fb7f14;p=clang Refactor Dead Stores error reporting to use the simplified BugReporter::EmitBasicReport interface. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@53573 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Analysis/PathSensitive/BugReporter.h b/include/clang/Analysis/PathSensitive/BugReporter.h index 1bbba40418..4374c1e40a 100644 --- a/include/clang/Analysis/PathSensitive/BugReporter.h +++ b/include/clang/Analysis/PathSensitive/BugReporter.h @@ -190,7 +190,18 @@ public: void EmitWarning(BugReport& R); void EmitBasicReport(const char* BugName, const char* BugStr, - SourceLocation Loc); + SourceLocation Loc, + SourceRange* RangeBeg, unsigned NumRanges); + + void EmitBasicReport(const char* BugName, const char* BugStr, + SourceLocation Loc) { + EmitBasicReport(BugName, BugStr, Loc, 0, 0); + } + + void EmitBasicReport(const char* BugName, const char* BugStr, + SourceLocation Loc, SourceRange R) { + EmitBasicReport(BugName, BugStr, Loc, &R, 1); + } static bool classof(const BugReporter* R) { return true; } }; diff --git a/lib/Analysis/BugReporter.cpp b/lib/Analysis/BugReporter.cpp index cbd61f8d25..ed17c9d0cb 100644 --- a/lib/Analysis/BugReporter.cpp +++ b/lib/Analysis/BugReporter.cpp @@ -767,14 +767,15 @@ void BugReporter::EmitWarning(BugReport& R) { void BugReporter::EmitBasicReport(const char* name, const char* str, - SourceLocation Loc) { + SourceLocation Loc, + SourceRange* RBeg, unsigned NumRanges) { SimpleBugType BT(name); DiagCollector C(BT); Diagnostic& Diag = getDiagnostic(); Diag.Report(&C, getContext().getFullLoc(Loc), Diag.getCustomDiagID(Diagnostic::Warning, str), - 0, 0, 0, 0); + 0, 0, RBeg, NumRanges); for (DiagCollector::iterator I = C.begin(), E = C.end(); I != E; ++I) EmitWarning(*I); diff --git a/lib/Analysis/DeadStores.cpp b/lib/Analysis/DeadStores.cpp index ded0ed0d89..0bbd878609 100644 --- a/lib/Analysis/DeadStores.cpp +++ b/lib/Analysis/DeadStores.cpp @@ -28,27 +28,25 @@ namespace { class VISIBILITY_HIDDEN DeadStoreObs : public LiveVariables::ObserverTy { ASTContext &Ctx; - Diagnostic &Diags; - DiagnosticClient &Client; + BugReporter& BR; ParentMap& Parents; public: - DeadStoreObs(ASTContext &ctx, Diagnostic &diags, DiagnosticClient &client, - ParentMap& parents) - : Ctx(ctx), Diags(diags), Client(client), Parents(parents) {} + DeadStoreObs(ASTContext &ctx, BugReporter& br, ParentMap& parents) + : Ctx(ctx), BR(br), Parents(parents) {} virtual ~DeadStoreObs() {} - unsigned GetDiag(VarDecl* VD, bool inEnclosing = false) { - std::string name(VD->getName()); - + void Report(VarDecl* V, bool inEnclosing, SourceLocation L, SourceRange R) { + + std::string name(V->getName()); std::string msg = inEnclosing ? "Although the value stored to '" + name + "' is used in the enclosing expression, the value is never actually" " read from '" + name + "'" : "Value stored to '" + name + "' is never read"; - return Diags.getCustomDiagID(Diagnostic::Warning, msg.c_str()); + BR.EmitBasicReport("dead store", msg.c_str(), L, R); } void CheckVarDecl(VarDecl* VD, Expr* Ex, Expr* Val, @@ -56,12 +54,9 @@ public: const LiveVariables::AnalysisDataTy& AD, const LiveVariables::ValTy& Live) { - if (VD->hasLocalStorage() && !Live(VD, AD)) { - SourceRange R = Val->getSourceRange(); - Diags.Report(&Client, - Ctx.getFullLoc(Ex->getSourceRange().getBegin()), - GetDiag(VD, hasEnclosing), 0, 0, &R, 1); - } + if (VD->hasLocalStorage() && !Live(VD, AD)) + Report(VD, hasEnclosing, Ex->getSourceRange().getBegin(), + Val->getSourceRange()); } void CheckDeclRef(DeclRefExpr* DR, Expr* Val, @@ -119,7 +114,7 @@ public: if (!V) continue; if (V->hasLocalStorage()) - if (Expr* E = V->getInit()) { + if (Expr* E = V->getInit()) if (!Live(V, AD)) { // Special case: check for initializations with constants. // @@ -128,15 +123,9 @@ public: // If x is EVER assigned a new value later, don't issue // a warning. This is because such initialization can be // due to defensive programming. - if (!E->isConstantExpr(Ctx,NULL)) { - // Flag a warning. - SourceRange R = E->getSourceRange(); - Diags.Report(&Client, - Ctx.getFullLoc(V->getLocation()), - GetDiag(V), 0, 0, &R, 1); - } + if (!E->isConstantExpr(Ctx,NULL)) + Report(V, false, V->getLocation(), E->getSourceRange()); } - } } } }; @@ -148,16 +137,6 @@ public: //===----------------------------------------------------------------------===// void clang::CheckDeadStores(LiveVariables& L, BugReporter& BR) { - - SimpleBugType BT("dead store"); - DiagCollector C(BT); - - DeadStoreObs A(BR.getContext(), BR.getDiagnostic(), C, BR.getParentMap()); + DeadStoreObs A(BR.getContext(), BR, BR.getParentMap()); L.runOnAllBlocks(*BR.getCFG(), &A); - - // Emit the bug reports. - - for (DiagCollector::iterator I = C.begin(), E = C.end(); I != E; ++I) - BR.EmitWarning(*I); } -