Is there an alternative to ctags that works better? [closed]
Asked Answered
A

5

41

I have just discovered the taglist plugin for vim, and read about how to use it with ctags.

However, disappointingly ctags is a very simple parser.

Is there an alternative that is more complete?

Specifically I'm looking for something that:

  • expands #define(x, y) x ## y style macros for function declarations

  • processes #include statements

  • allows include paths to be specified for dependencies

I see that clang provides a programatic api for accessing the c AST, so surely this isn't terribly hard to do?

Has someone already done it?

--

Edit:

These ones don't cut it:

  • clang_indexer - Doesn't compile; when it does (after hacking), doesn't work (endless errors).

  • clang_complete - Doesn't seem any better than ctags. No context specific recommendations, no struct completion, no function arguments, no macro expansion; just a list of symbols and the file they came from.

Agonist answered 13/10, 2011 at 1:33 Comment(5)
I don't know how clang_complete handles macros, but apart from that I am happy with it since a long time. The points you were unhappy with don't come from the same clang_complete that I use daily (from github).Isotropic
It doesn't do any of the things I've listed above with the version from github I just pulled. Control-N lists every symbol in every file its parsed. No context, no filtering except the first N characters you've typed. How do you get it to do magic like say, suggest context specific expansions for struct N *n; n->(pop up here)?Agonist
Anyway, that doesn't matter; I'm specifically looking for macro expansion, in the form described above. I don't really care that much about context specific completion. I just want suggestions with actual function names, when the function is defined as: #define blah(x) blah__ ## x // void blah(func) (int); as blah__func NOT as blah(func).Agonist
You are right, clang_complete doesn't work with your macro definition. For everything else try Ctrl-x Ctrl-o.Isotropic
now it is a world of language server: langserver.org but each language has its own server.Sighted
W
25

I've spent quite some time struggling with this myself.

The closest I ever got was something called gccsense. Unfortunately, the project seems abandoned and moreover it was difficult setting it up because English was not the author's first language.

I ended up approaching the problem from another angle. I made the decision that intellisense/autocomplete was more important to my coding than having all the available features of vim, so I chose an IDE like Eclipse, and then found a plugin for Eclipse that emulates Vim. So far the best kind of plugin like that that I found was Viable.

Here is the full list of options that I have tried and found unsatisfactory:

  • clang - requires you switch from gcc to a different and "better" compiler. The problem is gcc is much more mature [edit apparently you don't need to switch compilers see comments below, I may give this another try in the future.]
  • gccsense - great idea (using gcc to give you the code completion) however work on the project is abandoned :( and the version that is up is beta quality
  • xref in vim - xref is a great standalone tool and works great for parsing C. It can be made to work in vim with vxref, however from my experience xref lacks in parsing current C++ code and development on it has stopped (as well as development on vxref.)
  • eclim - seems to work great for Java support using eclipse, extremely slow and completely unreliable when parsing C++ or C code. What usually happens is everything works for a long while, but then suddenly, the parser stops parsing any new code that you write, and nothing short of loading up eclipse itself and forcing eclipse to reparse the project seems to help. Also, less of an important fact, but more of an annoyance is that eclim takes over handling errors, so it screws up the way vim usually parses errors from gcc meaning you have no access to the quickfix list which is annoying.
  • netbeans + jvi - alot of people swear by this, but I had all sorts of problems with jvi. One major problem I had was jvi would say I'm in normal mode, but really was in insert mode, nothing short of a restart would help.
  • eclipse + viplugin/vrapper - this was beginning to look like the best option; each had its own set of bugs + lacking features, but still was most attractive, until I found viable which seemed to be the most stable and have the most features.

If you do find a solution you are happy with please share it in a comment, because I would be interested in it.

Wiliness answered 18/10, 2011 at 17:32 Comment(9)
Your statement about clang is wrong -- one doesn't need to switch compilers to use clang_complete.Isotropic
If you don't aren't you risking writing code compatible with llvm but not gcc? Also, isn't clang a frontend for llvm not gcc? Seems backwards to use it for gcc.Wiliness
clang_complete uses a libclang to parse C++ or C code, i.e. clang isn't called. Also clang supports all the code written for g++ I came across (maybe after disabling some warnings). But like I wrote, clang isn't called by clang_complete.Isotropic
sadly nothing seems to fill my macro expansion needs, but I'm accepting this as the most comprehensive answer.Agonist
I personally use Vim if it succeeds 90% (tagbar for obj-c files is the other 10%). For coding in languages where Vim falls short, I also went with IDE + Vim plugin. No, you don't get the full benefit of VIM, but inbuilt IDE features like file navigation, code structure views (i.e. tagbar) and simple refactoring might be worth more to you. For Objective-C, Vim plugins include: XVim for XCode OR IdeaVim for AppCode. With the right key mappings, you could get the best of both worlds.Lindsy
since then, the constrary can be done: using intellij omnicompletion IN vim :) pretty much a POC which i didn't have time to test, but here it is: github.com/JetBrains/hackathon-EmbeditorScutage
YouCompleteMe is a newer autocompletion engine and general IDE-replacement thing that uses Clang underneath the hood. It can be a bit tricky to get working, but it is really really awesome. It's arguably not even Vim-specific, since it's mostly just a standalone server with some hooks into Vim, but I don't think any other editors or IDEs are making use of it yet. @Doug, you might also be interested in checking it out.Usurpation
@Doug: I know this is quite late, but I'm pretty sure you can accomplish macro expansion via gcc, from the manual: "-E Stop after the preprocessing stage; do not run the compiler proper. The output is in the form of preprocessed source code, which is sent to the standard output."Wiliness
@Wiliness That's actually a great tip. It actually works well~Agonist
I
7

I haven't tried this myself yet, but clang_indexer seems very close to what you are looking for (sources). It still uses an external database (i.e. doesn't index on the fly per se), but it seems to offer functionality one usually only finds in cscope (which doesn't support C++ too well).

For autocomplete I use clang_complete. It doesn't require generating an index and works most of the time pretty well. It also can do automatic syntax checking in vim so one is warned of possible issues. It doesn't autocomplete the macro example you gave

#define blah(x) blah__ ## x
void blah_<TAB>

as bla__x though (only as blah(x)). It would be nice for this to be more configurable, but I wouldn't consider this broken either.

Isotropic answered 13/10, 2011 at 1:39 Comment(1)
I tried clang_indexer myself, and finally got it working (this really is alpha quality at best and needed a lot of patching), but it seems to only give basic symbol usage information.Isotropic
R
4

CScout version 2.8 offers a command-line option (-C) that will create a vim-compatible tags file for the C source code it will process. CScout is a source code analyzer and refactoring browser for collections of C programs. It can process workspaces of multiple projects mapping the complexity introduced by the C preprocessor back into the original C source code files. Consequently, the generated tags file contains correct information for entities, like functions, variables, and structures, that are created through preprocessor macros. CScout will process include files, using the include file paths specified in the code's project configuration file (the equivalent of a Makefile). To try it out, download the package matching your setup, go into the example directory containing the awk source code and run

../bin/cscout -Cc awk.cs

You can see some types of preprocessor constructs that CScout can handle in this page. For example, if you process the following code

#define typefun(name, type, op) \
type type ## _ ## name(type a, type b) { return a op b; }

typefun(add, int, +)
typefun(sub, int, -)
typefun(mul, int, *)
typefun(div, int, /)
typefun(add, double, +)
typefun(sub, double, -)
typefun(mul, double, *)
typefun(div, double, /)

main()
{
        printf("%d\n", int_add(5, 4));
        printf("%g\n", double_mul(3.14, 2.0));
}

CScout will generate a tags file with the following entries.

double_add  test.c  8       ;"  f
double_div  test.c  11      ;"  f
double_mul  test.c  10      ;"  f
double_sub  test.c  9       ;"  f
int_add test.c  4       ;"  f
int_div test.c  7       ;"  f
int_mul test.c  6       ;"  f
int_sub test.c  5       ;"  f
main    test.c  13      ;"  f
typefun test.c  1       ;"  d

You can try it out yourself by adding a few code-generating macros in the example source code to see the tags CScout will create.

Reeva answered 23/10, 2011 at 20:57 Comment(2)
Does CScout only work with C or does it work with C++ as well? Would be an interesting option, though C++ is a far more complex grammar than C.Wiliness
Currently C (with gcc and Microsoft C extensions) and yacc/bison. As you say, C++ is a lot more complex.Reeva
M
1

One possibility is to use Eclim. It is a powerful tool that can be used in many ways; in your case you would have to start a headless Eclipse server and basically use Eclipse's indexer etc. I think you need to have your project registered as an Eclipse project though.

Moly answered 18/10, 2011 at 17:29 Comment(0)
M
1

I use 'global' system. After you install it you have 'global' command to search your code and 'gtags' for indexing.

I work with Linux kernel and it helps a lot: it works with defines more intelligent (but still not as good as I want it) and it knows about function prototype, function implementation and all references to the function from the code.

It's also possible to integrate 'global' with VIM, there is a plugin for it:

Good luck.

Mimimimic answered 8/12, 2013 at 9:30 Comment(2)
Are you still using global?Lauretta
Yes, I do. Actually, I even developed some automatic code scanners based on global/gtags.Mimimimic

© 2022 - 2024 — McMap. All rights reserved.