Swift
What .swift adds beyond Install, CLI specs, and Graph schema: heuristic vs the Swift index store, the CG_SWIFT_* flags, and the ids those pages do not spell out. Toolchain fit: cg doctor -h. Why a file stayed heuristic: cg coverage.
Modes
| mode | calls | needs |
|---|---|---|
| heuristic (default) | name resolution, labelled heuristic | tree-sitter (tree-sitter, tree-sitter-swift). No Xcode or swift |
| exact | compiler-resolved CALLS / INSTANTIATES; declarations and framework facts stay the syntax layer | a Swift toolchain, opt-in (Exact mode) |
--min-confidence resolved (or exact) hides every heuristic call edge. SwiftPM .build / .swiftpm, Xcode DerivedData, Pods, and Carthage are skipped.
tree-sitter-swift 0.7.3 and 0.7.4 (same graph on both) rewrite some sources before parse (same length, so lines and names stay). Index stats count files_with_<rewrite>. What still fails is in cg coverage --details.
| rewrite | when | stat |
|---|---|---|
#sourceLocation lines blanked | directive lines | source_location_directives |
#_name parsed as #line | #_sourceLocation and similar | underscore_macros |
#if / #endif lines dropped | a block that is only attributes (@Test under #if os) | the declaration keeps that platform |
| length-preserving patches | () as a value, @convention(c), a cast before ??, try before await in a condition, a binary operator starting a continuation line | names stay the original source |
| members pulled back into the type | the grammar closes the type early | declarations_recovered_into_type |
Exact mode
libIndexStore replaces heuristic call and initializer edges for each file the store covers. swift build runs the package manifest and its plugins, so it is opt-in. Coverage reports Swift index store (swift build), or the reason the layer did not run.
| source | how |
|---|---|
CG_SWIFT_INDEX=1 | root Package.swift: swift build --enable-index-store into <cache root>/swift-build/ (default ~/.cache/cg; never the checkout). Reused while sources, manifests, and the toolchain match. CG_NO_CACHE=1 forces a run; CG_INDEXER_TIMEOUT caps it |
CG_SWIFT_INDEX_STORE | an existing store (Xcode DerivedData/…/Index.noindex/DataStore, or Index/DataStore before Xcode 14; CI .build/<triple>/debug/index/store). Another checkout's paths map by longest common suffix |
CG_SWIFT, CG_LIBINDEXSTORE | swift and libIndexStore when they are not on PATH or next to swift |
A failed build keeps a partial store; the coverage reason includes the first error: line. Stats include exact_vs_heuristic and heuristic_kept_not_compiled.
| still heuristic | why |
|---|---|
no toolchain, not opted in, or no Package.swift | coverage says which; set CG_SWIFT_INDEX_STORE to an Xcode store |
| Apple-only targets on Linux | SwiftUI, UIKit, and tests that need Apple frameworks are not in that store |
inactive #if | edges stay, via: "not-compiled" |
| files the store omits | those files keep heuristic calls, counted in the reason |
Query specs
Type.method follows query targets. Swift-only shapes:
| spec or id | selects |
|---|---|
method:<Type>.body | a SwiftUI body. A computed property, observers, or lazy var is also method:<Type>.<name> (swift_kind) |
page:swift:<View> | NavigationLink(destination:), .navigationDestination, .sheet / .fullScreenCover / .popover, TabView, or the WindowGroup root (ROUTES_TO body, NAVIGATES_TO from the caller). Each case of navigationDestination(for:) is the target of NavigationLink(value:) and of navigate(to:) / push / append |
field:<Type>.<name> | a stored let / var: READS_PROP / WRITES_PROP for self.x, a bare x in the type, and v.x when v's type is known. cg readers Type.prop / cg writers Type.prop. A stored property declared again in another #if branch is its own field:<Type>.<name>@<line>; readers shows each site's platforms=, and field:<Type>.<name>@<line> names one declaration. @AppStorage("k") / @SceneStorage("k") fields that share a wrapper and key are SHARES_STORAGE (the two wrappers stay apart) |
| availability | @available(iOS 17, *) is node attrs.available (shown [iOS 17+]; members inherit the type). if / guard #available and the else of #unavailable set it on the edge, by column: the guarded branch starts after the check (conditions after it, if #available(iOS 26, *), x.isOn, count) and ends at its }, so } else { legacy() } on the same line is not guarded. A check the deployment target already meets is available_declared, not a filter (availability_always_true) |
impact shows an accessor as (get) / (set) / (willSet) / (didSet) and a candidate as (candidate) ((candidate, 1 of 3 methods named refreshStatus) when the fan-out is known). Candidates are counted apart from resolved callers (transitive callers: 3 (+1 candidate), callers_candidate in JSON) and left out of callers by module; a caller with no module (a free function) is grouped as <file> (top level). The second of a func and a static func with one name has id suffix ~static or ~instance. Before that, an unknown receiver is narrowed: a local initialised from a project function or a static factory whose declared return type is a project type (let r = makeBridge(), let s = try ShelfBridge.shared()) is a value of that type. So is a call chain: shop.engine().run(), makeShop().engine() and let e = shop.engine() use each step's declared return type, Self is the receiver's type (Sub.make() on a class func make() -> Self is a Sub), and a generic parameter is the receiver's argument (Garage<Engine>(…).get() with func get() -> T is an Engine, from a local's initializer or annotation or a property's declared type). A private / fileprivate member (or a member of such a type) declared in another file is not a candidate, so one remaining method binds as name. In heuristic mode, two to five methods of one selector are candidate edges (binding: "candidate"), flagged by cg tests / impact and left out of platform divergence. An SDK-typed receiver binds only a project extension of that type, or of a protocol / open class such as View. Package code binds only its module and Package.swift dependencies (candidates_outside_module). Exact mode keeps every index-store reference, including ones outside that module.
Framework facts
| area | what you can query |
|---|---|
| SwiftUI / UIKit | pages above; pushViewController / present the same way. An untyped .case matches when one destination type has that case; default: covers the rest |
| Strings | i18n:<key> from .xcstrings and .strings (one node per key, attrs.definitions per locale). String(localized:), NSLocalizedString, LocalizedStringResource, and Text("key") when the key is in an Apple catalog, are USES_I18N. A key built at run time is not a node. cg detect reports swiftui / uikit / vapor and modes.swift when exact calls are unavailable |
| HTTP | URLSession and Alamofire AF.request when the URL is built in the same function. "\(base)/x" keeps {base} so cg link can match. A base-like constant, or an Info.plist key filled from .xcconfig, sets the origin (attrs.base, or attrs.base_candidates per configuration) |
| Moya | TargetType → one http: node per case (how: "moya target"). provider.request(.case) calls it |
| Fluent | schema = "todos" → table:todos. query / find read; save / create / update / delete and database.schema(…).create() write |
| Vapor | app.get, routes.post, grouped / group prefixes, RouteCollection.boot → route:. Middleware on grouped is the guard |
| Tests | XCTest test* (xctest) and Swift Testing @Test (swift-testing, @Suite on the type). *UITests is UI; a path through @main is omitted unless --through-roots |
| Platforms | #if os / canImport / targetEnvironment / @available(..., unavailable) (cg platforms, --platform). Targets come from the Xcode project or Package.swift platforms:. A reference made under #if os(macOS) links only the per-platform definitions built on macOS, stored properties (READS_PROP / WRITES_PROP) included; code built everywhere links every definition. A #else type that declares no init uses its implicit one, not a sibling variant's |
| Default arguments | a parameter default's calls are marked default_arg on the declaration and not walked by impact (via default argument). A call that leaves the argument out gets its own edge to the default's callee (omits_default, default_of), so impact lists it; a call that passes the argument does not. TypeScript / JavaScript, Kotlin and PHP do the same (schema) |
cg index examples/bookstore-django --db /tmp/dj.db
cg index examples/bookstore-ios --db /tmp/ios.db
cg link --backend /tmp/dj.db --frontend /tmp/ios.db --db /tmp/ios-link.db
cg path "page:swift:CheckoutView" "table:catalog_order" --db /tmp/ios-link.db
Initializers: Self(x: 0) inside a type, an implicit .init(x: 1) whose type is the annotation (let p: Point = .init(...)) or the function's declared return type, and a nested type through its owner (Point.Delta(dx: 1)) are INSTANTIATES plus CALLS to the matching init. A bare class func / class var is a static member.
Attack surface
cg surface reads facts the index already holds and answers three questions: what does the code expose, what does it connect to, and where are credentials and transport protection weak. Nothing is rescanned: the checks that need source text (disabled verification, insecure gRPC channels, risky IPC settings) run once at index time and leave facts on the graph. A secret value is never stored or printed: a finding names the kind and the location, and the index keeps only a redacted marker for a credential literal (No secret values in the index).
TypeScript / JavaScript frameworks
What NestJS, Next.js, Astro, Express-style routers and React Router / Remix add on top of the TypeScript plugin. They share one program with the Nuxt layer, so aliases and types go through the checker. Plain JavaScript (CommonJS or ESM, with or without jsconfig.json) is indexed with allowJs. Samples: examples/bookstore-nest, bookstore-next, bookstore-express. Without a root tsconfig, the program is the per-package tsconfig.json next to a package.json one or two levels down, plus a tsconfig.json in src/, app/, lib/ or web/ without one. The extractor runs under Node.js 20+ or Bun (Installing and updating cg).