|
| 1 | +/** |
| 2 | + * A C++ receiver declared as a member type of a class template, like |
| 3 | + * google/leveldb's and facebook/rocksdb's `SkipList<Key, |
| 4 | + * Comparator>::Iterator iter(&list);`. The declaration was read from its |
| 5 | + * last name alone, `Iterator`, which C++ lookup from the calling function |
| 6 | + * then found in the enclosing namespace: leveldb's skip-list tests linked |
| 7 | + * `iter.Valid()` to `leveldb::Iterator::Valid`, and rocksdb's to |
| 8 | + * `IteratorBase::Valid`, instead of the skip list's own iterator. The type is |
| 9 | + * now read whole, and the member found in the class template it is written in. |
| 10 | + */ |
| 11 | +import { describe, it, expect, afterEach } from 'vitest'; |
| 12 | +import * as fs from 'fs'; |
| 13 | +import * as os from 'os'; |
| 14 | +import * as path from 'path'; |
| 15 | +import { CodeGraph } from '../src'; |
| 16 | +import type { Node } from '../src/types'; |
| 17 | + |
| 18 | +const roots: string[] = []; |
| 19 | +afterEach(() => { |
| 20 | + for (const root of roots.splice(0)) fs.rmSync(root, { recursive: true, force: true }); |
| 21 | +}); |
| 22 | + |
| 23 | +async function indexed(files: Record<string, string>): Promise<CodeGraph> { |
| 24 | + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'codegraph-cpp-template-member-')); |
| 25 | + roots.push(root); |
| 26 | + for (const [rel, content] of Object.entries(files)) { |
| 27 | + fs.mkdirSync(path.dirname(path.join(root, rel)), { recursive: true }); |
| 28 | + fs.writeFileSync(path.join(root, rel), content); |
| 29 | + } |
| 30 | + return CodeGraph.init(root, { index: true }); |
| 31 | +} |
| 32 | + |
| 33 | +function callable(cg: CodeGraph, qualifiedName: string): Node { |
| 34 | + const node = [...cg.getNodesByKind('method'), ...cg.getNodesByKind('function')].find((n) => n.qualifiedName === qualifiedName); |
| 35 | + if (!node) throw new Error(`no function or method ${qualifiedName}`); |
| 36 | + return node; |
| 37 | +} |
| 38 | + |
| 39 | +/** `calls` callees of a function or method, as `qualifiedName`, without constructors. */ |
| 40 | +function calls(cg: CodeGraph, caller: string): string[] { |
| 41 | + return cg |
| 42 | + .getCallees(callable(cg, caller).id) |
| 43 | + .filter((r) => r.edge.kind === 'calls' && !/::(\w+)::\1$/.test(r.node.qualifiedName)) |
| 44 | + .map((r) => r.node.qualifiedName) |
| 45 | + .sort(); |
| 46 | +} |
| 47 | + |
| 48 | +const lines = (...l: string[]): string => [...l, ''].join('\n'); |
| 49 | + |
| 50 | +const SKIPLIST = { |
| 51 | + 'include/iterator.h': lines( |
| 52 | + 'namespace leveldb {', |
| 53 | + 'class Iterator {', |
| 54 | + ' public:', |
| 55 | + ' virtual ~Iterator() {}', |
| 56 | + ' virtual bool Valid() const { return false; }', |
| 57 | + ' virtual void SeekToFirst() {}', |
| 58 | + '};', |
| 59 | + '} // namespace leveldb', |
| 60 | + ), |
| 61 | + 'db/skiplist.h': lines( |
| 62 | + '#include "include/iterator.h"', |
| 63 | + 'namespace leveldb {', |
| 64 | + 'template <typename Key, class Comparator>', |
| 65 | + 'class SkipList {', |
| 66 | + ' public:', |
| 67 | + ' class Iterator {', |
| 68 | + ' public:', |
| 69 | + ' explicit Iterator(const SkipList* list) : list_(list) {}', |
| 70 | + ' bool Valid() const { return true; }', |
| 71 | + ' void SeekToFirst() {}', |
| 72 | + ' private:', |
| 73 | + ' const SkipList* list_;', |
| 74 | + ' };', |
| 75 | + '};', |
| 76 | + '} // namespace leveldb', |
| 77 | + ), |
| 78 | +}; |
| 79 | + |
| 80 | +describe('a C++ receiver declared as a member type of a class template', () => { |
| 81 | + it('reproduction: `SkipList<Key, Comparator>::Iterator iter` is the skip list\'s iterator', async () => { |
| 82 | + const cg = await indexed({ |
| 83 | + ...SKIPLIST, |
| 84 | + 'db/skiplist_test.cc': lines( |
| 85 | + '#include "db/skiplist.h"', |
| 86 | + 'namespace leveldb {', |
| 87 | + 'typedef unsigned long Key;', |
| 88 | + 'struct Comparator {};', |
| 89 | + 'void Empty() {', |
| 90 | + ' SkipList<Key, Comparator> list;', |
| 91 | + ' SkipList<Key, Comparator>::Iterator iter(&list);', |
| 92 | + ' if (!iter.Valid()) iter.SeekToFirst();', |
| 93 | + '}', |
| 94 | + 'void Split() {', |
| 95 | + ' SkipList<Key, Comparator> list;', |
| 96 | + ' SkipList<Key, Comparator>::Iterator', |
| 97 | + ' iter(&list);', |
| 98 | + ' iter.Valid();', |
| 99 | + '}', |
| 100 | + '} // namespace leveldb', |
| 101 | + ), |
| 102 | + }); |
| 103 | + try { |
| 104 | + expect(calls(cg, 'leveldb::Empty')).toEqual(['leveldb::SkipList::Iterator::SeekToFirst', 'leveldb::SkipList::Iterator::Valid']); |
| 105 | + expect(calls(cg, 'leveldb::Split')).toEqual(['leveldb::SkipList::Iterator::Valid']); |
| 106 | + } finally { |
| 107 | + cg.close(); |
| 108 | + } |
| 109 | + }); |
| 110 | + |
| 111 | + it('written with `typename` in a template, and through a pointer', async () => { |
| 112 | + const cg = await indexed({ |
| 113 | + ...SKIPLIST, |
| 114 | + 'db/concurrent_test.cc': lines( |
| 115 | + '#include "db/skiplist.h"', |
| 116 | + 'namespace leveldb {', |
| 117 | + 'template <typename Key, class Comparator>', |
| 118 | + 'class ConcurrentTest {', |
| 119 | + ' public:', |
| 120 | + ' void ReadStep() {', |
| 121 | + ' typename SkipList<Key, Comparator>::Iterator iter(&list_);', |
| 122 | + ' iter.SeekToFirst();', |
| 123 | + ' }', |
| 124 | + ' bool Check(typename SkipList<Key, Comparator>::Iterator* it) {', |
| 125 | + ' return it->Valid();', |
| 126 | + ' }', |
| 127 | + ' private:', |
| 128 | + ' SkipList<Key, Comparator> list_;', |
| 129 | + '};', |
| 130 | + '} // namespace leveldb', |
| 131 | + ), |
| 132 | + }); |
| 133 | + try { |
| 134 | + expect(calls(cg, 'leveldb::ConcurrentTest::ReadStep')).toEqual(['leveldb::SkipList::Iterator::SeekToFirst']); |
| 135 | + expect(calls(cg, 'leveldb::ConcurrentTest::Check')).toEqual(['leveldb::SkipList::Iterator::Valid']); |
| 136 | + } finally { |
| 137 | + cg.close(); |
| 138 | + } |
| 139 | + }); |
| 140 | + |
| 141 | + it('a standard container\'s iterator is the library\'s, not a project class named `iterator`', async () => { |
| 142 | + const cg = await indexed({ |
| 143 | + 'util/ring.h': lines( |
| 144 | + 'class Ring {', |
| 145 | + ' public:', |
| 146 | + ' class iterator {', |
| 147 | + ' public:', |
| 148 | + ' int base() const { return 0; }', |
| 149 | + ' };', |
| 150 | + '};', |
| 151 | + ), |
| 152 | + 'src/touch.cc': lines( |
| 153 | + '#include <vector>', |
| 154 | + '#include "util/ring.h"', |
| 155 | + 'void Touch(std::vector<int>::iterator it) {', |
| 156 | + ' it.base();', |
| 157 | + '}', |
| 158 | + ), |
| 159 | + }); |
| 160 | + try { |
| 161 | + expect(calls(cg, 'Touch')).toEqual([]); |
| 162 | + } finally { |
| 163 | + cg.close(); |
| 164 | + } |
| 165 | + }); |
| 166 | + |
| 167 | + it('a library template\'s member that names another type may be one of the project\'s', async () => { |
| 168 | + const cg = await indexed({ |
| 169 | + 'include/slice.h': lines( |
| 170 | + 'class Slice {', |
| 171 | + ' public:', |
| 172 | + ' int size() const { return 0; }', |
| 173 | + '};', |
| 174 | + ), |
| 175 | + 'src/keys.cc': lines( |
| 176 | + '#include <vector>', |
| 177 | + '#include "include/slice.h"', |
| 178 | + 'int First(const std::vector<Slice>& keys) {', |
| 179 | + ' std::vector<Slice>::value_type key = keys[0];', |
| 180 | + ' return key.size();', |
| 181 | + '}', |
| 182 | + ), |
| 183 | + }); |
| 184 | + try { |
| 185 | + expect(calls(cg, 'First')).toEqual(['Slice::size']); |
| 186 | + } finally { |
| 187 | + cg.close(); |
| 188 | + } |
| 189 | + }); |
| 190 | + |
| 191 | + it('a member alias of a class template is the class it names', async () => { |
| 192 | + const cg = await indexed({ |
| 193 | + 'cache/clock_cache.h': lines( |
| 194 | + 'class Shard {', |
| 195 | + ' public:', |
| 196 | + ' int GetTable() const { return 0; }', |
| 197 | + '};', |
| 198 | + 'namespace cache {', |
| 199 | + 'template <class Table>', |
| 200 | + 'class ClockCacheShard {', |
| 201 | + ' public:', |
| 202 | + ' int GetTable() const { return 1; }', |
| 203 | + '};', |
| 204 | + 'template <class Table>', |
| 205 | + 'class BaseHyperClockCache {', |
| 206 | + ' public:', |
| 207 | + ' using Shard = ClockCacheShard<Table>;', |
| 208 | + ' int Report(const BaseHyperClockCache<Table>::Shard* shard) const {', |
| 209 | + ' return shard->GetTable();', |
| 210 | + ' }', |
| 211 | + '};', |
| 212 | + '} // namespace cache', |
| 213 | + ), |
| 214 | + }); |
| 215 | + try { |
| 216 | + expect(calls(cg, 'cache::BaseHyperClockCache::Report')).toEqual(['cache::ClockCacheShard::GetTable']); |
| 217 | + } finally { |
| 218 | + cg.close(); |
| 219 | + } |
| 220 | + }); |
| 221 | +}); |
0 commit comments