[cfe-commits] r129431 - in /cfe/trunk: lib/Analysis/UninitializedValues.cpp test/SemaCXX/uninit-variables.cpp

Ted Kremenek kremenek at apple.com
Wed Apr 13 18:54:14 PDT 2011


Once you add the CFG support for typeid, with the subexpression forced to be a block-level expression, this logic won't be needed anymore.

On Apr 13, 2011, at 1:18 AM, Chandler Carruth wrote:

> Author: chandlerc
> Date: Wed Apr 13 03:18:42 2011
> New Revision: 129431
> 
> URL: http://llvm.org/viewvc/llvm-project?rev=129431&view=rev
> Log:
> Teach -Wuninitialized about C++'s typeid expression, including both the
> evaluated and unevaluated contexts. Add some testing of sizeof and
> typeid.
> 
> Both of the typeid tests added here were triggering warnings previously.
> Now the one false positive is suppressed without suppressing the warning
> on actually buggy code.
> 
> Modified:
>    cfe/trunk/lib/Analysis/UninitializedValues.cpp
>    cfe/trunk/test/SemaCXX/uninit-variables.cpp
> 
> Modified: cfe/trunk/lib/Analysis/UninitializedValues.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/UninitializedValues.cpp?rev=129431&r1=129430&r2=129431&view=diff
> ==============================================================================
> --- cfe/trunk/lib/Analysis/UninitializedValues.cpp (original)
> +++ cfe/trunk/lib/Analysis/UninitializedValues.cpp Wed Apr 13 03:18:42 2011
> @@ -390,6 +390,7 @@
>   void VisitBinaryOperator(BinaryOperator *bo);
>   void VisitCastExpr(CastExpr *ce);
>   void VisitUnaryExprOrTypeTraitExpr(UnaryExprOrTypeTraitExpr *se);
> +  void VisitCXXTypeidExpr(CXXTypeidExpr *E);
>   void BlockStmt_VisitObjCForCollectionStmt(ObjCForCollectionStmt *fs);
> 
>   bool isTrackedVar(const VarDecl *vd) {
> @@ -618,6 +619,17 @@
>   }
> }
> 
> +void TransferFunctions::VisitCXXTypeidExpr(CXXTypeidExpr *E) {
> +  // typeid(expression) is potentially evaluated when the argument is
> +  // a glvalue of polymorphic type. (C++ 5.2.8p2-3)
> +  if (!E->isTypeOperand() && E->Classify(ac.getASTContext()).isGLValue()) {
> +    QualType SubExprTy = E->getExprOperand()->getType();
> +    if (const RecordType *Record = SubExprTy->getAs<RecordType>())
> +      if (cast<CXXRecordDecl>(Record->getDecl())->isPolymorphic())
> +        Visit(E->getExprOperand());
> +  }
> +}
> +
> //------------------------------------------------------------------------====//
> // High-level "driver" logic for uninitialized values analysis.
> //====------------------------------------------------------------------------//
> 
> Modified: cfe/trunk/test/SemaCXX/uninit-variables.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/uninit-variables.cpp?rev=129431&r1=129430&r2=129431&view=diff
> ==============================================================================
> --- cfe/trunk/test/SemaCXX/uninit-variables.cpp (original)
> +++ cfe/trunk/test/SemaCXX/uninit-variables.cpp Wed Apr 13 03:18:42 2011
> @@ -1,5 +1,8 @@
> // RUN: %clang_cc1 -fsyntax-only -Wuninitialized -fsyntax-only -fcxx-exceptions %s -verify
> 
> +// Stub out types for 'typeid' to work.
> +namespace std { class type_info {}; }
> +
> int test1_aux(int &x);
> int test1() {
>   int x;
> @@ -13,6 +16,20 @@
>   return x; // no-warning
> }
> 
> +// Don't warn on unevaluated contexts.
> +void unevaluated_tests() {
> +  int x;
> +  (void)sizeof(x);
> +  (void)typeid(x);
> +}
> +
> +// Warn for glvalue arguments to typeid whose type is polymorphic.
> +struct A { virtual ~A() {} };
> +void polymorphic_test() {
> +  A *a; // expected-note{{declared here}} expected-note{{add initialization}}
> +  (void)typeid(*a); // expected-warning{{variable 'a' is uninitialized when used here }}
> +}
> +
> // Handle cases where the CFG may constant fold some branches, thus
> // mitigating the need for some path-sensitivity in the analysis.
> unsigned test3_aux();
> 
> 
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits




More information about the cfe-commits mailing list