Skip to content

Commit 06eedff

Browse files
authored
[analyzer] Use explicit call description mode in iterator checkers (llvm#88913)
This commit explicitly specifies the matching mode (C library function, any non-method function, or C++ method) for the `CallDescription`s constructed in the iterator/container checkers. This change won't cause major functional changes, but isn't NFC because it ensures that e.g. call descriptions for a non-method function won't accidentally match a method that has the same name. Separate commits will perform (or have already performed) this change in other checkers. My goal is to ensure that the call description mode is always explicitly specified and eliminate (or strongly restrict) the vague "may be either a method or a simple function" mode that's the current default. I'm handling the iterator checkers in this separate commit because they're infamously complex; but I don't expect any trouble because this transition doesn't interact with the "central" logic of iterator handling.
1 parent c8dca5b commit 06eedff

File tree

6 files changed

+80
-48
lines changed

6 files changed

+80
-48
lines changed

clang/lib/StaticAnalyzer/Checkers/ContainerModeling.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,26 +72,31 @@ class ContainerModeling
7272
SVal) const;
7373

7474
CallDescriptionMap<NoItParamFn> NoIterParamFunctions = {
75-
{{{"clear"}, 0}, &ContainerModeling::handleClear},
76-
{{{"assign"}, 2}, &ContainerModeling::handleAssign},
77-
{{{"push_back"}, 1}, &ContainerModeling::handlePushBack},
78-
{{{"emplace_back"}, 1}, &ContainerModeling::handlePushBack},
79-
{{{"pop_back"}, 0}, &ContainerModeling::handlePopBack},
80-
{{{"push_front"}, 1}, &ContainerModeling::handlePushFront},
81-
{{{"emplace_front"}, 1}, &ContainerModeling::handlePushFront},
82-
{{{"pop_front"}, 0}, &ContainerModeling::handlePopFront},
75+
{{CDM::CXXMethod, {"clear"}, 0}, &ContainerModeling::handleClear},
76+
{{CDM::CXXMethod, {"assign"}, 2}, &ContainerModeling::handleAssign},
77+
{{CDM::CXXMethod, {"push_back"}, 1}, &ContainerModeling::handlePushBack},
78+
{{CDM::CXXMethod, {"emplace_back"}, 1},
79+
&ContainerModeling::handlePushBack},
80+
{{CDM::CXXMethod, {"pop_back"}, 0}, &ContainerModeling::handlePopBack},
81+
{{CDM::CXXMethod, {"push_front"}, 1},
82+
&ContainerModeling::handlePushFront},
83+
{{CDM::CXXMethod, {"emplace_front"}, 1},
84+
&ContainerModeling::handlePushFront},
85+
{{CDM::CXXMethod, {"pop_front"}, 0}, &ContainerModeling::handlePopFront},
8386
};
8487

8588
CallDescriptionMap<OneItParamFn> OneIterParamFunctions = {
86-
{{{"insert"}, 2}, &ContainerModeling::handleInsert},
87-
{{{"emplace"}, 2}, &ContainerModeling::handleInsert},
88-
{{{"erase"}, 1}, &ContainerModeling::handleErase},
89-
{{{"erase_after"}, 1}, &ContainerModeling::handleEraseAfter},
89+
{{CDM::CXXMethod, {"insert"}, 2}, &ContainerModeling::handleInsert},
90+
{{CDM::CXXMethod, {"emplace"}, 2}, &ContainerModeling::handleInsert},
91+
{{CDM::CXXMethod, {"erase"}, 1}, &ContainerModeling::handleErase},
92+
{{CDM::CXXMethod, {"erase_after"}, 1},
93+
&ContainerModeling::handleEraseAfter},
9094
};
9195

9296
CallDescriptionMap<TwoItParamFn> TwoIterParamFunctions = {
93-
{{{"erase"}, 2}, &ContainerModeling::handleErase},
94-
{{{"erase_after"}, 2}, &ContainerModeling::handleEraseAfter},
97+
{{CDM::CXXMethod, {"erase"}, 2}, &ContainerModeling::handleErase},
98+
{{CDM::CXXMethod, {"erase_after"}, 2},
99+
&ContainerModeling::handleEraseAfter},
95100
};
96101
};
97102

clang/lib/StaticAnalyzer/Checkers/DebugContainerModeling.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ class DebugContainerModeling
4242
CheckerContext &) const;
4343

4444
CallDescriptionMap<FnCheck> Callbacks = {
45-
{{{"clang_analyzer_container_begin"}, 1},
45+
{{CDM::SimpleFunc, {"clang_analyzer_container_begin"}, 1},
4646
&DebugContainerModeling::analyzerContainerBegin},
47-
{{{"clang_analyzer_container_end"}, 1},
47+
{{CDM::SimpleFunc, {"clang_analyzer_container_end"}, 1},
4848
&DebugContainerModeling::analyzerContainerEnd},
4949
};
5050

clang/lib/StaticAnalyzer/Checkers/DebugIteratorModeling.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ class DebugIteratorModeling
4343
CheckerContext &) const;
4444

4545
CallDescriptionMap<FnCheck> Callbacks = {
46-
{{{"clang_analyzer_iterator_position"}, 1},
46+
{{CDM::SimpleFunc, {"clang_analyzer_iterator_position"}, 1},
4747
&DebugIteratorModeling::analyzerIteratorPosition},
48-
{{{"clang_analyzer_iterator_container"}, 1},
48+
{{CDM::SimpleFunc, {"clang_analyzer_iterator_container"}, 1},
4949
&DebugIteratorModeling::analyzerIteratorContainer},
50-
{{{"clang_analyzer_iterator_validity"}, 1},
50+
{{CDM::SimpleFunc, {"clang_analyzer_iterator_validity"}, 1},
5151
&DebugIteratorModeling::analyzerIteratorValidity},
5252
};
5353

clang/lib/StaticAnalyzer/Checkers/IteratorModeling.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,19 +129,20 @@ class IteratorModeling
129129
CallDescriptionMap<AdvanceFn> AdvanceLikeFunctions = {
130130
// template<class InputIt, class Distance>
131131
// void advance(InputIt& it, Distance n);
132-
{{{"std", "advance"}, 2}, &IteratorModeling::handleAdvance},
132+
{{CDM::SimpleFunc, {"std", "advance"}, 2},
133+
&IteratorModeling::handleAdvance},
133134

134135
// template<class BidirIt>
135136
// BidirIt prev(
136137
// BidirIt it,
137138
// typename std::iterator_traits<BidirIt>::difference_type n = 1);
138-
{{{"std", "prev"}, 2}, &IteratorModeling::handlePrev},
139+
{{CDM::SimpleFunc, {"std", "prev"}, 2}, &IteratorModeling::handlePrev},
139140

140141
// template<class ForwardIt>
141142
// ForwardIt next(
142143
// ForwardIt it,
143144
// typename std::iterator_traits<ForwardIt>::difference_type n = 1);
144-
{{{"std", "next"}, 2}, &IteratorModeling::handleNext},
145+
{{CDM::SimpleFunc, {"std", "next"}, 2}, &IteratorModeling::handleNext},
145146
};
146147

147148
public:

clang/lib/StaticAnalyzer/Checkers/IteratorRangeChecker.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,15 @@ class IteratorRangeChecker
5656
using AdvanceFn = void (IteratorRangeChecker::*)(CheckerContext &, SVal,
5757
SVal) const;
5858

59+
// FIXME: these three functions are also listed in IteratorModeling.cpp,
60+
// perhaps unify their handling?
5961
CallDescriptionMap<AdvanceFn> AdvanceFunctions = {
60-
{{{"std", "advance"}, 2}, &IteratorRangeChecker::verifyAdvance},
61-
{{{"std", "prev"}, 2}, &IteratorRangeChecker::verifyPrev},
62-
{{{"std", "next"}, 2}, &IteratorRangeChecker::verifyNext},
62+
{{CDM::SimpleFunc, {"std", "advance"}, 2},
63+
&IteratorRangeChecker::verifyAdvance},
64+
{{CDM::SimpleFunc, {"std", "prev"}, 2},
65+
&IteratorRangeChecker::verifyPrev},
66+
{{CDM::SimpleFunc, {"std", "next"}, 2},
67+
&IteratorRangeChecker::verifyNext},
6368
};
6469
};
6570

clang/lib/StaticAnalyzer/Checkers/STLAlgorithmModeling.cpp

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,50 @@ class STLAlgorithmModeling : public Checker<eval::Call> {
3333
const CallExpr *) const;
3434

3535
const CallDescriptionMap<FnCheck> Callbacks = {
36-
{{{"std", "find"}, 3}, &STLAlgorithmModeling::evalFind},
37-
{{{"std", "find"}, 4}, &STLAlgorithmModeling::evalFind},
38-
{{{"std", "find_if"}, 3}, &STLAlgorithmModeling::evalFind},
39-
{{{"std", "find_if"}, 4}, &STLAlgorithmModeling::evalFind},
40-
{{{"std", "find_if_not"}, 3}, &STLAlgorithmModeling::evalFind},
41-
{{{"std", "find_if_not"}, 4}, &STLAlgorithmModeling::evalFind},
42-
{{{"std", "find_first_of"}, 4}, &STLAlgorithmModeling::evalFind},
43-
{{{"std", "find_first_of"}, 5}, &STLAlgorithmModeling::evalFind},
44-
{{{"std", "find_first_of"}, 6}, &STLAlgorithmModeling::evalFind},
45-
{{{"std", "find_end"}, 4}, &STLAlgorithmModeling::evalFind},
46-
{{{"std", "find_end"}, 5}, &STLAlgorithmModeling::evalFind},
47-
{{{"std", "find_end"}, 6}, &STLAlgorithmModeling::evalFind},
48-
{{{"std", "lower_bound"}, 3}, &STLAlgorithmModeling::evalFind},
49-
{{{"std", "lower_bound"}, 4}, &STLAlgorithmModeling::evalFind},
50-
{{{"std", "upper_bound"}, 3}, &STLAlgorithmModeling::evalFind},
51-
{{{"std", "upper_bound"}, 4}, &STLAlgorithmModeling::evalFind},
52-
{{{"std", "search"}, 3}, &STLAlgorithmModeling::evalFind},
53-
{{{"std", "search"}, 4}, &STLAlgorithmModeling::evalFind},
54-
{{{"std", "search"}, 5}, &STLAlgorithmModeling::evalFind},
55-
{{{"std", "search"}, 6}, &STLAlgorithmModeling::evalFind},
56-
{{{"std", "search_n"}, 4}, &STLAlgorithmModeling::evalFind},
57-
{{{"std", "search_n"}, 5}, &STLAlgorithmModeling::evalFind},
58-
{{{"std", "search_n"}, 6}, &STLAlgorithmModeling::evalFind},
36+
{{CDM::SimpleFunc, {"std", "find"}, 3}, &STLAlgorithmModeling::evalFind},
37+
{{CDM::SimpleFunc, {"std", "find"}, 4}, &STLAlgorithmModeling::evalFind},
38+
{{CDM::SimpleFunc, {"std", "find_if"}, 3},
39+
&STLAlgorithmModeling::evalFind},
40+
{{CDM::SimpleFunc, {"std", "find_if"}, 4},
41+
&STLAlgorithmModeling::evalFind},
42+
{{CDM::SimpleFunc, {"std", "find_if_not"}, 3},
43+
&STLAlgorithmModeling::evalFind},
44+
{{CDM::SimpleFunc, {"std", "find_if_not"}, 4},
45+
&STLAlgorithmModeling::evalFind},
46+
{{CDM::SimpleFunc, {"std", "find_first_of"}, 4},
47+
&STLAlgorithmModeling::evalFind},
48+
{{CDM::SimpleFunc, {"std", "find_first_of"}, 5},
49+
&STLAlgorithmModeling::evalFind},
50+
{{CDM::SimpleFunc, {"std", "find_first_of"}, 6},
51+
&STLAlgorithmModeling::evalFind},
52+
{{CDM::SimpleFunc, {"std", "find_end"}, 4},
53+
&STLAlgorithmModeling::evalFind},
54+
{{CDM::SimpleFunc, {"std", "find_end"}, 5},
55+
&STLAlgorithmModeling::evalFind},
56+
{{CDM::SimpleFunc, {"std", "find_end"}, 6},
57+
&STLAlgorithmModeling::evalFind},
58+
{{CDM::SimpleFunc, {"std", "lower_bound"}, 3},
59+
&STLAlgorithmModeling::evalFind},
60+
{{CDM::SimpleFunc, {"std", "lower_bound"}, 4},
61+
&STLAlgorithmModeling::evalFind},
62+
{{CDM::SimpleFunc, {"std", "upper_bound"}, 3},
63+
&STLAlgorithmModeling::evalFind},
64+
{{CDM::SimpleFunc, {"std", "upper_bound"}, 4},
65+
&STLAlgorithmModeling::evalFind},
66+
{{CDM::SimpleFunc, {"std", "search"}, 3},
67+
&STLAlgorithmModeling::evalFind},
68+
{{CDM::SimpleFunc, {"std", "search"}, 4},
69+
&STLAlgorithmModeling::evalFind},
70+
{{CDM::SimpleFunc, {"std", "search"}, 5},
71+
&STLAlgorithmModeling::evalFind},
72+
{{CDM::SimpleFunc, {"std", "search"}, 6},
73+
&STLAlgorithmModeling::evalFind},
74+
{{CDM::SimpleFunc, {"std", "search_n"}, 4},
75+
&STLAlgorithmModeling::evalFind},
76+
{{CDM::SimpleFunc, {"std", "search_n"}, 5},
77+
&STLAlgorithmModeling::evalFind},
78+
{{CDM::SimpleFunc, {"std", "search_n"}, 6},
79+
&STLAlgorithmModeling::evalFind},
5980
};
6081

6182
public:

0 commit comments

Comments
 (0)