Skip to content

Instantly share code, notes, and snippets.

@retorillo
Created February 28, 2018 09:55
Show Gist options
  • Save retorillo/a712a0dc6e817d3631421bb04526820f to your computer and use it in GitHub Desktop.
Save retorillo/a712a0dc6e817d3631421bb04526820f to your computer and use it in GitHub Desktop.
%INCLUDE%フォルダ上のヘッダファイルに対してtagsを生成する(Windows/ctags)

%INCLUDE%フォルダ上のヘッダファイルに対してtagsを生成する(Windows/ctags)

目的

ctagsコマンドで%INCLUDE%フォルダ上のヘッダーの定義情報をファイル(デフォルト名 tags)に格納しVimなどからリファレンスとして活用できるようにします。

要件

  • Visual Studio 2017 CommunityなどでC++開発環境がインストールされていること
  • Universal CTAGSctagsおよびreadtagsコマンドのPATHが通ってること

tagsファイルの作成

まず、開発用コマンドプロンプトを起動するか vcinitなどで%INCLUDE%などの変数にアクセスできるようにしてください。

そこで次のコマンドを適当な書き込み権限のあるフォルダで実行します。

ctags --extras=+q -h "..h" --langmap="C++:..h" -R "%INCLUDE:;=" "%"

処理には時間がかかります。ディスク容量(環境次第ですが100MB~)なども注意してください。 これでtagsファイルが作成されます。ここでの注意点はctagsのオプション-h "..h"--langmap=C++:..hです。

  • まずデフォルトでは拡張子のないファイルは読み取られませんので-h "..h"として.hおよび.すなわち拡張子のないファイルを読み取ることを指定します。(ctagsでは空のドットが拡張子のないファイルという意味になります)
  • 次に--langmap=C++:..hも同様に.hおよび拡張子のないファイルがC++言語のファイルであることを指定します。ctagsは拡張子で言語を判定しますのでこの指定がなければやはりタグが生成されません。また、ここでC++:ではなくC:としてしまうと、C++固有の箇所がタグ生成されませんので注意してください。

以上のことからこの2つのオプション指定がないと拡張子のないヘッダーファイル(standard libraryなど)が含まれません。詳細はtags --helpにてご確認ください。

tagsファイルの生成が完了したらreadtagコマンドなどで確認してください。

readtag CoCreateInstance
readtag vector
readtag wstring

拡張子のないファイル(vectorなど)もタグ付けできているはずです。 Visual Studioで頒布されるヘッダーファイルではhppなどの拡張子はなかったはずですが-hオプションは必要に応じよくご確認ください

なお、--extraについてはctags --list-extrasコマンドで確認できます。必要に応じご調整ください。

ctags --list-extras
#LETTER NAME              ENABLED LANGUAGE FIXED DESCRIPTION
-       subword           no      NONE     no    Include tags for subwords generated by splitting the original tag (only for ctags development)
F       fileScope         yes     NONE     no    Include tags of file scope
f       inputFile         no      NONE     no    Include an entry for the base file name of every input file
g       guest             no      NONE     no    Include tags generated by guest parsers
p       pseudo            yes     NONE     no    Include pseudo tags
q       qualified         no      NONE     no    Include an extra class-qualified tag entry for each tag
r       reference         no      NONE     no    Include reference tags
s       subparser         yes     NONE     no    Include tags generated by subparsers
-       funcmap           yes     QemuHX   no    Include mapping SQMP to C function name
-       whitespaceSwapped yes     Robot    no    Include tags swapping whitespace and underscore chars
@masatake
Copy link

拡張子が無いファイルであってもファイル中にヒントがあれば、それを利用できます。
例えば // -- C++ -- と書かれていれば ctags に -G オプションをつけると、C++パーザを
選択してくれます。

[jet@localhost]/usr/include/c++/7% head vector 
// <vector> -*- C++ -*-

// Copyright (C) 2001-2017 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

[jet@localhost]/usr/include/c++/7% ~/var/ctags/ctags --print-language vector 
vector: NONE
[jet@localhost]/usr/include/c++/7% ~/var/ctags/ctags -G --print-language vector
vector: C++
[jet@localhost]/usr/include/c++/7% 

@retorillo
Copy link
Author

コメントありがとうざいます!もしかすると使い方が間違っているかもしれませんが、確認してみたところVisual Studioで提供されるファイルには言語のヒントがないようで-Gを指定しても言語がうまく特定できないようでした。

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.13.26128\include>findstr C++ vector

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.13.26128\include>ctags -G --print-language vector
vector: NONE

Linuxで実行する際には参考にさせていただきます!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment