Szelethus updated this revision to Diff 158760. Szelethus added a comment. Forgot `git add` and `-U9999`.
https://reviews.llvm.org/D49438 Files: lib/StaticAnalyzer/Checkers/UninitializedObjectChecker.cpp test/Analysis/cxx-uninitialized-object-inheritance.cpp test/Analysis/cxx-uninitialized-object-no-dereference.cpp test/Analysis/cxx-uninitialized-object-notes-as-warnings.cpp test/Analysis/cxx-uninitialized-object-ptr-ref.cpp test/Analysis/cxx-uninitialized-object.cpp
Index: test/Analysis/cxx-uninitialized-object.cpp =================================================================== --- test/Analysis/cxx-uninitialized-object.cpp +++ test/Analysis/cxx-uninitialized-object.cpp @@ -1,6 +1,11 @@ -// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.cplusplus.UninitializedObject -analyzer-config alpha.cplusplus.UninitializedObject:Pedantic=true -std=c++11 -DPEDANTIC -verify %s - -// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.cplusplus.UninitializedObject -std=c++11 -verify %s +// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.cplusplus.UninitializedObject \ +// RUN: -analyzer-config alpha.cplusplus.UninitializedObject:Pedantic=true -DPEDANTIC \ +// RUN: -analyzer-config alpha.cplusplus.UninitializedObject:CheckPointeeInitialization=true \ +// RUN: -std=c++11 -verify %s + +// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.cplusplus.UninitializedObject \ +// RUN: -analyzer-config alpha.cplusplus.UninitializedObject:CheckPointeeInitialization=true \ +// RUN: -std=c++11 -verify %s //===----------------------------------------------------------------------===// // Default constructor test. Index: test/Analysis/cxx-uninitialized-object-ptr-ref.cpp =================================================================== --- test/Analysis/cxx-uninitialized-object-ptr-ref.cpp +++ test/Analysis/cxx-uninitialized-object-ptr-ref.cpp @@ -1,6 +1,11 @@ -// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.cplusplus.UninitializedObject -analyzer-config alpha.cplusplus.UninitializedObject:Pedantic=true -std=c++11 -DPEDANTIC -verify %s - -// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.cplusplus.UninitializedObject -std=c++11 -verify %s +// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.cplusplus.UninitializedObject \ +// RUN: -analyzer-config alpha.cplusplus.UninitializedObject:Pedantic=true -DPEDANTIC \ +// RUN: -analyzer-config alpha.cplusplus.UninitializedObject:CheckPointeeInitialization=true \ +// RUN: -std=c++11 -verify %s + +// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.cplusplus.UninitializedObject \ +// RUN: -analyzer-config alpha.cplusplus.UninitializedObject:CheckPointeeInitialization=true \ +// RUN: -std=c++11 -verify %s //===----------------------------------------------------------------------===// // Concrete location tests. Index: test/Analysis/cxx-uninitialized-object-notes-as-warnings.cpp =================================================================== --- test/Analysis/cxx-uninitialized-object-notes-as-warnings.cpp +++ test/Analysis/cxx-uninitialized-object-notes-as-warnings.cpp @@ -1,4 +1,7 @@ -// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.cplusplus.UninitializedObject -analyzer-config alpha.cplusplus.UninitializedObject:NotesAsWarnings=true -std=c++11 -verify %s +// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.cplusplus.UninitializedObject \ +// RUN: -analyzer-config alpha.cplusplus.UninitializedObject:NotesAsWarnings=true \ +// RUN: -analyzer-config alpha.cplusplus.UninitializedObject:CheckPointeeInitialization=true \ +// RUN: -std=c++11 -verify %s class NotesAsWarningsTest { int a; Index: test/Analysis/cxx-uninitialized-object-no-dereference.cpp =================================================================== --- /dev/null +++ test/Analysis/cxx-uninitialized-object-no-dereference.cpp @@ -0,0 +1,27 @@ +// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.cplusplus.UninitializedObject \ +// RUN: -std=c++11 -DPEDANTIC -verify %s + +class UninitPointerTest { + int *ptr; // expected-note{{uninitialized pointer 'this->ptr'}} + int dontGetFilteredByNonPedanticMode = 0; + +public: + UninitPointerTest() {} // expected-warning{{1 uninitialized field}} +}; + +void fUninitPointerTest() { + UninitPointerTest(); +} + +class UninitPointeeTest { + int *ptr; // no-note + int dontGetFilteredByNonPedanticMode = 0; + +public: + UninitPointeeTest(int *ptr) : ptr(ptr) {} // no-warning +}; + +void fUninitPointeeTest() { + int a; + UninitPointeeTest t(&a); +} Index: test/Analysis/cxx-uninitialized-object-inheritance.cpp =================================================================== --- test/Analysis/cxx-uninitialized-object-inheritance.cpp +++ test/Analysis/cxx-uninitialized-object-inheritance.cpp @@ -1,4 +1,7 @@ -// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.cplusplus.UninitializedObject -analyzer-config alpha.cplusplus.UninitializedObject:Pedantic=true -std=c++11 -verify %s +// RUN: %clang_analyze_cc1 -analyzer-checker=core,alpha.cplusplus.UninitializedObject \ +// RUN: -analyzer-config alpha.cplusplus.UninitializedObject:Pedantic=true -DPEDANTIC \ +// RUN: -analyzer-config alpha.cplusplus.UninitializedObject:CheckPointeeInitialization=true \ +// RUN: -std=c++11 -verify %s //===----------------------------------------------------------------------===// // Non-polymorphic inheritance tests Index: lib/StaticAnalyzer/Checkers/UninitializedObjectChecker.cpp =================================================================== --- lib/StaticAnalyzer/Checkers/UninitializedObjectChecker.cpp +++ lib/StaticAnalyzer/Checkers/UninitializedObjectChecker.cpp @@ -10,7 +10,7 @@ // This file defines a checker that reports uninitialized fields in objects // created after a constructor call. // -// This checker has two options: +// This checker has several options: // - "Pedantic" (boolean). If its not set or is set to false, the checker // won't emit warnings for objects that don't have at least one initialized // field. This may be set with @@ -24,6 +24,13 @@ // // `-analyzer-config alpha.cplusplus.UninitializedObject:NotesAsWarnings=true`. // +// - "CheckPointeeInitialization" (boolean). If set to true, the checker will +// not analyze the pointee of pointer/reference fields, and will only check +// whether tht itself is initialized. Defaults to true. +// +// `-analyzer-config \ +// alpha.cplusplus.UninitializedObject:CheckPointeeInitialization=true`. +// //===----------------------------------------------------------------------===// #include "ClangSACheckers.h" @@ -44,6 +51,7 @@ // These fields will be initialized when registering the checker. bool IsPedantic; bool ShouldConvertNotesToWarnings; + bool CheckPointeeInitialization; UninitializedObjectChecker() : BT_uninitField(new BuiltinBug(this, "Uninitialized fields")) {} @@ -109,13 +117,16 @@ const TypedValueRegion *const ObjectR; const bool IsPedantic; + const bool CheckPointeeInitialization; + bool IsAnyFieldInitialized = false; UninitFieldSet UninitFields; public: FindUninitializedFields(ProgramStateRef State, - const TypedValueRegion *const R, bool IsPedantic); + const TypedValueRegion *const R, bool IsPedantic, + bool CheckPointeeInitialization); const UninitFieldSet &getUninitFields(); private: @@ -261,8 +272,8 @@ if (!Object) return; - FindUninitializedFields F(Context.getState(), Object->getRegion(), - IsPedantic); + FindUninitializedFields F(Context.getState(), Object->getRegion(), IsPedantic, + CheckPointeeInitialization); const UninitFieldSet &UninitFields = F.getUninitFields(); @@ -326,8 +337,10 @@ //===----------------------------------------------------------------------===// FindUninitializedFields::FindUninitializedFields( - ProgramStateRef State, const TypedValueRegion *const R, bool IsPedantic) - : State(State), ObjectR(R), IsPedantic(IsPedantic) {} + ProgramStateRef State, const TypedValueRegion *const R, bool IsPedantic, + bool CheckPointeeInitialization) + : State(State), ObjectR(R), IsPedantic(IsPedantic), + CheckPointeeInitialization(CheckPointeeInitialization) {} const UninitFieldSet &FindUninitializedFields::getUninitFields() { isNonUnionUninit(ObjectR, FieldChainInfo()); @@ -474,6 +487,11 @@ return addFieldToUninits({LocalChain, FR}); } + if (!CheckPointeeInitialization) { + IsAnyFieldInitialized = true; + return false; + } + const FieldDecl *FD = FR->getDecl(); // TODO: The dynamic type of a void pointer may be retrieved with @@ -700,4 +718,6 @@ "Pedantic", /*DefaultVal*/ false, Chk); Chk->ShouldConvertNotesToWarnings = Mgr.getAnalyzerOptions().getBooleanOption( "NotesAsWarnings", /*DefaultVal*/ false, Chk); + Chk->CheckPointeeInitialization = Mgr.getAnalyzerOptions().getBooleanOption( + "CheckPointeeInitialization", /*DefaultVal*/ false, Chk); }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits