Skip to content

Commit 6752782

Browse files
authored
Merge pull request #1335 from EdoDodo/optimise-preprocessor
C++: Optimise quadratic code in PreprocessorBranchDirective
2 parents 5466ae6 + 30198c3 commit 6752782

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

cpp/ql/src/semmle/code/cpp/Preprocessor.qll

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,24 @@ abstract class PreprocessorBranchDirective extends PreprocessorDirective {
7373
* `somePreprocessorBranchDirective`.
7474
*/
7575
PreprocessorBranchDirective getNext() {
76-
getIf() = result.getIf() and
77-
getLocation().getStartLine() < result.getLocation().getStartLine() and
78-
not exists(PreprocessorBranchDirective other |
79-
getIf() = other.getIf() and
80-
getLocation().getStartLine() < other.getLocation().getStartLine() and
81-
other.getLocation().getStartLine() < result.getLocation().getStartLine()
76+
exists(PreprocessorBranch branch |
77+
this.getIndexInBranch(branch) + 1 = result.getIndexInBranch(branch)
8278
)
8379
}
80+
81+
/**
82+
* Gets the index of this branching directive within the matching #if,
83+
* #ifdef or #ifndef.
84+
*/
85+
private int getIndexInBranch(PreprocessorBranch branch) {
86+
this = rank[result](PreprocessorBranchDirective other |
87+
other.getIf() = branch
88+
|
89+
other
90+
order by
91+
other.getLocation().getStartLine()
92+
)
93+
}
8494
}
8595

8696
/**

0 commit comments

Comments
 (0)