Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[analyzer][UninitializedObjectChecker] New flag to ignore records bas…
…ed on it's fields Based on a suggestion from @george.karpenkov. In some cases, structs are used as unions with a help of a tag/kind field. This patch adds a new string flag (a pattern), that is matched against the fields of a record, and should a match be found, the entire record is ignored. For more info refer to http://lists.llvm.org/pipermail/cfe-dev/2018-August/058906.html and to the responses to that, especially http://lists.llvm.org/pipermail/cfe-dev/2018-August/059215.html. Differential Revision: https://reviews.llvm.org/D51680 llvm-svn: 342220
- Loading branch information
Showing
4 changed files
with
182 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
clang/test/Analysis/cxx-uninitialized-object-unionlike-constructs.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// 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:IgnoreRecordsWithField="[Tt]ag|[Kk]ind" \ | ||
// RUN: -std=c++11 -verify %s | ||
|
||
// expected-no-diagnostics | ||
|
||
// Both type and name contains "kind". | ||
struct UnionLikeStruct1 { | ||
enum Kind { | ||
volume, | ||
area | ||
} kind; | ||
|
||
int Volume; | ||
int Area; | ||
|
||
UnionLikeStruct1(Kind kind, int Val) : kind(kind) { | ||
switch (kind) { | ||
case volume: | ||
Volume = Val; | ||
break; | ||
case area: | ||
Area = Val; | ||
break; | ||
} | ||
} | ||
}; | ||
|
||
void fUnionLikeStruct1() { | ||
UnionLikeStruct1 t(UnionLikeStruct1::volume, 10); | ||
} | ||
|
||
// Only name contains "kind". | ||
struct UnionLikeStruct2 { | ||
enum Type { | ||
volume, | ||
area | ||
} kind; | ||
|
||
int Volume; | ||
int Area; | ||
|
||
UnionLikeStruct2(Type kind, int Val) : kind(kind) { | ||
switch (kind) { | ||
case volume: | ||
Volume = Val; | ||
break; | ||
case area: | ||
Area = Val; | ||
break; | ||
} | ||
} | ||
}; | ||
|
||
void fUnionLikeStruct2() { | ||
UnionLikeStruct2 t(UnionLikeStruct2::volume, 10); | ||
} | ||
|
||
// Only type contains "kind". | ||
struct UnionLikeStruct3 { | ||
enum Kind { | ||
volume, | ||
area | ||
} type; | ||
|
||
int Volume; | ||
int Area; | ||
|
||
UnionLikeStruct3(Kind type, int Val) : type(type) { | ||
switch (type) { | ||
case volume: | ||
Volume = Val; | ||
break; | ||
case area: | ||
Area = Val; | ||
break; | ||
} | ||
} | ||
}; | ||
|
||
void fUnionLikeStruct3() { | ||
UnionLikeStruct3 t(UnionLikeStruct3::volume, 10); | ||
} | ||
|
||
// Only type contains "tag". | ||
struct UnionLikeStruct4 { | ||
enum Tag { | ||
volume, | ||
area | ||
} type; | ||
|
||
int Volume; | ||
int Area; | ||
|
||
UnionLikeStruct4(Tag type, int Val) : type(type) { | ||
switch (type) { | ||
case volume: | ||
Volume = Val; | ||
break; | ||
case area: | ||
Area = Val; | ||
break; | ||
} | ||
} | ||
}; | ||
|
||
void fUnionLikeStruct4() { | ||
UnionLikeStruct4 t(UnionLikeStruct4::volume, 10); | ||
} | ||
|
||
// Both name and type name contains but does not equal to tag/kind. | ||
struct UnionLikeStruct5 { | ||
enum WhateverTagBlahBlah { | ||
volume, | ||
area | ||
} FunnyKindName; | ||
|
||
int Volume; | ||
int Area; | ||
|
||
UnionLikeStruct5(WhateverTagBlahBlah type, int Val) : FunnyKindName(type) { | ||
switch (FunnyKindName) { | ||
case volume: | ||
Volume = Val; | ||
break; | ||
case area: | ||
Area = Val; | ||
break; | ||
} | ||
} | ||
}; | ||
|
||
void fUnionLikeStruct5() { | ||
UnionLikeStruct5 t(UnionLikeStruct5::volume, 10); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters