summaryrefslogtreecommitdiff
path: root/elpa/irony-20220110.849/server/src/TUManager.h
blob: bd7730ddef28bcd207e1748d325281555a7738f1 (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
/**-*-C++-*-
 * \file
 * \author Guillaume Papin <guillaume.papin@epitech.eu>
 *
 * \brief Translation Unit manager.
 *
 * Keeps a cache of translation units, reparsing or recreating them as
 * necessary.
 *
 * This file is distributed under the GNU General Public License. See
 * COPYING for details.
 */

#ifndef IRONY_MODE_SERVER_TUMANAGER_H_
#define IRONY_MODE_SERVER_TUMANAGER_H_

#include "support/CIndex.h"
#include "support/NonCopyable.h"

#include <map>
#include <string>
#include <vector>

class TUManager : public util::NonCopyable {
public:
  TUManager();
  ~TUManager();

  /**
   * \brief Parse \p filename with flag \p flags.
   *
   * The first time call \c clang_parseTranslationUnit() and save the TU in the
   * member \c translationUnits_, The next call with the same \p filename will
   * call \c clang_reparseTranslationUnit().
   *
   * usage:
   * \code
   * std::vector<std::string> flags;
   * flags.push_back("-I../utils");
   * CXTranslationUnit tu = tuManager.parse("file.cpp", flags);
   *
   * if (! tu)
   *   std::cerr << "parsing translation unit failed\n";
   * \endcode
   *
   * \return The translation unit, if the parsing failed the translation unit
   *         will be \c NULL.
   */
  CXTranslationUnit parse(const std::string &filename,
                          const std::vector<std::string> &flags,
                          const std::vector<CXUnsavedFile> &unsavedFiles);

  /**
   * \brief Retrieve, creating it if necessary the TU associated to filename.
   *
   * \return The translation unit. Will be NULL if the translation unit couldn't
   *         be created.
   */
  CXTranslationUnit getOrCreateTU(const std::string &filename,
                          const std::vector<std::string> &flags,
                          const std::vector<CXUnsavedFile> &unsavedFiles);

  /**
   * \brief Invalidate a given cached TU, the next use of a TU will require
   *        reparsing.
   *
   * This can be useful for example: when the flags used to compile a file have
   * changed.
   *
   * \param filename    The filename for which the associated
   *                    translation unit flags need to be invalidated.
   *
   * \sa invalidateAllCachedTUs()
   */
  void invalidateCachedTU(const std::string &filename);

  /**
   * \brief Invalidate all cached TU, the next use of a TU will require
   *        reparsing.
   *
   * \sa invalidateCachedTU()
   */
  void invalidateAllCachedTUs();

private:
  /**
   * \brief Get a reference to the translation unit that matches \p filename
   *        with the given set of flags.
   *
   * The TU will be null if it has never been parsed or if the flags have
   * changed.
   *
   * \todo Find a proper name.
   */
  CXTranslationUnit &tuRef(const std::string &filename,
                           const std::vector<std::string> &flags);

private:
  typedef std::map<const std::string, CXTranslationUnit> TranslationUnitsMap;
  typedef std::map<const std::string, std::vector<std::string>> FilenameFlagsMap;

private:
  CXIndex index_;
  TranslationUnitsMap translationUnits_; // cache variable
  FilenameFlagsMap flagsPerFileCache_;
  unsigned parseTUOptions_;
};

#endif /* !IRONY_MODE_SERVER_TUMANAGER_H_ */