clang-tags
C/C++ source code indexing tool based on libclang
 All Classes Functions Variables Typedefs Groups Pages
application.hxx
1 #pragma once
2 
3 #include "storage.hxx"
4 #include "libclang++/libclang++.hxx"
5 #include <map>
6 #include <iostream>
7 
8 class Application {
9 public:
10  Application (Storage & storage)
11  : storage_ (storage)
12  {
13  const size_t size = 4096;
14  cwd_ = new char[size];
15  if (getcwd (cwd_, size) == NULL) {
16  // FIXME: correctly handle this case
17  throw std::runtime_error ("Not enough space to store current directory name.");
18  }
19  }
20 
21  ~Application () {
22  delete[] cwd_;
23  }
24 
25 
27  std::string fileName;
28  };
29  void compilationDatabase (CompilationDatabaseArgs & args, std::ostream & cout);
30 
31 
32  struct IndexArgs {
33  std::vector<std::string> exclude;
34  bool diagnostics;
35  };
36  void index (IndexArgs & args, std::ostream & cout);
37  void update (IndexArgs & args, std::ostream & cout);
38 
39 
41  std::string fileName;
42  int offset;
43  bool diagnostics;
44  bool mostSpecific;
45  bool fromIndex;
46  };
47  void findDefinition (FindDefinitionArgs & args, std::ostream & cout);
48 
49 
50  struct GrepArgs {
51  std::string usr;
52  };
53  void grep (const GrepArgs & args, std::ostream & cout);
54 
55 
56  struct CompleteArgs {
57  std::string fileName;
58  int line;
59  int column;
60  };
61  void complete (CompleteArgs & args, std::ostream & cout);
62 
63 
64 private:
65  void updateIndex_ (IndexArgs & args, std::ostream & cout);
66  void findDefinitionFromIndex_ (FindDefinitionArgs & args, std::ostream & cout);
67  void findDefinitionFromSource_ (FindDefinitionArgs & args, std::ostream & cout);
68 
69  LibClang::TranslationUnit & translationUnit_ (std::string fileName) {
70  std::string directory;
71  std::vector<std::string> clArgs;
72  storage_.getCompileCommand (fileName, directory, clArgs);
73 
74  // chdir() to the correct directory
75  // (whether we need to parse the TU for the first time or reparse it)
76  chdir (directory.c_str());
77 
78  auto it = tu_.find (fileName);
79  if (it == tu_.end()) {
80  LibClang::TranslationUnit tu = index_.parse (clArgs);
81  return tu_.insert (std::make_pair (fileName, tu)).first->second;
82  } else {
83  it->second.reparse();
84  return it->second;
85  }
86  }
87 
88  Storage & storage_;
89  LibClang::Index index_;
90  std::map<std::string, LibClang::TranslationUnit> tu_;
91  char* cwd_;
92 };