From 2551fbe928355be9359d080a531e0b21af146fb8 Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Thu, 17 Mar 2011 05:29:57 +0000 Subject: [PATCH] Don't construct two CFGs just to run -Wuninitialized. While this causes new warnings to be flagged under -Wconditional-uninitialized, this is something we can improve over time. llvm-svn: 127802 --- clang/lib/Sema/AnalysisBasedWarnings.cpp | 19 +-------------- .../SemaCXX/uninit-variables-conditional.cpp | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 18 deletions(-) create mode 100644 clang/test/SemaCXX/uninit-variables-conditional.cpp diff --git a/clang/lib/Sema/AnalysisBasedWarnings.cpp b/clang/lib/Sema/AnalysisBasedWarnings.cpp index 3d00c7fe7e08..6522d95ec3c0 100644 --- a/clang/lib/Sema/AnalysisBasedWarnings.cpp +++ b/clang/lib/Sema/AnalysisBasedWarnings.cpp @@ -611,24 +611,7 @@ AnalysisBasedWarnings::IssueWarnings(sema::AnalysisBasedWarnings::Policy P, != Diagnostic::Ignored || Diags.getDiagnosticLevel(diag::warn_maybe_uninit_var, D->getLocStart()) != Diagnostic::Ignored) { - ASTContext &ctx = D->getASTContext(); - llvm::OwningPtr tmpCFG; - bool useAlternateCFG = false; - if (ctx.getLangOptions().CPlusPlus) { - // Temporary workaround: implicit dtors in the CFG can confuse - // the path-sensitivity in the uninitialized values analysis. - // For now create (if necessary) a separate CFG without implicit dtors. - // FIXME: We should not need to do this, as it results in multiple - // CFGs getting constructed. - CFG::BuildOptions B; - B.AddEHEdges = false; - B.AddImplicitDtors = false; - B.AddInitializers = true; - tmpCFG.reset(CFG::buildCFG(D, AC.getBody(), &ctx, B)); - useAlternateCFG = true; - } - CFG *cfg = useAlternateCFG ? tmpCFG.get() : AC.getCFG(); - if (cfg) { + if (CFG *cfg = AC.getCFG()) { UninitValsDiagReporter reporter(S); runUninitializedVariablesAnalysis(*cast(D), *cfg, AC, reporter); diff --git a/clang/test/SemaCXX/uninit-variables-conditional.cpp b/clang/test/SemaCXX/uninit-variables-conditional.cpp new file mode 100644 index 000000000000..1a822853d948 --- /dev/null +++ b/clang/test/SemaCXX/uninit-variables-conditional.cpp @@ -0,0 +1,23 @@ +// RUN: %clang_cc1 -fsyntax-only -Wconditional-uninitialized -fsyntax-only %s -verify + +class Foo { +public: + Foo(); + ~Foo(); + operator bool(); +}; + +int bar(); +int baz(); +int init(double *); + +// This case flags a false positive under -Wconditional-uninitialized because +// the destructor in Foo fouls about the minor bit of path-sensitivity in +// -Wuninitialized. +double test() { + double x; // expected-note {{variable 'x' is declared here}} expected-note{{add initialization to silence this warning}} + if (bar() || baz() || Foo() || init(&x)) { + return x; // expected-warning {{variable 'x' is possibly uninitialized when used here}} + } + return 1.0; +}