clang API Documentation

EditedSource.h
Go to the documentation of this file.
00001 //===----- EditedSource.h - Collection of source edits ----------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #ifndef LLVM_CLANG_EDIT_EDITEDSOURCE_H
00011 #define LLVM_CLANG_EDIT_EDITEDSOURCE_H
00012 
00013 #include "clang/Edit/FileOffset.h"
00014 #include "llvm/ADT/DenseMap.h"
00015 #include "llvm/ADT/StringRef.h"
00016 #include "llvm/Support/Allocator.h"
00017 #include <map>
00018 
00019 namespace clang {
00020   class LangOptions;
00021   class PPConditionalDirectiveRecord;
00022 
00023 namespace edit {
00024   class Commit;
00025   class EditsReceiver;
00026 
00027 class EditedSource {
00028   const SourceManager &SourceMgr;
00029   const LangOptions &LangOpts;
00030   const PPConditionalDirectiveRecord *PPRec;
00031 
00032   struct FileEdit {
00033     StringRef Text;
00034     unsigned RemoveLen;
00035 
00036     FileEdit() : RemoveLen(0) {}
00037   };
00038 
00039   typedef std::map<FileOffset, FileEdit> FileEditsTy;
00040   FileEditsTy FileEdits;
00041 
00042   llvm::DenseMap<unsigned, SourceLocation> ExpansionToArgMap;
00043 
00044   llvm::BumpPtrAllocator StrAlloc;
00045 
00046 public:
00047   EditedSource(const SourceManager &SM, const LangOptions &LangOpts,
00048                const PPConditionalDirectiveRecord *PPRec = nullptr)
00049     : SourceMgr(SM), LangOpts(LangOpts), PPRec(PPRec),
00050       StrAlloc() { }
00051 
00052   const SourceManager &getSourceManager() const { return SourceMgr; }
00053   const LangOptions &getLangOpts() const { return LangOpts; }
00054   const PPConditionalDirectiveRecord *getPPCondDirectiveRecord() const {
00055     return PPRec;
00056   }
00057 
00058   bool canInsertInOffset(SourceLocation OrigLoc, FileOffset Offs);
00059 
00060   bool commit(const Commit &commit);
00061   
00062   void applyRewrites(EditsReceiver &receiver);
00063   void clearRewrites();
00064 
00065   StringRef copyString(StringRef str) {
00066     char *buf = StrAlloc.Allocate<char>(str.size());
00067     std::memcpy(buf, str.data(), str.size());
00068     return StringRef(buf, str.size());
00069   }
00070   StringRef copyString(const Twine &twine);
00071 
00072 private:
00073   bool commitInsert(SourceLocation OrigLoc, FileOffset Offs, StringRef text,
00074                     bool beforePreviousInsertions);
00075   bool commitInsertFromRange(SourceLocation OrigLoc, FileOffset Offs,
00076                              FileOffset InsertFromRangeOffs, unsigned Len,
00077                              bool beforePreviousInsertions);
00078   void commitRemove(SourceLocation OrigLoc, FileOffset BeginOffs, unsigned Len);
00079 
00080   StringRef getSourceText(FileOffset BeginOffs, FileOffset EndOffs,
00081                           bool &Invalid);
00082   FileEditsTy::iterator getActionForOffset(FileOffset Offs);
00083 };
00084 
00085 }
00086 
00087 } // end namespace clang
00088 
00089 #endif