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   const bool ForceCommitInSystemHeader;
32 
33   struct FileEdit {
34     StringRef Text;
35     unsigned RemoveLen;
36 
37     FileEdit() : RemoveLen(0) {}
38   };
39 
40   typedef std::map<FileOffset, FileEdit> FileEditsTy;
41   FileEditsTy FileEdits;
42 
43   llvm::DenseMap<unsigned, SourceLocation> ExpansionToArgMap;
44 
45   llvm::BumpPtrAllocator StrAlloc;
46 
47 public:
48   EditedSource(const SourceManager &SM, const LangOptions &LangOpts,
49                const PPConditionalDirectiveRecord *PPRec = 0,
50                const bool FCommitInSystemHeader = true)
51     : SourceMgr(SM), LangOpts(LangOpts), PPRec(PPRec),
52       ForceCommitInSystemHeader(FCommitInSystemHeader),
53       StrAlloc(/*size=*/512) { }
54 
55   const SourceManager &getSourceManager() const { return SourceMgr; }
56   const LangOptions &getLangOpts() const { return LangOpts; }
57   const PPConditionalDirectiveRecord *getPPCondDirectiveRecord() const {
58     return PPRec;
59   }
60 
61   bool getForceCommitInSystemHeader() const {
62     return ForceCommitInSystemHeader;
63   }
64 
65   bool canInsertInOffset(SourceLocation OrigLoc, FileOffset Offs);
66 
67   bool commit(const Commit &commit);
68 
69   void applyRewrites(EditsReceiver &receiver);
70   void clearRewrites();
71 
72   StringRef copyString(StringRef str) {
73     char *buf = StrAlloc.Allocate<char>(str.size());
74     std::memcpy(buf, str.data(), str.size());
75     return StringRef(buf, str.size());
76   }
77   StringRef copyString(const Twine &twine);
78 
79 private:
80   bool commitInsert(SourceLocation OrigLoc, FileOffset Offs, StringRef text,
81                     bool beforePreviousInsertions);
82   bool commitInsertFromRange(SourceLocation OrigLoc, FileOffset Offs,
83                              FileOffset InsertFromRangeOffs, unsigned Len,
84                              bool beforePreviousInsertions);
85   void commitRemove(SourceLocation OrigLoc, FileOffset BeginOffs, unsigned Len);
86 
87   StringRef getSourceText(FileOffset BeginOffs, FileOffset EndOffs,
88                           bool &Invalid);
89   FileEditsTy::iterator getActionForOffset(FileOffset Offs);
90 };
91 
92 }
93 
94 } // end namespace clang
95 
96 #endif
97