Skip to content

yaruopooner/ac-clang

Repository files navigation

img img

Japanese Manual

About ac-clang

The original version is emacs-clang-complete-async.

https://github.com/Golevka/emacs-clang-complete-async

The above fork and it was extended.

Provide Features

The C/C++ Code Completion and the jump to definition/declaration/inclusion-file is provided by libclang.

img

Basic Features

  • C/C++/Objective-C Code Completion
  • Syntax Check by flymake
  • Jump and return for definition/declaration
    It isn't necessary to generate a tag file beforehand, and it's possible to jump by on-the-fly.
    But this feature is not perfect.
    Because, the case you can't jump well also exists.

Extended Features

The original version is non-implementation.

  • The number of launch of clang-server is change to 1 process per Emacs.
    The original is 1 process per buffer.
    The clang-server is create a session and holds CFLAGS per source code buffer.
  • The template parameters expand
    Template parameters expand is possible at the time of arguments expand after completion.
  • Completion by manual operation
    It can be completion at any position.
  • Display brief comment of completion candidate
    brief comment show on minibuffer.
  • libclang CXTranslationUnit Flags support
    It is setable on lisp.
  • libclang CXCodeComplete Flags support
    It is setable on lisp.
  • Multibyte support
    Modified, because the original is not enough multibyte support.
  • Jump and return for inclusion file.
  • IPC packet format can be specified
    S-Expression, Json
  • Debug Logger
    Used for debugging.
    You are possible to confirm the message and the data that client was sent to clang-server.
  • Performance Profiler
    Measure the performance of client / server.
  • Miscellaneous
    Small change and addition

Optional Features

The original version is non-implementation.
Mainly Windows Platform support.

  • Project file generation by CMake.
    Visual Studio Project and Linux Makefile support.
  • Microsoft Visual Studio Platform support
    clang-server and libclang.dll(clang 6.0.0 RELEASE/FINAL) was built by Microsoft Visual Studio 2017/2015
    Before 2013, LLVM was excluded because it became out of support.
  • x86_64 Machine Architecture + Windows Platform support
    Required if you want to completion code for Visual Studio.(for _WIN64 build support)
    clang-server and libclang.dll is 64/32bit version.
    The binary was built by Visual Studio.
    Therefore, this also has become to those that conform to the machine architecture type of Visual Studio compiler predefined macros.
    If you build by mingw environment, Visual Studio predefined macros is interfere or not defined.

Other difference

clang-server is described by C++.(The original is C)

Installation(external program)

Linux & Windows(self-build)

Please installation is reference to the manual of clang-server for self-build.

Clang Server Manual

Windows(If you use redistributable release binary)

Installation of Visual C++ Redistributable Package

If you don't install Visual Studio 2017/2015, required Visual C++ Redistributable Package.
Please installer gets the vcredist_x64.exe from following page.

A copy of the external program

https://github.com/yaruopooner/ac-clang/releases

Please download the latest clang-server-X.X.X.zip from above, and unpack to ac-clang directory.

clang-server.exe
libclang.dll
You have to copy this two files to valid path.
e.g. /usr/local/bin

Precautions

libclang is not same the LLVM official binary.
Official libclang has problem that file is locked by LLVM file system used mmap.
libclang which is being distributed here solved the problem by patch applied to official source code.
If you want LLVM self-build, you have to apply a patch for solve the above problem.

Installation(lisp package)

Required Packages

Emacs built-in packages and installation required packages.

  • flymake(built-in)
  • auto-complete
  • pos-tip
  • yasnippet

Configuration of ac-clang

(require 'ac-clang)

(ac-clang-initialize)

It is complete.
If you call (ac-clang-initialize), a clang-server will resident.

If you want to use debug version, the following settings are required before (ac-clang-initialize) execution.

(require 'ac-clang)

(clang-server-type 'debug)
(ac-clang-initialize)

How to use

Configuration of libclang flags

It will change the flag of clang-server in the following way

(setq clang-server-translation-unit-flags FLAG-STRING)
(setq clang-server-complete-at-flags FLAG-STRING)
(ac-clang-initialize)

Configuration value is necessary to be set to variable before the initialization function execution.
Configuration value change after the clang-server launch, uses (clang-server-update-clang-parameters).

Configuration of CFLAGS

CFLAGS must be set in the variable of the source code buffer before ac-clang activation.

(setq clang-server-cflags CFLAGS)

It's set by this.

Activation

To execute the completion, you need to create a session associated the source code buffer in clang-server.
CFLAGS set to clang-server-cflags after following execution.
Run the activate function below after CFLAGS set to clang-server-cflags.

(ac-clang-activate)

Therefore, session associated with buffer is created on clang-server.

  • Lazy Activation
    You can delay the activation until the buffer is changed.
    This is used instead of (ac-clang-activate).

    (ac-clang-activate-after-modify)
    

    If you want to use this activation, it is better to run at c-mode-common-hook.

Deactivation

Delete the session created on clang-server.

(ac-clang-deactivate)

Update of libclang flags

It will change the flag of clang-server in the following way

(setq clang-server-translation-unit-flags FLAG-STRING)
(setq clang-server-complete-at-flags FLAG-STRING)
(clang-server-update-clang-parameters)

Before carrying out this function, the flag of a created session isn't changed.
A new flag is used for the created session after this function execution.

Update of CFLAGS

If there is a CFLAGS of updated after the session creation , there is a need to update the CFLAGS of the session .

(setq clang-server-cflags CFLAGS)
(clang-server-update-cflags)

When you do this, CFLAGS of the session will be updated.

This has the same effect.
But (clang-server-update-cflags) is small cost than following.

(ac-clang-deactivate)
(ac-clang-activate)

Debug Logger

When you make the following settings
The contents sent to clang-server are output to a buffer as "Clang-Log".

(setq clang-server-debug-log-buffer-p t)

It will put a limit on the logger buffer size.
If buffer size larger than designation size, the buffer is cleared.

(setq clang-server-debug-log-buffer-size (* 1024 1000))

If you don't want to be erased a logger buffer, you can set as follows.

(setq clang-server-debug-log-buffer-size nil)

Profiler

When you make the following settings
Profile result at command execution is output to "Messages".

(setq clang-server-debug-profiler-p t)

#+end_src

Completion

Auto Completion

Completion is executed when the following key input is performed just after the class or the instance object or pointer object.

  • .
  • ->
  • ::

If you want to invalidate autocomplete, it will set as follows.

(setq ac-clang-async-autocompletion-automatically-p nil)

Manual Completion

Completion is executed when the following key input is performed.

  • <tab>

Position to perform the key input is the same as auto-completion of the above-mentioned.
And it is possible completions between word of method or property.

struct Foo
{
    int     m_property0;
    int     m_property1;

    void    method( int in )
    {
    }
};

Foo        foo;
Foo*       foo0 = &foo;

foo.
-----
    ^  Execute a manual completion here.

foo->
------
     ^  Execute a manual completion here.

Foo::
------
     ^  Execute a manual completion here.

foo.m_pro
----------
         ^  Execute a manual completion here.

Also, if you want to completion the method of Objective-C/C++, you can only manually completion.

id obj = [[NSString alloc] init];
[obj 
------
     ^  Execute a manual completion here.

When manual completion is invalidate or keybind change, it will set as follows.

;; disable
(setq ac-clang-async-autocompletion-manualtrigger-key nil)
;; other key
(setq ac-clang-async-autocompletion-manualtrigger-key "M-:")

BriefComment Display

It is displayed by default setting.
To invalidate the display, remove the BriefComment flag from the following variables.

The flags of BriefComment are as follows.
clang-server-translation-unit-flags is CXTranslationUnit_IncludeBriefCommentsInCodeCompletion
clang-server-complete-at-flags is CXCodeComplete_IncludeBriefComments

About types and performance of completion candidate quick help window

The quick help window displays argument information etc of completion candidate.
There are two quick help window, popup.el and pos-tip.el.
By default, popup is used.
To change the popup window, set as follows.

;; popup(default)
(setq ac-clang-quick-help-prefer-pos-tip-p nil)
;; pos-tip
(setq ac-clang-quick-help-prefer-pos-tip-p t)
  • popup
    Although it is lightweight and scroll response is also good, the window may occasionally shift.
  • pos-tip
    I am using x-show-tip, and it looks nice and looks rich.
    But scroll behavior is heavyweight.
    Scrolling performance will degrade if you scroll by a large amount with a lot of completion candidates.

Jump and return for definition/declaration/inclusion-file

In the activated buffer, you move the cursor at word that want to jump.
Execute following, you can jump to the source file that the class / method / function / enum / macro did definition or declaration.
This is possible jump to inclusion file.

(ac-clang-jump-smart)

Keybind is "M-,"

The return operation is possible in the following.

(ac-clang-jump-back)

Keybind is "M-,"

The jump history is stacked, enabling continuous jump and continuous return.
If you execute jump operation in non-activation buffer, that buffer is automatically activated and jump.

  • (ac-clang-jump-smart)
    1st priority jump location is the definition.
    But if the definition is not found, it will jump to the declaration.
    Jump to inclusion-file.( Please run the command on the #include keyword )
  • (ac-clang-jump-inclusion)
    Jump to inclusion-file.
  • (ac-clang-jump-definition)
    Jump to definition.
  • (ac-clang-jump-declaration)
    Jump to declaration.

Limitation

Jump for definition(ac-clang-jump-definition / ac-clang-jump-smart) is not perfect.

The function / class method are subject to restrictions.
struct/class/typedef/template/enum/class-variable/global-variable/macro/preprocessor don't have problem.
libclang analyze current buffer and including headers by current buffer, and decide jump location from result.
Therefore, when function definition and class method definition is described in including headers, it is possible to jump.
If it is described in c / cpp, it is impossible to jump. Because libclang can't know c / cpp.
1st priority jump location is the declaration.
But if the declaration is not found, it will jump to the definition.
When emphasizing a definition jump, I'll recommend you use with GNU global(GTAGS).

Known Issues

nothing