|
| 1 | +/** |
| 2 | + * The C++ extractor does not support hidden friends, which are friend functions defined within a |
| 3 | + * class, rather than declared: |
| 4 | + * |
| 5 | + * ```cpp |
| 6 | + * struct A { |
| 7 | + * friend void hidden_friend(A) {} // Definition: this is a hidden friend |
| 8 | + * friend void not_hidden_friend(A); // Declaration: this is not a hidden friend |
| 9 | + * }; |
| 10 | + * ``` |
| 11 | + * |
| 12 | + * In the database, a `FriendDecl` is not created for the hidden friend. The hidden friend function |
| 13 | + * is created as a `TopLevel` function with no enclosing element. However, we can identify it as a |
| 14 | + * hidden friend by its location. |
| 15 | + */ |
| 16 | + |
| 17 | +import cpp |
| 18 | +import codingstandards.cpp.ast.Class |
| 19 | + |
| 20 | +/** |
| 21 | + * A class that, by our best logic, appears to possibly be a hidden friend. |
| 22 | + * |
| 23 | + * Hidden friends are not directly represented in the database. Instances of this class have been |
| 24 | + * found to have a location "within" the "body" of a class, and to satisfy other criteria that |
| 25 | + * indicates it may be a hidden friend. |
| 26 | + */ |
| 27 | +class PossibleHiddenFriend extends HiddenFriendCandidate { |
| 28 | + ClassCandidate cls; |
| 29 | + |
| 30 | + PossibleHiddenFriend() { hidesFriend(cls, this) } |
| 31 | + |
| 32 | + Class getFriendClass() { result = cls } |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Begin by limiting the number of candidate functions to consider. |
| 37 | + * |
| 38 | + * Only inline top level functions can be hidden friends. |
| 39 | + */ |
| 40 | +private class HiddenFriendCandidate extends TopLevelFunction { |
| 41 | + HiddenFriendCandidate() { this.isInline() } |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Only consider files which contain hidden friend candidates. |
| 46 | + */ |
| 47 | +private class FileCandidate extends File { |
| 48 | + FileCandidate() { exists(HiddenFriendCandidate c | c.getFile() = this) } |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Only consider classes in candidate files, that include hidden friend candidates. |
| 53 | + */ |
| 54 | +private class ClassCandidate extends Class { |
| 55 | + ClassCandidate() { getFile() instanceof FileCandidate } |
| 56 | + |
| 57 | + /** |
| 58 | + * Find the next declaration after this class that shares an enclosing element. |
| 59 | + * |
| 60 | + * This may be the next declaration after this class, or `getNextOrphanedDeclaration` may find the |
| 61 | + * true next declaration after this class. These are split for performance reasons. |
| 62 | + */ |
| 63 | + Declaration getNextSiblingDeclaration() { |
| 64 | + result = |
| 65 | + min(Declaration decl | |
| 66 | + decl.getEnclosingElement() = this.getEnclosingElement() and |
| 67 | + pragma[only_bind_out](decl.getFile()) = pragma[only_bind_out](this.getFile()) and |
| 68 | + decl.getLocation().getStartLine() > getLastLineOfClassDeclaration(this) |
| 69 | + | |
| 70 | + decl order by decl.getLocation().getStartLine(), decl.getLocation().getStartColumn() |
| 71 | + ) |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Get the next declaration after this class that does not have an enclosing element. |
| 76 | + * |
| 77 | + * This may be the next declaration after this class, or `getNextSiblingDeclaration` may find the |
| 78 | + * true next declaration after this class. These are split for performance reasons. |
| 79 | + * |
| 80 | + * Note that `OrphanedDeclaration` excludes hidden friend candidates, so this will find the next |
| 81 | + * orphan that is definitely not a hidden friend. |
| 82 | + */ |
| 83 | + Declaration getNextOrphanedDeclaration() { |
| 84 | + result = |
| 85 | + min(OrphanedDeclaration decl, int startLine, int startColumn | |
| 86 | + orphanHasLocation(decl, this.getFile(), startLine, startColumn) and |
| 87 | + startLine > getLastLineOfClassDeclaration(this) |
| 88 | + | |
| 89 | + decl order by startLine, startColumn |
| 90 | + ) |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Get the first declaration definitely after this class, and not a hidden friend declaration, to |
| 95 | + * determine the "end" location of this class. |
| 96 | + */ |
| 97 | + Declaration getFirstNonClassDeclaration() { |
| 98 | + result = |
| 99 | + min(Declaration decl | |
| 100 | + decl = getNextSiblingDeclaration() or decl = getNextOrphanedDeclaration() |
| 101 | + | |
| 102 | + decl order by decl.getLocation().getStartLine(), decl.getLocation().getStartColumn() |
| 103 | + ) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * Helper predicate to improve join performance. |
| 109 | + */ |
| 110 | +pragma[nomagic] |
| 111 | +private predicate orphanHasLocation( |
| 112 | + OrphanedDeclaration orphan, FileCandidate file, int startLine, int startColumn |
| 113 | +) { |
| 114 | + orphan.getFile() = file and |
| 115 | + orphan.getLocation().getEndLine() = startLine and |
| 116 | + orphan.getLocation().getEndColumn() = startColumn |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | + * Orphaned declarations to be found by `getNextOrphanedDeclaration`. |
| 121 | + * |
| 122 | + * These are declarations with no enclosing element. Note that we exclude hidden friend candidates, |
| 123 | + * as this class is used to find the declarations that are definitely not part of some class. This |
| 124 | + * is done so we can detect if hidden friends may be within that class definition. Therefore we must |
| 125 | + * exclude hidden friend candidates, even though those are also orphaned. |
| 126 | + */ |
| 127 | +private class OrphanedDeclaration extends Declaration { |
| 128 | + OrphanedDeclaration() { |
| 129 | + not exists(getEnclosingElement()) and |
| 130 | + not this instanceof HiddenFriendCandidate and |
| 131 | + getFile() instanceof FileCandidate and |
| 132 | + not isFromTemplateInstantiation(_) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +/** |
| 137 | + * Helper predicate to improve join performance. |
| 138 | + */ |
| 139 | +pragma[nomagic] |
| 140 | +private predicate classCandidateHasFile(ClassCandidate c, FileCandidate f) { c.getFile() = f } |
| 141 | + |
| 142 | +/** |
| 143 | + * Helper predicate to improve join performance. |
| 144 | + */ |
| 145 | +pragma[nomagic] |
| 146 | +private predicate hiddenFriendCandidateHasFile(HiddenFriendCandidate h, FileCandidate f) { |
| 147 | + h.getFile() = f |
| 148 | +} |
| 149 | + |
| 150 | +/** |
| 151 | + * Find the class locations that have declarations that could be hidden friend declarations, by |
| 152 | + * comparing the locations of the candidate hidden friend functions to the location of the first |
| 153 | + * declaration that clearly is outside that class. |
| 154 | + */ |
| 155 | +pragma[nomagic] |
| 156 | +private predicate hidesFriend(ClassCandidate c, HiddenFriendCandidate f) { |
| 157 | + exists(FileCandidate file, Location cloc, Location floc | |
| 158 | + classCandidateHasFile(c, file) and |
| 159 | + hiddenFriendCandidateHasFile(f, file) and |
| 160 | + cloc = c.getLocation() and |
| 161 | + floc = f.getLocation() and |
| 162 | + cloc.getEndLine() < floc.getStartLine() and |
| 163 | + floc.getEndLine() < c.getFirstNonClassDeclaration().getLocation().getStartLine() |
| 164 | + ) |
| 165 | +} |
0 commit comments