clang  3.9.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 
14 #include "clang/Edit/FileOffset.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/ADT/TinyPtrVector.h"
18 #include "llvm/Support/Allocator.h"
19 #include <map>
20 
21 namespace clang {
22  class LangOptions;
23  class PPConditionalDirectiveRecord;
24 
25 namespace edit {
26  class Commit;
27  class EditsReceiver;
28 
29 class EditedSource {
30  const SourceManager &SourceMgr;
31  const LangOptions &LangOpts;
32  const PPConditionalDirectiveRecord *PPRec;
33 
34  struct FileEdit {
35  StringRef Text;
36  unsigned RemoveLen;
37 
38  FileEdit() : RemoveLen(0) {}
39  };
40 
41  typedef std::map<FileOffset, FileEdit> FileEditsTy;
42  FileEditsTy FileEdits;
43 
44  llvm::DenseMap<unsigned, llvm::TinyPtrVector<IdentifierInfo*>>
45  ExpansionToArgMap;
47  CurrCommitMacroArgExps;
48 
49  IdentifierTable IdentTable;
50  llvm::BumpPtrAllocator StrAlloc;
51 
52 public:
53  EditedSource(const SourceManager &SM, const LangOptions &LangOpts,
54  const PPConditionalDirectiveRecord *PPRec = nullptr)
55  : SourceMgr(SM), LangOpts(LangOpts), PPRec(PPRec), IdentTable(LangOpts),
56  StrAlloc() { }
57 
58  const SourceManager &getSourceManager() const { return SourceMgr; }
59  const LangOptions &getLangOpts() const { return LangOpts; }
61  return PPRec;
62  }
63 
64  bool canInsertInOffset(SourceLocation OrigLoc, FileOffset Offs);
65 
66  bool commit(const Commit &commit);
67 
68  void applyRewrites(EditsReceiver &receiver);
69  void clearRewrites();
70 
71  StringRef copyString(StringRef str) { return str.copy(StrAlloc); }
72  StringRef copyString(const Twine &twine);
73 
74 private:
75  bool commitInsert(SourceLocation OrigLoc, FileOffset Offs, StringRef text,
76  bool beforePreviousInsertions);
77  bool commitInsertFromRange(SourceLocation OrigLoc, FileOffset Offs,
78  FileOffset InsertFromRangeOffs, unsigned Len,
79  bool beforePreviousInsertions);
80  void commitRemove(SourceLocation OrigLoc, FileOffset BeginOffs, unsigned Len);
81 
82  StringRef getSourceText(FileOffset BeginOffs, FileOffset EndOffs,
83  bool &Invalid);
84  FileEditsTy::iterator getActionForOffset(FileOffset Offs);
85  void deconstructMacroArgLoc(SourceLocation Loc,
86  SourceLocation &ExpansionLoc,
87  IdentifierInfo *&II);
88 
89  void startingCommit();
90  void finishedCommit();
91 };
92 
93 }
94 
95 } // end namespace clang
96 
97 #endif
void applyRewrites(EditsReceiver &receiver)
One of these records is kept for each identifier that is lexed.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:48
Implements an efficient mapping from strings to IdentifierInfo nodes.
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
const SourceManager & SM
Definition: Format.cpp:1184
const SourceManager & getSourceManager() const
Definition: EditedSource.h:58
Encodes a location in the source.
const TemplateArgument * iterator
Definition: Type.h:4233
Records preprocessor conditional directive regions and allows querying in which region source locatio...
const PPConditionalDirectiveRecord * getPPCondDirectiveRecord() const
Definition: EditedSource.h:60
StringRef copyString(StringRef str)
Definition: EditedSource.h:71
bool commit(const Commit &commit)
EditedSource(const SourceManager &SM, const LangOptions &LangOpts, const PPConditionalDirectiveRecord *PPRec=nullptr)
Definition: EditedSource.h:53
bool canInsertInOffset(SourceLocation OrigLoc, FileOffset Offs)
StringRef Text
Definition: Format.cpp:1195
This class handles loading and caching of source files into memory.
const LangOptions & getLangOpts() const
Definition: EditedSource.h:59