Home | History | Annotate | Download | only in Edit
      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/Basic/IdentifierTable.h"
     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   // Location of argument use inside the macro body
     45   typedef std::pair<IdentifierInfo*, SourceLocation> MacroArgUse;
     46   llvm::DenseMap<unsigned, SmallVector<MacroArgUse, 2>>
     47     ExpansionToArgMap;
     48   SmallVector<std::pair<SourceLocation, MacroArgUse>, 2>
     49     CurrCommitMacroArgExps;
     50 
     51   IdentifierTable IdentTable;
     52   llvm::BumpPtrAllocator StrAlloc;
     53 
     54 public:
     55   EditedSource(const SourceManager &SM, const LangOptions &LangOpts,
     56                const PPConditionalDirectiveRecord *PPRec = nullptr)
     57     : SourceMgr(SM), LangOpts(LangOpts), PPRec(PPRec), IdentTable(LangOpts),
     58       StrAlloc() { }
     59 
     60   const SourceManager &getSourceManager() const { return SourceMgr; }
     61   const LangOptions &getLangOpts() const { return LangOpts; }
     62   const PPConditionalDirectiveRecord *getPPCondDirectiveRecord() const {
     63     return PPRec;
     64   }
     65 
     66   bool canInsertInOffset(SourceLocation OrigLoc, FileOffset Offs);
     67 
     68   bool commit(const Commit &commit);
     69 
     70   void applyRewrites(EditsReceiver &receiver, bool adjustRemovals = true);
     71   void clearRewrites();
     72 
     73   StringRef copyString(StringRef str) { return str.copy(StrAlloc); }
     74   StringRef copyString(const Twine &twine);
     75 
     76 private:
     77   bool commitInsert(SourceLocation OrigLoc, FileOffset Offs, StringRef text,
     78                     bool beforePreviousInsertions);
     79   bool commitInsertFromRange(SourceLocation OrigLoc, FileOffset Offs,
     80                              FileOffset InsertFromRangeOffs, unsigned Len,
     81                              bool beforePreviousInsertions);
     82   void commitRemove(SourceLocation OrigLoc, FileOffset BeginOffs, unsigned Len);
     83 
     84   StringRef getSourceText(FileOffset BeginOffs, FileOffset EndOffs,
     85                           bool &Invalid);
     86   FileEditsTy::iterator getActionForOffset(FileOffset Offs);
     87   void deconstructMacroArgLoc(SourceLocation Loc,
     88                               SourceLocation &ExpansionLoc,
     89                               MacroArgUse &ArgUse);
     90 
     91   void startingCommit();
     92   void finishedCommit();
     93 };
     94 
     95 }
     96 
     97 } // end namespace clang
     98 
     99 #endif
    100