|
| 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 | +class PossibleHiddenFriend extends HiddenFriendCandidate { |
| 21 | + ClassCandidate cls; |
| 22 | + |
| 23 | + PossibleHiddenFriend() { hidesFriend(cls, this) } |
| 24 | + |
| 25 | + Class getFriendClass() { result = cls } |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Begin by limiting the number of candidate functions to consider. |
| 30 | + * |
| 31 | + * Only inline top level functions can be hidden friends. |
| 32 | + */ |
| 33 | +private class HiddenFriendCandidate extends TopLevelFunction { |
| 34 | + HiddenFriendCandidate() { this.isInline() } |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Only consider files which contain hidden friend candidates. |
| 39 | + */ |
| 40 | +private class FileCandidate extends File { |
| 41 | + FileCandidate() { exists(HiddenFriendCandidate c | c.getFile() = this) } |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Only consider classes in candidate files, that include hidden friend candidates. |
| 46 | + */ |
| 47 | +private class ClassCandidate extends Class { |
| 48 | + ClassCandidate() { getFile() instanceof FileCandidate } |
| 49 | + |
| 50 | + Declaration getNextSiblingDeclaration() { |
| 51 | + exists(LastClassDeclaration last | |
| 52 | + last.getDeclaringType() = this and |
| 53 | + result = |
| 54 | + min(Declaration decl | |
| 55 | + decl.getEnclosingElement() = this.getEnclosingElement() and |
| 56 | + pragma[only_bind_out](decl.getFile()) = pragma[only_bind_out](this.getFile()) and |
| 57 | + decl.getLocation().getStartLine() > last.getLocation().getEndLine() |
| 58 | + | |
| 59 | + decl order by decl.getLocation().getStartLine(), decl.getLocation().getStartColumn() |
| 60 | + ) |
| 61 | + ) |
| 62 | + } |
| 63 | + |
| 64 | + Declaration getNextOrphanedDeclaration() { |
| 65 | + exists(LastClassDeclaration last | |
| 66 | + last.getDeclaringType() = this and |
| 67 | + result = |
| 68 | + min(OrphanedDeclaration decl, Location locLast, Location locDecl | |
| 69 | + orphanHasFile(decl, this.getFile()) and |
| 70 | + locDecl = decl.getLocation() and |
| 71 | + locLast = last.getLocation() and |
| 72 | + locLast.getStartLine() < locDecl.getEndLine() |
| 73 | + | |
| 74 | + decl order by locDecl.getStartLine(), locDecl.getStartColumn() |
| 75 | + ) |
| 76 | + ) |
| 77 | + } |
| 78 | + |
| 79 | + Declaration getFirstNonClassDeclaration() { |
| 80 | + exists(LastClassDeclaration last | |
| 81 | + last.getDeclaringType() = this and |
| 82 | + result = |
| 83 | + min(Declaration decl | |
| 84 | + decl = getNextSiblingDeclaration() or decl = getNextOrphanedDeclaration() |
| 85 | + | |
| 86 | + decl order by decl.getLocation().getStartLine(), decl.getLocation().getStartColumn() |
| 87 | + ) |
| 88 | + ) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +pragma[nomagic] |
| 93 | +private predicate orphanHasFile(OrphanedDeclaration orphan, FileCandidate file) { |
| 94 | + orphan.getFile() = file |
| 95 | +} |
| 96 | + |
| 97 | +private class OrphanedDeclaration extends Declaration { |
| 98 | + OrphanedDeclaration() { |
| 99 | + not exists(getEnclosingElement()) and |
| 100 | + not this instanceof HiddenFriendCandidate and |
| 101 | + getFile() instanceof FileCandidate and |
| 102 | + not isFromTemplateInstantiation(_) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +pragma[nomagic] |
| 107 | +private predicate classCandidateHasFile(ClassCandidate c, FileCandidate f) { c.getFile() = f } |
| 108 | + |
| 109 | +pragma[nomagic] |
| 110 | +private predicate hiddenFriendCandidateHasFile(HiddenFriendCandidate h, FileCandidate f) { |
| 111 | + h.getFile() = f |
| 112 | +} |
| 113 | + |
| 114 | +pragma[nomagic] |
| 115 | +private predicate hidesFriend(ClassCandidate c, HiddenFriendCandidate f) { |
| 116 | + exists(FileCandidate file, Location cloc, Location floc | |
| 117 | + classCandidateHasFile(c, file) and |
| 118 | + hiddenFriendCandidateHasFile(f, file) and |
| 119 | + cloc = c.getLocation() and |
| 120 | + floc = f.getLocation() and |
| 121 | + cloc.getEndLine() < floc.getStartLine() and |
| 122 | + floc.getEndLine() < c.getFirstNonClassDeclaration().getLocation().getStartLine() |
| 123 | + ) |
| 124 | +} |
0 commit comments