EditedSource.h revision 7ba87f38462dde08e4962936a7612b7869e06c0f
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/Support/Allocator.h"
15#include "llvm/ADT/DenseMap.h"
16#include "llvm/ADT/StringRef.h"
17#include <map>
18
19namespace clang {
20  class LangOptions;
21  class PreprocessingRecord;
22
23namespace edit {
24  class Commit;
25  class EditsReceiver;
26
27class EditedSource {
28  const SourceManager &SourceMgr;
29  const LangOptions &LangOpts;
30  const PreprocessingRecord *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 PreprocessingRecord *PPRec = 0)
49    : SourceMgr(SM), LangOpts(LangOpts), PPRec(PPRec),
50      StrAlloc(/*size=*/512) { }
51
52  const SourceManager &getSourceManager() const { return SourceMgr; }
53  const LangOptions &getLangOptions() const { return LangOpts; }
54  const PreprocessingRecord *getPreprocessingRecord() const { return PPRec; }
55
56  bool canInsertInOffset(SourceLocation OrigLoc, FileOffset Offs);
57
58  bool commit(const Commit &commit);
59
60  void applyRewrites(EditsReceiver &receiver);
61  void clearRewrites();
62
63  StringRef copyString(StringRef str) {
64    char *buf = StrAlloc.Allocate<char>(str.size());
65    std::memcpy(buf, str.data(), str.size());
66    return StringRef(buf, str.size());
67  }
68  StringRef copyString(const Twine &twine);
69
70private:
71  bool commitInsert(SourceLocation OrigLoc, FileOffset Offs, StringRef text,
72                    bool beforePreviousInsertions);
73  bool commitInsertFromRange(SourceLocation OrigLoc, FileOffset Offs,
74                             FileOffset InsertFromRangeOffs, unsigned Len,
75                             bool beforePreviousInsertions);
76  void commitRemove(SourceLocation OrigLoc, FileOffset BeginOffs, unsigned Len);
77
78  StringRef getSourceText(FileOffset BeginOffs, FileOffset EndOffs,
79                          bool &Invalid);
80  FileEditsTy::iterator getActionForOffset(FileOffset Offs);
81};
82
83}
84
85} // end namespace clang
86
87#endif
88