EditedSource.h revision 263508
1181053Srwatson//===----- EditedSource.h - Collection of source edits ----------*- C++ -*-===//
2187215Srwatson//
3155192Srwatson//                     The LLVM Compiler Infrastructure
4155192Srwatson//
5155192Srwatson// This file is distributed under the University of Illinois Open Source
6155192Srwatson// License. See LICENSE.TXT for details.
7155192Srwatson//
8155192Srwatson//===----------------------------------------------------------------------===//
9155192Srwatson
10155192Srwatson#ifndef LLVM_CLANG_EDIT_EDITEDSOURCE_H
11155192Srwatson#define LLVM_CLANG_EDIT_EDITEDSOURCE_H
12155192Srwatson
13155192Srwatson#include "clang/Edit/FileOffset.h"
14155192Srwatson#include "llvm/ADT/DenseMap.h"
15155192Srwatson#include "llvm/ADT/StringRef.h"
16155192Srwatson#include "llvm/Support/Allocator.h"
17180701Srwatson#include <map>
18155192Srwatson
19155192Srwatsonnamespace clang {
20155192Srwatson  class LangOptions;
21155192Srwatson  class PPConditionalDirectiveRecord;
22155192Srwatson
23155192Srwatsonnamespace edit {
24155192Srwatson  class Commit;
25155192Srwatson  class EditsReceiver;
26155192Srwatson
27155192Srwatsonclass EditedSource {
28155192Srwatson  const SourceManager &SourceMgr;
29155192Srwatson  const LangOptions &LangOpts;
30155192Srwatson  const PPConditionalDirectiveRecord *PPRec;
31155192Srwatson  const bool ForceCommitInSystemHeader;
32155192Srwatson
33155192Srwatson  struct FileEdit {
34178186Srwatson    StringRef Text;
35178186Srwatson    unsigned RemoveLen;
36178186Srwatson
37196971Sphk    FileEdit() : RemoveLen(0) {}
38155192Srwatson  };
39155192Srwatson
40159259Srwatson  typedef std::map<FileOffset, FileEdit> FileEditsTy;
41155192Srwatson  FileEditsTy FileEdits;
42155192Srwatson
43155192Srwatson  llvm::DenseMap<unsigned, SourceLocation> ExpansionToArgMap;
44155192Srwatson
45155192Srwatson  llvm::BumpPtrAllocator StrAlloc;
46155192Srwatson
47155192Srwatsonpublic:
48155192Srwatson  EditedSource(const SourceManager &SM, const LangOptions &LangOpts,
49155192Srwatson               const PPConditionalDirectiveRecord *PPRec = 0,
50155192Srwatson               const bool FCommitInSystemHeader = true)
51155192Srwatson    : SourceMgr(SM), LangOpts(LangOpts), PPRec(PPRec),
52155192Srwatson      ForceCommitInSystemHeader(FCommitInSystemHeader),
53155192Srwatson      StrAlloc(/*size=*/512) { }
54155192Srwatson
55155192Srwatson  const SourceManager &getSourceManager() const { return SourceMgr; }
56155192Srwatson  const LangOptions &getLangOpts() const { return LangOpts; }
57155192Srwatson  const PPConditionalDirectiveRecord *getPPCondDirectiveRecord() const {
58155192Srwatson    return PPRec;
59155192Srwatson  }
60155192Srwatson
61155192Srwatson  bool getForceCommitInSystemHeader() const {
62155192Srwatson    return ForceCommitInSystemHeader;
63155192Srwatson  }
64155192Srwatson
65155192Srwatson  bool canInsertInOffset(SourceLocation OrigLoc, FileOffset Offs);
66155192Srwatson
67155192Srwatson  bool commit(const Commit &commit);
68155192Srwatson
69243751Srwatson  void applyRewrites(EditsReceiver &receiver);
70243751Srwatson  void clearRewrites();
71243751Srwatson
72243751Srwatson  StringRef copyString(StringRef str) {
73243751Srwatson    char *buf = StrAlloc.Allocate<char>(str.size());
74243751Srwatson    std::memcpy(buf, str.data(), str.size());
75243751Srwatson    return StringRef(buf, str.size());
76243751Srwatson  }
77243751Srwatson  StringRef copyString(const Twine &twine);
78243751Srwatson
79243751Srwatsonprivate:
80243751Srwatson  bool commitInsert(SourceLocation OrigLoc, FileOffset Offs, StringRef text,
81243751Srwatson                    bool beforePreviousInsertions);
82243751Srwatson  bool commitInsertFromRange(SourceLocation OrigLoc, FileOffset Offs,
83243751Srwatson                             FileOffset InsertFromRangeOffs, unsigned Len,
84243751Srwatson                             bool beforePreviousInsertions);
85243751Srwatson  void commitRemove(SourceLocation OrigLoc, FileOffset BeginOffs, unsigned Len);
86243751Srwatson
87243751Srwatson  StringRef getSourceText(FileOffset BeginOffs, FileOffset EndOffs,
88243751Srwatson                          bool &Invalid);
89243751Srwatson  FileEditsTy::iterator getActionForOffset(FileOffset Offs);
90243751Srwatson};
91243751Srwatson
92243751Srwatson}
93243751Srwatson
94243751Srwatson} // end namespace clang
95243751Srwatson
96243751Srwatson#endif
97243751Srwatson