EditedSource.h revision 6bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89
1//===----- EditedSource.h - Collection of source edits ----------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLVM_CLANG_EDIT_EDITEDSOURCE_H
11#define LLVM_CLANG_EDIT_EDITEDSOURCE_H
12
13#include "clang/Edit/FileOffset.h"
14#include "llvm/ADT/DenseMap.h"
15#include "llvm/ADT/StringRef.h"
16#include "llvm/Support/Allocator.h"
17#include <map>
18
19namespace clang {
20  class LangOptions;
21  class PPConditionalDirectiveRecord;
22
23namespace edit {
24  class Commit;
25  class EditsReceiver;
26
27class EditedSource {
28  const SourceManager &SourceMgr;
29  const LangOptions &LangOpts;
30  const PPConditionalDirectiveRecord *PPRec;
31
32  struct FileEdit {
33    StringRef Text;
34    unsigned RemoveLen;
35
36    FileEdit() : RemoveLen(0) {}
37  };
38
39  typedef std::map<FileOffset, FileEdit> FileEditsTy;
40  FileEditsTy FileEdits;
41
42  llvm::DenseMap<unsigned, SourceLocation> ExpansionToArgMap;
43
44  llvm::BumpPtrAllocator StrAlloc;
45
46public:
47  EditedSource(const SourceManager &SM, const LangOptions &LangOpts,
48               const PPConditionalDirectiveRecord *PPRec = nullptr)
49    : SourceMgr(SM), LangOpts(LangOpts), PPRec(PPRec),
50      StrAlloc() { }
51
52  const SourceManager &getSourceManager() const { return SourceMgr; }
53  const LangOptions &getLangOpts() const { return LangOpts; }
54  const PPConditionalDirectiveRecord *getPPCondDirectiveRecord() const {
55    return PPRec;
56  }
57
58  bool canInsertInOffset(SourceLocation OrigLoc, FileOffset Offs);
59
60  bool commit(const Commit &commit);
61
62  void applyRewrites(EditsReceiver &receiver);
63  void clearRewrites();
64
65  StringRef copyString(StringRef str) {
66    char *buf = StrAlloc.Allocate<char>(str.size());
67    std::memcpy(buf, str.data(), str.size());
68    return StringRef(buf, str.size());
69  }
70  StringRef copyString(const Twine &twine);
71
72private:
73  bool commitInsert(SourceLocation OrigLoc, FileOffset Offs, StringRef text,
74                    bool beforePreviousInsertions);
75  bool commitInsertFromRange(SourceLocation OrigLoc, FileOffset Offs,
76                             FileOffset InsertFromRangeOffs, unsigned Len,
77                             bool beforePreviousInsertions);
78  void commitRemove(SourceLocation OrigLoc, FileOffset BeginOffs, unsigned Len);
79
80  StringRef getSourceText(FileOffset BeginOffs, FileOffset EndOffs,
81                          bool &Invalid);
82  FileEditsTy::iterator getActionForOffset(FileOffset Offs);
83};
84
85}
86
87} // end namespace clang
88
89#endif
90