clang  3.7.0
EditedSource.h
Go to the documentation of this file.
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 
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 
46 public:
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; }
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 
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
void applyRewrites(EditsReceiver &receiver)
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:48
SourceManager & SM
const SourceManager & getSourceManager() const
Definition: EditedSource.h:52
Encodes a location in the source. The SourceManager can decode this to get at the full include stack...
Records preprocessor conditional directive regions and allows querying in which region source locatio...
const PPConditionalDirectiveRecord * getPPCondDirectiveRecord() const
Definition: EditedSource.h:54
StringRef copyString(StringRef str)
Definition: EditedSource.h:65
bool commit(const Commit &commit)
EditedSource(const SourceManager &SM, const LangOptions &LangOpts, const PPConditionalDirectiveRecord *PPRec=nullptr)
Definition: EditedSource.h:47
bool canInsertInOffset(SourceLocation OrigLoc, FileOffset Offs)
This class handles loading and caching of source files into memory.
const LangOptions & getLangOpts() const
Definition: EditedSource.h:53