Skip to content

Commit 8437c7c

Browse files
committed
Deviation: Refactor begin/end for clarity.
1 parent 665a7d1 commit 8437c7c

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

cpp/common/src/codingstandards/cpp/deviations/CodeIdentifierDeviation.qll

+23-13
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,14 @@ private predicate hasDeviationCommentFileOrdering(
141141
)
142142
}
143143

144-
private predicate mkBeginStack(DeviationRecord record, File file, BeginStack stack, int index) {
144+
/**
145+
* Calculate the stack of deviation begin markers related to the given deviation record, in the given file,
146+
* at the given `markerRecordFileIndex` into the list of deviation markers for that record in that file.
147+
*/
148+
private BeginStack calculateBeginStack(DeviationRecord record, File file, int markerRecordFileIndex) {
145149
// Stack is empty at the start
146-
index = 0 and
147-
stack = TEmptyBeginStack() and
150+
markerRecordFileIndex = 0 and
151+
result = TEmptyBeginStack() and
148152
// Only initialize when there is at least one such comment marker for this file and record
149153
// pairing
150154
exists(CommentDeviationRangeMarker marker |
@@ -154,36 +158,42 @@ private predicate mkBeginStack(DeviationRecord record, File file, BeginStack sta
154158
// Next token is begin, so push it to the stack
155159
exists(DeviationBegin begin, BeginStack prev |
156160
record = begin.getRecord() and
157-
hasDeviationCommentFileOrdering(record, begin, file, index) and
158-
mkBeginStack(record, file, prev, index - 1) and
159-
stack = TConsBeginStack(begin, prev)
161+
hasDeviationCommentFileOrdering(record, begin, file, markerRecordFileIndex) and
162+
prev = calculateBeginStack(record, file, markerRecordFileIndex - 1) and
163+
result = TConsBeginStack(begin, prev)
160164
)
161165
or
162166
// Next token is end
163167
exists(DeviationEnd end, BeginStack prevStack |
164168
record = end.getRecord() and
165-
hasDeviationCommentFileOrdering(record, end, file, index) and
166-
mkBeginStack(record, file, prevStack, index - 1)
169+
hasDeviationCommentFileOrdering(record, end, file, markerRecordFileIndex) and
170+
prevStack = calculateBeginStack(record, file, markerRecordFileIndex - 1)
167171
|
168172
// There is, so pop the most recent begin off the stack
169-
prevStack = TConsBeginStack(_, stack)
173+
prevStack = TConsBeginStack(_, result)
170174
or
171-
// Error, no begin on the stack, ignore and continue
175+
// Error, no begin on the stack, ignore the end and continue
172176
prevStack = TEmptyBeginStack() and
173-
stack = TEmptyBeginStack()
177+
result = TEmptyBeginStack()
174178
)
175179
}
176180

177181
newtype TBeginStack =
178182
TConsBeginStack(DeviationBegin begin, TBeginStack prev) {
179183
exists(File file, int index |
180184
hasDeviationCommentFileOrdering(begin.getRecord(), begin, file, index) and
181-
mkBeginStack(begin.getRecord(), file, prev, index - 1)
185+
prev = calculateBeginStack(begin.getRecord(), file, index - 1)
182186
)
183187
} or
184188
TEmptyBeginStack()
185189

190+
/**
191+
* A stack of begin markers that occur in the same file, referring to the same record.
192+
*/
186193
private class BeginStack extends TBeginStack {
194+
/** Gets the top begin marker on the stack. */
195+
DeviationBegin peek() { this = TConsBeginStack(result, _) }
196+
187197
string toString() {
188198
exists(DeviationBegin begin, BeginStack prev | this = TConsBeginStack(begin, prev) |
189199
result = "(" + begin + ", " + prev.toString() + ")"
@@ -198,7 +208,7 @@ predicate isDeviationRangePaired(DeviationRecord record, DeviationBegin begin, D
198208
exists(File file, int index |
199209
record = end.getRecord() and
200210
hasDeviationCommentFileOrdering(record, end, file, index) and
201-
mkBeginStack(record, file, TConsBeginStack(begin, _), index - 1)
211+
begin = calculateBeginStack(record, file, index - 1).peek()
202212
)
203213
}
204214

0 commit comments

Comments
 (0)