summaryrefslogtreecommitdiff
path: root/elpa/irony-20220110.849/server/src/TUManager.cpp
blob: afbdc828a0b7ce9df929eddf7b3386c74c65692e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
 * \file
 * \author Guillaume Papin <guillaume.papin@epitech.eu>
 *
 * \brief See TUManager.hh
 *
 * This file is distributed under the GNU General Public License. See
 * COPYING for details.
 *
 */

#include "TUManager.h"

#include <iostream>

TUManager::TUManager()
  : index_(clang_createIndex(0, 0))
  , translationUnits_()
  , parseTUOptions_(clang_defaultEditingTranslationUnitOptions()) {

  // this seems necessary to trigger "correct" reparse (/codeCompleteAt)
  // clang_reparseTranslationUnit documentation states:
  //
  // >  * \param TU The translation unit whose contents will be re-parsed. The
  // >  * translation unit must originally have been built with
  // >  * \c clang_createTranslationUnitFromSourceFile().
  //
  // clang_createTranslationUnitFromSourceFile() is just a call to
  // clang_parseTranslationUnit() with
  // CXTranslationUnit_DetailedPreprocessingRecord enabled but because we want
  // some other flags to be set we can't just call
  // clang_createTranslationUnitFromSourceFile()
  parseTUOptions_ |= CXTranslationUnit_DetailedPreprocessingRecord;

#if HAS_BRIEF_COMMENTS_IN_COMPLETION
  parseTUOptions_ |= CXTranslationUnit_IncludeBriefCommentsInCodeCompletion;
#endif

  // XXX: A bug in old version of Clang (at least '3.1-8') caused the completion
  // to fail on the standard library types when
  // CXTranslationUnit_PrecompiledPreamble is used. We disable this option for
  // old versions of libclang. As a result the completion will work but
  // significantly slower.
  //
  // -- https://github.com/Sarcasm/irony-mode/issues/4
  if (CINDEX_VERSION < 6) {
    parseTUOptions_ &= ~CXTranslationUnit_PrecompiledPreamble;
  }

#if CINDEX_VERSION >= 34
  // Keep going even after fatal errors, or syntax checking will stop after the
  // first error. For instance unused variables will not be reported until the
  // error has been fixed.
  parseTUOptions_ |= CXTranslationUnit_KeepGoing;
#endif
}

TUManager::~TUManager() {
  clang_disposeIndex(index_);
}

CXTranslationUnit &TUManager::tuRef(const std::string &filename,
                                    const std::vector<std::string> &flags) {
  CXTranslationUnit &tu = translationUnits_[filename];

  // if the flags changed since the last time, invalidate the translation unit
  auto &flagsCache = flagsPerFileCache_[filename];
  if (flagsCache.size() != flags.size() ||
      !std::equal(flagsCache.begin(), flagsCache.end(), flags.begin())) {
    if (tu) {
      clang_disposeTranslationUnit(tu);
      tu = nullptr;
    }
    // remember the flags for the next parse
    flagsCache = flags;
  }
  return tu;
}

CXTranslationUnit
TUManager::parse(const std::string &filename,
                 const std::vector<std::string> &flags,
                 const std::vector<CXUnsavedFile> &unsavedFiles) {
  CXTranslationUnit &tu = tuRef(filename, flags);

  if (tu == nullptr) {
    std::vector<const char *> argv;

#ifdef CLANG_RESOURCE_DIR
    // Make sure libclang find its builtin headers, this is a known issue with
    // libclang, see:
    // - http://lists.cs.uiuc.edu/pipermail/cfe-dev/2012-July/022893.html
    //
    // > Make sure that Clang is using its own . It will be in a directory
    // > ending in clang/3.2/include/ where 3.2 is the version of clang that you
    // > are using. You may need to explicitly add it to your header search.
    // > Usually clang finds this directory relative to the executable with
    // > CompilerInvocation::GetResourcesPath(Argv0, MainAddr), but using just
    // > the libraries, it can't automatically find it.
    argv.push_back("-resource-dir");
    argv.push_back(CLANG_RESOURCE_DIR);
#endif

    for (auto &flag : flags) {
      argv.push_back(flag.c_str());
    }

    tu = clang_parseTranslationUnit(
        index_,
        filename.c_str(),
        argv.data(),
        static_cast<int>(argv.size()),
        const_cast<CXUnsavedFile *>(unsavedFiles.data()),
        unsavedFiles.size(),
        parseTUOptions_);
  }

  if (tu == nullptr) {
    std::clog << "error: libclang couldn't parse '" << filename << "'\n";
    return nullptr;
  }

  // Reparsing is necessary to enable optimizations.
  //
  // From the clang mailing list (cfe-dev):
  // From: Douglas Gregor
  // Subject: Re: Clang indexing library performance
  // ...
  // You want to use the "default editing options" when parsing the translation
  // unit
  //    clang_defaultEditingTranslationUnitOptions()
  // and then reparse at least once. That will enable the various
  // code-completion optimizations that should bring this time down
  // significantly.
  if (clang_reparseTranslationUnit(
          tu,
          unsavedFiles.size(),
          const_cast<CXUnsavedFile *>(unsavedFiles.data()),
          clang_defaultReparseOptions(tu))) {
    // a 'fatal' error occured (even a diagnostic is impossible)
    clang_disposeTranslationUnit(tu);
    std::clog << "error: libclang couldn't reparse '" << filename << "'\n";
    tu = 0;
    return nullptr;
  }

  return tu;
}

CXTranslationUnit
TUManager::getOrCreateTU(const std::string &filename,
                         const std::vector<std::string> &flags,
                         const std::vector<CXUnsavedFile> &unsavedFiles) {
  if (auto tu = tuRef(filename, flags))
    return tu;

  return parse(filename, flags, unsavedFiles);
}

void TUManager::invalidateCachedTU(const std::string &filename) {
  TranslationUnitsMap::iterator it = translationUnits_.find(filename);

  if (it != translationUnits_.end()) {
    if (CXTranslationUnit &tu = it->second)
      clang_disposeTranslationUnit(tu);

    translationUnits_.erase(it);
  }
}

void TUManager::invalidateAllCachedTUs() {
  TranslationUnitsMap::iterator it = translationUnits_.begin();

  while (it != translationUnits_.end()) {
    if (CXTranslationUnit &tu = it->second) {
      clang_disposeTranslationUnit(tu);
      translationUnits_.erase(it++); // post-increment keeps the iterator valid
    } else {
      ++it;
    }
  }
}