|
| 1 | +private predicate hasDefinition(@function f) { |
| 2 | + exists(@fun_decl fd | fun_decls(fd, f, _, _, _) | fun_def(fd)) |
| 3 | +} |
| 4 | + |
| 5 | +private predicate onlyOneCompleteFunctionExistsWithMangledName(@mangledname name) { |
| 6 | + strictcount(@function f | hasDefinition(f) and mangled_name(f, name, true)) = 1 |
| 7 | +} |
| 8 | + |
| 9 | +/** Holds if `f` is a unique function with a definition named `name`. */ |
| 10 | +private predicate isFunctionWithMangledNameAndWithDefinition(@mangledname name, @function f) { |
| 11 | + hasDefinition(f) and |
| 12 | + mangled_name(f, name, true) and |
| 13 | + onlyOneCompleteFunctionExistsWithMangledName(name) |
| 14 | +} |
| 15 | + |
| 16 | +/** Holds if `f` is a function without a definition named `name`. */ |
| 17 | +private predicate isFunctionWithMangledNameAndWithoutDefinition(@mangledname name, @function f) { |
| 18 | + not hasDefinition(f) and |
| 19 | + mangled_name(f, name, true) |
| 20 | +} |
| 21 | + |
| 22 | +/** |
| 23 | + * Holds if `incomplete` is a function without a definition, and there exists |
| 24 | + * a unique function `complete` with the same name that does have a definition. |
| 25 | + */ |
| 26 | +private predicate hasTwinWithDefinition(@function incomplete, @function complete) { |
| 27 | + not function_instantiation(incomplete, complete) and |
| 28 | + ( |
| 29 | + not compgenerated(incomplete) or |
| 30 | + not compgenerated(complete) |
| 31 | + ) and |
| 32 | + exists(@mangledname name | |
| 33 | + isFunctionWithMangledNameAndWithoutDefinition(name, incomplete) and |
| 34 | + isFunctionWithMangledNameAndWithDefinition(name, complete) |
| 35 | + ) |
| 36 | +} |
| 37 | + |
| 38 | +import Cached |
| 39 | + |
| 40 | +cached |
| 41 | +private module Cached { |
| 42 | + /** |
| 43 | + * If `f` is a function without a definition, and there exists a unique |
| 44 | + * function with the same name that does have a definition, then the |
| 45 | + * result is that unique function. Otherwise, the result is `f`. |
| 46 | + */ |
| 47 | + cached |
| 48 | + @function resolveFunction(@function f) { |
| 49 | + hasTwinWithDefinition(f, result) |
| 50 | + or |
| 51 | + not hasTwinWithDefinition(f, _) and |
| 52 | + result = f |
| 53 | + } |
| 54 | + |
| 55 | + cached |
| 56 | + predicate isFunction(@function f) { f = resolveFunction(_) } |
| 57 | +} |
0 commit comments