[cfe-commits] r128843 - in /cfe/trunk: lib/Sema/AnalysisBasedWarnings.cpp test/Sema/uninit-variables.c
Ted Kremenek
kremenek at apple.com
Mon Apr 4 13:56:00 PDT 2011
Author: kremenek
Date: Mon Apr 4 15:56:00 2011
New Revision: 128843
URL: http://llvm.org/viewvc/llvm-project?rev=128843&view=rev
Log:
-Wuninitialized: use "self-init" warning when issue uninitialized values warnings from the dataflow analysis that include within the initializer of a variable.
Modified:
cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp
cfe/trunk/test/Sema/uninit-variables.c
Modified: cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp?rev=128843&r1=128842&r2=128843&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp (original)
+++ cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp Mon Apr 4 15:56:00 2011
@@ -24,6 +24,7 @@
#include "clang/AST/ExprCXX.h"
#include "clang/AST/StmtObjC.h"
#include "clang/AST/StmtCXX.h"
+#include "clang/AST/EvaluatedExprVisitor.h"
#include "clang/Analysis/AnalysisContext.h"
#include "clang/Analysis/CFG.h"
#include "clang/Analysis/Analyses/ReachableCode.h"
@@ -377,6 +378,45 @@
// -Wuninitialized
//===----------------------------------------------------------------------===//
+namespace {
+class ContainsReference : public EvaluatedExprVisitor<ContainsReference> {
+ bool containsReference;
+ const DeclRefExpr *dr;
+public:
+ ContainsReference(ASTContext &context,
+ const DeclRefExpr *dr) :
+ EvaluatedExprVisitor<ContainsReference>(context),
+ containsReference(false), dr(dr) {}
+
+ void VisitExpr(Expr *e) {
+ // Stop evaluating if we already have a reference.
+ if (containsReference)
+ return;
+
+ EvaluatedExprVisitor<ContainsReference>::VisitExpr(e);
+ }
+
+ void VisitDeclRefExpr(DeclRefExpr *e) {
+ if (e == dr)
+ containsReference = true;
+ else
+ EvaluatedExprVisitor<ContainsReference>::VisitDeclRefExpr(e);
+ }
+
+ bool doesContainReference() const { return containsReference; }
+};
+}
+
+static bool isSelfInit(ASTContext &context,
+ const VarDecl *vd, const DeclRefExpr *dr) {
+ if (const Expr *exp = vd->getInit()) {
+ ContainsReference contains(context, dr);
+ contains.Visit(const_cast<Expr*>(exp));
+ return contains.doesContainReference();
+ }
+ return false;
+}
+
typedef std::pair<const Expr*, bool> UninitUse;
namespace {
@@ -432,8 +472,11 @@
const bool isAlwaysUninit = vi->second;
if (const DeclRefExpr *dr = dyn_cast<DeclRefExpr>(vi->first)) {
S.Diag(dr->getLocStart(),
- isAlwaysUninit ? diag::warn_uninit_var
- : diag::warn_maybe_uninit_var)
+ isAlwaysUninit ?
+ (isSelfInit(S.Context, vd, dr)
+ ? diag::warn_uninit_self_reference_in_init
+ : diag::warn_uninit_var)
+ : diag::warn_maybe_uninit_var)
<< vd->getDeclName() << dr->getSourceRange();
}
else {
Modified: cfe/trunk/test/Sema/uninit-variables.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/uninit-variables.c?rev=128843&r1=128842&r2=128843&view=diff
==============================================================================
--- cfe/trunk/test/Sema/uninit-variables.c (original)
+++ cfe/trunk/test/Sema/uninit-variables.c Mon Apr 4 15:56:00 2011
@@ -92,7 +92,7 @@
}
void test15() {
- int x = x; // expected-warning{{variable 'x' is possibly uninitialized when used here}} expected-note{{variable 'x' is declared here}}
+ int x = x; // expected-warning{{variable 'x' is uninitialized when used within its own initialization}} expected-note{{variable 'x' is declared here}}
}
// Don't warn in the following example; shows dataflow confluence.
More information about the cfe-commits
mailing list