apple ios

Uncovering Xcode Indexing

Lovely dog

I exploit Xcode on a daily basis, however I do know not anything about Xcode. In this text, I can discover what indexing is and the way it works. At the tip of the thing, I’ve written a easy command-line software to question the tips saved in indexing DataStore.

1. Xcode Indexing
1.1. What indexing is
2. How indexing paintings
2.1. Unit record and file record
2.2. Using IndexRetailer to question DataStore
2.3. Xcode Indexing Log
3. Extend Reading

What indexing is

Indexing strengthen a spread of IDE options like code navigation, syntax highlighting, code auto-completion, jump-to-definition, to find usages, refactor. You use those options on a daily basis as an iOS developer.

How indexing works

When Xcode quite a bit a task that hasn’t been construct, it’s going to get started indexing the task.

Activity observe is a handy gizmo that can assist you to find out the working processes, the Xcode and SKAgent pop up from the record when I open the task.

So, the query is what SKAgent is? The SKAgent is the element of SourceKit framework runs the compilation instructions to generate the indexing knowledge. The SKAgent executable binary locates at /Applications/

The SKAgent is a XPC Service operated by means of SourceKit makes use of the XPC mechanism to keep up a correspondence with Xcode.

I alter the surroundings variable, SOURCEKIT_LOGGING, to permit the logging of SourceKit. And then open the terminal, input SOURCEKIT_LOGGING=3 /Applications/ &> ~/Desktop/indexing.log. The record indexing.log has a variety of knowledge, so I spotlight the content material for higher demonstration. SourceKit-Client, aka, the Xcode, calls for indexer to open or create indexing with the next arguments.

Client calls for indexer to check in items include the assets record.

SourceKit posts notification that compiler will get started compiling the supply record ViewController.swfit to the buyer.

SourceKit posts notification that compiler did end compiling the ViewController.swift to the buyer.

The indexing knowledge shop within the route laid out in the argument move by means of the command -index-store-path spotlight in Figure-3 with blue colour.

After the SourceKit finishes indexing, I attempt to soar to definition, upload a brand new record, upload new serve as and rename purposes to ensure how the indexing paintings.

  • Jump to definition

Just proper click on the hi serve as after which click on soar to definition

  • Add a brand new record

Add new record, Modal, to task.

  • Add a brand new serve as

Rename the serve as declared in Modal.swift

The motion described above you are making, Xcode will turn into right into a request. The request will ship to SourceKit. There are a number of roughly requests

  • Code Completion
  • Cursor Info
  • Demangling
  • Mangling
  • Documentation
  • Module interface era
  • Indexing
  • Protocol Version
  • Compiler Version

You can move to the SourceKit protocol report for extra element. After SourceKit receives the request, it’s going to question the indexing knowledge(the DataStore) to meet the request and generates the corresponding reaction to Xcode. SourceKit and Xcode paintings as CS structure. SourceKit is the server, Xcode is the SourceKit-client.

Unit record and file record

Both the clang and swift compilers strengthen the command line possibility -index-store-path <dir>, which tells the compiler to supply indexing knowledge within the type of unit information and file information within the given listing. When the -index-store-path possibility is provide, clang will upload the fronted motion, IndexRecordMotion. It makes use of an IndexASTConsumer to gathering image occurrences from the type-checked AST. In addition to this, despite the fact that, it additionally makes use of an IndexDependencyProvider to gather the supply information and modules concerned within the compilation, and any related record inclusion and module import members of the family. When the frontend motion is finished (i.e. IndexRecordMotion’s completed() way is named) the amassed indexing knowledge is written out to the index knowledge shop.

Writing the indexing knowledge to the shop is treated by means of IndexRecordCreator and IndexUnitCreator categories.

FYI, you’ll download the demo right here.

Unit information

Unit information are information that file details about the interpretation unit of the compiler invocation, monitoring the the set of supply record paths and matching file information that contain them, in addition to the unit information of every other compilation gadgets they rely on. They observe the naming conference “basename-hash” the place basename is the basename of the output record (the one handed to the compiler within the -o possibility), and hash is the LLVM Hash of absolutely the route of the output record — if the trail given to the compiler within the -o possibility is relative, it’s reworked to an absolute route ahead of hashing.

The content material of the ViewController unit record proven underneath

Record information

Record information file details about the symbols encountered in several information used all over compilation. They observe the naming conference “basename-hash” the place basename is the basename of the record learn by means of the compiler (e.g. stdio.h), and hash is the hash of the contents of the record after being preprocessed.

The content material of the ViewController file record proven underneath

To get the c-index-test software, you’ll clone the llvm, and observe the professional directions. After development the llvm, the executable locates at llvm-project/construct/bin.

Using IndexRetailer to question DataStore

Also, you’ll use IndexRetailer-db to question the indexing knowledge saved in a listing laid out in -index-store-path. I’ve written a command-line software the use of IndexRetailer to seek out all of the references of symbols laid out in the enter price.

First, download the demo. Second, open the IndexingDemo.xcodeproj and construct. After development, move to the Report navigator, to find out the shop route laid out in each and every assemble unit.

Open the IndexStoreCmd.xcodeproj, upload the shop route, and the logo you need to go looking.

After working the IndexStoreCmd, the outcome as Figure-5 displays, style is referenced by means of ViewController and ModalSupervisor.

Comparing the Figure-5 and Figure-6, the outcome fits the references declared within the supply information. The diagram underneath depicts the structure of Xcode, SourceKit, Compiler, and IndexStore_db, no longer professional, please inform me if anything else unsuitable.

Xcode Indexing Log

You can override the Xcode environment, IDEIndexDisplayLog, to appear up the indexing log straight away. Open terminal, input defaults write IDEIndexDisplayLog -bool YES. Because the indexing occurs within the background, it lets you do one thing indexing motion, like soar to definition, straight away after coding.

Uncovering SourceKit


XPC Service

Adding Index-While-Building strengthen to Clang

The LLVM Compiler Infrastructure

Apple IndexRetailer DB

video: Index‐While‐Building, pdf: Index-While-Building

clang index headers



Xcode Setting