Code Completion

GCC C++ frontend is a parser that also generates code. Clang is quite similar: it is parser that also calls various actions along with creating AST. Completion implemented via inserting virtual token, on which parser calls one of Sema class methods, implemented in SemaCodeComplete.cpp.

Main pros of this approach is context-sensitive completion: parser will not produce any completion results if it doesn’t expect completion token at given point, otherwise it will call one of ~40 methods from SemaCodeComplete.cpp, choice depends on context. There are declarations of some code complete methods, parser calls one of them:

  1.   void CodeCompleteTag(Scope *S, unsigned TagSpec);
  2.   void CodeCompleteTypeQualifiers(DeclSpec &DS);
  3.   void CodeCompleteCase(Scope *S);
  4.   void CodeCompleteCall(Scope *S, Expr *Fn, ArrayRef<Expr *> Args);
  5.   void CodeCompleteInitializer(Scope *S, Decl *D);
  6.   void CodeCompleteReturn(Scope *S);
  7.   void CodeCompleteAfterIf(Scope *S);
  8.   void CodeCompleteAssignmentRHS(Scope *S, Expr *LHS);

Additional notes:

  • Builder pattern [en.wikipedia.org] used to create completion results, see ResultBuilder class.
  • Declarations will be collected by CodeCompletionDeclConsumer class, passed to Sema::LookupVisibleDecls() method.

Categories: