Permalink
Fetching contributors…
Cannot retrieve contributors at this time
398 lines (283 sloc) 13.6 KB

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.
The basic features same emacs-clang-complete-async.
But changed internal implementation method.

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 support
    Template parameters expand is possible at the time of arguments expand after completion.
  • Completion by manual operation support
    It can be completion at any position.
  • 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.
  • Debug Logger support
    Used for debugging.
    You are possible to confirm the message and the data that client was sent to clang-server.
  • Jump and return for inclusion file.
  • 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(clang4.0.0 RELEASE/FINAL) was built by Microsoft Visual Studio 2017/2015/2013
  • 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/2013, 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.

ac-clang/clang-server/binary/clang-server.exe
ac-clang/clang-server/library/x86_XX/release/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)

(ac-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 ac-clang-clang-translation-unit-flags FLAG-STRING)
(setq ac-clang-clang-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 (ac-clang-update-clang-parameters).

Configuration of CFLAGS

CFLAGS have to set to variable before ac-clang activation.

(setq ac-clang-cflags CFLAGS)

It's set by this.

Activation

To execute the completion you need to create the source code buffer session on clang-server.
CFLAGS set to ac-clang-cflags after following execution.
Run the activate function below after CFLAGS set to ac-clang-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 ac-clang-clang-translation-unit-flags FLAG-STRING)
(setq ac-clang-clang-complete-at-flags FLAG-STRING)
(ac-clang-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 ac-clang-cflags CFLAGS)
(ac-clang-update-cflags)

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

This has the same effect.
But (ac-clang-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 ac-clang-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 ac-clang-debug-log-buffer-size (* 1024 1000))

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

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

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-:")

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