This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] safety-no-vector-bool
AbandonedPublic

Authored by jbcoe on Jan 25 2017, 1:27 AM.

Details

Summary

Add a new clang-tidy module for safety-critical checks.

Include a check which finds instances of vector<bool> which is banned by 'High Integrity C++'.

Diff Detail

Event Timeline

jbcoe created this revision.Jan 25 2017, 1:27 AM

i think the test is not mentioned in the release notes is it?

JonasToth added inline comments.Jan 25 2017, 2:24 AM
clang-tools-extra/clang-tidy/safety/NoVectorBoolCheck.cpp
51

i think all those diag() calls can be merged into one. inside the if/else-if you can just set a StringRef with the specific part of the warning, and have a parameterized diag() at the end of the function.

in NoMallocCheck there is a similar pattern:

const CallExpr *Call = nullptr;                                              
StringRef Recommendation;                                                    
                                                                             
if ((Call = Result.Nodes.getNodeAs<CallExpr>("aquisition")))                 
  Recommendation = "consider a container or a smart pointer";                
else if ((Call = Result.Nodes.getNodeAs<CallExpr>("realloc")))               
  Recommendation = "consider std::vector or std::string";                    
else if ((Call = Result.Nodes.getNodeAs<CallExpr>("free")))                  
  Recommendation = "use RAII";                                               
                                                                             
assert(Call && "Unhandled binding in the Matcher");                          
                                                                             
diag(Call->getLocStart(), "do not manage memory manually; %0")               
    << Recommendation << SourceRange(Call->getLocStart(), Call->getLocEnd());
53

maybe an safety else with an failing assert, so you can see that unexpected behaviour, see comment above

clang-tools-extra/test/clang-tidy/safety-no-vector-bool.cpp
38

what happens for types where std::vector<bool> would be an template argument? for example std::pair and tuple could contain a vector<bool>.
is there a warning as well?

djehuti added inline comments.
clang-tools-extra/clang-tidy/safety/NoVectorBoolCheck.cpp
51

Except with braces, right? (That's another High-Integrity C++ rule btw.) ;)

Please mention new checks group in docs/clang-tidy/index.rst (in alphabetical order).

Please mention this check in docs/ReleaseNotes.rst (see previous releases as example).

clang-tools-extra/clang-tidy/safety/NoVectorBoolCheck.cpp
22

Unnecessary empty line.

clang-tools-extra/docs/clang-tidy/checks/safety-no-vector-bool.rst
8

may be `std::vector` will be better?

Eugene.Zelenko set the repository for this revision to rL LLVM.
Eugene.Zelenko added a subscriber: cfe-commits.
jbcoe updated this revision to Diff 85967.Jan 26 2017, 2:24 PM
jbcoe marked 2 inline comments as done.

Responses to some change requests. Input needed on template argument matcher.

clang-tools-extra/clang-tidy/safety/NoVectorBoolCheck.cpp
51

I agree that this _can_ be done but I'm not convinced it helps readability. Repetition is partial and very localized. I'll happily make the change if you feel strongly that it's an improvement.

clang-tools-extra/test/clang-tidy/safety-no-vector-bool.cpp
38

Nicely spotted. Those won't get picked up right now and need to be.

I'm struggling to build a matcher for this. We really need to find any place where std::vector<bool> is used as a template argument.

JonasToth added inline comments.Jan 27 2017, 7:21 AM
clang-tools-extra/clang-tidy/safety/NoVectorBoolCheck.cpp
51

i think either is ok. maybe someone else prefers one strongly over the other, but i dont mind.

but i think the else path should exist, make an failing assert or sth like that, for the safety ;)

clang-tools-extra/test/clang-tidy/safety-no-vector-bool.cpp
38

i found hasAnyTemplateArgument in the ast matcher refrence. did u use that one?

djehuti added inline comments.Jan 27 2017, 7:54 AM
clang-tools-extra/clang-tidy/safety/NoVectorBoolCheck.cpp
53

You mean like High Integrity C++ rule 6.1.2? 😉

jbcoe marked 5 inline comments as done.Jan 29 2017, 2:18 PM

Handling code like std::pair<std::vector<bool>, int> is currently eluding me.

If I define std::pair to have members first and second then std::pair<std::vector<bool>, int> triggers the vector<bool> diagnostic but in the template, not the instantiation point.

I need to match class definitions that have vector<bool> as a template argument and exclude them from later matchers. I can't see how to do this right now. I suspect some more sophisticated matchers or approaches are needed.

clang-tools-extra/clang-tidy/safety/NoVectorBoolCheck.cpp
51

Agreed on the else, especially as it agrees with the safety-critical standards we're checking.

51

LLVM/Clang tends not to use braces for single statement control flow, but maybe we should follow safety-critical rules in a safety-critical check?

jbcoe abandoned this revision.Jan 29 2017, 2:22 PM
jbcoe marked 2 inline comments as done.
jbcoe updated this revision to Diff 86225.Jan 29 2017, 2:36 PM

Minor changes in response to review comments.

Patch abandoned as approach taken cannot handle templates.

jbcoe abandoned this revision.Feb 1 2017, 10:59 AM

Posting a comment re-opened this patch. The approach taken cannot handle templates using std::vector<bool>, a radical rework is needed.

https://reviews.llvm.org/D29267 introduces the safety critical module with a much simpler check (no assembler).