summaryrefslogtreecommitdiffstats
path: root/include/clang/Index/IndexDataStore.h
blob: b95bf4e0b062db3ca0b1c1c28e7a3c7cbda11bdf (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
//===--- IndexDataStore.h - Index data store info -------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_INDEX_INDEXDATASTORE_H
#define LLVM_CLANG_INDEX_INDEXDATASTORE_H

#include "clang/Basic/LLVM.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringRef.h"
#include <functional>
#include <memory>
#include <string>
#include <vector>

namespace clang {
namespace index {

class AbstractDirectoryWatcher {
public:
  enum class EventKind {
    /// A file was added.
    Added,
    /// A file was removed.
    Removed,
    /// A file was modified.
    Modified,
    /// The watched directory got deleted. No more events will follow.
    DirectoryDeleted,
  };

  struct Event {
    EventKind Kind;
    std::string Filename;
    timespec ModTime;
  };

  typedef std::function<void(ArrayRef<Event> Events, bool isInitial)>
      EventReceiver;
  typedef std::unique_ptr<AbstractDirectoryWatcher>(CreateFnTy)(
      StringRef Path, EventReceiver Receiver, bool waitInitialSync,
      std::string &Error);

  virtual ~AbstractDirectoryWatcher() {}
};

class IndexDataStore {
public:
  ~IndexDataStore();

  static std::unique_ptr<IndexDataStore> create(StringRef IndexStorePath,
                                                std::string &Error);

  StringRef getFilePath() const;
  bool foreachUnitName(bool sorted,
                       llvm::function_ref<bool(StringRef unitName)> receiver);

  static unsigned getFormatVersion();

  enum class UnitEventKind {
    Added,
    Removed,
    Modified,
    /// The directory got deleted. No more events will follow.
    DirectoryDeleted,
  };
  struct UnitEvent {
    UnitEventKind Kind;
    StringRef UnitName;
    timespec ModTime;
  };
  struct UnitEventNotification {
    bool IsInitial;
    ArrayRef<UnitEvent> Events;
  };
  typedef std::function<void(UnitEventNotification)> UnitEventHandler;

  void setUnitEventHandler(UnitEventHandler Handler);
  /// \returns true if an error occurred.
  bool startEventListening(
      llvm::function_ref<AbstractDirectoryWatcher::CreateFnTy> createFn,
      bool waitInitialSync, std::string &Error);
  void stopEventListening();

  void discardUnit(StringRef UnitName);
  void discardRecord(StringRef RecordName);

  void purgeStaleData();

private:
  IndexDataStore(void *Impl) : Impl(Impl) {}

  void *Impl; // An IndexDataStoreImpl.
};

} // namespace index
} // namespace clang

#endif