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 
19 namespace clang {
20   class LangOptions;
21   class PPConditionalDirectiveRecord;
22 
23 namespace edit {
24   class Commit;
25   class EditsReceiver;
26 
27 class EditedSource {
28   const SourceManager &SourceMgr;
29   const LangOptions &LangOpts;
30   const PPConditionalDirectiveRecord *PPRec;
31 
32   struct FileEdit {
33     StringRef Text;
34     unsigned RemoveLen;
35 
FileEditFileEdit36     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 
46 public:
47   EditedSource(const SourceManager &SM, const LangOptions &LangOpts,
48                const PPConditionalDirectiveRecord *PPRec = nullptr)
SourceMgr(SM)49     : SourceMgr(SM), LangOpts(LangOpts), PPRec(PPRec),
50       StrAlloc() { }
51 
getSourceManager()52   const SourceManager &getSourceManager() const { return SourceMgr; }
getLangOpts()53   const LangOptions &getLangOpts() const { return LangOpts; }
getPPCondDirectiveRecord()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 
copyString(StringRef str)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 
72 private:
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