aaron.ballman added a comment.

In https://reviews.llvm.org/D33672#830492, @gamesh411 wrote:

> As for the the loss of precision problem, in the special case of char the 
> size of char is known. However does the analysis have the necessary 
> information in this stage to know the size of an int for example? I found 
> bit-width specifying information in the llvm::Type class which is used in the 
> code generation phase. It could be done by checking on a per type basis, but 
> then again, it could possibly lead to false positives. Correct me if I am 
> wrong.

The frontend has this information available to it as well, through the 
`ASTContext`. See `getTypeSize()`, `getTypeSizeInChars()`, etc.

Comment at: lib/StaticAnalyzer/Checkers/EnumCastOutOfRangeChecker.cpp:74-75
+  EnumValueVector DeclValues;
+  for (const auto *D : ED->decls()) {
+    const auto ECD = dyn_cast<EnumConstantDecl>(D);
+    DeclValues.push_back(ECD->getInitVal());
aaron.ballman wrote:
> Instead of enumerating over `decls()` and then casting, just enumerate over 
> `enumerators()` and  the cast isn't needed. Or, even better:
> ```
> EnumValueVector DeclValues(ED->enumerator_end() - ED->enumerator_begin());
> std::transform(ED->enumerator_begin(), ED->enumerator_end(), 
> DeclValues.begin(),
>                        [](const EnumConstantDecl *D) { return 
> D->getInitVal(); });
> ```
I think my suggestion was a bit more efficient than your implementation, was 
there a reason you avoided `std::transform()`?

Comment at: lib/StaticAnalyzer/Checkers/EnumCastOutOfRangeChecker.cpp:35
+  const DefinedOrUnknownSVal CompareValue;
+  const ProgramStateRef PS;
You can remove the newline.


cfe-commits mailing list

Reply via email to